Webcam light still on after cam.release() - python

I am using opencv and Python3 to read webcam.
The webcam light keeps on even though I released the webcam.
I am doing:
webcam = cv2.VideoCapture(0)
webcam.release()
After the release command, if I try to read the cam I get: (False, None) but the webcam itself is still <VideoCapture 0EE62DC0>. I don't know if this is the issue, but the light keeps on until I kill all python script or python terminal.
I am on a Windows 10, python 3.5, opencv 4.0.1. Am I doing something wrong?

I had exactly the same problem. Was using python3.6 and opencv 3.4.2 (also on Windows10), reinstalled opencv-python version to 3.4.0.14. That worked for me.
Seems that there is a problem with opencv 3.4.2.
That specific version you can install by running the command:
pip install opencv-python==3.4.0.14

Same here, using c++ and OpenCV 3.4.3 on Windows10.
Problem seems related to MSMF backend, disabling creating a environment variable with value:
OPENCV_VIDEOIO_PRIORITY_MSMF=0
solve the problem. Source: here

I had the same problem.
My problem was solved when I changed the while loop condition from:
while(cap.isOpened()):
#Your code
with:
while(True):
ret, img = cap.read()
#Your code
Before changing the loop I also applied the following command on cmd prompt:
setx OPENCV_VIDEOIO_PRIORITY_MSMF 0
Please restart the PC once you applied the above command!
Hope this work for you too.
Cheers!!

Related

Windows 10 wsl2 unbuntu 20.04 python cv2 cannot see watec 933 camera

I am trying to run a video feed into a tkinter canvas widget by using cv2. I am using Windows 10, version 21H2, OS build 19044.1348.(I am not allowed to upgrade due to where I work.) I am using wsl2 and running Ubuntu 20.04. I am using a Watec 933 video camera. As a diagnostic I am able to see the video by using VLC's video streaming function.
When I look at /dev I do not see any video mounts at all.
When I run my python code, which is just the following set of commands :
import cv2
import time
cap = cv2.VideoCapture(0)
time.sleep(2)
(ref, frame) = cap.read()
I get the following error:
global /io/opencv/modules/videoio/src/cap_v4l.cpp (889) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
My question is: Does anyone know how to diagnose this error?
Clearly ubuntu is not showing any devices but at the same time I am able to see the video using VLC. I am confused about the interplay between windows and ubuntu.
Does anyone have any experience with this? I have looked on the web but to no avail.
I found the answer. I had to change:
cap = cv2.VideoCapture(0)
to :
cap = cv2.VideoCapture('rtsp://192.168.1.250:554/h264')
I found a variation of this solution on stack overflow.

Open cv 3 Python 3 AttributeError: module 'cv2' has no attribute 'VideoCapture' Windows 10

I want to load a live video from my web cam using open cv v3 and Python 3.5 on Windows 10 but I always get this 'AttributeError: module 'cv2' has no attribute 'VideoCapture' when loaded from a file. When I import cv2 using the shell, all the modules are accessible. But when I use dir(cv2) and run it in a file, I only see a few modules. Here is my code
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
How can I access the 'VideoCapture' attr from cv2? I need help please. All the solution I find on online don't seem to work. thanks
I am not sure if this will fix the issue but I have had a problems while using OpenCV 3.3/OpenCV 2.4 with Python 3.5. I switched to Python 2.7 and many crashes and similar errors got fixed. I use cv2.VideoCapture() in my projects and it works like a charm! (Using opencv3.3 and python 2.7)
SOLVED: I think Shawn Mathew is right, I had multiple Python Versions installed. When I removed other versions and then I installed Anaconda with Python 3.5, I installed opencv with pip using the binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/. It worked, I hope this will help someone having the same issue
NB: Don't forget to also install Microsoft C++ Redistributable from official Microsoft website

OpenCV: cv2.videoCapture successful, but isOpen fails right after

I have found some other threads but they didn't help my problem.
The lines that I'm confused about are:
print("videopath",os.path.isfile(video_path),video_path)
self.cap = cv2.VideoCapture(video_path)
print("is open?",self.cap.isOpened())
It says my file exists (the first line returns true, and prints the direct path to my .mp4), and when I put cv2.VideoCapture in a try/catch, it succeeded without an issue. However it says the video is not open and I can't pull any frames from it. I'm on Ubuntu 16.04, using OpenCV 3.3 and Python 3.5.
Any suggestions would be much appreciated, thank you.
In the end it was because I had the wheel version of OpenCV installed on top of my manual installation. DON'T use pip install opencv-python as it is not official and errors like this will occur. This was an incredible guide that made it very easy: https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/
To be clear though, I had gone through this whole process once, and then tried pip just because, and it broke it, so once you install it manually, don't touch the wheel packages!

How to check the default linked codec used by openCV-Python?

I just came into the world of openCV and python, and I'm having a strange issue with opening the video stream. This is the code that I'm using:
import numpy
import cv2
cap = cv2.VideoCapture('SampleVideo.mp4')
print(cap.isOpened())
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
This code can successfully open the video stream when i run it in terminal(windows command line) but not in Pycharm. This is the result of the run in Pycharm:
False
Process finished with exit code 0
Any help would be appreciated. Thank you all!
Codec: FFmpeg. OS: Windows 8.1. Python: 3.5.2. OpenCV: 3.1
Update Sep.27:
I noticed that the code runs successfully in IDLE, but not in PyCharm. Now I'm really confused.
I can also confirm that imread and imwrite works properly in PyCharm. So maybe it is the Codec that is not working? How to check my codec in OpenCV?
Update Sep.29:
Now I think I've ruled out the possibility that the terminal and PyCharm are using different interpreter. I really think it's somehow that the default codec is not linked in the PyCharm environment. How do I check my default system codec either in OpenCV or Python?
The problem is solved.
I solved it by purging everything, Pycharm, Python and all the packages. So obviously this issue came from some sort of dependency mess-up. Not really sure what it is, but purging is the way to go people.

Video Capture with OpenCV on minimal Ubuntu

I am trying to write a small test app with openCV, using Python, to capture frames from a Webcam.
My system is very minimal, and therefor I installed a minimal Ubuntu with Fluxbox.
I can't get any video capture happening with openCV.
When I try the same code on a more complete installation of Ubuntu (such as desktop, or Xubuntu and even Lubuntu) I am able to capture video easily.
My conclusion is that my minimal Ubuntu installation is missing something, and I am trying to find out what it is.
As for the details: I do see my camera listed with I run 'lsusb'
Also, when I execute:
cap = cv.CaptureFromCAM(0)
I do get a valid capture object. But when I try:
frame = cv.QueryFrame(cap)
I get a null object.
I installed python-opencv using apt-get rather than building it myself. It seems to work just fine on Xubuntu, Lubuntu etc.
Thanks in advance for the help
I had the same probleme before, you must to have FFMEPG library installed on your computer for use this function
after you must do a cmake again and recompile opencv library
If you are using python,u can install
pip install opencv-python
then you can use
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('video', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It works on any minimal setup.Thank you.

Categories

Resources