lampongyuen commited on
Commit
048f158
·
1 Parent(s): 69bd671

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -17
app.py CHANGED
@@ -1,21 +1,65 @@
1
  import gradio as gr
2
  import cv2
3
- import numpy as np
4
- invideo = cv2.VideoCapture('https://www.youtube.com/watch?v=5WbClF6WXRE')
5
- cv2.namedWindow('Live cam',cv.WINDOW_NORMAL)
6
- if not invideo.isOpened():
7
- print("Cannot open camera")
8
- exit()
9
- while True:
10
- # Capture frame-by-frame
11
- read_correct, frame = invideo.read()
12
- if not read_correct:
13
- print("cannot read correctly")
14
- break
15
- cv2.imshow('Video Capture from Camera', frame)
16
- if cv.waitKey(25) == ord('q'):
17
- break
18
- invideo.release()
19
- cv.destroyAllWindows()
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
 
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
+ def predict(video_in, image_in_video, image_in_img):
10
+ if video_in == None and image_in_video == None and image_in_img == None:
11
+ raise gr.Error("Please upload a video or image.")
12
+ if image_in_video or image_in_img:
13
+ print("image", image_in_video, image_in_img)
14
+ image = image_in_video or image_in_img
15
+ return image
16
+
17
+ return video_in
18
+
19
+
20
+ def toggle(choice):
21
+ if choice == "webcam":
22
+ return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
23
+ else:
24
+ return gr.update(visible=False, value=None), gr.update(visible=True, value=None)
25
+
26
+
27
+ with gr.Blocks() as blocks:
28
+ gr.Markdown("### Video or Image? WebCam or Upload?""")
29
+ with gr.Tab("Video") as tab:
30
+ with gr.Row():
31
+ with gr.Column():
32
+ video_or_file_opt = gr.Radio(["webcam", "upload"], value="webcam",
33
+ label="How would you like to upload your video?")
34
+ video_in = gr.Video(source="webcam", include_audio=False)
35
+ video_or_file_opt.change(fn=lambda s: gr.update(source=s, value=None), inputs=video_or_file_opt,
36
+ outputs=video_in, queue=False, show_progress=False)
37
+ with gr.Column():
38
+ video_out = gr.Video()
39
+ run_btn = gr.Button("Run")
40
+ run_btn.click(fn=predict, inputs=[video_in], outputs=[video_out])
41
+ gr.Examples(fn=predict, examples=[], inputs=[
42
+ video_in], outputs=[video_out])
43
+
44
+ with gr.Tab("Image"):
45
+ with gr.Row():
46
+ with gr.Column():
47
+ image_or_file_opt = gr.Radio(["webcam", "file"], value="webcam",
48
+ label="How would you like to upload your image?")
49
+ image_in_video = gr.Image(source="webcam", type="filepath")
50
+ image_in_img = gr.Image(
51
+ source="upload", visible=False, type="filepath")
52
+
53
+ image_or_file_opt.change(fn=toggle, inputs=[image_or_file_opt],
54
+ outputs=[image_in_video, image_in_img], queue=False, show_progress=False)
55
+ with gr.Column():
56
+ image_out = gr.Image()
57
+ run_btn = gr.Button("Run")
58
+ run_btn.click(fn=predict, inputs=[
59
+ image_in_img, image_in_video], outputs=[image_out])
60
+ gr.Examples(fn=predict, examples=[], inputs=[
61
+ image_in_img, image_in_video], outputs=[image_out])
62
+
63
+ blocks.queue()
64
+ blocks.launch()
65