how can i detect fps=4.5 for a video? - opencv

I write a video using cv2.VideoWriter with fps=5; VLC plays it back at 5fps; and ffprobe reports tbr=5.
I write it with fps=4.5; VLC plays it back at 4.5fps; but ffprobe reports tbr=9.
How can I detect 4.5 fps?
EDIT: BTW the metadata shown in windows file manager and using cv2 get(cv2.CAP_PROP_FPS) is 600fps.
EDIT2:
Turns out the issue is only on Raspberry pi. Looks like cv2 does not write the metadata correctly as rpi/ffprobe works fine on a file created on laptop. However even the rpi created file plays fine so there must be a way VLC detects fps.
import cv2
source = "test.avi"
reader = cv2.VideoCapture(source)
writer = cv2.VideoWriter("temp.avi", cv2.VideoWriter_fourcc(*'DIVX'), 4.5, (800, 600))
while True:
res, img = reader.read()
if not res:
break
writer.write(img)
reader.release()
writer.release()

Related

Video created by OpenCV is corrupted in Google Colab

I'm creating a video sample by combining cropped-faced images using Deepface python library.
I saved the cropped faces in a list called cropped_faces. And it has saved all the frames successfully.
The list content of cropped_faces[]:
Finally, I am using cv2.write() method in a for loop to write the frames to the video. The code is as follows:
height,width,layers = cropped_faces[0].shape
print(height,width,layers)
fourcc = cv2.VideoWriter_fourcc(*'H264')
video = cv2.VideoWriter('/content/gdrive/MyDrive/Kaggle/Data/output.mp4', fourcc,1,(width,height))
for j in range(0,5):
video.write(cropped_faces[j])
video.release()
cv2.destroyAllWindows()
print("The video was successfully saved")
The output:
Although there are no errors in the code, the video saved in the location '/content/gdrive/MyDrive/Kaggle/Data/' is not playable. Only a size 258bytes video is created.
Can somebody please help me to find the reason and solve this issue, please?

VLC is able to connect rtsp, but openCV and ffmpeg

I am trying to access live video stream via OpenCV. Firstly, I am typing the rtsp url in VLC and I can see the video without any problem
However, when I put the same rtsp url into my python code and ffmpeg, it is not able to catch any information.
The python code is very easy as following and I always got "error" print message at very first time.
import cv2
vcap = cv2.VideoCapture('rtsp://XX.XX.XX.XX/mystream')
while True:
ret, frame = vcap.read()
if ret:
cv2.imshow("test", frame)
cv2.waitKey(1)
else:
print("error")
break
and also, the ffmpeg part I just simply type following command to test is there any information I can access or not. But it is still not working.
ffmpeg -i rtsp://XX.XX.XX.XX/mystream
The codec I got from the VLC as the picture below:
Does anyone have the same problem with me? and how to figure it out?
Thank you.

alternative to cvWaitKey() function in iOS

ok, what i am trying to do is retrieve a frame from an existing video file, do some work on the frame and then save it to a new file,
what actually happens is that it writes some frames and then crashes as the code is quite fast,
if i don't put cvWaitKey() i get the same error i get when writing video frames with AVFoundation Library without using
AVAssetWriterInput.readyForMoreMediaData
OpenCV video writer is implemented using AVFoundation classes but we lose access to
AVAssetWriterInput.readyForMoreMediaData
or am i missing something ?
here is the code similar to what i'm trying to do,
while (grabResult&&frameResult) {
grabResult = cvGrabFrame(capture); // capture a frame
if(grabResult){
img = cvRetrieveFrame(capture, 0); // retrieve the captured frame
cvFlip(img,NULL,0); // edit img
frameResult = cvWriteFrame(writer,img); // add the frame to the file
cvWaitKey(-1); or anything that helps to finish adding the previous frame
}
}
I am trying to convert a video file using OpenCV (without displaying)
in my iPhone/iPad app, everything works except cvWaitKey() function
and I get this error:
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvWaitKey,
Without this function frames are dropped as there's no way to know if
the video writer is ready, is there an alternative to my problem?
I am using OpenCV 2.4.2 and I get same error with the latest
precompiled version of OpenCV.
Repaint the UIImageView:
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.0f]];
followed by your imshow or cvShowImage
I don't know anything about iOS development, but did you try calling the C++ interface method waitKey()?

Create 1080p videos with javacv and ffmpeg or alternate lib

