gradio icon indicating copy to clipboard operation
gradio copied to clipboard

Error displaying video generated by open-cv

Open zhewei-mt opened this issue 1 year ago • 3 comments

Describe the bug

Hello, I tried the following code to write frames into video:

    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    vid_out = cv2.VideoWriter(args.output, fourcc, fps, (w, h))
    ret = True
    while ret:
        ret, frame = cap.read()
        if ret:
                frame = inference_frame(frame)
                if len(frame) == 0:
                    continue
                detected += 1
                if args.output is not None:
                    vid_out.write(frame)
    cap.release()
    vid_out.release()

but this code gives an error result on website. 捕获 I tried different codecs, i.e. avc1, h264, avc1, VP90, theo, and only VP90 works but it is super slow.

And also, the uploaded video is mirrored for some reason. 捕获

Last, is there any way to change the 'content' in browser tags? Now it is displaying the same thing as the "title". 捕获2

Any solutions to those problems? Thanks!

Is there an existing issue for this?

  • [X] I have searched the existing issues

Reproduction

The code to reproduce the problem is attached in description.

Screenshot

No response

Logs

No logs although set debug = True

System Info

3.1.1

Severity

serious, but I can work around it

zhewei-mt avatar Aug 10 '22 09:08 zhewei-mt

Hi @zhewei-mt, thanks for creating this issue. Did you take a look at the discussion here: https://github.com/gradio-app/gradio/issues/1508?

Particularly this comment may be helpful: https://github.com/gradio-app/gradio/issues/1508#issuecomment-1154545730

abidlabs avatar Aug 10 '22 12:08 abidlabs

As far as the mirroring goes, you can disable it using the mirror_webcam parameter in the Video component. See here: https://gradio.app/docs/#video

abidlabs avatar Aug 10 '22 12:08 abidlabs

Hi @zhewei-mt, thanks for creating this issue. Did you take a look at the discussion here: #1508?

Particularly this comment may be helpful: #1508 (comment)

Already tried the methods mentioned in https://github.com/gradio-app/gradio/issues/1508#issuecomment-1154545730 but still having problems. What I did is simple change the parameter in cv2.VideoWriter_fourcc(), tried "avc1", "VP90" and "theo", only "VP90" gives the right answer but is slow.

zhewei-mt avatar Aug 11 '22 02:08 zhewei-mt

@zhewei-mt One solution is to manually convert the video to libx264 (mp4) codecs which is playable in the browser

import tempfile
import subprocess

# temp_file is original video created by opencv
# out_file is the new output with a browser-playable codecs
out_file = tempfile.NamedTemporaryFile(suffix="out.mp4", delete=False)
subprocess.run(f"ffmpeg -y -loglevel quiet -stats -i {temp_file.name} -c:v libx264 {out_file.name}".split())

freddyaboulton avatar Aug 11 '22 14:08 freddyaboulton