Skip to content

Save Output to Video format#126

Merged
thtrieu merged 4 commits into
thtrieu:masterfrom
Prakash19921206:master
Apr 1, 2017
Merged

Save Output to Video format#126
thtrieu merged 4 commits into
thtrieu:masterfrom
Prakash19921206:master

Conversation

@Prakash19921206

@Prakash19921206 Prakash19921206 commented Apr 1, 2017

Copy link
Copy Markdown
Contributor

Tested and working fine.

along with --demo ,user need to provide --saveVideo 1 (1 means True, 0 means False which is default )
i'm writing video to the same input resolution provided.
i'm calling get_fps(self,frame) because for cpu it will be around 0.4fps for gpu will be around 30 fps

also i have added namedWindow, so that output window can be resized (full screen or whichever size)

Comment thread flow Outdated

if FLAGS.demo:
tfnet.camera(FLAGS.demo)
tfnet.camera(FLAGS.demo,FLAGS.saveVideo)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest a space coming after the comma

Comment thread net/help.py Outdated
self.sess.run(op, {plh: val})

def camera(self, file):
def camera(self, file,SaveVideo):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest a space coming after the comma

Comment thread net/help.py Outdated
fps = get_fps(self,frame)
height, width, _ = frame.shape
videoWriter = cv2.VideoWriter('video.avi', fourcc, fps, (width,height))
if(SaveVideo):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if SaveVideo:

Comment thread net/help.py Outdated
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = get_fps(self,frame)
height, width, _ = frame.shape
videoWriter = cv2.VideoWriter('video.avi', fourcc, fps, (width,height))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(width, height)

Comment thread net/help.py Outdated
videoWriter = cv2.VideoWriter('video.avi', fourcc, fps, (width,height))
while camera.isOpened():
_, frame = camera.read()
if (frame is None):

@thtrieu thtrieu Apr 1, 2017

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if frame is None: (no need parantheses)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not mine, will change it anyway :)

Comment thread net/help.py Outdated
net_out = self.sess.run(self.out,feed_dict)[0]
processed = self.framework.postprocess(net_out, frame, False)
videoWriter.write(processed)
if (SaveVideo):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if SaveVideo:

Comment thread net/help.py Outdated
if(SaveVideo):
_, frame = camera.read()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = get_fps(self,frame)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fps = get_fps(self, frame)

@thtrieu

thtrieu commented Apr 1, 2017

Copy link
Copy Markdown
Owner

Thanks!

I suggest:

  1. A few style fixes as I commented in the code.
  2. Adding a section into README, note that --saveVideo is enough for saving the video, and not specifying --saveVideo means no saving, there is no need for values 0, 1.

@thtrieu thtrieu reopened this Apr 1, 2017
@thtrieu thtrieu merged commit 300f014 into thtrieu:master Apr 1, 2017
@borasy

borasy commented Apr 2, 2017

Copy link
Copy Markdown
Contributor

I tested on an mpeg4 codec video, and i get the following error, and the video is not saved.

[mpeg4 @ 0x3714460] framerate not set
Could not open codec 'mpeg4': Unspecified error

fps printed from your get_fps() is 0.249...
frame rate from original video is 25
If i set the fps value to 25, manually, the video is saved fine. @Prakash19921206 @thtrieu

@thtrieu

thtrieu commented Apr 2, 2017

Copy link
Copy Markdown
Owner

I am no expert on codec, so waiting for people to come help ...

@borasy

borasy commented Apr 2, 2017

Copy link
Copy Markdown
Contributor

@thtrieu the problem is not with codec here.. the problem is with the fps.. like i said, i put 25 manually in the videoWriter = cv2.VideoWriter('video.avi', fourcc, 25, (width, height)) and it works...

Update After testing on various video codec, i can confirm it's not the codec that's the problem, it's the incorrect fps. FPS should be the same as the input video. not calculated by the processed time of single frame.

@Prakash19921206

Prakash19921206 commented Apr 2, 2017

Copy link
Copy Markdown
Contributor Author

