基本运动检测-python3

时间:2021-04-25 13:32:07

标签: python-3.x video-streaming motion motion-detection

我正在尝试制作一个基本的运动检测程序 它给出了多个错误。 你能解释一下为什么会这样吗? 如果你也能解释一下,那将是一个很大的帮助..

这是程序:-

from imutils.video import VideoStream      
import argparse                            
import datetime                            
import imutils                              
import time
import cv2                                


ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="Path of video file" )
ap.add_argument("-a", "--min-area", type=int, default=500, help="Minimum area size")
args=vars(ap.parse_args())


#if the video argument is none, so start webcam
if args.get("video", None) is None:
    vs = VideoStream(scr=0).start()
    time.sleep(2.0)


# otherwise read from video file
else:
    vs = cv2.VideoCapture(args["video"])

# inititaizing the first frame
firstFrame = None

#loop for frames of vid
while True:
    frame=vs.read()
    frame=frame if args.get("video", None) is None else frame[1]
    text = "no motion is dectected"

    #if wew can read file , then end the vid
    if frame is None:
        break


    #resixing the frame
    frame = imutils.resize(frame, width=500)
    gray = cv2.cvtColor(frame, cv2.COLOR_BAYER_BG2BGR)          <<------------- line 42--------------
    gray = cv2.GaussianBlur(gray, (21,21),0)          #avg pixel intensity to smooth frame


    #if first is none
    if firstFrame is None:
        firstFrame = gray
        continue

    #to compair the absolute diffrence b/w the current and the first frame, which is stored
    frameDelta = cv2.absdiff(firstFrame, gray)
    thresh = cv2.threshold(frameDelta, 25, 255 ,cv2.THRESH_BINARY)[1]


    #dilate the threshold image to fill in holes, then find contours on thresholded image
    thresh = cv2.dilate(thresh, None, iterations=2)
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)

    #loop the counters
    for c in cnts:
        #if counter is too small, ignor them
        if cv2.contourArea(c) < args["min_area"]:

            #compute it, update text accordingly
            (x, y, w, h) = cv2.boundingRect(c)
            cv2.rectangle(frameDelta, (x,y), (x+w, y+h), (0,255, 0), 2)
            text = "Motion is dected"
        else:
            pass
        font = cv2.FONT_HERSHEY_SIMPLEX 
        cv2.putText(frame, '{+} Room Status: %s' % (text), 
            (10,20), cv2.FONT_HERSHEY_SIMPLEX , 0.5, (0, 0, 255), 2)
        cv2.imshow('Security Feed', frame)
        cv2.imshow('Threshold(foreground mask)', dilate_image)
        cv2.imshow('Frame_delta', frame_delta)

        key = cv2.waitKey(1) & 0xFF # (1) = time delay in seconds before execution, and 0xFF takes the last 8 bit to check value or sumin
        if key == ord('q'):
            cv2.destroyAllWindows()
            break
#clear up screen
vs.stop() if args.get("video", None) is None else vs.release()
cv2.destroyAllWindows()

错误是:

文件“f:\projects\python\motion detection.py”,第 42 行,在 灰色 = cv2.cvtColor(框架,cv2.COLOR_BAYER_BG2BGR) cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-hhhrywxd\opencv\modules\imgproc\src\demosaicing.cpp:1721: 错误: ( -215:断言失败) scn == 1 && (dcn == 3 || dcn == 4) in function 'cv::demosaicing'

File "f:\projects\python\motion detection.py", line 42, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BAYER_BG2BGR)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-hhhrywxd\opencv\modules\imgproc\src\demosaicing.cpp:1721: error: (-215:Assertion failed) scn == 1 && (dcn == 3 || dcn == 4) in function 'cv::demosaicing'

感谢您的帮助!!

0 个答案:

没有答案
相关问题