Capturing video using OpenCV - python

I am having an issue capturing the video feed, my camera light goes on and then off when i run this code. I tried using different values as well. And my camera seems to be working. but this code is not.
import cv2
cap = cv2.VideoCapture(0) # zero instead of one
while True:
ret, frame = cap.read()
if not ret: # exit loop if there was problem to get frame to display
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Related

Using OpenCV for python to capture webcam get white screen grab

I'm using python 2.7 on DragonBoard 410c with Debian OS to capture webcam Logitech C920 by opencv but get a white sceen result.
Here is my whole code.
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It worked well without the code "cap.set(3,1280)" and "cap.set(4,720)".
I can get the frame with 640x480.
but if I added the code to change the resolution, the window would change but the frame is white.
https://imgur.com/flsKfTp
By the way, if I set the resolution to 800x600, the frame will flash between white sceen and camera view.
How can I solve this problem?

Python OpenCv how do I detect when a video finishes playing?

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.

Trying to read an online video using OpenCV in python, Not seeing the output but the code is not throwing any error

Not sure what is the problem. Code is running fine and not showing any error. But Not seeing any output.
import numpy as np
import cv2
cap = cv2.VideoCapture("https://youtu.be/_3elg-_1m_c")
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()
If, I am using video saved in my laptop, I can see the output, but for online vidoes, I am not.

How do I create an executable in python using Opencv on OsX ?

Here is my code :
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
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()
cv2.destroyAllWindows()
I used py2app to make an executable,
and when I run the code, an error window opens
"name of file error"
How do I fix this code in order to make a working executable ?

Webcam + Open CV Python | Black screen

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.

Categories