本文已参与「新人创作礼」活动,一起开启掘金创作之路。
前言:
今天不讲Java,来一个小插曲
有朋友想学小程序行程码识别系统,今天就来简单的讲一讲
首先要有一个服务器设备:树莓派4b,舵机驱动板
运用python软件的tk模块来控制截图,每当用户点击识别按钮
的时候系统自动进行截图,并运行识别函数,进行比对。通过识别
文字函数调用了百度ocr识别的api,对图片的文字进行提取。然后
对返回的json进行判断,得出一个值,如果结果为可用数据,则运
行,运行打开舵机的函数,结果为假数据则舵机归位。并且舵机打
开之后在五秒之后将会自动归位。
encoding: utf-8
import time import requests import base64 import os from tkinter import *
def camera(): os.system("fswebcam --no-banner -r 1920*1080 new.jpg") text1.delete(0.0, END) text1.insert("insert", '正在识别中.....\n') text1.update() text1.insert("insert", '捕获图像完成!\n') text1.update()
def judge(): request_url = "aip.baidubce.com/rest/2.0/oc…" f = open(r'/home/pi/Desktop/demo/new.jpg', 'rb') # f = open(r"C:\Users\Administrator\Desktop\6.jpeg", "rb") img = base64.b64encode(f.read()) count = 0
text1.insert("insert", '识别图像完成!\n')
text1.update()
params = {"image": img}
access_token = '24.0751cceecef2bb40343aba5bb9970ab4.2592000.1656209556.282335-26317738'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
places = ""
if response:
words_result = response.json()["words_result"]
# 记录途经地区结束的下标
end_index = len(words_result)
for x in range(len(words_result)):
if "结果" in list(words_result[x].values())[0] or "包含" in list(words_result[x].values())[0] or "小时" in \
list(words_result[x].values())[0]:
end_index = x
for index in range(len(words_result)):
v = words_result[index].values()
if "您于前14天内到达或途经" in list(v)[0]:
count += 1
places = list(v)[0][13:]
for i in range(index + 1, end_index):
places += list(words_result[i].values())[0]
print(places)
places_list = places.split(",")
p1 = '您去过' + places + '.....\n'
text1.insert("insert", p1)
text1.update()
if len(places_list) == 1:
if "杭州" in places_list[0]:
count = 1
else:
count = 2
elif len(places_list) > 1:
count = 2
else:
count = 0
print(count)
if count >= 2:
text1.insert("insert", '您14天内出过杭州市,不可通行!\n')
text1.update()
os.system("python3 pwm0.py")
elif count == 1:
text1.insert("insert", '您14天内未出过杭州市,可通行!')
text1.update()
os.system("python3 pwm90.py")
elif count == 0:
text1.insert("insert", '未检测到有效的行程码!')
text1.update()
def run():
camera()
time.sleep(1)
judge()
window = Tk()
window.minsize(600, 500)
window.title("行程码自动识别系统")
btn = Button(window, text='开始识别', command=run, height=4, width=23, font=('黑体', 22, 'bold'), bd=5)
btn.pack(pady=25)
text1 = Text(window, width=45, height=10, font=('黑体', 14, 'bold'))
text1.pack()
window.mainloop()
- 以上代码仅供参考