is there a option in JavaCV to capture 1080p videos from images?
If i use the FFmpegFrameRecorde, i only find 480p.
Or is there a alternate library for Java?
I want to use it to create a video from kind of pictures (with zooming and rotating effects)
greetings
//EDIT
Okay,
now i've tested a very simple code:
FrameRecorder recorder = FFmpegFrameRecorder.createDefault("out.avi", 1920, 1080);
recorder.start();
recorder.record(iplImage);
recorder.stop();
and it's works! But the file is very large (10sec around 300MB...)
Now i want to add a codec like xvid. I've get the following eyxception:
com.googlecode.javacv.FrameRecorder$Exception: codec not found
But i've installed the xvid paket. Must i add the codec in a special folder like the ffmpeg bin?
Okay,
now i test the mp4 codec and all works fine :)
//UPDATE
for JavaCV 0.2
FrameRecorder recorder = FFmpegFrameRecorder.createDefault("out.avi", 1920, 1080);
recorder.setCodecID(CODEC_ID_MPEG4);
recorder.setPixelFormat(PIX_FMT_YUV420P);
recorder.start();
.....
recorder.stop();
for JavaCV 0.3
FrameRecorder recorder = FFmpegFrameRecorder.createDefault("out.avi", 1920, 1080);
recorder.setVideoCodec(CODEC_ID_MPEG4);
recorder.setFrameRate(fps);
recorder.setFormat("avi");
recorder.start();
.....
recorder.stop();

Can't access webcam with OpenCV

I'm using OpenCV 2.2 with visual studio 2010 on a win 7 64 bit pc.
I'm able to display pictures and play AVI files through OpenCV as given in the book "Learning OpenCV" but I'm not able to capture webcam images. Even the samples given along with the OpenCV files cant access the webcam.
I get asked for " video source -> capture source" and there are two options: HP webcam Splitter and HP webcam. If I select HP webcam the window closes immediately without displaying any error. (i think any error message is too fast to be seen before it closes). If I select HP Webcam splitter then the new window, where the webcam images(video) are supposed to come, is filled with uniform gray. The webcam LED is on but no video is seen. My webcam works fine with flash (www.testmycam.com) and with DirectShow http://www.codeproject.com/KB/audio-video/WebcamUsingDirectShowNET.aspx
I did try getting some error message by using this:
#include "cv.h"
#include "highgui.h"
#include <iostream>
using namespace cv;
using namespace std;
int main(int, char**)
{
VideoCapture cap("0"); // open the default camera
if(!cap.isOpened()) // check if we succeeded
{
cout << "Error opening camera!";
getchar();
return -1;
}
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
And the error message I got was:
warning: Error opening file (C:\Users\vp\work\ocv\opencv\modules\highgui\src\cap
_ffmpeg.cpp:454)
Error opening camera!
I don't know what this "cap_ffmpeg.cpp" is and I don't know if this is any issue with the nosy "HP Media Smart" stuff.
Any help will be greatly appreciated.
I had the same issue on Windows 7 64-bit. I had to recompile opencv_highgui changing the "Preprocesser Definitions" in the C/C++ panel of the properties page to include:
HAVE_VIDEOINPUT
HAVE_DSHOW
Hope this helps
The cap_ffmpeg.cpp is the source file which uses ffmpeg to perform capturing of the device. If the default example given from OpenCV doesn't work with your webcam, you are out of luck. I suggest you buy another one that is supported.
Recently I have installed OpenCV 2.2 and NetBeans 6.9.1. I had a problem with camera capture, the image in the window was black but the program runs perfectly, without errors. I had to run NetBeans as admin user to fix this problem.
I hope this can help you all.
I just switched to OpenCV 2.2 and am having essentially the same problem but a 32 bit compture running Vista. The webcam would start but I'd get an error message setting the width property for the camera. If I specifically request the DirectShow camera, the cvCreateCameraCapture would fail.
What I think is going on is that the distribution version of HighGUI was build excluding the DirectShow camera. The favored Windows camera on OpenCV used to be Video For Windows, VFW but that has been deprecated since Windows Vista came out and has created all sorts of problems. Why they don't just include it, I don't know. Check the source file cap.cpp
My next step is to rebuild HighGUI myself and make sure the flag HAVE_DSHOW is set. I seem to remember having the same problem with the last version of OpenCV I've been using until I rebuilt it making sure the DirectShow version was enabled.
I experienced the same problem. My Vaio Webcam LED is on but no image on the screen.
Then I tried to export the first frame to a JPEG file and its working. Then I tried to insert a delay of 33ms before capture any frame, this time it works like a charm. Hope this'll help.
Here's an article I wrote some time back. It uses the videoInput library to get input from webcams. It uses DirectX, so it works with almost every webcam out there. Capturing images with DirectX
Once you create the cv::VideoCapture you should give an integer not a string (since string implies the input is a file).
To open the default camera, open the stream with
cv::VideoCapture capture(0);
and it will work fine.
CMAKE GUI, MSVC++10E, Vista 32bit, OpenCV2.2
It looks like HAVE_VIDEOINPUT/WITH_VIDEOINPUT option doesn't work.
However adding: /D HAVE_DSHOW /D HAVE_VIDEOINPUT to CMAKE_CXX_FLAGS, and CMAKE_C_FLAGS did the trick for me (there will be warns due to macro redefinitions).

Resources