import cv2
video_capture = cv2.VideoCapture(0)
#video_capture = cv2.VideoCapture('video/ros.mp4')
while(True):
retVal, frame = video_capture.read()
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#frame = cv2.resize(frame, (0,0), fx=0.5,fy=0.5)
#cv2.line(frame,(0,0),(511,511),(255,0,0),5)
cv2.imshow("Frame",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
its my code in ubuntu 20.04 but cant open the camera the result is
Faced same issue while running opencv code in ubuntu 20.04 on vmware.
Tried Below:
Open Player->Manage->Virtual Machine Settings -> Usb Controller -> Change the Usb Controller under Connections to USB 3.1 or USB 1.1 . Both resolved my issue. Initially, it was by default set to USB 2.0. Hope it helps.
Related
I have tried many attempts to run the cameras connected to my jetson nano. When I run this command:
gst-launch-1.0 -v nvarguscamerasrc ! 'video/x-raw(memory:NVMM),format=NV12,width=1280,height=720,framerate=30/1' ! autovideosink
The camera works but when I try to perform a simple camera read in python with this code
import sys
import cv2
def read_cam():
G_STREAM_TO_SCREEN = "videotestsrc num-buffers=50 ! videoconvert ! appsink"
cap = cv2.VideoCapture(G_STREAM_TO_SCREEN, cv2.CAP_GSTREAMER)
if cap.isOpened():
cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
while True:
ret_val, img = cap.read()
cv2.imshow('demo',img)
cv2.waitKey(1)
else:
print ("Unable to use pipline")
cv2.destroyAllWindows()
if __name__ == '__main__':
read_cam()
the camera does not work in the code above and it returns "Unable to use pipline".
What I am doing wrong and how can I access the camera feed in python?
My opencv installation recently stopped working for reasons I'm not sure of. I have two scripts that all give different errors:
Script A:
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
gives me this error:
select timeout
VIDIOC_DQBUF: Resource temporarily unavailable
Traceback (most recent call last):
File "camera.py", line 19, in <module>
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.error: OpenCV(4.0.1-dev) /home/me/Packages/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
And Script B:
import cv2
def show_webcam(mirror=False):
cam = cv2.VideoCapture(0)
while True:
ret_val, img = cam.read()
if mirror:
img = cv2.flip(img, 1)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
def main():
show_webcam(mirror=True)
gives me this error:
select timeout
VIDIOC_DQBUF: Resource temporarily unavailable
Traceback (most recent call last):
File "camera3.py", line 26, in <module>
main()
File "camera3.py", line 22, in main
show_webcam(mirror=True)
File "camera3.py", line 15, in show_webcam
cv2.imshow('my webcam', img)
cv2.error: OpenCV(4.0.1-dev) /home/david/Packages/opencv/modules/highgui/src/window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
Here's the main issue: I've used OpenCV before and both of those errors usually occur when opencv can't find the webcam. But I do have a webcam attached, and when I open Cheese Webcam Booth it works fine, and takes pictures fine. Is there a way to repair this without reinstalling OpenCV?
I'm on Ubuntu 18.04.
I would review the OpenCV installation, since Script A works just fine by me, on the same version of the library and similar OS (Mint 19).
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
cv2.waitKey(10)
The installation through
pip install opencv-python
is known for having "problems" with 3rd party modules (https://github.com/opencv/opencv/issues/8471).
Try building and installing from source, this way the interface modules will be built and linked as well (V4L, FFMpeg, etc).
Two possible solutions: 1) set the correct fps; 2) upgrade to the newer version of opencv for python.
in both cases, the camera was activated but is now occupied. So now, in the main "while" loop try putting
while(True):
ret_val, img = cap.read()
if(ret_val==False)
cap.open(0)
continue
...
I have been running python script with nodejs using python-shell package.. and i am getting this error:
Error: init done
at PythonShell.parseError (F:\github\pythonShellDemo\node_modules\python-shell\index.js:191:17)
at terminateIfNeeded (F:\github\pythonShellDemo\node_modules\python-shell\index.js:98:28)
at ChildProcess.<anonymous> (F:\github\pythonShellDemo\node_modules\python-shell\index.js:89:9)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
after some debugging and research i got to know that this error is with opencv but i cant find any solution..
here is the code:
import cv2
import zbar
from PIL import Image
import sys
video = cv2.VideoCapture(0)
count=0
qrcode=[]
while True:
ret, frame = video.read()
cv2.imshow('Camera', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
image = Image.fromarray(grayscale)
width, height = image.size
zbarimage = zbar.Image(width, height,'Y800', image.tobytes())
scanner = zbar.ImageScanner()
scanner.scan(zbarimage)
for x in zbarimage:
if count == 0:
qrcode=x.data
count=count+1
if qrcode:
break
video.release()
cv2.destroyAllWindows()
print(qrcode)
sys.stdout.flush()
I am using python 2.7
UPDATE:
nodejs code for calling python script:
PythonShell.run('python/scan.py', options, function (err, results) {
if (err) {
console.log(err)
reject(err)
}
// results is an array consisting of messages collected during execution
console.log(results)
resolve(results)
})
UPDATE:
i tried running opencv only without zbar.. still got the error
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()
The OpenCV library prints init done to the standard error output. It's not an error, but just a debug print. python-shell then turns this into an error. From the python-shell documentation:
If the script writes to stderr or exits with a non-zero code, an error will be thrown.
Suppressing the output seems to be only possible by recompiling the library with a parameter set.
I've got a script in Python which reads out my webcam and shows it in a window. I now want to store the results, so following this tutorial I wrote the following code:
import cv2
import imutils
camera = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while True:
try:
(grabbed, frame) = camera.read() # grab the current frame
frame = imutils.resize(frame, width=640, height=480)
cv2.imshow("Frame", frame) # show the frame to our screen
key = cv2.waitKey(1) & 0xFF # I don't really have an idea what this does, but it works..
video_writer.write(frame) # Write the video to the file system
except KeyboardInterrupt:
break
# cleanup the camera and close any open windows
camera.release()
video_writer.release()
cv2.destroyAllWindows()
print "\n\nBye bye\n"
This perfectly shows the real time video footage from my webcam in a new window. But writing the video file seems to fail. It does create a file called output.avi, but the file is empty (zero bytes) and on the command line I see the following errors:
OpenCV: Frame size does not match video size.
OpenCV: Frame size does not match video size.
OpenCV: Frame size does not match video size.
etc.
I clearly resize the frame to the size in which I want to save the video (640x480) so I'm not sure why it wouldn't match.
When I run the script again (so in this case the empty output.avi already exists), it shows these errors:
2017-04-17 10:57:14.147 Python[86358:5848730] AVF: AVAssetWriter status: Cannot Save
2017-04-17 10:57:14.332 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
2017-04-17 10:57:14.366 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
2017-04-17 10:57:14.394 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
etc.
In the tutorial it says that the Four digit FourCC code is used to specify the video codec which is platform dependent and that the list of available codes can be found in fourcc.org. I'm on OSX so I tried a bunch of different codec-codes: DIVX, XVID, MJPG, X264, WMV1, WMV2. But unfortunately none of them work for me. They all give the same errors, except for MJPG, which gives me the following error:
OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
Traceback (most recent call last):
File "store_video.py", line 15, in <module>
video_writer.write(frame) # Write the video to the file system
cv2.error: /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write
Does anybody know what could be wrong here? All tips are welcome!
It's probably because you built OpenCV with AVFoundation and it doesn't support XVID or other codec. You can try mp4v and m4v extension.
import cv2
camera = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
video_writer = cv2.VideoWriter('output.m4v', fourcc, 30.0, (640, 480))
while True:
(grabbed, frame) = camera.read() # grab the current frame
frame = cv2.resize(frame, (640,480))
cv2.imshow("Frame", frame) # show the frame to our screen
key = cv2.waitKey(33) & 0xFF # I don't really have an idea what this does, but it works..
video_writer.write(frame) # Write the video to the file system
if key==27:
break;
# cleanup the camera and close any open windows
camera.release()
video_writer.release()
cv2.destroyAllWindows()
print("\n\nBye bye\n")
On the other note, the error
OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
means that you messed up the dimension with
frame = imutils.resize(frame, width=640, height=480)
You can try cv2.resize as I used in my code. There's no need to use another library when cv2 can do that already.
I want to capture images from webcam and then further do image processing for ANPR (Automatic number plate Recognition) in python 2.7 using opencv 2.4.10 in Ubuntu 14.04. When I run this simple code, it detects my camera once and then camera stops working.
Code is:
import cv2
cam = cv2.VideoCapture(0)
s, img = cam.read()
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
while s:
cv2.imshow( winName,img )
s, img = cam.read()
key = cv2.waitKey(10) & 0xFF
if key == 27:
cv2.destroyWindow(winName)
break
print "Goodbye"
Can someone please help me with this?
Got the answer. I was not releasing cam. It works fine now