当前位置:   article > 正文

ocr大pk(阿里读光,百度,face++,有道)_读光ocr

读光ocr

当下视角:为客户找到你的价值

​​​​​​​未来视角:为你的价值找到客户

 

阿里读光

  1. #https://market.aliyun.com/products/57124001/cmapi023866.html?spm=a2c0j.8222507.973837.1.4dd8e532YZ2wUE#sku=yuncode1786600000
  2. #https://market.aliyun.com/products/57124001/cmapi028554.html?spm=a2c0j.8222507.1092900.1.4dd8e532YZ2wUE#sku=yuncode2255400000
  3. #图像数据:base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和url参数只能同时存在一个\"img\":\"\",
  4. #图像url地址:图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和img参数只能同时存在一个\"url\":\"\",
  5. #是否需要识别结果中每一行的置信度,默认不需要。true:需要false:不需要\"prob\":false,
  6. #是否需要单字识别功能,默认不需要。true:需要false:不需要\"charInfo\":false,
  7. #是否需要自动旋转功能,默认不需要。true:需要false:不需要\"rotate\":false,
  8. #是否需要表格识别功能,默认不需要。true:需要false:不需要\"table\":false
  9. import urllib
  10. import urllib.request
  11. import urllib.parse
  12. import json
  13. import ssl
  14. import base64
  15. import cv2
  16. class ALI_OCR():
  17. def __init__(self):
  18. """ APPID AK SK """
  19. self.APPCODE = '47fcde151a52426db0f61fede1c69507'
  20. #self.APPCODE = 'cb1f7559255140ab989d319be313c1b7'
  21. #印刷文字识别-文档小说图片文字识别
  22. self.url="https://ocrapi-document.taobao.com/ocrservice/document"
  23. #通用文字识别-高精版
  24. #self.url="https://ocrapi-advanced.taobao.com/ocrservice/advanced"
  25. self.headers = {
  26. 'Authorization': 'APPCODE '+self.APPCODE,
  27. 'Content-Type': 'application/json; charset=UTF-8'
  28. }
  29. """ 读取图片 """
  30. def get_file_content(self,filePath):
  31. with open(filePath, 'rb') as fp:
  32. return base64.b64encode(fp.read())
  33. def draw_rectangle(self,img,coordinate_list,color):
  34. cv2.line(img,(int(coordinate_list[0]["x"]),int(coordinate_list[0]["y"])),(int(coordinate_list[1]["x"]),int(coordinate_list[1]["y"])),color,thickness=2)
  35. cv2.line(img,(int(coordinate_list[1]["x"]),int(coordinate_list[1]["y"])),(int(coordinate_list[2]["x"]),int(coordinate_list[2]["y"])),color,thickness=2)
  36. cv2.line(img,(int(coordinate_list[2]["x"]),int(coordinate_list[2]["y"])),(int(coordinate_list[3]["x"]),int(coordinate_list[3]["y"])),color,thickness=2)
  37. cv2.line(img,(int(coordinate_list[3]["x"]),int(coordinate_list[3]["y"])),(int(coordinate_list[0]["x"]),int(coordinate_list[0]["y"])),color,thickness=2)
  38. return img
  39. def process(self,image_name):
  40. try:
  41. img = self.get_file_content(image_name).decode()
  42. dict = {'img': img,'prob': 'false'}
  43. params=json.dumps(dict).encode(encoding='UTF8')
  44. req = urllib.request.Request(self.url, params,self.headers)
  45. ctx = ssl.create_default_context()
  46. ctx.check_hostname = False
  47. ctx.verify_mode = ssl.CERT_NONE
  48. r = urllib.request.urlopen(req, context=ctx)
  49. html =r.read()
  50. return html.decode("utf8")
  51. except urllib.error.HTTPError as e:
  52. print(e.code)
  53. print(e.read().decode("utf8"))
  54. def save_result_pic_txt(self,input_img,input_json,save_pic=True,save_txt=True,save_dir="./result/"):
  55. result_json=json.loads(input_json)
  56. img=cv2.imread(input_img)
  57. if save_txt:
  58. txt_name=save_dir+input_img.split("/")[-1].split(".")[0]+".txt"
  59. file_writer=open(txt_name,"w",encoding="utf-8")
  60. for lines in result_json["prism_wordsInfo"]:
  61. coordinate_list=lines["pos"]
  62. img=scan_ocr.draw_rectangle(img,coordinate_list,color=(0,0,255))
  63. print(lines['word'])
  64. file_writer.write(lines['word']+"\n")
  65. file_writer.close()
  66. if save_pic:
  67. #cv2.imshow("result",img)
  68. #cv2.waitKey()
  69. cv2.imwrite(save_dir+input_img.split("/")[-1],img)
  70. if __name__=="__main__":
  71. scan_ocr=ALI_OCR()
  72. image_name='test.png'
  73. result_str=scan_ocr.process(image_name)
  74. scan_ocr.save_result_pic_txt(image_name,result_str,save_pic=True,save_txt=True,save_dir="./result_ali/")

 

