radames commited on
Commit
fa573e9
1 Parent(s): 8011d72

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def predict(video_in, image_in_video, image_in_img):
4
+ if video_in == None and image_in_video == None and image_in_img == None:
5
+ raise gr.Error("Please upload a video or image.")
6
+ if image_in_video or image_in_img:
7
+ print("image", image_in_video, image_in_img)
8
+ image = image_in_video or image_in_img
9
+ return image
10
+
11
+ return video_in
12
+
13
+
14
+ def toggle(choice):
15
+ if choice == "webcam":
16
+ return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
17
+ else:
18
+ return gr.update(visible=False, value=None), gr.update(visible=True, value=None)
19
+
20
+
21
+ with gr.Blocks() as blocks:
22
+ gr.Markdown("### Video or Image? WebCam or Upload?""")
23
+ with gr.Tab("Video") as tab:
24
+ with gr.Row():
25
+ with gr.Column():
26
+ video_or_file_opt = gr.Radio(["webcam", "upload"], value="webcam",
27
+ label="How would you like to upload your video?")
28
+ video_in = gr.Video(source="webcam", include_audio=False)
29
+ video_or_file_opt.change(fn=lambda s: gr.update(source=s, value=None), inputs=video_or_file_opt,
30
+ outputs=video_in, queue=False)
31
+ with gr.Column():
32
+ video_out = gr.Video()
33
+ run_btn = gr.Button("Run")
34
+ run_btn.click(fn=predict, inputs=[video_in], outputs=[video_out])
35
+ gr.Examples(fn=predict, examples=[], inputs=[
36
+ video_in], outputs=[video_out])
37
+
38
+ with gr.Tab("Image"):
39
+ with gr.Row():
40
+ with gr.Column():
41
+ image_or_file_opt = gr.Radio(["webcam", "file"], value="webcam",
42
+ label="How would you like to upload your image?")
43
+ image_in_video = gr.Image(source="webcam", type="filepath")
44
+ image_in_img = gr.Image(
45
+ source="upload", visible=False, type="filepath")
46
+
47
+ image_or_file_opt.change(fn=toggle, inputs=[image_or_file_opt],
48
+ outputs=[image_in_video, image_in_img], queue=False)
49
+ with gr.Column():
50
+ image_out = gr.Image()
51
+ run_btn = gr.Button("Run")
52
+ run_btn.click(fn=predict, inputs=[
53
+ image_in_img, image_in_video], outputs=[image_out])
54
+ gr.Examples(fn=predict, examples=[], inputs=[
55
+ image_in_img, image_in_video], outputs=[image_out])
56
+
57
+ blocks.launch()