I'm working on project about vehicles detection and counting, and i'm trying to use trained HAAR cascade provided by opencv using anaconda3, but cv2.VideoCapture(0).isOpened() return False, means that it couldnt open the frames correctly,what should i do ? thank you.
I've already tried to change the parameter 0 to -1 as recommended on the net but it didn't work :/
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
cap.isOpened()
#This one returns False !
Maybe it's the problem with the driver.
You could test if your camera works well in other applications.
It occured to me once on my Ubuntu16.
For Linux, you could use cheese or command line:
ls /dev/video*
to see if your camera is mounted.
For Windows, just open you camera application. But I think the camera driver could barely fail on Windows
Related
I am very new in programming with python :)
My Setup:
Windows 11 Pro
Python 3.7
OpenCV 4.7
Webcam: HD Pro Webcam C920 from logitech
I want to access the camera image in Python. Everything works with my integrated camera.
When I want to access the USB camera, nothing works. I do not get video from my USB webcam.
I try the following code to see, if there is a camera.
import cv2
cap = cv2.VideoCapture(0)
print(cap.isOpened())
It works with my integrated Webcam. I get a True
With cap = cv2.VideoCapture(2) i get False
But if i try this with cap = cv2.VideoCapture(1) i get nothing back and there is no error. The program just keeps running. My conclusion: Somehow the USB camera has to work. Otherwise I would also expect a False for cap = VideoCapture(1).
I have already found and tried many things on the Internet. Unfortunately, nothing has helped.
I have also tried pygame, but that does not seem to run under windows.
How do I get video from my USB camera?
Thanks for your help :)
I am trying to build a Python script that returns True every time the camera gets opened by an app and I came across several Open-CV scripts but could not get them working for this purpose. Also I tried using the cam.isOpened() inside an infinite loop to check if it worked while accessing the camera through another app, as given below, but it did not work.
import numpy as np
import cv2
cam = cv2.VideoCapture(0)
while(True):
if cam.isOpened():
return("True")
Is there a way this task could be achieved?
OpenCV can't do this. it's not meant to. you're using its APIs wrong.
you need operating system APIs if you want to check whether anything currently accesses a camera.
While running a simple opencv video capture script, i am getting False as the result. I suspect it is due to some security setting in Windows 10 which is not allowing camera access. I checked Privacy > Camera settings, but there was no option to allow a script to access the camera. I can see that the camera is not turned on when running the following opencv based test script.
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
print(ret)
The Key to the answer is "Give time for Microsoft Windows to initalize WebCAM"
import time
capWebcam = cv2.VideoCapture(0)
time.sleep(1.000) # Make sure, you need to give time
# for MS Windows to initialize Camera
It's called "Allow access to classic application" or "Desktop applications" something like this in the bottom of the setting page, under Windows Store type applications.
This gain camera access to all EXE and DLL standalone applications.
One setting for all of them.
More info on exceptions here https://support.microsoft.com/en-us/help/4468234/windows-10-desktop-apps-and-privacy
Works for me in
'cv2.__version__ 4.2.0'
just installed latest opencv and python8 on latest windows10.
As suggested in previous helpful answers, after checking windows camera security setting, adding time delay, and running windows camera app, the program works fine.
i use this code to start the camera
from imutils.video import Videostream
vs = VideoStream(0).start()
but when i try this
vs.stop()
the camera doesn't stop and the variable vs show this value :
<imutils.video.webcamvideostream.WebcamVideoStream instance at 0x7f2c40e75b00>
imutils actually uses cv2.VideoCapture() to initialize stream, but didn't release it in stop.
vs.stream.release() shall work.
I've created pull request in imutils github repo :)
https://github.com/jrosebr1/imutils/pull/81/files
vs.stream.release() didn't work for me.
vs.stream.stream.release() worked perfectly.
Imutils build using OpenCV, so we need to use release() to release the webcam. I have tried stop() earlier, then I realized and used release() and it is working fine for me.
vs = VideoStream(src=0).start() ## To launch the cam
vs.stream.release() # To release the camera
I'm trying to learn SimpleCV using Python 2.7 in IDLE.
Once the camera form SimpleCV is initialized the camera become unavailable to other programs like native webcam application or skype etc.
from SimpleCV import *
camera = Camera()
After restarting the pc or logoff and logon the webcam becomes to those applications. It seems that even closing out from python IDLE, it doesn't close the camera stream. Is there any way to stop the camera stream of simplecv?
I couldn't replicate your issue, but if the webcam is still running even after your program terminates/you close IDLE, you can end the camera by going into task manager and killing all running Python processes.
After some experimenting, I found that if you want to accomplish the same thing directly inside the code, you could try simply deleting the reference altogether:
>>> import SimpleCV as scv
>>> cam = scv.Camera()
>>> del cam
Calling del cam caused the webcam indicator light on my laptop to turn off. Granted, this appears to be an undocumented (??) solution, so I'm not sure how robust it is. I would probably try testing this on several different laptops/webcams first, to make sure it works reliably and consistently.