Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在OpenCV中设定时间后自动关闭摄影机流?_Opencv_Computer Vision_Video Processing - Fatal编程技术网

如何在OpenCV中设定时间后自动关闭摄影机流?

如何在OpenCV中设定时间后自动关闭摄影机流?,opencv,computer-vision,video-processing,Opencv,Computer Vision,Video Processing,我正在尝试编写一个程序,该程序将检测运动,并在使用OpenCV超过5秒没有运动时关闭相机。该代码打印“运动”并在关闭相机之前捕获运动约8-10秒,无论是否有运动。我不确定是哪部分代码导致了这种情况,如果您能提供帮助,我们将不胜感激 import cv2 import time cap=cv2.VideoCapture(0) ret1,background= cap.read() gray1 = cv2.cvtColor(background, cv2.COLOR_BGR2GRAY) gray

我正在尝试编写一个程序,该程序将检测运动,并在使用OpenCV超过5秒没有运动时关闭相机。该代码打印“运动”并在关闭相机之前捕获运动约8-10秒,无论是否有运动。我不确定是哪部分代码导致了这种情况,如果您能提供帮助,我们将不胜感激

import cv2
import time

cap=cv2.VideoCapture(0)

ret1,background= cap.read()
gray1 = cv2.cvtColor(background, cv2.COLOR_BGR2GRAY)
gray1 = cv2.GaussianBlur(gray1, (21, 21), 0)
cv2.imshow('window',background)
t0 = time.time() # start time in seconds
imgCounter = 0

def getMovement (img):
    motion = None
    gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
    gray2 = cv2.GaussianBlur(gray2, (21, 21), 0)

    frameComparison=cv2.absdiff(gray1,gray2)
    threshold = cv2.threshold(frameComparison, 25, 255, cv2.THRESH_BINARY)[1]
    threshold = cv2.dilate(threshold,None)
    countour,heirarchy = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for i in countour:
        if cv2.contourArea(i) < 50:
            motion = False
            continue
        motion = True
    return motion



while(True):
    ret2,frame2=cap.read()
    motion = getMovement(frame2)

    cv2.imshow('window',frame2)

    cv2.waitKey(1)
    if motion:
        print("MOTION")
    else: # no motion for a bit, so the timer starts 
         t1 = time.time() # current time
         num_seconds = t1 - t0 # diff
         if num_seconds > 5: 
         # once it hits 5s, it should take a picture and turn off
            print("No motion in 5s")
            cv2.imwrite("Motion test.png", frame2)
            break
cap.release()
cv2.destroyAllWindows()
导入cv2
导入时间
cap=cv2.视频捕获(0)
ret1,background=cap.read()
gray1=cv2.CVT颜色(背景,cv2.COLOR\u BGR2GRAY)
gray1=cv2.GaussianBlur(gray1,(21,21,0)
cv2.imshow(“窗口”,背景)
t0=时间。时间()#以秒为单位的开始时间
imgCounter=0
def getMovement(img):
运动=无
gray2=cv2.CVT颜色(frame2,cv2.COLOR_BGR2GRAY)
gray2=cv2.GaussianBlur(gray2,(21,21,0)
frameComparison=cv2.absdiff(灰色1,灰色2)
threshold=cv2.threshold(帧比较,25255,cv2.THRESH_二进制)[1]
阈值=cv2。扩张(阈值,无)
countour,继承人=cv2.findContours(阈值,cv2.RETR\u外部,cv2.CHAIN\u近似值\u简单值)
对我来说:
如果cv2.面积(i)<50:
运动=假
持续
运动=真
返回运动
虽然(正确):
ret2,frame2=cap.read()
运动=获取运动(第2帧)
cv2.imshow('窗口',框架2)
cv2.等待键(1)
如果动议:
打印(“动议”)
否则:#一点也不动,所以计时器启动
t1=时间。时间()#当前时间
num_seconds=t1-t0#diff
如果num_seconds>5:
#一旦达到5s,它应该拍照并关闭
打印(“5s内无动作”)
imwrite(“Motion test.png”,frame2)
打破
第1章释放()
cv2.destroyAllWindows()

我认为有一个小错误,即当检测到运动时,您不是
t0
。因此,在检查无运动的情况下,取当前时间与计时器在开始时启动的时间之间的差值

因此,在循环中尝试以下方法:

if motion:
    t0 = time.time()
    print("MOTION")

我认为有一个小错误,即当检测到运动时,您不是
t0
。因此,在检查无运动的情况下,取当前时间与计时器在开始时启动的时间之间的差值

因此,在循环中尝试以下方法:

if motion:
    t0 = time.time()
    print("MOTION")

我现在刚试过,这正是我所需要的!我没想到要重新启动计时器。谢谢。我现在刚试过,这正是我所需要的!我没想到要重新启动计时器。非常感谢。