百度翻译的图片翻译API效果还不错,能实现整图的译文实景贴合,对于部分场景很有帮助,我也尝试着接入了下。
实现需求:本地的文件夹有n张图片,将这些图片挨个去请求百度的图片翻译API,然后将识别结果分别保存,翻译后的译文图单独存为文件,翻译后的识别结果和位置等json数据单独存为txt文件。
具体源码如下:
# -*- coding: utf-8 -*-
import requests
import random
import json
import os
import sys
import base64
from hashlib import md5
#定义APPID和appkey,需要去百度翻译开放平台申请
app_id = 'XXX'
app_key = 'XXX'
#从文件夹中取出图片文件名
ocr_path='/Users/lid/downloads/图片'
file_path=os.listdir(ocr_path)
#去掉取出来的.Ds_Store(针对Mac系统)
for x in file_path:
if x.startswith('.'):
file_path.remove(x)
#针对每一个图片文件,调图片翻译API得到翻译后的图片
for x in file_path:
file_name = x
#拼接请求接口
endpoint = 'https://tuna.thesaurus.com'
path = '/pageData/{}'.format(a)
url = endpoint + path
header={'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'}
cookies=
#定义请求参数:源语言、目标语言、cuid、mac、paste、version
from_lang = 'en'
to_lang = 'zh'
cuid = 'APICUID'
mac = 'mac'
version = 3
paste = 1
#paste=1表示返回整图贴合,0表示不返回,2表示返回块区擦除
# 生成 salt and sign
def get_md5(string, encoding='utf-8'):
return md5(string.encode(encoding)).hexdigest()
def get_file_md5(file_name):
with open(os.path.join(ocr_path,file_name), 'rb') as f:
data = f.read()
return md5(data).hexdigest()
salt = random.randint(32768, 65536)
sign = get_md5(app_id + get_file_md5(file_name) + str(salt) + cuid + mac + app_key)
# 构建请求参数
payload = {'from': from_lang, 'to': to_lang, 'appid': app_id, 'salt': salt, 'sign': sign, 'cuid': cuid, 'mac': mac, 'paste': paste, 'version': version}
image = {'image': (os.path.basename(file_name), open(os.path.join(ocr_path,file_name), 'rb'), "multipart/form-data")}
# 发送请求
response = requests.post(url, params = payload, files = image)
#输出请求返回的结果
result = response.json()
print(json.dumps(result,indent=4,ensure_ascii=False))
#处理请求回来的json,得到base64图片
trans= result['data']
trans_end= trans['pasteImg']
#base64图片保存成图片,存到原文件夹目录下,文件名后带_trans
imgdata = base64.b64decode(trans_end)
a=os.path.splitext(file_name)
file_name1=a[0]+'_trans.jpg'
with open(os.path.join(ocr_path,file_name1), 'wb') as f:
f.write(imgdata)
#识别结果保存成文本,存到原文件夹目录下,文件名后带_trans
file_name2=a[0]+'_trans.txt'
with open(os.path.join(ocr_path,file_name2), 'w+') as f:
f.write(json.dumps(result,indent=4,ensure_ascii=False))