What is the difference between cv2.videocapture(1) and cv2.videocapture(0)? - python

import cv2
cap= cv2.VideoCapture(0)
while True:
ret,frame= cap.read()
cv2.imshow('Our live sketch',frame)
if cv2.waitKey(1)==13:
break
cap.release()
When I use cv2.VideoCapture(1), the programs shows error but the program works properly if I use cv2.VideoCapture(0)

That's the index of the camera it is used to select different cameras if you have more than one attached. By default 0 is your main one.

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.

OpenCV freezes while trying to run VideoCapture(0)

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

OpenCV Callback Lag

I'm running OpenCV 3 on Python 3.4 on a 2015 15" MacBook Pro. Below is a minimal example that illustrates my problem:
import cv2 as cv
import numpy as np
def mouse_callback(event, x, y, flags, param):
print("Callback!")
cv.namedWindow("Display")
cv.setMouseCallback("Display", mouse_callback)
cap = cv.VideoCapture(0)
while True:
ret, frame = cap.read()
cv.imshow("Display", frame)
if cv.waitKey(1) == ord("q"):
break
When I click on the screen, the text "Callback!" takes about 3 seconds to appear on the terminal screen. I'm not sure why I'm seeing this much lag--my laptop shouldn't be so bad that I can't even run this simple script.
Additionally, the problem persists when I reduce the webcam resolution, or even when I replace the webcam altogether with a still image. I rewrote a similar program in C++, and the C++ OpenCV libraries also suffer from this lag.
Any tips on how I can reduce or eliminate the lag?
Try this :
import cv2 as cv
import numpy as np
def mouse_callback(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN:
print("Callback!")
cv.namedWindow("Display")
cv.setMouseCallback("Display", mouse_callback)
cap = cv.VideoCapture(0)
while True:
ret, frame = cap.read()
cv.imshow("Display", frame)
if cv.waitKey(1) == ord("q"):
break
Although its mostly a problem with something else. Maybe your webcam resolution is too high.
So try this also:
cap.set(3,640)
cap.set(4,480)
Put the above code above the while loop and check.

Can't save video on Raspberry PI by using python with OpenCV

import cv
import cv2
cap = cv2.VideoCapture(0)
cap.set(8,100)
out = cv2.VideoWriter('/home/pi/Desktop/output.mp4',cv2.cv.CV_FOURCC('D','I','V','X'),20.0,(640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(10) == 27:
break
cv2.VideoCapture(0).release()
out.release()
cv2.destroyAllWindows()
This code worked but it never stopped and no video file had been saved. Does anyone know how to solve this? Thanks a lot.
Both release() calls can be removed.
cv2.VideoCapture(0).release()
would call release() on a new VideoCapture, what you meant was cap.release().
For the VideoWriter, the release method doesn't exist - you don't have to care about releasing the VideoWriter or VideoCapture in Python. They will be released when their object is destroyed at the end of your program.

Categories