2021-08-08 识别二维码学习

python初学者,在医院看到抽血时,在收集血管的瓶子上扫描一下二维码后,就可以录入信息,于是想着参考编写一个二维码扫描后打卡小程序,程序可以读取二维码信息作为一个输入姓名和id号,二维码解析从网上参考的,程序如下,后面会在此基础上加入多线程继续完善

from pyzbar.pyzbar import  decode
import cv2
import numpy as np
import threading
import time

# param name is str
# param work_id is str
class Person:
    def __init__(self, name, work_id):
        self.name = name
        self.work_id = work_id
        self.work_on_time = 0
        self.work_off_time = 0

    def time_record(self, time):
        if  self.work_on_time == 0:
            self.work_on_time = time
        else:
            self.work_off_time = time

# 对象列表
person_list = []

# 画框
def paint_line(info, img):
    pts = np.array([info.polygon], np.int32)
    pts = pts.reshape((-1,1,2))
    cv2.polylines(img, [pts], True, (255, 0, 255), 5)
    pts2 = info.rect
    cv2.putText(img, info.data.decode('utf-8'), (pts2[0], pts2[1]), cv2.FONT_HERSHEY_COMPLEX, 0.9, (255, 0, 255))

def decode_img_info(img):

    # print('开始识别信息:')

    for info in decode(img):
        info_decode = info.data.decode('utf-8')

        print(info)
        print(info_decode)
        # 画框
        paint_line(info, img)
        print((info_decode))

        my_info = info_decode.split()
        name_str = my_info[0][5:]
        id_str = my_info[1][3:]
        print(my_info)

        print('姓名:', name_str)
        print('工号:', id_str)

        cnt = 0
        for person in person_list:
            cnt = cnt +1
            if person.name == name_str and person.work_id == id_str:
                print('已经有存储:\n 姓名:', name_str, ' 工号:', id_str)
                break

            if cnt == len(person_list):
                cnt = 0
                print('数据库中没有您的信息, 请至管理员添加信息')


def add_person():
    person1 = Person('zhao', '349318')
    person2 = Person('zhao', '1234')
    person_list.append(person1)
    person_list.append(person2)

if __name__ == '__main__':

    add_person()
    print('现在时间=', time.time())
    print('二维码测试')

    cap = cv2.VideoCapture(0)

    while True:
        is_succ, img = cap.read()
        decode_img_info(img)

        cv2.imshow('QRCode', img)
        if (cv2.waitKey(1) == 27):
            break

    cap.release()
    cv2.destroyAllWindows()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值