OCR_实时翻译


OCR_实时翻译

  • 采用了easyocr
  • 爬虫,调取百度翻译接口

调用屏幕接口

调用屏幕的方法很简单:

import pyautogui
import cv2
import numpy as np
img = pyautogui.screenshot(region=[0, 0, 1920, 100])  # x,y,w,h
# img.save('screenshot.png')
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
cv2.imshow("1",img)
cv2.waitKey(0)

但感觉效率很低,最后改成了虚拟摄像头+cap=cv2.VideoCapture(1)的方式捕获。

调用百度翻译接口

import urllib.request
import urllib.parse
import json
import pyautogui
import cv2
import numpy as np
url = 'https://fanyi.baidu.com/sug'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
}
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=url, data=data, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
obj = json.loads(content)
print(obj)

这个只能做单词翻译,还需要进一步优化。

easyocr

这个参考EasyOCR光学字符识别

结果

最终实现效果,在摄像头上和屏幕截取上都进行了测试.

首先是摄像头

然后是屏幕

最后是源码

import time

import easyocr
import ssl
import cv2
# from matplotlib import pyplot as plt

import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import urllib.request
import urllib.parse
import json
import pyautogui
import cv2
import numpy as np


ssl._create_default_https_context = ssl._create_unverified_context
# 设置识别中英文两种语言
reader = easyocr.Reader(['ch_sim', 'en'])  # need to run only once to load model into memory
Image3='https://www.codekp.cn/download/img/ubuntu/NVIDIA1.png'
Image2=r"H:\blog\wan_lab\blog_2020\source\download\img\ubuntu\NVIDIA1.png"
Image4="1.jpg"
url = 'https://fanyi.baidu.com/sug'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
}


cap=cv2.VideoCapture(0)


while True:
    ret,img=cap.read()
    # img = pyautogui.screenshot(region=[0, 0, 500, 500])  # x,y,w,h
    # img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
    cv2.imwrite("1.jpg",img)
    result = reader.readtext(Image4, paragraph="False")

    start=time.time()
    for detection in result:
        top_left = tuple(detection[0][0])
        bottom_right = tuple(detection[0][2])
        text = detection[1]
        img = cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 3)
        img = cv2ImgAddText(img, text, bottom_right[0], bottom_right[1], (0, 255, 0), 18)
        data = {
            'kw': text
        }
        data = urllib.parse.urlencode(data).encode('utf-8')
        request = urllib.request.Request(url=url, data=data, headers=headers)
        response = urllib.request.urlopen(request)
        content = response.read().decode('utf-8')
        obj = json.loads(content)
        print(obj)
        try:
            text2 = str(obj['data'][0]['v'])
            img = cv2ImgAddText(img, text2, bottom_right[0], bottom_right[1]+20, (0, 255, 0), 18)
        except:
            pass
    cv2.imshow("1",img)
    cv2.waitKey(1)
    print((time.time()-start)*1000)
capture.release()

扩展

将百度翻译的内容,和原始图像显示出来并列到一起就有了一个比较完整的东西。


文章作者: 万鲲鹏
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 万鲲鹏 !
评论
  目录