# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author : v_jiaohaicheng@baidu.com
@des :
"""
import pyzbar.pyzbar
from PIL import Image, ImageEnhance
import cv2
import matplotlib.pyplot as plt
import numpy as np
from threading import Thread
from datetime import datetime
import qrcode
import hashlib
from queue import Queue
from PIL import ImageFont, ImageDraw, Image
MAP = {}
class ScanCode():
def __init__(self):
self.shop_list = []
# self.set_log = Queue()
self.scan()
def make_md5(self,item: str, salt: bytes = b""):
"""
计算输入文本的 md5
:param item:输入的文本
:return: 输入的文本对应的md5
"""
# item = item.rstrip("\n")
md5_machine = hashlib.md5(salt)
md5_machine.update(item.encode('utf-8'))
return md5_machine.hexdigest()
def decode(self,input):
barcodes = pyzbar.decode(input)
if barcodes == []:
pass
else: # 识别出二维码
for barcode in barcodes:
barcodeData = barcode.data.decode("utf-8")
# print(barcodeData)
name,sell,weight,date1,date2 = barcodeData.split(" ")
sell = float(sell.replace("元/公斤",""))
weight = float(weight.replace("公斤",""))
date_shelves = "{} {}".format(date1,date2)
price = float(sell*weight)
info = {
"name":name,
"sell":sell,
"weight":weight,
"date_shelves":date_shelves,
"date_sold":str(datetime.now()).split(".")[0],
"price":price
}
secret = "{}{}{}{}{}".format(name,sell,weight,date_shelves,price)
md5_info = self.make_md5(secret)
if MAP.get(md5_info) is None :
print("商品信息: ",name, sell, weight, "{} {}".format(date1, date2))
self.shop_list.append(info)
MAP[md5_info] = info
def calculate_result(self):
all_price = 0
for i in self.shop_list:
all_price += i["price"]
return round(all_price,2)
def scan(self):
camera = cv2.VideoCapture(0)
while camera.isOpened():
# 读取当前帧
ret, frame = camera.read()
cv2.imshow("video", frame)
# 转为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
Thread(target=self.decode,args=(gray,)).start()
key = cv2.waitKey(1) # 每一帧图像显示时间,相当于刷新率
if key == 27:
all_price = self.calculate_result()
print("本次共计消费:{}元".format(all_price))
self.shop_list.clear()
MAP.clear()
# break
camera.release()
cv2.destroyAllWindows()
class MakeCode():
"""
生成商品信息二维码
"""
def __init__(self,name,sell,weight):
self.name = name
self.sell = sell
self.weight = weight
self.make_code()
def make_code(self):
contain = "{} {}元/公斤 {}公斤 {}".format(self.name,self.sell,self.weight,str(datetime.now()).split(".")[0])
qr = qrcode.QRCode(version=2,
error_correction=qrcode.constants.ERROR_CORRECT_H,
)
qr.add_data(contain)
qr.make(fit=True)
img = qr.make_image()
img.save(f'{name}-{sell}(二维码).png')
img = Image.open(f'{name}-{sell}(二维码).png')
draw = ImageDraw.Draw(img)
ttfront = ImageFont.truetype(R"D:\Projects\SupportRequirements\Project\image\mark\SourceHanSerifSC-Bold.otf", 14)
content = "{}\t{}元/公斤\t{}公斤".format(self.name,self.sell,self.weight)
draw.text((140, 460), content,font=ttfront)
img.save(f'{name}-{sell}(二维码).png')
if __name__ == '__main__':
ScanCode()
# name = "新鲜豆角2"
# sell = 8.88
# weight = 1.99
# MakeCode(name,sell,weight)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156868.html