admin 管理员组

文章数量: 887021

retail

import cv2
import time
import requests
import base64
from PIL import Image,ImageDraw,ImageFont
import numpy as np
import serial      #导入serial函数
import RPi.GPIO as GPIOfon = cv2.FONT_HERSHEY_SIMPLEX
font = ImageFont.truetype("simsun.ttc", 30, encoding="utf-8") # 参数1:字体文件路径,参数2:字体大小
ser = serial.Serial('/dev/ttyAMA0', 9600)#使用树莓派的GPIO口连接串行口,TTYS0根据自己系统设置,115200为波特率
if ser.isOpen == False:       #看看这个串口是否已经被打开ser.open()                # 打开串口
ser.flushInput()
def get_data(imgarray, score_thr):retval, buffer = cv2.imencode('.jpg', imgarray)image = str(base64.b64encode(buffer), 'utf-8')json_data = {'image': image, 'score_thr': score_thr}st = time.time()r = requests.post("http://172.16.16.209:5004/decimage", json=json_data).text    #设置另一台机器端口号et = time.time()objs = r.encode('utf-8').decode('unicode_escape')print('time', et-st)return objsif __name__=='__main__':cam = cv2.VideoCapture(0)    # 调用0号摄像头color = (255, 0, 0)          # 为左上角字体颜色score_thr = 0.7             #致信率#count the frame, only one frame test in each N frames count = 0                   #帧数N = 1                       #下面代码对帧数取余使用print(1)                    #循环前的检测节点while cam.isOpened():count +=1       #帧数自加,之后调整改帧的尺寸is_opened, frame = cam.read()frame = cv2.resize(frame, (1280, 720))print(2)        #检测摄像头是否处于开启状态if count%N==0:st = time.time()result = eval(get_data(frame, score_thr))et = time.time()num = 0num1 = 0num2 = 0num3 = 0num4 = 0print(result)for re in result: #上下对应na = re.get('name')if na ==  ("Kele") :#画框cv2.rectangle(frame, (int(re.get('xmin')), int(re.get('ymin'))),#x轴小,y轴小,x轴大,y轴大,(int(re.get('xmax')), int(re.get('ymax'))), (0, 0, 255), 4)#四个角坐标,矩形框颜色,四边形#放文字cv2.putText(frame, re.get('similarity'), (int(re.get('xmin')), int(re.get('ymax'))), fon, 1, (255, 0, 0), 2)#获得一体机中'similarity'的信息,坐标,字体,字体大小,字体颜色,字体粗细num = num + 1       #该品种饮料数量if na ==  ("Xuebi") :cv2.rectangle(frame, (int(re.get('xmin')), int(re.get('ymin'))),(int(re.get('xmax')), int(re.get('ymax'))), (0, 0, 255), 4)cv2.putText(frame, re.get('similarity'), (int(re.get('xmin')), int(re.get('ymax'))), fon, 1, (0, 0, 255), 2)num1 = num1 + 1if na ==  ("Sprite") :cv2.rectangle(frame, (int(re.get('xmin')), int(re.get('ymin'))),(int(re.get('xmax')), int(re.get('ymax'))), (0, 255, 0), 4)cv2.putText(frame, re.get('similarity'), (int(re.get('xmin')), int(re.get('ymax'))), fon, 1, (0, 255, 0), 2)num2 = num2 + 1if na ==  ("Fenda") :cv2.rectangle(frame, (int(re.get('xmin')), int(re.get('ymin'))),(int(re.get('xmax')), int(re.get('ymax'))), (0,255, 255), 4)cv2.putText(frame, re.get('similarity'), (int(re.get('xmin')), int(re.get('ymax'))), fon, 1, (0,255, 255), 2)num3 = num3 + 1if na ==  ("Ganten") :cv2.rectangle(frame, (int(re.get('xmin')), int(re.get('ymin'))),(int(re.get('xmax')), int(re.get('ymax'))), (255, 0, 0), 4)cv2.putText(frame, re.get('similarity'), (int(re.get('xmin')), int(re.get('ymax'))), fon, 1, (255, 0, 0), 2)num4 = num4 + 1		yinliao = num + num1 + num2 + num3 + num4m_m = 3.5            #美年达单价k_m = 3.0x_m = 2.5f_m = 4.0g_m = 2.0s_m = m_m * num2          #美年达总价s_k = k_m * nums_x = x_m * num1s_f = f_m * num3s_g = g_m * num4s_z =s_m + s_k + s_x + s_f + s_g    #饮料总价uart5="n5.val="+str(num2)ser.write((uart5).encode("utf-8"))#发送美年达数量ser.write(b'\xFF\xFF\xFF') #十六进制uart6="n6.val="+str(num)ser.write((uart6).encode("utf-8"))#发送可乐数量ser.write(b'\xFF\xFF\xFF')uart7="n7.val="+str(num1)ser.write((uart7).encode("utf-8"))#发送雪碧数量ser.write(b'\xFF\xFF\xFF')uart8="n8.val="+str(num3)ser.write((uart8).encode("utf-8"))#发送芬达数量ser.write(b'\xFF\xFF\xFF')uart9="n9.val="+str(num4)ser.write((uart9).encode("utf-8"))#发送百岁山数量ser.write(b'\xFF\xFF\xFF')uart10="t0.txt="+'"'+str(s_m)+'"' #str(s_m)两边加上'"' 是字符串ser.write((uart10).encode("utf-8"))#发送美年达总价ser.write(b'\xFF\xFF\xFF')uart11="t1.txt="+'"'+str(s_k)+'"'ser.write((uart11).encode("utf-8"))#发送可乐总价ser.write(b'\xFF\xFF\xFF')uart12="t2.txt="+'"'+str(s_x)+'"'ser.write((uart12).encode("utf-8"))#发送雪碧总价ser.write(b'\xFF\xFF\xFF')print(uart12)uart14="t3.txt="+'"'+str(s_f)+'"'ser.write((uart14).encode("utf-8"))#发送芬达总价ser.write(b'\xFF\xFF\xFF')uart13="t4.txt="+'"'+str(s_g)+'"'ser.write((uart13).encode("utf-8"))#发送百岁山总价ser.write(b'\xFF\xFF\xFF')cv2img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)pilimg = Image.fromarray(cv2img)draw = ImageDraw.Draw(pilimg)draw.text((60,10),"智能零售系统",(255,255,255),font=font)draw.text((10,40)," 名称  数量 单价 金额"+'\n'+'可乐  : '+str(num)+'    '+str(k_m)+'  '+str(s_k)+'\n'+'雪碧  : '+str(num1)+'    '+str(x_m)+'  '+str(s_x)+'\n'+'芬达  : '+str(num3)+'    '+str(f_m)+'  '+str(s_f)+'\n'+'百岁山: '+str(num4)+'    '+str(g_m)+'  '+str(s_g)+'\n'+'美年达: '+str(num2)+'    '+str(m_m)+'  '+str(s_m)+'\n'+'共计  : '+str(yinliao)+'         '+str(s_z),(255,255,255),font=font)cv2charimg = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)cv2.namedWindow('retail',cv2.WINDOW_FREERATIO)cv2.resizeWindow('retail',1820,920)cv2.imshow('retail', cv2charimg)cv2.waitKey(1)k = cv2.waitKey(10) & 0xffif k == 27:break
cam.release()
cv2.destroyAllWindows()
ser.write("page 0".encode("utf-8"))#发送可乐总价
ser.write(b'\xFF\xFF\xFF')
ser.flushInput()
ser.close()

 

本文标签: Retail