Ask Your Question
0

cv2.VideoCapture(0).read() return always 'none' value

asked 2019-02-13 08:23:16 -0600

Selman KOYAN gravatar image

updated 2019-02-14 03:53:54 -0600

LBerger gravatar image
import cv2
camera = cv2.VideoCapture(-1)
facealg=cv2.CascadeClassifier("/home/resistablee/Documents/Projects/Python/Kameradan Yüz Tanima/haarcascade_frontalface_default.xml")
while True:
     ret, frame=camera.read()
     gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
     faces=  facealg.detectMultiScale(gray,1.3,5)
     for (x,y,w,h) in faces:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2 )
    cv2.imshow("deneme",gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
            break
cap.release()
cv2.destroyAllWindows()

This my codes.

I get the following error when I run the code:

Exception has occurred: cv2.error OpenCV(3.4.4) ../modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor' File "/home/resistablee/Documents/Projects/Python/Kameradan Yüz Tanıma/KameradanYuzTanima.py", line 6, in <module> gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) File "/home/resistablee/Documents/Anaconda3/envs/Resistablee/lib/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/resistablee/Documents/Anaconda3/envs/Resistablee/lib/python3.6/runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "/home/resistablee/Documents/Anaconda3/envs/Resistablee/lib/python3.6/runpy.py", line 263, in run_path pkg_name=pkg_name, script_name=fname)

And i cant access the camera.

edit retag flag offensive close merge delete

Comments

I think I know where the problem is.

import cv2
camera = cv2.VideoCapture(0)
facealg=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
frame1=camera.read()
ret, frame2=camera.read()
print("frame1 value:",bool(frame1),"\nframe2 value:", bool(frame2))

Output:

frame1 value: True 
frame2 value: False
Selman KOYAN gravatar imageSelman KOYAN ( 2019-02-14 16:45:40 -0600 )edit

@Selman KOYAN. You are missing condition block.

While True:
frame1=camera.read()
ret, frame2=camera.read()
print("frame1 value:",bool(frame1),"\nframe2 value:", bool(frame2))
:
:
:
supra56 gravatar imagesupra56 ( 2019-02-15 03:52:48 -0600 )edit

This codes just test codes

Selman KOYAN gravatar imageSelman KOYAN ( 2019-02-15 04:30:04 -0600 )edit

Here is tutorial: Getting Started with Videos

supra56 gravatar imagesupra56 ( 2019-02-15 04:43:03 -0600 )edit

2 answers

Sort by » oldest newest most voted
0

answered 2019-02-14 03:39:35 -0600

Kushal gravatar image

updated 2019-02-14 03:49:34 -0600

keep haar cascade file and python file on the same folder. no need to give the entire path of haar cascade file, just give name of the xml file.

in place of cap.release() [cap is not defined] you have to replace with camera.release()

i don't think cv2.VideoCapture(-1) will work replace it with cv2.VideoCapture(1) [1 for front camera and 0 for rear camera] if it is still not working use ip webcam.

edit flag offensive delete link more

Comments

@Kushal It will worked. But the path isn't accurately. The cv2.VideoCapture(1) meaning containing 2 cameras.The cv2.VideoCapture(0) contained 1 camera.

supra56 gravatar imagesupra56 ( 2019-02-14 09:32:28 -0600 )edit

My problem is still going on. I think the problem will be because I use Deepin. I don't think he's allowed to access the camera. What are your thoughts?

Selman KOYAN gravatar imageSelman KOYAN ( 2019-02-14 16:00:29 -0600 )edit

Why camera.read() output value fale/none?

Selman KOYAN gravatar imageSelman KOYAN ( 2019-02-14 16:56:08 -0600 )edit

@supra56 you are right about camera

Kushal gravatar imageKushal ( 2019-02-21 04:42:16 -0600 )edit
0

answered 2019-02-13 11:30:39 -0600

supra56 gravatar image

updated 2019-02-13 11:38:36 -0600

I, actually don't understand filename containing between whitespace /Python/Kameradan Yüz Tanıma/. It should be slash instead of whitespace. Btw, camera = cv2.VideoCapture(-1) should be ok. No need to change.

Perhap, it should be like this but no whitespace "/home/resistablee/Documents/Projects/Python/KameradanYüzTanima/

edit flag offensive delete link more

Comments

https://hizliresim.com/lq4ZLE

did not work unfortunately.

Selman KOYAN gravatar imageSelman KOYAN ( 2019-02-13 22:15:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-13 08:23:16 -0600

Seen: 7,441 times

Last updated: Feb 14 '19