1、安装OCR库
#这次装了不少的库主要是库之间有依赖pip3 install common dual tight data prox shapely paddle paddleocr paddlepaddle
2、文本识别
from paddleocr import PaddleOCR, draw_ocrfrom PIL import Imageocr = PaddleOCR(use_angle_cls=True, lang='ch')img_path = '/Users/linux/Downloads/WechatIMG1.jpeg'result = ocr.ocr(img_path, cls=True)city_exist = Falsefor text in result:exist = False#print(text[1][0])if text[1][0].find("动态行程卡") > -1:exist = Trueelif text[1][0].find('更新于') > -1:exist = Trueelif text[1][0].find('14天内到达') > -1:city_exist = Trueelif text[1][0].find("结果包含") > -1:city_exist = Falseif exist == True or city_exist == True:print(text[1][0])if text[1][0].find("绿色行程卡") > -1:print("绿色")elif text[1][0].find("红色行程卡") > -1:print("红色")
3、健康码识别结果

(图一)
(图二)
(图三)
小程序
健康码截图
识别结果
通信行程卡
(如上图一)
绿色
xxxxxxx的动态行程卡
更新于:2022.04.1115:15:42
您于前14天内到达或途经:河北省xxxx市
北京健康宝
(如上图三)
2022年04月11日
15:17:54
未见异常①
名:
姓
x
身份证号:
查询时间:
04-1115:17
失效时间:
04-1124:00
河北健康码
(如上图二)
河北健康码
姓名
xx
身份证号
xxxxxxxx
2022-04-1115:15:58
核酸检测:阴性(2021-11-26
检测机构:北京xxxx医院(西城院区)
4、颜色识别
代码中我们是根据识别的文本来判断是绿码还是红码,难免根据小程序的升级判断结果有误。希望可以根据截图的图片的主要颜色来判断绿码还是红码保险准确些。
import colorsysimport PIL.Image as Imagedef get_dominant_color(image): max_score = 0.0001 dominant_color = None for count,(r,g,b) in image.getcolors(image.size[0]image.size[1]): # 转为HSV标准 saturation = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)[1] y = min(abs(r2104+g4130+b802+4096+131072)>>13,235) y = (y-16.0)/(235-16) #忽略高亮色 if y > 0.9: continue score = (saturation+0.1)count if score > max_score: max_score = score dominant_color = (r,g,b) return dominant_colorif __name__ == '__main__': image = Image.open('/Users/linux/Downloads/WechatIMG1.jpeg') image = image.convert('RGB') print(get_dominant_color(image)) 执行结果: (83, 164, 108)[RGB绿色]
5、结论
根据我们识别通信行程卡、北京健康宝、河北健康码的截图。整体OCR的识别还是很准确的。基本上重要的我们想要的数据都可以识别出来,以后可以多用PaddleOCR库识别图片。大家有什么不同的观点可以提出来沟通讨论。
感谢大家的评论、点赞、分享、关注。。。