ocr数字识别的python例子
时间: 2023-11-16 10:58:17 浏览: 40
使用Python进行OCR数字识别的例子有很多,以下是其中的一些例子:
1. 使用pytesseract库进行OCR数字识别的例子:
```
import pytesseract
from PIL import Image
# 打开图片
img = Image.open('test.png')
# 识别图片中的数字
result = pytesseract.image_to_string(img, config='digits')
# 输出识别结果
print(result)
```
2. 使用腾讯云OCR进行OCR数字识别的例子:
```
import requests
import hmac
import hashlib
import base64
# 设置请求参数
app_id = 'your_app_id'
app_key = 'your_app_key'
time_stamp = str(int(time.time()))
nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))
image = base64.b64encode(open('test.png', 'rb').read()).decode('utf-8')
# 生成签名
sign_str = 'app_id=' + app_id + '&image=' + image + '&nonce_str=' + nonce_str + '&time_stamp=' + time_stamp + '&app_key=' + app_key
sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest().upper()
# 发送请求
url = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_digitocr'
data = {
'app_id': app_id,
'time_stamp': time_stamp,
'nonce_str': nonce_str,
'image': image,
'sign': sign
}
response = requests.post(url, data=data)
# 输出识别结果
result = response.json()
print(result['data']['item_list'][0]['itemstring'])
```
3. 使用cnocr库进行OCR数字识别的例子:
```
from cnocr import CnOcr
# 初始化OCR对象
ocr = CnOcr()
# 识别图片中的数字
result = ocr.ocr('test.png')
# 输出识别结果
print(result)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)