演示与讲解视频:
b站视频
健康云:
核酸码:
#环境支持:pytorch(GPU版本更快)及导入的库
#使用方法:
#将该文件与所有图片放在同一目录下(路径无中文)即可,勿改文件名
import os
import re
import easyocr
import cv2
import openpyxl
#文件夹的路径
# path1 = 'C:/Users/ASUS/Desktop/ocr'
path1 = os.getcwd()
reader = easyocr.Reader(['ch_sim','en'])
name_com = {} #现名:原名
files1 = os.listdir(path1)
files1.remove('test.py')
# files1.remove('ocr.ipynb')
#批量重命名文件(图片),
index_no = 1
for file in files1:
imagepath = os.path.join(path1, file)
newname = str(index_no) + '.jpg'
newimagename = os.path.join(path1, newname)
os.rename(imagepath, newimagename)
name_com[str(index_no)+'.jpg'] = file
# print("以前的文件名和对应现在的序号", file + '---' + str(index_no))
index_no += 1
files1 = os.listdir(path1)
files1.remove('test.py')
# files1.remove('ocr.ipynb')
#print(files1)
dic = {}
#更改图像大小
for i in range(len(files1)):
img = cv2.imread(path1+'/'+files1[i])
size = (1080 ,2340) #注意排序是反的,出来顺序是500*400
img = cv2.resize(img, size)
cv2.imwrite(path1+'/'+files1[i],img)
#编号,需将所有图片与本文件放置在同意目录下
files1 = os.listdir(path1)
files1.remove('test.py')
# files1.remove('ocr.ipynb')
for j in range(len(files1)):
print(files1[j])
#识别每一个
result = reader.readtext(files1[j]) #easyocr
content = str(result)
#2022-04 -04.1820:20
p = re.compile(r'(\d{4}-\d{2}[-.:\d]*)')
print(p.findall(content))
dic[files1[j]] =p.findall(content)
num = 0
#显示核酸码,不用要删掉另一个
for i in dic.keys():
if(len(dic[i]) >=3):
print(i+'检测时间为:',end='')
print(dic[i][1],end='')
print(i+'结果时间为:',end='')
print(dic[i][2])
else:
print(i+'异常')
num = num + 1
print('异常数目:'+str(num))
# 创建一个Excel workbook 对象
book = openpyxl.Workbook()
# 创建时,会自动产生一个sheet,通过active获取
sh = book.active
sh.title = '年龄表'
# 写标题栏
sh['A1'] = '原图名称'
sh['B1'] = '现在名称'
sh['C1'] = '截图时间'
sh['D1'] = '第一次检测时间(最新)'
sh['E1'] = '第一次结果时间(最新)'
sh['F1'] = '第二次检测时间'
sh['G1'] = '第二次结果时间'
sh['H1'] = '第三次检测时间'
sh['I1'] = '第三次结果时间'
# 写入内容
row = 2
#先写名字
for name,num in dic.items():
sh.cell(row, 1).value = name_com[name]
sh.cell(row, 2).value = name
for i in range(len(num)):
sh.cell(row, i+3).value = num[i][0:10]
row += 1
# 保存文件
book.save('信息.xlsx')