Reputation: 255
what i am trying to do in my code below is to create an opencv program with python to open my laptop webcam and the filter the camera so that it will only show my clothes. but i coudnt even run the program because i have encounter an error that seem to be coming from the 10th line of the code. it is definitely not a misspeal error, i double checked it.
the code sample
#color filtering
import cv2
import numpy as np
#use camera
cap = cv2.VideoCapture(1)
while True:
_, frame = cap.read()
`this line seem to be the source-->` hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# hsv hue sat value
# try to get the value of the color that you want
lower_red = np.array([150,150,150])
upper_red = np.array([180,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(frame,frame, mask = mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('result',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
#release camera
cap.release()
the error
Traceback (most recent call last):
File "D:/Program_Files/Python/legit8.py", line 10, in <module>
hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cv2.error: C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:10705: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor
Upvotes: 0
Views: 416
Reputation: 5172
Your frame is probably None
. This could be because of VideoCapture(1)
. If your webcam is the only cam connected to your computer, use VideoCapture(0)
!
Upvotes: 3