asif00 commited on
Commit
c4f5fa9
·
1 Parent(s): 04dda20
Files changed (1) hide show
  1. app.py +15 -33
app.py CHANGED
@@ -24,53 +24,35 @@ def process_frame(frame):
24
  segments = results[0].plot()
25
  return segments, f'FPS: {int(1 // (end - start))}'
26
 
27
- def process_webcam(start_button, stop_button, frame):
28
  global process, model
29
- if start_button and not process and frame is not None:
30
  process = True
31
  model = load_yolo_model()
32
- return process_frame(frame)
33
- elif stop_button and process:
34
- process = False
35
- return None, ""
36
-
37
- def process_video(start_button, stop_button, uploaded_video):
38
- global process, model
39
- if start_button and not process and uploaded_video is not None:
40
- process = True
41
- model = load_yolo_model()
42
- cap = cv.VideoCapture(uploaded_video.name)
43
- ret, frame = cap.read()
44
- if ret:
45
  return process_frame(frame)
 
 
 
 
 
46
  elif stop_button and process:
47
  process = False
48
  return None, ""
49
 
50
  start_button = gr.components.Checkbox(label="Start")
51
  stop_button = gr.components.Checkbox(label="Stop")
 
52
  frame = gr.components.Video(label="Webcam Feed")
53
  uploaded_video = gr.components.Video(label="Upload Video")
54
 
55
- webcam_interface = gr.Interface(
56
- fn=process_webcam,
57
- inputs=[start_button, stop_button, frame],
58
- outputs=[gr.components.Image(), gr.components.Textbox()],
59
- live=True,
60
- title="YOLO Image Segmentation (Webcam)",
61
- description="This application uses the YOLO model to perform image segmentation on a webcam feed. Check 'Start' to begin. Check 'Stop' to end the process.",
62
- )
63
-
64
- video_interface = gr.Interface(
65
- fn=process_video,
66
- inputs=[start_button, stop_button, uploaded_video],
67
  outputs=[gr.components.Image(), gr.components.Textbox()],
68
  live=True,
69
- title="YOLO Image Segmentation (Video)",
70
- description="This application uses the YOLO model to perform image segmentation on an uploaded video. Upload a video and check 'Start' to begin. Check 'Stop' to end the process.",
71
  )
72
 
73
- gr.TabbedInterface(
74
- [webcam_interface, video_interface],
75
- tab_names=['Webcam inference', 'Video inference']
76
- ).launch(share=True)
 
24
  segments = results[0].plot()
25
  return segments, f'FPS: {int(1 // (end - start))}'
26
 
27
+ def process_inputs(start_button, stop_button, mode_selection, frame, uploaded_video):
28
  global process, model
29
+ if start_button and not process:
30
  process = True
31
  model = load_yolo_model()
32
+ if mode_selection == "Webcam" and frame is not None:
 
 
 
 
 
 
 
 
 
 
 
 
33
  return process_frame(frame)
34
+ elif mode_selection == "Video" and uploaded_video is not None:
35
+ cap = cv.VideoCapture(uploaded_video.name)
36
+ ret, frame = cap.read()
37
+ if ret:
38
+ return process_frame(frame)
39
  elif stop_button and process:
40
  process = False
41
  return None, ""
42
 
43
  start_button = gr.components.Checkbox(label="Start")
44
  stop_button = gr.components.Checkbox(label="Stop")
45
+ mode_selection = gr.components.Radio(["Webcam", "Video"], label="Mode Selection")
46
  frame = gr.components.Video(label="Webcam Feed")
47
  uploaded_video = gr.components.Video(label="Upload Video")
48
 
49
+ iface = gr.Interface(
50
+ fn=process_inputs,
51
+ inputs=[start_button, stop_button, mode_selection, frame, uploaded_video],
 
 
 
 
 
 
 
 
 
52
  outputs=[gr.components.Image(), gr.components.Textbox()],
53
  live=True,
54
+ title="YOLO Image Segmentation",
55
+ description="This application uses the YOLO model to perform image segmentation on a webcam feed or an uploaded video. Select 'Webcam' or 'Video', upload a video (if applicable), and check 'Start' to begin. Check 'Stop' to end the process.",
56
  )
57
 
58
+ iface.launch(share=True)