lampongyuen commited on
Commit
2a352a6
·
1 Parent(s): 4ab9c9c

Upload app.py

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