pythonはスクリーンショットを実現して画像の内容を取得して翻訳します

12449 ワード

pythonはスクリーンショットを実現して画像の内容を取得します
  • pythonコード
  • macOSは起動起動スクリプトを使用し、一部のパラメータは自分で修正する必要があります.[macOS起動スクリプトの設定方法](https://jingyan.baidu.com/article/9c69d48fe7a2c913c9024eb6.html)

  • 画像やスクリーンショットをfileDirディレクトリに入れるだけで画像の内容を抽出できます
    pythonコード
    import os
    import time
    import pyperclip
    from aip import AipOcr
    
    
    def init():
        APP_ID = "21197258"
        API_KEY = "2nh0SuB0Z1zbZFtR6mfATYVb"
        SECRET_KEY = "rwXvvdP1iufWGhWGWFfwp0oSPE2gDTfN"
        aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
        return aipOcr
    
    
    def get_file_content(filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()
    
    
    def result(filePath, options):
        result = ""
        try:
            data = aipOcr.basicGeneral(get_file_content(filePath), options)
        except:
            raise ConnectionError("      !")
        words_result = data['words_result']
        for i in range(len(words_result)):
            result += words_result[i]['words']
        pyperclip.copy(result)
    
    
    if __name__ == '__main__':
    
        """          """
        fileDir = r"/Users/jeffrey/screen"
    
        """        """
        fileType = ["png", "jpg", "bmp"]
    
        """      """
        waitTime = 2
    
        """      """
        options = {
            'detect_direction': 'true',
            'language_type': 'CHN_ENG',
        }
    
        """          ,             """
        oldPhotoList = os.listdir(fileDir)
        while True:
            time.sleep(waitTime)
            newPhotoList = os.listdir(fileDir)
            """  set               """
            diffList = list((set(oldPhotoList) ^ set(newPhotoList)))
            if len(diffList) >= 1:
                for i in diffList:
                    oldPhotoList.append(i)
                    diffList.remove(i)
                    target = fileDir + os.sep + i
                    if not os.path.exists(target):
                        print("     ")
                        continue
                    if os.path.splitext(target)[-1].replace(".", "") not in fileType:
                        print("       ")
                        continue
                    aipOcr = init()
                    result(target, options)
                    print("          ")
        pass
    
    

    macOSは起動起動スクリプトを使用して、一部のパラメータは自分で修正しなければならなくて、どのようにmacOS起動スクリプトを設定します
    # >>> conda initialize >>>
    #    python38
    # !! Contents within this block are managed by 'conda init' !!
    __conda_setup="$('/Users/jeffrey/opt/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
    if [ $? -eq 0 ]; then
        eval "$__conda_setup"
    else
        if [ -f "/Users/jeffrey/opt/miniconda3/etc/profile.d/conda.sh" ]; then
            . "/Users/jeffrey/opt/miniconda3/etc/profile.d/conda.sh"
        else
            export PATH="/Users/jeffrey/opt/miniconda3/bin:$PATH"
        fi
    fi
    unset __conda_setup
    
    
    conda activate py38
    cd /Users/jeffrey/Desktop/    /py
    python OCR    .py```