微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当对象触摸特定行集时如何在视频帧中设置计时器?

如何解决当对象触摸特定行集时如何在视频帧中设置计时器?

当物体检测(在我的情况下检测汽车)触及预定义的矩形边缘时,我试图设置一个计时器。在最终工作的细节中,当汽车边缘接触到预定义矩形边缘的边缘时,计时器开始计数,几秒钟后,如果汽车仍在帧中,则计数器变量加 1。 This is the shown image of my code.

以下是我的代码

import numpy as np
from time import sleep
from datetime import datetime

width_min=50 #Minimum width of the rectangle
height_min=50 #Minimum height of the rectangle

offset=6 #Allowable error between pixel

pos_line=550 #Count Line Position

delay= 60 #Video FPS

detec = []
cartop = 0
carbottom = 0
carleft = 0
carright = 0

object_id_list = []
dtime = dict()
dwell_time = dict()
c = 0

    
def catch_centro(x,y,w,h):
    x1 = int(w / 2)
    y1 = int(h / 2)
    cx = x + x1
    cy = y + y1
    return cx,cy

img = cv2.imread('2017-02-03-image-9.jpg')
cap = cv2.VideoCapture('video.mp4')
subtracao = cv2.bgsegm.createBackgroundSubtractormog()

while True:
    ret,frame1 = cap.read()
    tempo = float(1/delay)
    sleep(tempo) 
    grey = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(grey,(3,3),5)
    img_sub = subtracao.apply(blur)
    dilat = cv2.dilate(img_sub,np.ones((5,5)))
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
    dilatada = cv2.morphologyEx (dilat,cv2. MORPH_CLOSE,kernel)
    dilatada = cv2.morphologyEx (dilatada,kernel)
    contorno,h=cv2.findContours(dilatada,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
   
    cv2.rectangle(frame1,(125,225),(pos_line,pos_line),(255,127,0),3) 
   
    for(i,c) in enumerate(contorno):
        (x,h) = cv2.boundingRect(c)
        validar_contorno = (w >= width_min) and (h >= height_min)
        
        if not validar_contorno:
            continue

        if x > 150 and x < 900:

            cv2.rectangle(frame1,(x,y),(x+w,y+h),(0,255,2) 
            z = y+h
            o = x+w
            centro = catch_centro(x,h)
            detec.append((x,z,o))

            #for (i) in detec:
            

            for (x,o) in detec:
                if y<(pos_line+offset) and y>(pos_line-offset) and x < 550:
                    cartop+=1
                    cv2.rectangle(frame1,(550,255),3) 
                    detec.remove((x,o))
                    #print("car is detected : "+str(cartop))      
                elif (z)<(pos_line+offset) and (z)>(pos_line-offset) and x < 550:
                    carbottom+=1
                    cv2.rectangle(frame1,pos_line+offset),pos_line-offset),o))
                    if i not in object_id_list:
                        object_id_list.append(i)
                        dtime[i] = datetime.Now()
                        dwell_time[i] = 0
                    else:
                        curr_time = datetime.Now()
                        old_time = dtime[i]
                        time_diff = curr_time - old_time
                        dtime[i] = datetime.Now()
                        sec = time_diff.total_seconds()
                        dwell_time[i] += sec
                
               
                    cv2.putText(frame1,str(int(dwell_time[i])),y-5),cv2.FONT_HERShey_COMPLEX_SMALL,1,1)
                    print(int(dwell_time[i]))
                elif (o)<(pos_line+offset) and (o)>(pos_line-offset) and z < 550:
                    carright+=1
                    cv2.rectangle(frame1,o))
                elif (y)<(229) and (y)>(221) and z < 550:
                    carleft+=1
                    cv2.rectangle(frame1,o))
                    
                 
       
    cv2.putText(frame1,"VEHICLE COUNT : "+str(cartop) + "(" + str(carbottom) + ")" + "(" + str(carleft) + ")" + "(" + str(carright) + ")",(400,70),cv2.FONT_HERShey_SIMPLEX,2,5)
    cv2.imshow("Video Original",frame1)
    cv2.imshow("Detectar",dilatada)

    if cv2.waitKey(1) == 27:
        break
    
cv2.destroyAllWindows()
cap.release()

当我使用 i 访问列表时,该列表在代码中混淆,其中显示的计时器与dave_time 中的索引不对应。

定时器参考:https://www.youtube.com/watch?v=qn26XSinYfg&list=PLWw98q-Xe7iH8UHARl8RGk8MRj1raY4Eh&index=12

计数器和视频参考:https://www.youtube.com/watch?v=IdBnsNttXq0

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?