百度

  1. # coding=utf-8
  2. ##https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html
  3. import os
  4. import cv2
  5. from aip import AipOcr
  6. #pip3 install baidu-aip
  7. class BAIDU_OCR():
  8. def __init__(self):
  9. """ APPID AK SK """
  10. """
  11. self.APP_ID = '10122282'
  12. self.API_KEY = 'weGcK2iHjzOA80VnwT14d1yZ'
  13. self.SECRET_KEY = '8z7YkTPNF6rS4a6l6F98Fc5a06cvLM44'
  14. """
  15. self.APP_ID = '10122118'
  16. self.API_KEY = 'LI8ZEszLmscbLwYN38azHGgG'
  17. self.SECRET_KEY = '2aWkfvdHXNUVrXzdVvA6nBQxoeIgbvbG'
  18. self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY)
  19. """ 读取图片 """
  20. def get_file_content(self,filePath):
  21. with open(filePath, 'rb') as fp:
  22. return fp.read()
  23. def process(self,image_name):
  24. image = self.get_file_content(image_name)
  25. """ 调用通用文字识别(含位置信息版), 图片参数为本地图片 """
  26. result=self.client.general(image)
  27. """ 调用通用文字识别(含位置高精度版) """
  28. #result=self.client.accurate(image)
  29. return result
  30. def save_result_pic_txt(self,input_img,input_json,save_pic=True,save_txt=True,save_dir="./result/"):
  31. img=cv2.imread(input_img)
  32. if save_txt:
  33. txt_name=save_dir+input_img.split("/")[-1].split(".")[0]+".txt"
  34. file_writer=open(txt_name,"w",encoding="utf-8")
  35. for word_result in result["words_result"]:
  36. x1=word_result["location"]["left"]
  37. y1=word_result["location"]["top"]
  38. x2=x1+word_result["location"]["width"]
  39. y2=y1+word_result["location"]["height"]
  40. cv2.rectangle(img,(x1,y1),(x2,y2),color=(0,0,255),thickness=2)
  41. file_writer.write(word_result["words"]+"\n")
  42. print(word_result["words"])
  43. file_writer.close()
  44. if save_pic:
  45. #cv2.imshow("result",img)
  46. #cv2.waitKey()
  47. cv2.imwrite(save_dir+input_img.split("/")[-1],img)
  48. if __name__=="__main__":
  49. scan_ocr=BAIDU_OCR()
  50. image_name='test.png'
  51. result=scan_ocr.process(image_name)
  52. scan_ocr.save_result_pic_txt(image_name,result,save_pic=True,save_txt=True,save_dir="./result_baidu/")

