gpt4 book ai didi

python - 如何在opencv python中放置驻留时间

转载 作者:行者123 更新时间:2023-12-02 16:33:50 24 4
gpt4 key购买 nike

您好,我是一名大学生,我的代码有一些问题。在系统上检测到摩托车之后,我想在代码中给出3秒的等待时间,然后发出声音。我尝试操纵时间,但不起作用。有人可以帮忙我的代码吗

import cv2
import numpy as np
import pygame
import datetime as dt
from pygame import mixer
import time

#=============== Variable Mouse ==================#
drawing = False
point1 = ()
point2 = ()

drawingTwo = False
pointTwo_1 = ()
pointTwo_2 = ()
Mouse_count = False
#================================================#
def mouse_drawing(event, x, y, flags, params):
global point1, point2, drawing
global pointTwo_1, pointTwo_2, drawingTwo, Mouse_count

#----------Mouse 1-------
if Mouse_count == False:
if event == cv2.EVENT_LBUTTONDOWN:
if drawing is False:
drawing = True
point1 = (x, y)
#else:
#drawing = False

elif event == cv2.EVENT_MOUSEMOVE:
if drawing is True:
point2 = (x, y)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
Mouse_count = True

#----------Mouse 2-------#
if Mouse_count == True:
if event == cv2.EVENT_LBUTTONDOWN:
if drawingTwo is False:
drawingTwo = True
pointTwo_1 = (x, y)
elif event == cv2.EVENT_MOUSEMOVE:
if drawingTwo is True:
pointTwo_2 = (x, y)
elif event == cv2.EVENT_LBUTTONUP:
if drawingTwo is True:
drawingTwo = False
Mouse_count = False


#================================================#
lastTime = dt.datetime.now()
currentTime = dt.datetime.now()

#Make Sound
pygame.mixer.init()


#create VideoCapture object and read from video file


cap = cv2.VideoCapture('test13.mp4')

cv2.namedWindow("Detecion motor")
cv2.setMouseCallback("Detecion motor", mouse_drawing)

while True:
ret, frame = cap.read()
car_cascade = cv2.CascadeClassifier('cascade11.xml')

#============================== ROI One ============================#
if point1 and point2:

#Rectangle marker
r = cv2.rectangle(frame, point1, point2, (100, 50, 200), 5)
frame_ROI = frame[point1[1]:point2[1],point1[0]:point2[0]]

#------------------Detect car ROI-------------------#
if drawing is False:
#convert video into gray scale of each frames
ROI_grayscale = cv2.cvtColor(frame_ROI, cv2.COLOR_BGR2GRAY)
#detect cars in the video
cars_ROI = car_cascade.detectMultiScale(ROI_grayscale, 1.1, 3)
if len(cars_ROI) > 0:
if (currentTime-lastTime).seconds > 3:
lastTime = dt.dateTime.now()
sound = mixer.Sound('sirine2.wav')
sound.play()

for (x,y,w,h) in cars_ROI:
cv2.rectangle(frame_ROI,(x,y),(x+w,y+h),(0,255,0),2)
currentTime = dt.datetime.now()
#cv2.putText(frame_ROI, "Jumlah Motor : " + str(cars_ROI.shape[0]), (10,frame_ROI.shape[0] -25), cv2.FONT_HERSHEY_TRIPLEX, 0.5,(0,255,0), 1)
#-------------------------------------------------#
#==================================================================#

#============================== ROI Two ============================#

#==================================================================#
cv2.imshow("Detecion motor", frame)

if cv2.waitKey(25) & 0xFF == ord('q'):
break


cap.release()
cv2.destroyAllWindows()


我不知道我已经尝试使用time.sleep但它只是使我的系统延迟不能解决我的问题

最佳答案

您的缩进在示例中是错误的:

if (currentTime-lastTime).seconds > 3:
lastTime = dt.dateTime.now()
sound = mixer.Sound('sirine2.wav')
sound.play()

每次检测到摩托车并且您刚刚重置了上次变量时,它将播放声音。而是这样做:
if (currentTime-lastTime).seconds > 3:
lastTime = dt.dateTime.now()
sound = mixer.Sound('sirine2.wav')
sound.play()

关于python - 如何在opencv python中放置驻留时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61581273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com