Python3实现腾讯云OCR识别

yipeiwu_com6年前Python基础

废话不多说,在网上找了下腾讯云OCR识别的,示例不多,用Python的还是Python2.7,花了点时间改成Python3的。
先上图,腾讯自己的示例图:


下面是代码:

import requests
import hmac
import hashlib
import base64
import time
import random
import re


appid = "你自己的appid"
bucket = " 这个是优图上面的,可以不填" #参考本文开头提供的链接
secret_id = "填自己的" #参考官方文档
secret_key = "填自己的" #同上
expired = time.time() + 2592000
onceExpired = 0
current = time.time()
rdm = ''.join(random.choice("0123456789") for i in range(10))
userid = "0"
fileid = "tencentyunSignTest"

info = "a=" + appid + "&b=" + bucket + "&k=" + secret_id + "&e=" + str(expired) + "&t=" + str(current) + "&r=" + str(
 rdm) + "&u=0&f="

signindex = hmac.new(bytes(secret_key,'utf-8'),bytes(info,'utf-8'), hashlib.sha1).digest() # HMAC-SHA1加密
sign = base64.b64encode(signindex + bytes(info,'utf-8')) # base64转码,也可以用下面那行转码
#sign=base64.b64encode(signindex+info.encode('utf-8'))

url = "http://recognition.image.myqcloud.com/ocr/general"
headers = {'Host': 'recognition.image.myqcloud.com',
   "Authorization": sign,
   }
files = {'appid': (None,appid),
 'bucket': (None,bucket),
 'image': ('1.jpg',open('D:/codes/images/form.jpg','rb'),'image/jpeg')
 }  

r = requests.post(url, files=files,headers=headers)

responseinfo = r.content
data = responseinfo.decode('utf-8')

r_index = r'itemstring":"(.*?)"' # 做一个正则匹配
result = re.findall(r_index, data)
for i in result:

 print(i)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

pandas 对series和dataframe进行排序的实例

本问主要写根据索引或者值对series和dataframe进行排序的实例讲解 代码: #coding=utf-8 import pandas as pd import numpy a...

Python Nose框架编写测试用例方法

1. 关于Nose nose项目是于2005年发布的,也就是 py.test改名后的一年。它是由 Jason Pellerin 编写的,支持与 py.test 相同的测试习惯做法,但是...

初步探究Python程序的执行原理

初步探究Python程序的执行原理

1. 过程概述 Python先把代码(.py文件)编译成字节码,交给字节码虚拟机,然后虚拟机一条一条执行字节码指令,从而完成程序的执行。 2. 字节码 字节码在Python虚拟机程序里对...

Python os模块学习笔记

一、os模块概述 Python os模块包含普遍的操作系统功能。例如文件的复制、创建、修改、删除文件及文件夹... 二、常用方法 1、os.listdir()   返...

python的exec、eval使用分析

简介 python 动态执行字符串代码片段(也可以是文件), 一般会用到exec,eval。 exec exec_stmt ::= "exec" or_expr ["in" exp...