face++

  1. #https://console.faceplusplus.com.cn/documents/7776484
  2. #图片要求:
  3. #图片格式:JPG(JPEG),PNG
  4. #图片像素尺寸:最小48*48像素,最大800*800像素
  5. #图片文件大小:2MB
  6. import requests
  7. from json import JSONDecoder
  8. import json
  9. import cv2
  10. class FACEPLUSPLUS_OCR():
  11. def __init__(self):
  12. """ APPID AK SK """
  13. self.api_key = "1mCONgxd9oRMjezN9PJd0l-hjdqVT5xS"
  14. self.api_secret="FrX9rbKZi6CNsiN8ee-ISaVnRhRVkPTc"
  15. self.url="https://api-cn.faceplusplus.com/imagepp/v1/recognizetext"
  16. self.data = {"api_key": self.api_key, "api_secret": self.api_secret}
  17. def draw_rectangle(self,img,coordinate_list,color):
  18. cv2.line(img,(int(coordinate_list[0]["x"]),int(coordinate_list[0]["y"])),(int(coordinate_list[1]["x"]),int(coordinate_list[1]["y"])),color,thickness=2)
  19. cv2.line(img,(int(coordinate_list[1]["x"]),int(coordinate_list[1]["y"])),(int(coordinate_list[2]["x"]),int(coordinate_list[2]["y"])),color,thickness=2)
  20. cv2.line(img,(int(coordinate_list[2]["x"]),int(coordinate_list[2]["y"])),(int(coordinate_list[3]["x"]),int(coordinate_list[3]["y"])),color,thickness=2)
  21. cv2.line(img,(int(coordinate_list[3]["x"]),int(coordinate_list[3]["y"])),(int(coordinate_list[0]["x"]),int(coordinate_list[0]["y"])),color,thickness=2)
  22. return img
  23. def process(self,image_name):
  24. try:
  25. files = {"image_file": open(image_name, "rb")}
  26. response = requests.post(self.url, data=self.data, files=files)
  27. req_con = response.content.decode('utf-8')
  28. req_dict = JSONDecoder().decode(req_con)
  29. return req_dict
  30. except Exception as e:
  31. print(e)
  32. def save_result_pic_txt(self,input_img,input_json,save_pic=True,save_txt=True,save_dir="./result/"):
  33. img=cv2.imread(input_img)
  34. if save_txt:
  35. txt_name=save_dir+input_img.split("/")[-1].split(".")[0]+".txt"
  36. file_writer=open(txt_name,"w",encoding="utf-8")
  37. for objects in input_json["result"]:
  38. print(objects["value"])
  39. file_writer.write(objects["value"]+"\n")
  40. for lines in objects["child-objects"]:
  41. coordinate_list=lines["position"]
  42. img=scan_ocr.draw_rectangle(img,coordinate_list,color=(0,0,255))
  43. #print(lines['value'])
  44. file_writer.close()
  45. if save_pic:
  46. #cv2.imshow("result",img)
  47. #cv2.waitKey()
  48. cv2.imwrite(save_dir+input_img.split("/")[-1],img)
  49. if __name__=="__main__":
  50. scan_ocr=FACEPLUSPLUS_OCR()
  51. image_name='test.png'
  52. result_json=scan_ocr.process(image_name)
  53. scan_ocr.save_result_pic_txt(image_name,result_json,save_pic=True,save_txt=True,save_dir="./result_faceplusplus/")

