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.
Related
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).
I am having a code where I need to read a video file using opencv and get the frames out of that video. i am using Python for that and doing the following:
video = cv2.VideoCapture(video_path)
if not video.isOpened():
self.logger.error("Error opening video from file {}".format(video_path))
ret, img = video.read()
while ret:
frames.append(img)
ret, img = video.read()
total_nbr_frames = len(frames)
I pass a video on one machine and I get a result of 35 frames. but when I use a different machine I get 7 frames.
Another video I tried was working on the first machine (27 frames) on the other the video was open but I couldn't read the frames (total = 0)
What could be the reason for that? is it hardware related? am I missing a library?
As far as I see this is totally hardware related. There's no library to help you increase the frame read speed.
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 ;-)
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.
I plan on building a ROV and I am working on my video feed atm. I will be using fiber optics for all communications and I am tinkering with opencv to stream a webcam feed with python. I might choose to use IP cameras but I wanted to learn more about how to capture frames from a webcam in python first. Since I didn't know what I was going to use in the end I bought a cheap-as-they-get noname USB webcam just to try and get everything working. This camera feed is going to be used for navigation, a seperate video recorder will probably be used for recording video.
Enough about that, now to my issue. I am getting only 8 FPS when I am capturing the frames but I suspect that is due to the cheap webcam. The webcam is connected to a pcduino 3 nano which is connected to a arduino for controlling thrusters and reading sensors. I never thought of how to utilize hardware in encoding and decoding images, I don't know enough about that part yet to tell if I can utilize any of the hardware.
Do you guys believe it's my web cam that is the bottleneck? Is it a better idea to use a IP camera or should I be able to get a decent FPS using a webcam connected to a pcduino 3 nano capturing frames with opencv or perhaps some other way? I tried capturing frames with Pygame which gave me the same result, I also tried mjpg-streamer.
Im programming in Python, this is the test I made:
import cv2, time
FPS = 0
cap = cv2.VideoCapture(0)
last = time.time()
for i in range(0,100):
before = time.time()
rval, frame = cap.read()
now = time.time()
print("cap.read() took: " + str(now - before))
if(now - last >= 1):
print(FPS)
last = now
FPS = 0
else:
FPS += 1
cap.release()
And the result is in the line of:
cap.read() took: 0.118262052536
cap.read() took: 0.118585824966
cap.read() took: 0.121902942657
cap.read() took: 0.116680860519
cap.read() took: 0.119271993637
cap.read() took: 0.117949008942
cap.read() took: 0.119143009186
cap.read() took: 0.122378110886
cap.read() took: 0.116139888763
8
The webcam should explicitly state its frame rate in its specifications, and that will definitively tell you whether the bottleneck is the camera.
However, I would guess that the bottleneck is the pcDuino3. Most likely it can't decode the video very fast and that causes the low frame rate. You can try this exact code on an actual computer to verify this. Also, I believe OpenCV and mjpg-streamer both use libjpeg to decode the jpeg frames, so their similar frame rate is not surprising.