asif00 commited on
Commit
04c39e7
·
1 Parent(s): 4886b30
Files changed (1) hide show
  1. app.py +38 -18
app.py CHANGED
@@ -24,33 +24,53 @@ def process_frame(frame):
24
  segments = results[0].plot()
25
  return segments, f'FPS: {int(1 // (end - start))}'
26
 
27
- def process_inputs(action, mode_selection, frame, uploaded_video):
28
  global process, model
29
- if action == "Start" and not process and mode_selection:
30
- if mode_selection == "Webcam" and frame is not None:
31
- process = True
32
- model = load_yolo_model()
 
 
 
 
 
 
 
 
 
 
 
 
33
  return process_frame(frame)
34
- elif mode_selection == "Video" and uploaded_video is not None:
35
- process = True
36
- model = load_yolo_model()
37
- return process_frame(uploaded_video)
38
- elif action == "Stop" and process:
39
  process = False
 
40
 
41
- action = gr.inputs.Dropdown(choices=["Start", "Stop"], label="Action")
42
- mode_selection = gr.inputs.Radio(["Webcam", "Video"], label="Mode Selection")
43
  frame = gr.inputs.Video(label="Webcam Feed")
44
  uploaded_video = gr.inputs.Video(label="Upload Video")
45
 
46
- iface = gr.Interface(
47
- fn=process_inputs,
48
- inputs=[action, mode_selection, frame, uploaded_video],
 
 
 
 
 
 
 
 
 
 
49
  outputs=[gr.components.Image(), gr.components.Textbox()],
50
  live=True,
51
- title="YOLO Image Segmentation",
52
- 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 select 'Start' to begin. Select 'Stop' to end the process.",
53
  theme="huggingface"
54
  )
55
 
56
- iface.launch()
 
 
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.inputs.Checkbox(label="Start")
51
+ stop_button = gr.inputs.Checkbox(label="Stop")
52
  frame = gr.inputs.Video(label="Webcam Feed")
53
  uploaded_video = gr.inputs.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
+ theme="huggingface"
63
+ )
64
+
65
+ video_interface = gr.Interface(
66
+ fn=process_video,
67
+ inputs=[start_button, stop_button, uploaded_video],
68
  outputs=[gr.components.Image(), gr.components.Textbox()],
69
  live=True,
70
+ title="YOLO Image Segmentation (Video)",
71
+ 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.",
72
  theme="huggingface"
73
  )
74
 
75
+ webcam_interface.launch()
76
+ video_interface.launch()