我是靠谱客的博主 冷艳摩托,这篇文章主要介绍树莓派摄像头拍照上传阿里云自定义OCR识别,现在分享给大家,希望可以做个参考。

树莓派加按钮,实现每次按下按钮摄像头拍照上传OCR识别。本次使用的是自定义识别。

主函数

主要是拍照和自定义OCR两个板块

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Project : Raspberry_OCR File Name : main Description : Author : lrh Data : 2021-07-18 ------------------------------------------------------------------------------- """ import autoocr, cameraa from gpiozero import Button from picamera import PiCamera # from User.button import buttonInit, judge # import time button = Button(23) # GPIO对应的BCM名称 # camera = PiCamera() if __name__ == '__main__': while True: # print('等待按钮按下中.......') if button.is_pressed: print('按钮被按下啦') cameraa.catpure_img() pathstr = '/home/pi/Desktop/Pictures/b.jpg' autoocr.ocr(pathstr) print('一次识别完成啦') print('*************************')

拍照函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Project : Raspberry_OCR File Name : camera Description : Author : lrh Data : 2021-07-18 ------------------------------------------------------------------------------- """ from picamera import PiCamera from time import sleep camera = PiCamera() def catpure_img(): camera.start_preview() sleep(1) camera.capture('/home/pi/Desktop/Pictures/b.jpg') camera.stop_preview()

阿里云自定义OCR

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Project : Raspberry_OCR File Name : 自定义ocr Description : 阿里云自定义OCR python3代码 Author : lrh Data : 2021-07-17 ------------------------------------------------------------------------------- """ import urllib.request import urllib.error import urllib.parse import json import time import base64 import re finddata = re.compile(r'"items":{"(.*)":"(.*)","(.*)":"(.*)","(.*)":"(.*)"},"request_id"') # 因为是自定义,所以需要自己重新设定正则表达式,或者是改成通用的代码也行 ''' {"config_str": "{"template_list":["daec2576-6148-4142-8e42-5c0a78e4d0fa1626528669"]}", "items":{"低压":"80","脉搏":"90","高压":"130"}, "request_id":"20210718113816_93d1a52542b7c07551363490ea875648", "success":true,"template_id":"daec2576-6148-4142-8e42-5c0a78e4d0fa1626528669"} <class 'str'> ''' # 请求头 headers = { 'Authorization': 'APPCODE 9c*****5e', # APPCODE +你的appcod,一定要有空格! 'Content-Type': 'application/json; charset=UTF-8' # 根据接口的格式来 } url_request = "https://ocrdiy.market.alicloudapi.com/api/predict/ocr_sdt" # 对照官网API改 def posturl(url, data={}): try: params = json.dumps(data).encode(encoding='UTF8') req = urllib.request.Request(url, params, headers) r = urllib.request.urlopen(req) html = r.read() r.close() return html.decode("utf8") except urllib.error.HTTPError as e: print(e.code) print(e.read().decode("utf8")) time.sleep(1) def ocr(image_path): # 本地图片 with open(image_path, 'rb') as f: # 以二进制读取本地图片 data = f.read() # json.dumps(data, cls=MyEncoder) encodestr = str(base64.b64encode(data), 'utf-8') # base64编码图片 bodys = { "image": encodestr, "configure": {"template_list": ["daec*****8669"] # 自定义模板ID } } dict = bodys # print(type(dict)) html = posturl(url_request, data=dict) # print(html, type(html)) # <class 'str'> data = re.findall(finddata, html)[0] print(data[0], ':', data[1]) print(data[2], ':', data[3]) print(data[4], ':', data[5]) # ocr('/home/pi/Desktop/test2.png')

这里的自定义格式为上一篇文章中自定义OCR的血压指数。

最后

以上就是冷艳摩托最近收集整理的关于树莓派摄像头拍照上传阿里云自定义OCR识别的全部内容,更多相关树莓派摄像头拍照上传阿里云自定义OCR识别内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部