I am trying to open a video using opencv 3.1 and python 3.6 using an Industrial camera "catchBest" . Its driver is installed and device is listed in device manager. But it is not opening using opencv.
Here's the code:
import cv2
video = cv2.VideoCapture(0)
while True:
cam = video.read()
cv2.imshow("video", cam)
cv2.waitKey(0)
video.release()
cv2.destroyAllWindows()
I tried index from 0 to 9 but its not working.
This should work:
import cv2
#you can run a loop from 0 to 100 to check which gives true, if you're unsure which number to use.
video = cv2.VideoCapture(0)
print(video.isOpened()) #this should return True if camera opens successfully.
while True:
#first returned value of video.read() is boolean and second is frame so we have to use second.
ret,cam = video.read()
cv2.imshow("video",cam)
#you have to break from infinite loop to release the camera usage, here I'm using escape key to break you can choose any.
if cv2.waitKey(1) & 0xFF == 27:
break
video.release()
cv2.destroyAllWindows()
Related
I'm working on Windows10 and coding in Python.
I'm trying to read live stream from webcam.
webcam = cv2.VideoCapture(0)
(rval, im) = webcam.read()
After I checked value of 'im'; it is 'None'.
Am I missing anything? Please help.
import cv2
import numpy as np
webcam = cv2.VideoCpature(0) # If 0 doesn't work try swapping it with 1
while True:
ret, frame = webcam.read()
cv2.imshow("frame", frame)
cv2.waitKey(1)
webcam.release()
cv2.destroyAllWindows()
try running that (source)
After cap.release() the only Frame is getting closed, webcam light is still ON.
import cv2
cap = cv2.VideoCapture(0)
#cap = cv2.VideoCapture(-1) if i give '-1' instead of '0' then light is getting OFF
#but camera is not working because i don't have second camera to laptop.
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cap.isOpened() #returns False
cv2.destroyAllWindows()
By pressing 'q', the Frame is getting closed but webcam light is still ON.
How to OFF the webcam? (It is getting OFF after python shell is closed.)
If possible, tell me the path of cv2.VideoCapture() class source code.
Set OPENCV_VIDEOIO_PRIORITY_MSMF=0 in your environment variables. Seems like there is an instance leak in opencv library. If you're on windows maybe use setx in your cmd to set the value setx OPENCV_VIDEOIO_PRIORITY_MSMF 0.
Reference to the issue : here
And it looks like the issue has been fixed too. So try updating your opencv library or reinstalling altogether.
That should solve your problem.
I am using Python 3.5 and Opencv for an interactive video. However I can't figure out how to detect when my video finishes playing. Any ideas how I can detect when the video ends?
Thanks a bunch.
When ret is False, it means that the video is in the last frame.
Here is my code.You can try it.
import cv2
video_capture = cv2.VideoCapture("huge.mp4")
while True:
ret, frame = video_capture.read()
if ret:
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
video_capture.release()
cv2.destroyAllWindows()
check this link out. You can use the identifier CV_CAP_PROP_FRAME_COUNT to get the Number of frames in the video file.
I am using the code below, but I get a black image. Could you please help me rectify the error?
import cv2
import numpy as np
c = cv2.VideoCapture(0)
while(1):
_,f = c.read()
cv2.imshow('e2',f)
if cv2.waitKey(5)==27:
break
cv2.destroyAllWindows()
Update: See github.com/opencv/opencv/pull/11880 and linked conversations, only few backends support -1 as index.
Although this is an old post, this answer can help people who are still facing the same problem. If you have a single webcam but it renders all black, use cv2.VideoCapture(-1). This will get you the working camera.
Just change cv2.waitKey(0) to cv2.waitKey(30) and this issue will be resolved.
I've faced with same problem. Updating neither opencv nor webcam driver works. I am using kaspersky as antivirus. When I disable the kaspersky, then black output problem solved.
BTW, I can see the running .py file in kaspersky console > reports > host intrusion prevention. It reports application privilege control rule triggered - application: myfile.py, result: blocked: access to video capturing devices
Try this:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
This worked for me:
I did a pip install imutils. Imutils is a library with series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, displaying Matplotlib images, sorting contours, detecting edges, and much more easier with OpenCV and both Python 2.7 and Python 3.
import cv2
import imutils
cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop)
ret, frame = cap.read() # return a single frame in variable `frame`
while (True):
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
(grabbed, frame) = cap.read()
frame = imutils.resize(frame, width=400)
cv2.imshow('img1', frame) # display the captured image
if cv2.waitKey(1) & 0xFF == ord('q'): # save on pressing 'y'
cv2.imwrite('capture.png', frame)
cv2.destroyAllWindows()
break
cap.release()
Try put -0 on the index and pause any antivirus running
import cv2
import numpy as np
cap = cv2.VideoCapture(-0)
cap.set(3,640)
cap.set(3,480)
while(True):
success, img = cap.read()
cv2.imshow('frame',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I faced the same issue after many calls with:
cap = cv2.VideoCapture(0)
and it solved when I changed the index to 1 :
cap = cv2.VideoCapture(1)
In my case just disabling Kaspersy has solved the problem.
Well, it seems like my question had been asked many times before and unfortunately, no one replied. I hope someone will help.
I have an Easycap device that converts the analog images from my analog camera to digital signals through a USB port.
The device is identified to the system in the Device Manager under "Sound, Video and game controllers" category as "SMI Grabber Device".
I use a simple Python code to display the video from this device. I also have an embedded webcam in my laptop.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.imwrite('screenshot.jpg',frame)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
First, when I unplug the Easycap, CaptureVideo(0) returns the embedded webcam video stream. However, when I plug the Easycap, an error appears:
"Traceback (most recent call last):
File "C:\Users\DELL\Desktop\code\cam.py", line 10, in
cv2.imshow('frame',frame)
error: ......\src\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0"
Notice that, any number except 0 makes the program display the webcam image. So if I tried cap = cv2.CaptureVideo(1), it will show the webcam, cap = cv2.CaptureVideo(20) is the same.
I also tried to enter "SMI Grabber Device" instead of 0 or 1 in the VideoCaptureconstructor function, but it didn't make any difference.
I'm using Windows 8, and I've installed the accompanying driver for Easycap. The software that comes with the driver (called ULead) works fine and display the CCTV camera video. I tried to display the images while I'm closing that program, and without, the result is the same.
I used before a C# program with Aforge library which had getCamList method or something which allowed me to choose the specific device I want to display from a comboBox. I can't find a similar function is opencv.
I'm using OpenCV 2.4.6. I didn't try the code on prior versions.
I really need to understand why this code doesn't work, knowing that I'm just a very beginner of opencv and image processing.
I hope someone can help.
I am using EasyCAP too.
You must check that ret is True.
I am use below code
while True:
ret, frame = vc.read()
if ret:
break
cv2.waitKey(10)
h, w = frame.shape[:2]
print h, w
while True:
ret, frame = vc.read()
if ret:
cv2.imshow(WID, frame)
if cv2.waitKey(1) == 27:
break
Let there be light!
On serious note, I struggled with the same problem and I hope this helps!
the original thread + answer