有道

  1. #/usr/bin/env python
  2. #coding=utf8
  3. ##http://ai.youdao.com/docs/doc-ocr-api.s#p01
  4. import hashlib
  5. import urllib
  6. import urllib.request
  7. import random
  8. import json
  9. import base64
  10. import cv2
  11. httpClient = None
  12. class YOUDAO_OCR():
  13. def __init__(self):
  14. """ APPID AK SK """
  15. #self.appKey = '7b02eb435b51584d'
  16. #self.secretKey = '5nr7r2QO0fS6J4fNRwVgiqNVOhjA6ZBw'
  17. self.appKey = '054adec25ae90a75'
  18. self.secretKey = 'Fe894uffWfQv5KMy77olO8wODVlt0dRw'
  19. #按字识别:10011,按行识别:10012
  20. self.detectType = '10012'
  21. #1,表示base64
  22. self.imageType = '1'
  23. #中英混合:zh-en,英文:en
  24. self.langType = 'zh-en'
  25. self.salt = random.randint(1, 65536)
  26. self.url='http://openapi.youdao.com/ocrapi'
  27. """ 读取图片 """
  28. def get_file_content(self,filePath):
  29. with open(filePath, 'rb') as fp:
  30. return base64.b64encode(fp.read())
  31. def draw_rectangle(self,img,coordinate_list,color):
  32. cv2.line(img,(int(coordinate_list[0]),int(coordinate_list[1])),(int(coordinate_list[2]),int(coordinate_list[3])),color,thickness=2)
  33. cv2.line(img,(int(coordinate_list[2]),int(coordinate_list[3])),(int(coordinate_list[4]),int(coordinate_list[5])),color,thickness=2)
  34. cv2.line(img,(int(coordinate_list[4]),int(coordinate_list[5])),(int(coordinate_list[6]),int(coordinate_list[7])),color,thickness=2)
  35. cv2.line(img,(int(coordinate_list[6]),int(coordinate_list[7])),(int(coordinate_list[0]),int(coordinate_list[1])),color,thickness=2)
  36. return img
  37. def process(self,image_name):
  38. try:
  39. img = self.get_file_content(image_name)
  40. sign = self.appKey+img.decode()+str(self.salt)+self.secretKey
  41. m1 = hashlib.md5()
  42. m1.update(sign.encode("utf8"))
  43. sign = m1.hexdigest()
  44. data = {'appKey':self.appKey,'img':img,'detectType':self.detectType,'imageType':self.imageType,'langType':self.langType,'salt':str(self.salt),'sign':sign}
  45. data = urllib.parse.urlencode(data).encode(encoding='utf-8')
  46. req = urllib.request.Request(self.url,data)
  47. #response是HTTPResponse对象
  48. response = urllib.request.urlopen(req)
  49. output_str=response.read().decode('utf-8')
  50. return output_str
  51. except Exception as e:
  52. print ("except:",e)
  53. finally:
  54. if httpClient:
  55. httpClient.close()
  56. return ""
  57. def save_result_pic_txt(self,input_img,input_json,save_pic=True,save_txt=True,save_dir="./result/"):
  58. result_json=json.loads(input_json)
  59. img=cv2.imread(input_img)
  60. if save_txt:
  61. txt_name=save_dir+input_img.split("/")[-1].split(".")[0]+".txt"
  62. file_writer=open(txt_name,"w",encoding="utf-8")
  63. for regions in result_json["Result"]["regions"]:
  64. img=scan_ocr.draw_rectangle(img,regions["boundingBox"].split(','),color=(0,255,0))
  65. for lines in regions['lines']:
  66. coordinate_list=lines["boundingBox"]
  67. img=scan_ocr.draw_rectangle(img,coordinate_list.split(','),color=(0,0,255))
  68. print(lines['text'])
  69. file_writer.write(lines['text']+"\n")
  70. file_writer.close()
  71. if save_pic:
  72. #cv2.imshow("result",img)
  73. #cv2.waitKey()
  74. cv2.imwrite(save_dir+input_img.split("/")[-1],img)
  75. if __name__=="__main__":
  76. scan_ocr=YOUDAO_OCR()
  77. image_name='test.png'
  78. result_str=scan_ocr.process(image_name)
  79. scan_ocr.save_result_pic_txt(image_name,result_str,save_pic=True,save_txt=True,save_dir="./result_youdao/")

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/155780
推荐阅读
相关标签
  

闽ICP备14008679号