這期內容當中小編將會給大家帶來有關使用python 實現批量圖片識別并翻譯的方法,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
識別過程如下:
逐個看看效果哈!make up for ever 雖然沒翻譯成玫珂菲,哈哈哈但是關鍵詞長期保濕、固定噴霧都翻譯出來了~~棒
這個更是不明覺厲,韓文、英文混合都能翻譯~~~
櫻花水的表現也不錯哦~
再亂入一個開起來更像包裝盒的圖片識別,效果不錯,沒受圖片上文字傾斜等影響 :
調用API的準備工作——生成調用所需要的應用id和密鑰
根據有道智云的接口約定,需要先在有道智云的個人頁面上生成調用所需要的應用id和密鑰,以便作為你的調用標識以及收費參考。。
具體步驟是:在有道智云的個人頁面上創建實例、創建應用、綁定應用和實例,獲取調用接口用到的應用的id和密鑰。具體個人注冊的過程和應用創建過程詳見文章分享一次批量文件翻譯的開發過程
開發過程介紹
1、api接口介紹
先介紹下該工程的核心部分,有道智云圖片翻譯服務的調用接口
API HTTPS地址:https://openapi.youdao.com/ocrtransapi
接口調用方式:POST
請求格式:表單
相應格式:JSON
接口調用參數
調用API需要向接口發送以下字段來訪問服務。
字段名 | 類型 | 含義 | 必填 | 備注 |
---|---|---|---|---|
type | text | 文件上傳類型 | True | 目前支持Base64,請置該字段值為1 |
from | text | 源語言 | True | 參考下方的 支持語言 (可設置為auto) |
to | text | 目標語言 | True | 參考下方的 支持語言 (可設置為auto) |
appKey | text | 應用ID | True | 可在 應用管理 查看 |
salt | text | UUID | True | 1995882C5064805BC30A39829B779D7B |
sign | text | 簽名 | True | md5(應用Id+q+salt+應用密鑰) |
ext | text | 翻譯結果音頻格式,支持mp3 | false | mp3 |
q | text | 要識別的圖片 | true | type為1時必填,圖片的Base64編碼 |
docType | text | 服務器響應類型,目前只支持json | false | json |
render | text | 是否需要服務端返回渲染的圖片,0:否;1:是,默認是0 | false | 0 |
nullIsError | text | 如果ocr沒有檢測到文字,是否返回錯誤,false:否;true:是,默認是false | false | 注意是字符串 |
簽名生成方法如下:
1、將請求參數中的 應用ID appKey , 圖片的Base64編碼 q ,UUID salt 和 應用密鑰 按照 應用ID+q+salt+應用密鑰的順序拼接得到字符串 str 。
2、對字符串 str 做 md5,得到32位大寫的 sign (參考Java生成MD5示例,可點擊右側的JAVA示例)。
輸出結果
返回的結果是json格式,具體說明如下:
字段名 | 字段說明 |
---|---|
orientation | 圖片所對應的方向 |
lanFrom | ocr所識別出來認為的圖片中的語言 |
textAngle | 圖片的傾斜角度 |
errorCode | 錯誤碼 |
lanTo | 目標語言 |
resRegions | 圖片翻譯的具體內容 |
-boundingBox | 區域范圍,四個值: 左上角的x值,左上角的y值,區域的的寬,區域的高 例如:134,0,1066,249 |
-linesCount | 行數(用于前端排版) |
-lineheight | 行高 |
-context | 該區域的原文 |
-linespace | 行間距 |
-tranContent | 翻譯結果 |
2、詳細開發
這個demo使用python3開發,包括maindow.py,transclass.py,pictranslate.py三個文件。maindow.py主要實現界面部分,使用python自帶的tkinter庫,來進行圖片文件選擇、選擇結果存放路徑。transclass.py實現了圖片讀取、處理等邏輯,最后通過pictranslate.py中的方法來調用圖片翻譯API。
1、界面部分
主要元素:
root=tk.Tk() root.title("netease youdao translation test") frm = tk.Frame(root) frm.grid(padx='50', pady='50') btn_get_file = tk.Button(frm, text='選擇待翻譯圖片', command=get_files) btn_get_file.grid(row=0, column=0, ipadx='3', ipady='3', padx='10', pady='20') text1 = tk.Text(frm, width='40', height='10') text1.grid(row=0, column=1) btn_get_result_path=tk.Button(frm,text='選擇翻譯結果路徑',command=set_result_path) btn_get_result_path.grid(row=1,column=0) text2=tk.Text(frm,width='40', height='2') text2.grid(row=1,column=1) btn_sure=tk.Button(frm,text="翻譯",command=translate_files) btn_sure.grid(row=2,column=1) root.mainloop()
獲取待翻譯圖片文件的方法(此處設置的僅支持.jpg文件):
def get_files(): files = filedialog.askopenfilenames(filetypes=[('text files', '.jpg')]) translate.file_paths=files if files: for file in files: text1.insert(tk.END, file + '\n') text1.update() else: print('你沒有選擇任何文件')
獲取結果存儲路徑:
def set_result_path(): result_path=filedialog.askdirectory() translate.result_root_path=result_path text2.insert(tk.END,result_path)
翻譯按鈕,調用了translate_files,該文件中的translate_files()方法最終調用了translate類的translate_files()方法:
def translate_files(): if translate.file_paths: translate.translate_files() tk.messagebox.showinfo("提示","搞定") else : tk.messagebox.showinfo("提示","無文件")
2、批量圖片處理
transclass.py實現了圖片讀取、處理等邏輯,Translate類定義如下:
class Translate(): def __init__(self,name,file_paths,result_root_path,trans_type): self.name=name self.file_paths=file_paths # 待翻譯文件路徑 self.result_root_path=result_root_path # 結果存放路徑 self.trans_type=trans_type def translate_files(self): for file_path in self.file_paths: #對批量圖片逐個處理 file_name=os.path.basename(file_path) print('==========='+file_path+'===========') trans_reult=self.translate_use_netease(file_path) #對單個圖片調用接口 resul_file=open(self.result_root_path+'/result_'+file_name.split('.')[0]+'.txt','w').write(trans_reult) #返回結果寫入 def translate_use_netease(self,file_content): #調用有道接口,并返回結果 result= connect(file_content) return result
3、有道api調用
pictranslate.py中封裝了調用有道智云API的一些方法,其中最核心的是connect()方法,按照接口要求拼接了所需參數,發起請求并返回結果。
def connect(file_content,fromLan,toLan): f = open(file_content, 'rb') # 二進制方式打開圖文件 q = base64.b64encode(f.read()).decode('utf-8') # 讀取文件內容,轉換為base64編碼 f.close() data = {} # data['from'] = '源語言' # data['to'] = '目標語言' data['from'] = 'auto' data['to'] = 'auto' data['type'] = '1' data['q'] = q salt = str(uuid.uuid1()) signStr = APP_KEY + q + salt + APP_SECRET sign = encrypt(signStr) data['appKey'] = APP_KEY data['salt'] = salt data['sign'] = sign response = do_request(data) result=json.loads(str(response.content, encoding="utf-8")) print(result) translateResults=result['resRegions'] print(translateResults) pictransresult="" for i in translateResults: pictransresult=pictransresult+i['tranContent']+"\n" return pictransresult
上述就是小編為大家分享的使用python 實現批量圖片識別并翻譯的方法了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。