文字识别两种方法

"""
# 导入easyocr
import cv2
import easyocr
from PIL import Image
# 创建reader对象
reader = easyocr.Reader(['ch_sim','en'], gpu= False)
# 读取图像
image = cv2.imread('123.jpg')
# image = cv2.imread('企业微信截图_16607054374430.jpg')

# image = cv2.resize(image, (800, 600))
# print(image)
result = reader.readtext(image, paragraph=True)
# 结果
print(result)
for i in result:
    word = i[1]
    print(word)
"""

# 导入easyocr
import cv2
import easyocr
import time

def ocr_sim(imagepath):
    # 创建reader对象
    reader = easyocr.Reader(['ch_sim','en'], gpu= True)

    # 读取图像(转灰度)
    image = cv2.imread(imagepath, cv2.IMREAD_GRAYSCALE)
    # image = cv2.resize(image, (800, 600))
    # a = cv2.imshow(image)
    print(image.shape)
    # 输出样式
    # result = reader.readtext(image)                                # 全输出
    # result = reader.readtext(image,detail = 0)                   # 精简输出
    result = reader.readtext(image,detail = 0, paragraph=True)   # 整体输出
    # 结果
    # print(result)
    for i in result:
        word = i
        print(word)

if __name__ == "__main__":
    a = time.time()
    ocr_sim(imagepath="789.jpg")
    b = time.time() - a
    print(b)

以上是导入easycor方法;

下面是调用百度的方法:

# encoding:utf-8

import requests
import base64

'''
通用文字识别
https://blog.csdn.net/qq_44725872/article/details/109386651?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166072686116782350816981%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=166072686116782350816981&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-109386651-null-null.142^v41^new_blog_pos_by_title,185^v2^tag_show&utm_term=30%E8%A1%8C%E4%BB%A3%E7%A0%81%E6%95%99%E4%BD%A0%E5%81%9A%E4%B8%AA%E9%80%9A%E7%94%A8%E8%AF%86%E5%88%AB&spm=1018.2226.3001.4187
'''
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=CPQGKpSXmb135OO35BzE13CY&client_secret=KkVt388TnCpRwEFsuE2u50ld9gkRBMGg'
response = requests.get(host)
if response:
    access_token = response.json()['access_token']

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
# 二进制方式打开图片文件
f = open('123.jpg', 'rb')
img = base64.b64encode(f.read())

params = {"image": img}
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)
if response:
    words = response.json()['words_result']
    print ("图像文字识别结果:")
    for word in words:
        print(word['words'],end='')

你可能感兴趣的:(ocr,文字识别,easyocr)