OpenCV freezes while trying to run VideoCapture(0) - python

I am playing around with OpenCV. I am following the documentation example (link)
I installed GTK webcam application on Ubuntu to validate that my webcam works. I am able to start the webcam and see the video feedback in GTK.
I added some print message in the tutorial code to see where I get.
I added a print before and after this line: cap = cv2.VideoCapture(0)
All I get, when running the Python file, is the print that I added before the cap = cv2.VideoCapture(0) and nothing else.
I tried increasing the waitKey to 20, 40, 100 but it didn't help.
Does anyone know why it does not get further and display the frame?
My code:
import numpy as np
import cv2
videoFeed = cv2.VideoCapture(0)
while (True):
ret, frame = videoFeed.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Feed', frame_gray)
if cv2.waitKey(10) & 0xFF = ord("q"):
break
videoFeed.release()
cv2.destroyAllWindows()
My setup:
Windows 10 host
Ubuntu 18.04 guest host
Integrated Webcam
Using PIP to install python module (numpy, scipi, pillow, open_cv, etc.)
Using venv python

You have a bug in your code at if cv2.waitKey(10) & 0xFF = ord("q"):. You should've gotten a syntax error here though.
import numpy as np
import cv2
videoFeed = cv2.VideoCapture(0)
while (True):
ret, frame = videoFeed.read()
if ret == False:
print("Failed to retrieve frame")
break
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Feed', frame_gray)
if cv2.waitKey(10) & 0xFF == ord("q"):
break
videoFeed.release()
cv2.destroyAllWindows()
Tested your code. Works fine. Only other suggestion is to check whether your Ubuntu guest has permission to access your webcam. If you're using VirtualBox I remember seeing an option for this in the interface

Related

Camera don't show image in openCV

I am using below code to access my laptop camera or usb webcam but it doesn't work. I am not sure how to debug this to get to root cause of this issue. Code and output:
import cv2
vid = cv2.VideoCapture(0)
print (vid)
print (vid.isOpened())
while(vid.isOpened()):
ret, frame = vid.read()
print (ret)
print (frame)
if not ret:
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()
The output of above is
<VideoCapture 0000000002DAE970>
True
False
None
When the cv2.VideoCapture(0) is executed I can see my laptop's camera light lit up and same happens when cv2.VideoCapture(1) is executed, I can see my webcam's light lit up. I am not sure why ret, frame = vid.read() ret is being return as False and image as None.
Please note that both cameras work in skype and other utilities. Even on VLC as well. I have checked in my device manager and they both show up. I am running this on Windows 7, openCV version is 4.5.5 and Python version is 3.8.6
Regards,
Yasar

OpenCV exit code -1073741819 (0xC0000005)

This is my program:
import numpy as np
import cv2
cap = cv2.VideoCapture(1)
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()
And the result:
exit code -1073741819 (0xC0000005)
I am running my program in Windows 7. My Python version is 3.6 in Pycharm.
I got this code to run on my computer by changing the 1 to a 0 in the line cap = cv2.VideoCapture(1). This integer value determines which camera is used to capture an image. Passing 0 tells OpenCV to use the first camera on your device. If your device, like mine, only has one camera, then telling it to use the second camera by passing 1 will result in errors.

Can We connect handycam to Opencv

Everyone knows to connect webcam in opencv we can connect as cap = cv2.VideoCapture(0) But can we connect Sony Handycam in the same way or we need to do something else i am new in cv2 searched a lot on how to connect Handycam in cv2 but did not found if anyone tried or have idea (reference) can you tell me how can I do that
Connect the handycam to the computer via USB and install all necessary drivers. Then you can use any normal code with cv2 for handycam access, except you might have to change the device ID.
import cv2
cam_ID=0 ## Change the 0 value to whatever ID your device has.(0 is first camera, 1 is second camera and so on...)
cam = cv2.VideoCapture(cam_ID)
while True:
value, img = cam.read()
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()

python Attribute error. Im not sure what is causing this. Is it because my web cam not recogonized?

I just began to learn image processing using python 3.5, in Ubuntu. As I began to learning processing video using the webcam I got stuck with an AttributeError. error is module 'cv2.cv2' has no attribute 'Videocapture'
the code I used is
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()
Thank you in advance.
You have a typo, the object is called cv2.VideoCapture (note the capital C) and Python is case-sensitive.

OpenCv code throws segmentation error(core dumped) Ubuntu 14.04

I'm beginner learning opencv from the official documentation http://docs.opencv.org/trunk/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video
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()
It's giving me error "Segmentation fault (core dumped)"
Can anyone please tell me Why is that happening and how to resolve that issue?
Thanks in advance.
Maybe its a little late but what "user3154952" says is true, when you are working with the C++ api you don't need to use the release method, it is already in the video capture Destructor.
This is the code i tested and worked fine:
import sys
import cv2
cap = cv2.VideoCapture(0)
while(1):
ret,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
Update:
i have been messing around with my ps3 eye and i've realized that with that camera you get the segmentation error for using only the destroyAllWindows method, to fix that i replaced the destroyAllWindows method with the release method and worked fine, i don't know exactly why that happened i'm just sharing in case someone get that issue. I hope that was helpful.

Categories