赞
踩
- #https://market.aliyun.com/products/57124001/cmapi023866.html?spm=a2c0j.8222507.973837.1.4dd8e532YZ2wUE#sku=yuncode1786600000
- #https://market.aliyun.com/products/57124001/cmapi028554.html?spm=a2c0j.8222507.1092900.1.4dd8e532YZ2wUE#sku=yuncode2255400000
-
- #图像数据:base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和url参数只能同时存在一个\"img\":\"\",
- #图像url地址:图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和img参数只能同时存在一个\"url\":\"\",
- #是否需要识别结果中每一行的置信度,默认不需要。true:需要false:不需要\"prob\":false,
- #是否需要单字识别功能,默认不需要。true:需要false:不需要\"charInfo\":false,
- #是否需要自动旋转功能,默认不需要。true:需要false:不需要\"rotate\":false,
- #是否需要表格识别功能,默认不需要。true:需要false:不需要\"table\":false
- import urllib
- import urllib.request
- import urllib.parse
- import json
- import ssl
- import base64
- import cv2
-
- class ALI_OCR():
- def __init__(self):
- """ APPID AK SK """
- self.APPCODE = '47fcde151a52426db0f61fede1c69507'
- #self.APPCODE = 'cb1f7559255140ab989d319be313c1b7'
-
- #印刷文字识别-文档小说图片文字识别
- self.url="https://ocrapi-document.taobao.com/ocrservice/document"
- #通用文字识别-高精版
- #self.url="https://ocrapi-advanced.taobao.com/ocrservice/advanced"
-
- self.headers = {
- 'Authorization': 'APPCODE '+self.APPCODE,
- 'Content-Type': 'application/json; charset=UTF-8'
- }
- """ 读取图片 """
- def get_file_content(self,filePath):
- with open(filePath, 'rb') as fp:
- return base64.b64encode(fp.read())
- def draw_rectangle(self,img,coordinate_list,color):
- 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)
- 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)
- 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)
- 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)
-
- return img
-
- def process(self,image_name):
- try:
- img = self.get_file_content(image_name).decode()
- dict = {'img': img,'prob': 'false'}
- params=json.dumps(dict).encode(encoding='UTF8')
- req = urllib.request.Request(self.url, params,self.headers)
-
- ctx = ssl.create_default_context()
- ctx.check_hostname = False
- ctx.verify_mode = ssl.CERT_NONE
-
- r = urllib.request.urlopen(req, context=ctx)
- html =r.read()
- return html.decode("utf8")
- except urllib.error.HTTPError as e:
- print(e.code)
- print(e.read().decode("utf8"))
-
- def save_result_pic_txt(self,input_img,input_json,save_pic=True,save_txt=True,save_dir="./result/"):
- result_json=json.loads(input_json)
- img=cv2.imread(input_img)
- if save_txt:
- txt_name=save_dir+input_img.split("/")[-1].split(".")[0]+".txt"
- file_writer=open(txt_name,"w",encoding="utf-8")
- for lines in result_json["prism_wordsInfo"]:
- coordinate_list=lines["pos"]
- img=scan_ocr.draw_rectangle(img,coordinate_list,color=(0,0,255))
- print(lines['word'])
- file_writer.write(lines['word']+"\n")
- file_writer.close()
- if save_pic:
- #cv2.imshow("result",img)
- #cv2.waitKey()
- cv2.imwrite(save_dir+input_img.split("/")[-1],img)
-
-
- if __name__=="__main__":
- scan_ocr=ALI_OCR()
- image_name='test.png'
- result_str=scan_ocr.process(image_name)
- scan_ocr.save_result_pic_txt(image_name,result_str,save_pic=True,save_txt=True,save_dir="./result_ali/")
- # coding=utf-8
- ##https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html
- import os
- import cv2
- from aip import AipOcr
-
- #pip3 install baidu-aip
-
-
- class BAIDU_OCR():
- def __init__(self):
- """ APPID AK SK """
- """
- self.APP_ID = '10122282'
- self.API_KEY = 'weGcK2iHjzOA80VnwT14d1yZ'
- self.SECRET_KEY = '8z7YkTPNF6rS4a6l6F98Fc5a06cvLM44'
- """
- self.APP_ID = '10122118'
- self.API_KEY = 'LI8ZEszLmscbLwYN38azHGgG'
- self.SECRET_KEY = '2aWkfvdHXNUVrXzdVvA6nBQxoeIgbvbG'
-
- self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY)
-
- """ 读取图片 """
- def get_file_content(self,filePath):
- with open(filePath, 'rb') as fp:
- return fp.read()
-
- def process(self,image_name):
- image = self.get_file_content(image_name)
- """ 调用通用文字识别(含位置信息版), 图片参数为本地图片 """
- result=self.client.general(image)
- """ 调用通用文字识别(含位置高精度版) """
- #result=self.client.accurate(image)
- return result
-
- def save_result_pic_txt(self,input_img,input_json,save_pic=True,save_txt=True,save_dir="./result/"):
- img=cv2.imread(input_img)
- if save_txt:
- txt_name=save_dir+input_img.split("/")[-1].split(".")[0]+".txt"
- file_writer=open(txt_name,"w",encoding="utf-8")
- for word_result in result["words_result"]:
- x1=word_result["location"]["left"]
- y1=word_result["location"]["top"]
- x2=x1+word_result["location"]["width"]
- y2=y1+word_result["location"]["height"]
- cv2.rectangle(img,(x1,y1),(x2,y2),color=(0,0,255),thickness=2)
- file_writer.write(word_result["words"]+"\n")
- print(word_result["words"])
- file_writer.close()
- if save_pic:
- #cv2.imshow("result",img)
- #cv2.waitKey()
- cv2.imwrite(save_dir+input_img.split("/")[-1],img)
-
- if __name__=="__main__":
- scan_ocr=BAIDU_OCR()
- image_name='test.png'
- result=scan_ocr.process(image_name)
- scan_ocr.save_result_pic_txt(image_name,result,save_pic=True,save_txt=True,save_dir="./result_baidu/")
-
- #https://console.faceplusplus.com.cn/documents/7776484
- #图片要求:
- #图片格式:JPG(JPEG),PNG
- #图片像素尺寸:最小48*48像素,最大800*800像素
- #图片文件大小:2MB
- import requests
- from json import JSONDecoder
- import json
- import cv2
-
-
- class FACEPLUSPLUS_OCR():
- def __init__(self):
- """ APPID AK SK """
- self.api_key = "1mCONgxd9oRMjezN9PJd0l-hjdqVT5xS"
- self.api_secret="FrX9rbKZi6CNsiN8ee-ISaVnRhRVkPTc"
-
- self.url="https://api-cn.faceplusplus.com/imagepp/v1/recognizetext"
- self.data = {"api_key": self.api_key, "api_secret": self.api_secret}
-
- def draw_rectangle(self,img,coordinate_list,color):
- 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)
- 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)
- 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)
- 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)
-
- return img
-
- def process(self,image_name):
- try:
- files = {"image_file": open(image_name, "rb")}
-
- response = requests.post(self.url, data=self.data, files=files)
- req_con = response.content.decode('utf-8')
- req_dict = JSONDecoder().decode(req_con)
-
- return req_dict
- except Exception as e:
- print(e)
-
- def save_result_pic_txt(self,input_img,input_json,save_pic=True,save_txt=True,save_dir="./result/"):
- img=cv2.imread(input_img)
- if save_txt:
- txt_name=save_dir+input_img.split("/")[-1].split(".")[0]+".txt"
- file_writer=open(txt_name,"w",encoding="utf-8")
- for objects in input_json["result"]:
- print(objects["value"])
- file_writer.write(objects["value"]+"\n")
- for lines in objects["child-objects"]:
- coordinate_list=lines["position"]
- img=scan_ocr.draw_rectangle(img,coordinate_list,color=(0,0,255))
- #print(lines['value'])
-
- file_writer.close()
- if save_pic:
- #cv2.imshow("result",img)
- #cv2.waitKey()
- cv2.imwrite(save_dir+input_img.split("/")[-1],img)
-
-
- if __name__=="__main__":
- scan_ocr=FACEPLUSPLUS_OCR()
- image_name='test.png'
- result_json=scan_ocr.process(image_name)
- scan_ocr.save_result_pic_txt(image_name,result_json,save_pic=True,save_txt=True,save_dir="./result_faceplusplus/")
- #/usr/bin/env python
- #coding=utf8
- ##http://ai.youdao.com/docs/doc-ocr-api.s#p01
-
-
- import hashlib
- import urllib
- import urllib.request
- import random
- import json
- import base64
- import cv2
-
-
-
-
- httpClient = None
-
-
-
- class YOUDAO_OCR():
- def __init__(self):
- """ APPID AK SK """
-
- #self.appKey = '7b02eb435b51584d'
- #self.secretKey = '5nr7r2QO0fS6J4fNRwVgiqNVOhjA6ZBw'
-
- self.appKey = '054adec25ae90a75'
- self.secretKey = 'Fe894uffWfQv5KMy77olO8wODVlt0dRw'
-
- #按字识别:10011,按行识别:10012
- self.detectType = '10012'
- #1,表示base64
- self.imageType = '1'
- #中英混合:zh-en,英文:en
- self.langType = 'zh-en'
- self.salt = random.randint(1, 65536)
- self.url='http://openapi.youdao.com/ocrapi'
-
- """ 读取图片 """
- def get_file_content(self,filePath):
- with open(filePath, 'rb') as fp:
- return base64.b64encode(fp.read())
- def draw_rectangle(self,img,coordinate_list,color):
- cv2.line(img,(int(coordinate_list[0]),int(coordinate_list[1])),(int(coordinate_list[2]),int(coordinate_list[3])),color,thickness=2)
- cv2.line(img,(int(coordinate_list[2]),int(coordinate_list[3])),(int(coordinate_list[4]),int(coordinate_list[5])),color,thickness=2)
- cv2.line(img,(int(coordinate_list[4]),int(coordinate_list[5])),(int(coordinate_list[6]),int(coordinate_list[7])),color,thickness=2)
- cv2.line(img,(int(coordinate_list[6]),int(coordinate_list[7])),(int(coordinate_list[0]),int(coordinate_list[1])),color,thickness=2)
- return img
-
- def process(self,image_name):
- try:
- img = self.get_file_content(image_name)
- sign = self.appKey+img.decode()+str(self.salt)+self.secretKey
- m1 = hashlib.md5()
- m1.update(sign.encode("utf8"))
- sign = m1.hexdigest()
- data = {'appKey':self.appKey,'img':img,'detectType':self.detectType,'imageType':self.imageType,'langType':self.langType,'salt':str(self.salt),'sign':sign}
- data = urllib.parse.urlencode(data).encode(encoding='utf-8')
- req = urllib.request.Request(self.url,data)
- #response是HTTPResponse对象
- response = urllib.request.urlopen(req)
- output_str=response.read().decode('utf-8')
- return output_str
- except Exception as e:
- print ("except:",e)
- finally:
- if httpClient:
- httpClient.close()
- return ""
-
- def save_result_pic_txt(self,input_img,input_json,save_pic=True,save_txt=True,save_dir="./result/"):
- result_json=json.loads(input_json)
- img=cv2.imread(input_img)
- if save_txt:
- txt_name=save_dir+input_img.split("/")[-1].split(".")[0]+".txt"
- file_writer=open(txt_name,"w",encoding="utf-8")
- for regions in result_json["Result"]["regions"]:
- img=scan_ocr.draw_rectangle(img,regions["boundingBox"].split(','),color=(0,255,0))
- for lines in regions['lines']:
- coordinate_list=lines["boundingBox"]
- img=scan_ocr.draw_rectangle(img,coordinate_list.split(','),color=(0,0,255))
- print(lines['text'])
- file_writer.write(lines['text']+"\n")
- file_writer.close()
- if save_pic:
- #cv2.imshow("result",img)
- #cv2.waitKey()
- cv2.imwrite(save_dir+input_img.split("/")[-1],img)
-
-
- if __name__=="__main__":
- scan_ocr=YOUDAO_OCR()
- image_name='test.png'
- result_str=scan_ocr.process(image_name)
- scan_ocr.save_result_pic_txt(image_name,result_str,save_pic=True,save_txt=True,save_dir="./result_youdao/")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。