@borasy, MPEG4 or mp4 is only container name (aka extension), can you mention codec name?
use MediaInfo software to check details about your video..can you revert back with details shown? i'll try video with same settings can you share few details reg your OS, CPU or GPU ? and are you using latest version of OpenCV ?
this could be problem of codec although it looks like setting frame rate issue

make sure u have installed codec pack from here or here
i tested on few video formats like MPEG-4 Visual (H.264 Codec) , MPEG-4 (H.263 Codec), WMV (WVC1 Codec) it worked fine

for camera demo, we have to set the same fps as you get output (Note: for camera if you set fps as 25 & record video at lower resolution like 0.5, then playback will be extremely fast)
but for input video we can create smooth playback by getting original fps and setting the same fps in code.
this seems to be a small bug in OpenCv, when time permits today. i'll change code accordingly and create a pull request.

@borasy

borasy commented Apr 2, 2017

Copy link
Copy Markdown
Contributor

@Prakash19921206 yes, i understand what you mean. At this point we can declare that the codec is not the problem. Sorry my first comment made this confusion. The problem is with the FPS indeed. I think the simplest solution is to just get the FPS from the input video and set it into VideoWriter(), there's no need of calculation. I used this fps = camera.get(cv2.CAP_PROP_FPS) to get the FPS from the original input video, and everything works fine. I can create the pull request if you don't mind. @thtrieu

@Prakash19921206

Copy link
Copy Markdown
Contributor Author

@borasy , sure please create PR, i'm slightly occupied now..
also can you test if the code fps = camera.get(cv2.CAP_PROP_FPS) is working fine for webcam? (check playback once after saving with webcam input)

i think, for webcam we have to calculate fps.. or else it video playback might be faster or slower..
if above happens then , can you find a workaround? there are many stackoverflow questions on this..

also change this line to

    if SaveVideo:
        videoWriter.release()

this will avoid one error message, in case if user do not want to save video...

@borasy

borasy commented Apr 2, 2017

Copy link
Copy Markdown
Contributor

@Prakash19921206 My webcam is broken at the moment. After i got a new one, i'll test on the webcam, too. I also fixed your mentioned bug and created a PR already. Have a nice weekend.

@Prakash19921206

Copy link
Copy Markdown
Contributor Author

cv2.CAP_PROP_FPS is not stable. with my desktop, i have usb webcam
when i tried fps = camera.get(cv2.CAP_PROP_FPS) i'm getting 0 for fps hence video also not recording

but it works for video input

@borasy

borasy commented Apr 2, 2017

Copy link
Copy Markdown
Contributor

@Prakash19921206 I see the problem. This should fix it.
https://www.learnopencv.com/how-to-find-frame-rate-or-frames-per-second-fps-in-opencv-python-cpp/
But i don't have a webcam to test it yet, so can u fix this one?

@Prakash19921206

Copy link
Copy Markdown
Contributor Author

i think for webcam, the method i have given to calculate fps is proper (what you see is what you record)

i tried above method, it showed 13fps
so in code i set to 13, for this video playback was very fast.

only change we need to do is to set fps = camera.get(cv2.CAP_PROP_FPS) for video input and not for camera input

@borasy

borasy commented Apr 3, 2017

Copy link
Copy Markdown
Contributor

hmm.. i see. You're right. We should use your get_fps() for camera/webcam. But we also need one more change. Your method is calculating the X time it takes for 1 frame to be processed, right? So if we want to know how many frames it can process in 1 second. then fps should be equals to 1/X. Then it should be
X = get_fps(self, frame) and fps = 1/X. Do you agree?

@Prakash19921206

Copy link
Copy Markdown
Contributor Author

yeah i had missed it..thanks for pointing it out.. what i noticed is, opencv had a bug.
for camera, if input is below 1 fps it doesn't save video....so in code i'm checking if fps is lower than 1

if fps < 1:
    fps = 1

there was code conflict in PR, after that 1 function was missed from my fork!
waiting for @thtrieu to add get_fps in code as said here

@borasy

borasy commented Apr 3, 2017

Copy link
Copy Markdown
Contributor

Cool. for now that's the way to do it. Awesome job.

@evgkarasev

Copy link
Copy Markdown

Well done, guys! It works like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants