网上找了下Python 做OCR识别的,如果是用本地识别,通常用 tesseract, 但是好像识别率不高,
找了下还是用腾讯云的 OCR 实现比较快,而且每个月有1000的免费额度,但是网上的示例不多,用Python的还是Python2.7,代码也都是拼接的乱七八糟, 花了点时间改成Python3的
Python 3.10
import hashlib, hmac, json, os, sys, time
import requests
from datetime import datetime
def heads():
secret_id = "AKIDI1SfDx6vqcAHtnG14JjQpC*****"
secret_key = "OH3ZSLemhaOs6yGqRSJwPFwlY****"
service = "ocr"
host = "ocr.tencentcloudapi.com"
endpoint = "https://" + host
region = "ap-guangzhou"
action = "GeneralBasicOCR"
version = "2018-11-19"
algorithm = "TC3-HMAC-SHA256"
timestamp = int(time.time())
date = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d")
params = {
"ImageUrl": "https://main.qcloudimg.com/raw/929bf9094ce2012473bcc4233a383e01.png"
}
http_request_method = "POST"
canonical_uri = "/"
canonical_querystring = ""
ct = "application/json; charset=utf-8"
payload = json.dumps(params)
canonical_headers = "content-type:%s\nhost:%s\n" % (ct, host)
signed_headers = "content-type;host"
hashed_request_payload = hashlib.sha256(payload.encode("utf-8")).hexdigest()
canonical_request = (http_request_method + "\n" +
canonical_uri + "\n" +
canonical_querystring + "\n" +
canonical_headers + "\n" +
signed_headers + "\n" +
hashed_request_payload)
print(canonical_request)
credential_scope = date + "/" + service + "/" + "tc3_request"
hashed_canonical_request = hashlib.sha256(canonical_request.encode("utf-8")).hexdigest()
string_to_sign = (algorithm + "\n" +
str(timestamp) + "\n" +
credential_scope + "\n" +
hashed_canonical_request)
print(string_to_sign)
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
secret_date = sign(("TC3" + secret_key).encode("utf-8"), date)
secret_service = sign(secret_date, service)
secret_signing = sign(secret_service, "tc3_request")
signature = hmac.new(secret_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
print(signature)
authorization = (algorithm + " " +
"Credential=" + secret_id + "/" + credential_scope + ", " +
"SignedHeaders=" + signed_headers + ", " +
"Signature=" + signature)
print(authorization)
print('curl -X POST ' + endpoint
+ ' -H "Authorization: ' + authorization + '"'
+ ' -H "Content-Type: application/json; charset=utf-8"'
+ ' -H "Host: ' + host + '"'
+ ' -H "X-TC-Action: ' + action + '"'
+ ' -H "X-TC-Timestamp: ' + str(timestamp) + '"'
+ ' -H "X-TC-Version: ' + version + '"'
+ ' -H "X-TC-Region: ' + region + '"'
+ " -d '" + payload + "'")
return timestamp, authorization
def req():
timestamp, authorization = heads()
headers = {
'Authorization': '%s' % authorization,
'Content-Type': 'application/json; charset=utf-8',
'Host': 'ocr.tencentcloudapi.com',
'X-TC-Action': 'GeneralBasicOCR',
'X-TC-Timestamp': '%s' % timestamp,
'X-TC-Version': '2018-11-19',
'X-TC-Region': 'ap-guangzhou',
}
data = '{"ImageUrl": "https://main.qcloudimg.com/raw/929bf9094ce2012473bcc4233a383e01.png"}'
r = requests.post('https://ocr.tencentcloudapi.com', headers=headers, data=data)
status_code = r.status_code
print(status_code)
r_json = r.json()
text_list = r_json["Response"]["TextDetections"]
text_ret = map(lambda x: x["DetectedText"], text_list)
ret = ''.join(text_ret)
print(ret)
req()
Base64 图片上传
import hashlib, hmac, json, os, sys, time
import requests
from datetime import datetime
def heads(image_path):
secret_id = "AKIDI1SfDx6vqcAHtnG14JjQpC0p******"
secret_key = "OH3ZSLemhaOs6yGqRSJwPFwlY*****"
service = "ocr"
host = "ocr.tencentcloudapi.com"
endpoint = "https://" + host
region = "ap-guangzhou"
action = "GeneralBasicOCR"
version = "2018-11-19"
algorithm = "TC3-HMAC-SHA256"
timestamp = int(time.time())
date = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d")
image_base64 = base64(image_path)
params = {
"ImageBase64": image_base64
}
http_request_method = "POST"
canonical_uri = "/"
canonical_querystring = ""
ct = "application/json; charset=utf-8"
payload = json.dumps(params)
canonical_headers = "content-type:%s\nhost:%s\n" % (ct, host)
signed_headers = "content-type;host"
hashed_request_payload = hashlib.sha256(payload.encode("utf-8")).hexdigest()
canonical_request = (http_request_method + "\n" +
canonical_uri + "\n" +
canonical_querystring + "\n" +
canonical_headers + "\n" +
signed_headers + "\n" +
hashed_request_payload)
print(canonical_request)
credential_scope = date + "/" + service + "/" + "tc3_request"
hashed_canonical_request = hashlib.sha256(canonical_request.encode("utf-8")).hexdigest()
string_to_sign = (algorithm + "\n" +
str(timestamp) + "\n" +
credential_scope + "\n" +
hashed_canonical_request)
print(string_to_sign)
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
secret_date = sign(("TC3" + secret_key).encode("utf-8"), date)
secret_service = sign(secret_date, service)
secret_signing = sign(secret_service, "tc3_request")
signature = hmac.new(secret_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
print(signature)
authorization = (algorithm + " " +
"Credential=" + secret_id + "/" + credential_scope + ", " +
"SignedHeaders=" + signed_headers + ", " +
"Signature=" + signature)
print(authorization)
print('curl -X POST ' + endpoint
+ ' -H "Authorization: ' + authorization + '"'
+ ' -H "Content-Type: application/json; charset=utf-8"'
+ ' -H "Host: ' + host + '"'
+ ' -H "X-TC-Action: ' + action + '"'
+ ' -H "X-TC-Timestamp: ' + str(timestamp) + '"'
+ ' -H "X-TC-Version: ' + version + '"'
+ ' -H "X-TC-Region: ' + region + '"'
+ " -d '" + payload + "'")
return timestamp, authorization, payload
def req(image_path):
timestamp, authorization, data = heads(image_path)
headers = {
'Authorization': '%s' % authorization,
'Content-Type': 'application/json; charset=utf-8',
'Host': 'ocr.tencentcloudapi.com',
'X-TC-Action': 'GeneralBasicOCR',
'X-TC-Timestamp': '%s' % timestamp,
'X-TC-Version': '2018-11-19',
'X-TC-Region': 'ap-guangzhou',
}
r = requests.post('https://ocr.tencentcloudapi.com', headers=headers, data=data)
status_code = r.status_code
print(status_code)
r_json = r.json()
print("json:", r_json)
text_list = r_json["Response"]["TextDetections"]
text_ret = map(lambda x: x["DetectedText"], text_list)
ret = ''.join(text_ret)
print(ret)
def base64(file_path):
import base64
file = open(file_path, 'rb')
encoded = base64.b64encode(file.read()).decode()
return "data:image/png;base64," + encoded
req("test_ocr.png")
参考
|