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()
Related
so I had tried to get a code for taking and saving pictures from a USB camera on raspberry pi. in my previous question the answer had the errors in the code fixed but for some reason the pictures won't be saved and this is the code with the error shown:
Code:
#new camera code
import cv2
import imutils
import time
#loop for taking picture and saving
cap = cv2.VideoCapture(0)
def takePicture():
global showimg, frame
frame = cap.read()
showimg = frame
cv2.imshow('image', showimg) # display the captured image
cv2.waitKey(1)
time.sleep(0.3) # Wait 300 miliseconds
image ='C:/Users/whale/Desktop/REMOVE/capture.png'
cv2.imwrite(image, frame)
return showimg
takePicture()
cap.release()
error:
/tmp/geany_run_script_N7B4Z1.sh: 7: ./seconddraftDSA: Permission denied
(program exited with code: 126)
Press return to continue
does someone have experience with linux systems and can perhaps help me solve this problem? I need to get this right for my bachelors thesis
I tried to get the code for taking and saving pictures from a USB webcam for raspberry pi but this error doesn't allow the same
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.
Does anyone know a way to create a camera source using python? So for example I have 2 cameras:
Webcam
USB Cam
Whenever I use any web application or interface which requires camera, I have an option to select a camera source from the above two mentioned.
What I want to achieve is that I am processing real time frames in my python program with a camera portal of my own like this:
import numpy as np
import cv2
while True:
_,frame = self.cam.read()
k = cv2.waitKey(1)
if k & 0xFF == ord('q'):
self.cam.release()
cv2.destroyAllWindows()
break
else:
cv2.imshow("Frame",frame)
Now I want to use this frame as a camera source so next time whenever I open a software or web app which requires camera then it show option as follows:
Webcam
USB Cam
Python Cam (Or whatever be the name)
Does anyone has any advice or hint on how to go about that? I have seen some premium softwares which generate their own camera source but they're written in c++. I was wondering if this could happen in python or not.
Here is an example of the same:
As you can see there are multiple camera source there. I want to add one camera source which displays the frames processed by python in its feed.
You can use v4l2loopback (https://github.com/umlaeute/v4l2loopback) for that. It is written in C but there are some python wrappers. I've used virtualvideo (https://github.com/Flashs/virtualvideo).
The virtualvideo README is pretty straight forward but here is my modification of their github example to match your goal:
import virtualvideo
import cv2
class MyVideoSource(virtualvideo.VideoSource):
def __init__(self):
self.cam = cv2.VideoCapture(0)
_, img = self.cam.read()
size = img.shape
#opencv's shape is y,x,channels
self._size = (size[1],size[0])
def img_size(self):
return self._size
def fps(self):
return 30
def generator(self):
while True:
_, img = self.cam.read()
yield img
vidsrc = MyVideoSource()
fvd = virtualvideo.FakeVideoDevice()
fvd.init_input(vidsrc)
fvd.init_output(2, 640, 480, pix_fmt='yuyv422')
fvd.run()
in init_output the first argument is the virtual camera resource thar i've created when adding the kernel module:
sudo modprobe v4l2loopback video_nr=2 exclusive_caps=1
the last arguments are my webcam size and the pixel format.
You should see this option in hangouts:
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
I am trying to take a picture from the defualt carmera with python, to do this I am using openCV (import cv2 as cv from python shell). However, when I attempt to disable the camera it closes but with the error [ WARN:0] terminating async callback.
This is code I am trying to run:
import cv2 as cv
camera_port = 0
camera = cv.VideoCapture(camera_port)
return_value, image = camera.read()
cv.imwrite("image.png", image)
camera.release() # Error is here
The code outputs the desired result, it takes and saves an image, but I do not understand why the error message occurs or how to remove it
I had the same warning.
Just modify the line
camera = cv.VideoCapture(camera_port)
to
camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW)
It's probably showing a warning because you're not releasing the handle to the webcam.
try adding this to the end of the code
camera.release()
cv2.destroyAllWindows()
I hope this helps!
camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW)
cv.destroyAllWindows()
I did this & I don't see that warning there after.(only for Windows OS)
Open cmd and type:
setx OPENCV_VIDEOIO_PRIORITY_MSMF 0
This seems to be a bug in MSMF backend of opencv.
If you are using windows then you can change the backend to DirectShow backend.
So, change VideoCapture like this:
captureDevice = cv.VideoCapture(0, cv.CAP_DSHOW)
It works for me as indicated Sumit Kumar
camera_port = 0
#camera = cv2.VideoCapture(camera_port)
camera = cv2.VideoCapture(camera_port,cv2.CAP_DSHOW)
# Check if the webcam is opened correctly
if not camera.isOpened():
raise IOError("Cannot open webcam")
return_value, image = camera.read()
print("We take a picture of you, check the folder")
cv2.imwrite("image.png", image)
camera.release() # Error is here
cv2.destroyAllWindows()
hey guys found the solution pip install opencv-contrib-python==3.4.7.28 try like this we have to specifically say the version try lesser version mine was 4.x so I did and no error popped up
first:add cv.destroyAllWindows()
second:the camera permission you have banned,and then check it.
camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW) # Added cv.CAP_DSHOW
return_value, image = camera.read()
cv.imwrite("image.png", image)
camera.release()
cv.destroyAllWindows() # Handles the releasing of the camera accordingly