I am trying to write a program with python using opencv library to create a security camera that detect faces and movements.
CODE :
import cv2
import time
import datetime
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
_, frame = cap.read()
cv2.imshow("Security Camera", frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
When i try to run the code i get this error message after the camera window opens and crashes within a second :
ERROR MESSAGE :
C:/Users/DELL/AppData/Local/Programs/Python/Python39/python.exe e:/coding/python/cv2-sec/main.py Traceback (most recent call last):
File "e:\coding\python\cv2-sec\main.py", line 10, in cv2.imshow("Security Camera", frame) cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
I tried all possible solutions available online with no luck. please support if possible. thnx.
CodePudding user response:
Try this:
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,640) # Width
cap.set(4,480) # Height
while True:
ret, img = cap.read()
cv2.imshow('video',img)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()