Mediapipe Webcam code showing Unknown C++ exception from Opencv code - python

We are trying to execute below mediapipe code for webcam feed it was not running showing below error. Also webcam was showing black and white with clumsy pictures.
Code:
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
cv2.imshow('Raw Webcam Feed', frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
cap.release()
cv2.destroyAllWindows()
I have installed latest versions of mediapipe and opencv. There was no issues with webcam tried with tried with other applications. Can someone suggest on this.
Error:
Unknown C++ exception from Opencv code
[1]: https://i.stack.imgur.com/C1J3D.png --- Error Image
[1]: https://i.stack.imgur.com/bzJ1J.png --- Code Image

Following my answer that helped in the comments:
Solution
Sometimes there are some issues with the OpenCV video capturing for USB webcam in Windows. Some webcams are handled exclusively from DirectShow(or outdated VFW). You can read more about it here.
this should solve your problem:
change cap = cv2.VideoCapture(0) to cap = cv2.VideoCapture(0,cv2.CAP_DSHOW).

Related

Problem with Stereo Camera on one usb connect with OpenCV 4 and Python 3.7

I have this cam in test (https://www.amazon.de/gp/product/B01JLU20C0/ref=ppx_yo_dt_b_asin_title_o04_s00?ie=UTF8&psc=1) for a stero vision project.
The module has two cameras which are connected to the computer via a USB port. With this I would like to test depth detection for a project. If I only take photos, it works very well. Only the live stream doesn't work the same for both cameras. I already tried all possible resolutions, unfortunately no success. Does anyone have an idea?
THX
Windows 10, Python 3.7, CV4
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
# Second Cam
cap2 = cv2.VideoCapture(0)
cap2.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
ret2, frame2 = cap2.read()
# Display the resulting frame
cv2.imshow('frame 1',frame)
cv2.imshow('frame 2',frame2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I have now switched to two other cameras, each of which is connected specifically to USB. Now everything works fine. I'll install them in a housing and then Stereo Vision really gets going. As soon as I'm finished there will be everything on GitHub and Youtube..but it will take some time ;-)

How to fix rainbow like video with python-opencv?

I am trying to create a video monitoring system on my Raspberry Pi. The OS version is the latest Raspbian Buster.
I decided to move recently on Buster since it has just come out. Before I was on Stretch. It was working really fine. The only problem I got was that when I tried to increase my window size, the FPS was decreasing leading to a small latency. So that is why I moved to Buster to see if it was working better or if was a limitation due to the Raspberry Pi.
So here is my code:
import cv2
cap = cv2.VideoCapture()
ret, im = cap.read()
while True:
ret, im = cap.read()
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.imshow('frame', im)
key = cv2.waitKey(10)
This is what my expected output should looks like:
This is my actual output:
Do you have any idea of what the problem could be? And how to solve it?

OpenCV + OS X + external webcam = very slow

I'm using openCV on OS X with my external webcam (Microsoft Cinema HD Lifecam) and its performance is very low even with the simplest camera readout code.
import cv2
cap = cv2.VideoCapture(1)
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow("Output", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I tried the same webcam with Photo Booth and it works well with high FPS. Also, I tried the same code with the built-in Facetime camera of my mac and it worked pretty fast. So, it seems like that I have some kind of configuration issue in OpenCV.
Has somebody ever experienced something like this?
Thanks for your answers.
It seems like I could solve my problem.
I just had to decrease the resolution of the camera.
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
I think Photo Booth sets the resolution automatically in order to increase the speed or the readout, however, one has to set this manually in OpenCV. Not sure about correctness of this explanation tough.
Try to enforce specific reader implementation, see here. Options to try CAP_QT and CAP_AVFOUNDATION, full list is here . Note, that OpenCV has to be built to support reader implementations.

Python-Opencv Write x264 video on memory buffer

I have a problem for writing x264 video(or single frame) on memory buffer. In opencv for images, imencode and imdecode do this task. But i want save x264 video frame for lower memory usage and sending on internet. I am able to with jpeg but jpeg size bigger than x264 video frame and quality much worser. I searched but i can't find how i write video frame on buffer.
Here is the example code to taking frames on webcam
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,320)
cap.set(4,240)
cap.set(5,30)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'x264')
out = cv2.VideoWriter('.....sample.avi',fourcc, 30.0, (320,240))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
cv2.imshow('frame',frame)
out.write(frame) #I want to write memory not disk
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Unfortunately, there is no way to do with cv2.VideoWriter because you can't reach the encoded video frames before out.release().
The way I have found for my project is implementing cap_ffmpeg_impl.hpp from D:\your_directory\opencv\sources\modules\highgui\src and send your captured frames in that library. You will send encoded frames via UDP or TCP/IP and decode where they reach with the same library. Also remember, you need to compile right ffmpeg version to use it.

OpenCV, set bit-depth when acquiring image from camera. Python preferred.

I want to acquire images from a scientific camera as 16bit grey scale. The default with OpenCV is 8 bit grey scale. I can't figure out from the documentation how to change the bit depth on acquisition. (The camera is capable and I can configure and acquire images with other programs in 16 bit grey).
import cv2
cap = cv2.VideoCapture(1)
while True:
ret, img = cap.read()
print img.dtype
cv2.imshow("input", img)
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(1).release()
Can anyone familiar with setting camera settings in opencv lend a hand?
Thank you!

Categories

Resources