Skip to content

Fix video mirroring issue#2475

Merged
freddyaboulton merged 6 commits into
mainfrom
1804-fix-video-mirroring
Oct 18, 2022
Merged

Fix video mirroring issue#2475
freddyaboulton merged 6 commits into
mainfrom
1804-fix-video-mirroring

Conversation

@freddyaboulton
Copy link
Copy Markdown
Collaborator

@freddyaboulton freddyaboulton commented Oct 17, 2022

Description

Fixes #1804

There are two problems that I found:

  1. When source="upload", the front-end was flipping videos since mirror_webcam is always set to True in the config. Regardless of whether the source is a webcam. This is the problem causing Video mirroring #1804.
  2. When the source is webcam and the format is specified (not None), the video was not flipped in the backend. There isn't an issue about this filed I think.

Issue 1 Test code

import gradio as gr

def video_test(video):
    return video

inputs_video_p5 = gr.Video(format="mp4", source="upload", label="原始视频")  # webcam
outputs_webcam_p5 = gr.Video(format='mp4', label="检测视频")

gyd = gr.Interface(
    fn=video_test,
    inputs=[inputs_video_p5],
    outputs=[outputs_webcam_p5],
)
gyd.launch(
    inbrowser=True,
)

no_flip_if_upload

Issue 2 Test Code

import gradio as gr

def video_test(video):
    return video

inputs_video_p5 = gr.Video(format="mp4", source="webcam", label="原始视频")  # webcam
outputs_webcam_p5 = gr.Video(format='mp4', label="检测视频")

gyd = gr.Interface(
    fn=video_test,
    inputs=[inputs_video_p5],
    outputs=[outputs_webcam_p5],
)
gyd.launch(
    inbrowser=True,
)

video_mirrored_if_format_change

Checklist:

  • I have performed a self-review of my own code
  • I have added a short summary of my change to the CHANGELOG.md
  • My code follows the style guidelines of this project
  • I have commented my code in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

A note about the CHANGELOG

Hello 👋 and thank you for contributing to Gradio!

All pull requests must update the change log located in CHANGELOG.md, unless the pull request is labeled with the "no-changelog-update" label.

Please add a brief summary of the change to the Upcoming Release > Full Changelog section of the CHANGELOG.md file and include
a link to the PR (formatted in markdown) and a link to your github profile (if you like). For example, "* Added a cool new feature by [@myusername](link-to-your-github-profile) in [PR 11111](https://github.com/gradio-app/gradio/pull/11111)".

If you would like to elaborate on your change further, feel free to include a longer explanation in the other sections.
If you would like an image/gif/video showcasing your feature, it may be best to edit the CHANGELOG file using the
GitHub web UI since that lets you upload files directly via drag-and-drop.

@github-actions
Copy link
Copy Markdown
Contributor

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2475-all-demos

Comment thread gradio/components.py Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Consolidating the two FFmpeg calls into one.

@freddyaboulton freddyaboulton force-pushed the 1804-fix-video-mirroring branch from 1aa3ba6 to 2013aa9 Compare October 17, 2022 21:04
@freddyaboulton freddyaboulton marked this pull request as ready for review October 17, 2022 21:04
@pngwn
Copy link
Copy Markdown
Member

pngwn commented Oct 17, 2022

Code looks good but I could never reproduce this, so will leave for someone else to test. It works fine for me, but it always did.

Comment thread gradio/components.py Outdated
uploaded_format = file_name.suffix.replace(".", "")

modify_format = self.format is not None and uploaded_format != self.format
flip = self.source == "webcam" and self.mirror_webcam is True
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
flip = self.source == "webcam" and self.mirror_webcam is True
flip = self.source == "webcam" and self.mirror_webcam

Comment thread gradio/components.py Outdated
flip_suffix = "_flip" if flip else ""
output_file_name = str(
file_name.with_name(
f"{file_name.name.replace(file_name.suffix, '')}{flip_suffix}{format}"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would this work?

Suggested change
f"{file_name.name.replace(file_name.suffix, '')}{flip_suffix}{format}"
f"{file_name.stem}{flip_suffix}{format}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes!

@abidlabs
Copy link
Copy Markdown
Member

Looks awesome @freddyaboulton!

@freddyaboulton
Copy link
Copy Markdown
Collaborator Author

Thanks for the review all!

@freddyaboulton freddyaboulton merged commit dae17bb into main Oct 18, 2022
@freddyaboulton freddyaboulton deleted the 1804-fix-video-mirroring branch October 18, 2022 15:07
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.

Video mirroring

3 participants