视频识别函数##

视频识别函数##

#!/usr/bin/env python
# encoding: utf-8
# author: zs
# license: (C) Copyright 2019-2020
# software: pycharm
# file: HDliveObjDetection.py
# time: 2020/4/10 下午4:16

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 GPIO

fon = 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:5005/decimage", json=json_data).text    #设置另一台机器端口号
    et = time.time()
    objs = r.encode('utf-8').decode('unicode_escape')
    print('time', et-st)
    return objs


if __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 = 0
            num1 = 0
            num2 = 0
            num3 = 0
            num4 = 0
            print(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 + 1
                if 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 + 1
                if 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 + 1
                    
                if 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 + num4
            m_m = 3.5            #美年达单价
            k_m = 3.0
            x_m = 2.5
            f_m = 4.0
            g_m = 2.0

            s_m = m_m * num2          #美年达总价
            s_k = k_m * num
            s_x = x_m * num1
            s_f = f_m * num3
            s_g = g_m * num4

            

            s_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) & 0xff
            if k == 27:
                break
cam.release()
cv2.destroyAllWindows()
ser.write("page 0".encode("utf-8"))#发送可乐总价
ser.write(b'\xFF\xFF\xFF')
ser.flushInput()
ser.close()





        
   

 

上一篇:局域网群聊


下一篇:多对多表的增删改查与drf的分页