Python实现图片识别加翻译功能

yipeiwu_com6年前Python基础

Python使用百度AI接口实现图片识别加翻译

python诞生30周年

# encoding:utf-8
import requests
import base64
from PIL import Image
import pytesseract
# 这里需要安装一下 Tesseract-OCR
# 链接:https://pan.baidu.com/s/1D2eODet7x9xshBVi6ZUZ_Q 
# 提取码:qfef
# 安装好之后别忘了把Tesseract-OCR路径添加到环境变量中
import json
import requests
import keyboard #监听按键库
from PIL import ImageGrab #图像处理库
import time
from aip import AipOcr #pip install baidu_aip
# print("开始截图")
# 1. 截取图片
keyboard.wait(hotkey='ctrl+alt+a')
# print("键盘按下了'ctrl+alt+a'")
keyboard.wait('enter')
# print("键盘按下了'enter'")
# 模拟延迟,来解决grabclipboard函数的缓存问题(grabclipboard函数操作太快,它就会读取上一次的内容)
time.sleep(0.1)
# 2. 保存图片到电脑上
image = ImageGrab.grabclipboard()
image.save('screen.png')
#*************************************************************************************
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
# 二进制方式打开图片文件
f = open('screen.png', 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
access_token = "你自己的access_token,百度AI里面有教程,我把网址放下面了"
# https://ai.baidu.com/ai-doc/OCR/vk3h7y58v
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
print("文字识别:")
if response:
  locList = response.json()['words_result']
  for i in locList:
    print(i['words'])
print("\n翻译:")
if response:
  locList = response.json()['words_result']
  for i in locList:
    text = i['words']
##================================================================================##
    # 翻译函数,word 需要翻译的内容
    def translate(word):
      # 有道词典 api
      url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
      # 传输的参数,其中 i 为需要翻译的内容
      key = {
        'type': "AUTO",
        'i': word,
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "ue": "UTF-8",
        "action": "FY_BY_CLICKBUTTON",
        "typoResult": "true"
      }
      # key 这个字典为发送给有道词典服务器的内容
      response = requests.post(url, data=key)
      # 判断服务器是否相应成功
      if response.status_code == 200:
        # 然后相应的结果
        return response.text
      else:
        print("有道词典调用失败")
        # 失败就返回空
        return None
    def get_reuslt(repsonse):
      # 通过 json.loads 把返回的结果加载成 json 格式
      result = json.loads(repsonse)
      print("%s" % result['translateResult'][0][0]['tgt'])
    def main():
      list_trans = translate(text)
      get_reuslt(list_trans)
    if __name__ == '__main__':
      main()

保存的图片如下:

在这里插入图片描述

打印结果如下:

在这里插入图片描述

总结

以上所述是小编给大家介绍的Python实现图片识别加翻译功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

CentOS7下安装python3.6.8的教程详解

CentOS7下安装python3.6.8的教程详解

由于最近有个任务需要在python环境下跑,项目是python3.6 + tensorflow1.3.1.现总结安装环境: 卸载Python3.6方法: 首先用命令: whereis...

K-means聚类算法介绍与利用python实现的代码示例

K-means聚类算法介绍与利用python实现的代码示例

聚类 今天说K-means聚类算法,但是必须要先理解聚类和分类的区别,很多业务人员在日常分析时候不是很严谨,混为一谈,其实二者有本质的区别。 分类其实是从特定的数据中挖掘模式,作出判断的...

python实现爬山算法的思路详解

python实现爬山算法的思路详解

问题 找图中函数在区间[5,8]的最大值  重点思路 爬山算法会收敛到局部最优,解决办法是初始值在定义域上随机取乱数100次,总不可能100次都那么倒霉。 实现 imp...

python 实时遍历日志文件

open 遍历一个大日志文件 使用 readlines() 还是 readline() ? 总体上 readlines() 不慢于python 一次次调用 readline(...

Python中property函数用法实例分析

本文实例讲述了Python中property函数用法。分享给大家供大家参考,具体如下: 通常我们在访问和赋值属性的时候,都是在直接和类(实例的)的__dict__打交道,或者跟数据描述符...