YUYV Framerate faster than MJPG from USB Camera OpenCV - python

I am using a SOM that is running a cortex A5 #500MHz and am trying to maximise the frame rate received from a USB camera. My camera supports video capture with YUYV and MJPEG.
Other posts suggested forcing opencv to read MJPEG frames from the camera, however this slowed down the frame rate.
I can currently get about 18 fps reading YUYV format and about 10 fps reading MJPEG's at 640x480. Currently I am just grabbing frames and am not doing any other processing. I am getting the CAP_PROP_FOURCC format each loop to ensure that opencv is setting the capture format correctly.
I am currently running opencv 4 and python3.5
Any ideas why this may be happening?
EDIT: Capture code:
# Repeatedly capture current image
while True:
ret, image = cap.read()
if image is None:
time.sleep(0.5)
continue
codec = cap.get(cv2.CAP_PROP_FOURCC)
print(codec)
# Print the framerate.
text = '{:.2f}, {:.2f}, {:.2f} fps'.format(*fps.tick())
print(text)

Please provide the exact SOM and the camera that you are using.
There are many factors, for example the format of the images captured by the camera, how they are transferred and how they are received and managed by the SOM.
Transferring them shouldn't be the problem in terms of bandwidth.
I am assuming that the settings in opencv only apply to the SOM and won't change the format of the camera capture and therefore the SOM has more processing to do, hence the frame rate drops.
[EDIT]
I cannot comment yet so I hope you read this ... your camera link is dead :/

Related

Why couldn't OpenCV 3 read the video frames although OpenCV 4 could read?

I have a problem about video reading in Python OpenCV. Although I searched it, I could not find the reason.
I have a bunch of videos that I need to get the frames to process them. It worked for almost all videos. However, I got errors in only four videos. When I tried to run my code with OpenCV 3.4.2 the VideoCapture.read() returned False (ret in the below code), although video_stream.get(cv2.CAP_PROP_FPS) line returned the FPS without problem. However, when I tried the same code with the same four videos using OpenCV 4.2.0, it returned True and was able to read the frames. Here is my code which is a simple video reading code:
video_stream = cv2.VideoCapture(video_path)
fps = video_stream.get(cv2.CAP_PROP_FPS)
ret, frame = video_stream.read()
The videos were recorded by the same device. The video codec is H.264 for all of them and the resolutions are also the same; 1920 x 1080.

How to change the resolution of PS5 camera in OpenCV?

I am trying to change the resolution of PS5 camera in OpenCV, Python.
The problem is that PS5 Camera officially isn't supported on PC, and I have to use custom camera drivers from GitHub: https://github.com/Hackinside/PS5_camera_files
Default image resolution by this code is 640x376
self.capture = cv2.VideoCapture(name)
I found out that supported resolutions of this camera are 640x376 and 5148×1088, so I tried to do next:
res = self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 5148)
res = self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1088)
But in both cases res is False, and resolution doesn't change. I can recieve only small resolution frame.
Camera 100% can work in 5148×1088, because if I launch Windows Camera application it shows me high quality images
Okay, the problem was, that I had a piece of code, where I read a frame from the capture using a loop:
while True:
self.capture.read()
It was in a parallel thread so changing the resolution was at the same time as reading images. It was a reason why the change resolution process always failed.
So the provided code in question should work if you do it before starting reading images.

Why opencv video reading fps is not same with video encrypted fps?

In OpenCV with Python, when the fps of the webcam and a video file in the directory are same, why does the video file play in fast forward whereas the webcam continues to show the frames at a normal rate? What role does the cv2.waitKey() function play here
The fps of a video file means how it was encrypted, how many frames contain within a second as the name reveals. For example, if extracted 1 second of this video will produce only that number of frames (images).
The corresponding fps of the web camera means how many frames that camera can capture in a second. If saved to a video file that would mean how many frames are contained within each 1-second span.
There is a third (probably hidden to you) concept here though. How fast the opencv can read a video file. Normally, and for typical resolutions on a modern computer this fps is larger than the actual video. So, your computer seem to playback the video in fast forward mode because it reads (and displays) frames in a faster pace than the video file's fps.
Theoretically, you can calculate the delay you should import to the video playback to force it to displayed with normal pace. I am not sure how easily you can accomplish that (in a scientific way and not trial and error mode).
Hope this clarifies the issue.

Why does opencv Videocapture function read video frames with wrong pixel values?

I am capturing videos from a camera and saving them using the opencv VideoWriter function. I save the captured videos as uncompressed avi files. When I finish recording the video, I have another script that is supposed to read the video frame by frame, process the pixel values. However, when I try to read the frames of the saved video, the pixel values are off a bit.
For example, comparing the first frames of the video being written, and the video being read (supposed to be 100% identical), I notice that the pixel values are off by a small number (RGB values off by a small number, usually less than 5).
I have already made sure that I am using the exact same video codex when writing the video, and when reading the video (Check code below)
def write_video():
out = cv2.VideoWriter("encrypted.avi" ,cv2.VideoWriter_fourcc(*'XVID'),30, (640,480))
foreach frame:
out.write(frame)
def read_video():
cap = cv2.VideoCapture("encrypted.avi")
cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'XVID'))
while(cap.isOpened()):
ret, frame = cap.read()
For my application, the frames being written and read should match 100%. I have included an image highlighting the difference between the first frame in the video being written, and the video being read. Any help is much appreciated!
These are the compression artifacts, since you are using lossy compression. If you would like your frames match down to the last bit, write them as a sequence of .PNG files -- these are losslessly compressed and preserve everything. Beware that .PNG will take much more of your HDD space than compressed video.

OpenCV gives incorrect FPS and frame count of a video

I'm trying to read, do some processing and then save an .mp4 video using OpenCV in python, but cap.get(cv2.CAP_PROP_FPS) returns the wrong FPS for some videos.
So I'm not gonna go in to the full details of what the code does, because its irrelevant to the problem at hand. I've noticed the output video from my program plays too fast, and when debugging I decided to just see how it looks when I simply playback the input video.
So the playback code looks something like this:
cap = cv2.VideoCapture(video_path)
video_fps = cap.get(cv2.CAP_PROP_FPS)
#returns 49.8
while cap.isOpened():
ret, frame = cap.read()
if ret:
cv2.imshow('a', frame)
cv2.waitkey(int(1000/video_fps))
else:
break
But the video plays too fast, so I go right click -> properties -> details, and sure enough it says frames per seconds: 49... but I know most of my videos are 25 fps (which is about half of 49.8), so just out of curiosity I change the delay to twice as much: cv2.waitkey(int(2000/video_fps))
And suddenly the video plays in perfect speed.
Another oddity is that when I divide the result of cap.get(cv2.CAP_PROP_POS_FRAMES) by the length of the video in seconds, I get yet again 49.8 and since I know the length must be correct, I can only guess that OpenCV gets the number of frames wrong (?)
So my questions are:
1) What the hell is going on here?
2) Is there a better\more reliable way to check video fps and frame count?
I know this thread is old however people still experiencing this, it seems like it is an ongoing issue: https://github.com/opencv/opencv/issues/16821. I ran into this same issue with .mp4 and what solved it for me was exporting to .mov instead. Not sure why .mp4 is having issues but seems to be a common trend in the github issue

Categories