使用网络摄像头的事故检测项目

问题描述 投票:0回答:1

我在下面的代码中出现错误。我想从网络摄像头获取输入,如果发生任何意外,那么它将表明发生了意外。但是使用下面的代码,我什至没有得到摄像机视频提要。它显示以下错误:

AttributeError: 'module' object has no attribute 'CV_WINDOW_AUTOSIZE'

如果将其更改为cv2.WINDOW_NORMAL,则显示此错误:

C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\imgproc\src\color.cpp:9748: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

有时它会打开我的笔记本电脑摄像头LED,我想从网络摄像头中拍摄视频。

#Import necessary packages
import numpy as np
import cv2
import math, operator

#Function to find difference in frames
def diffImg(t0, t1, t2):
  d1 = cv2.absdiff(t2, t1)    
  d2 = cv2.absdiff(t1, t0)
  return cv2.bitwise_and(d1, d2)

j=1
#Import video from webcam
cam = cv2.VideoCapture(0)

#Creating window to display 
winName = "Accident Detector"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)

#Reading frames at multiple instances from webcam to different variables
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)


while True:
  #Display video out through the window we created
  cv2.imshow( winName, diffImg(t_minus, t, t_plus) )

  #Calling function diffImg() and assign the return value to 'p'
  p=diffImg(t_minus, t, t_plus)

  #Writing 'p' to a directory
  cv2.imwrite("D:\photos\shot.jpg",p)

  #From Python Image Library(PIL) import Image class
  from PIL import Image

  #Open image from the directories and returns it's histogram's
  h1 = Image.open("D:\motionpic\shot"+str(j)+".jpg").histogram()
  h2 = Image.open("D:\photos\shot.jpg").histogram()
  j=j+1

  #Finding rms value of the two images opened before        
  rms = math.sqrt(reduce(operator.add,map(lambda a,b: (a-b)**2, h1, h2))/len(h1))  
  print int(rms)

  #If the RMS value of the images are under our limit 
  if (rms<250):
    #Then there is a similarity between images. i.e., Scene similar to an accident is found
    print "accident\a"


  #Updates the frames
  t_minus = t
  t = t_plus
  t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

  #Destroys the window after key press
  key = cv2.waitKey(10)
  if key == 27:
    cv2.destroyWindow(winName)
    break 
python opencv anaconda webcam
1个回答
0
投票

创建新窗口的正确语法是

cv2.namedWindow(winName, cv2.WINDOW_AUTOSIZE)

也应在while循环下读取摄像机帧。

© www.soinside.com 2019 - 2024. All rights reserved.