TDN-M commited on
Commit
24fa457
·
verified ·
1 Parent(s): 1b1a8e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -2,9 +2,8 @@ import cv2
2
  import mediapipe as mp
3
  import numpy as np
4
  import gradio as gr
5
- import base64
6
- import time
7
  import os
 
8
 
9
  # Set MPLCONFIGDIR to avoid Matplotlib cache permission issues
10
  os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib-cache"
@@ -31,7 +30,7 @@ def process_frame(frame, seg_enabled, blur_bg, set_bg, set_color, bg_color, blur
31
  "blur_bg": blur_bg,
32
  "set_bg": set_bg,
33
  "set_color": set_color,
34
- "bg_color": tuple(map(int, bg_color.split(","))) if set_color else (0, 0, 0),
35
  "blur_intensity": blur_intensity
36
  })
37
 
@@ -81,12 +80,12 @@ with gr.Blocks() as demo:
81
  gr.Markdown("# AI Background Remover")
82
  with gr.Row():
83
  with gr.Column():
84
- webcam = gr.Image(sources="webcam", label="Live Video", streaming=True)
85
  output_image = gr.Image(label="Processed Output")
86
  seg_enabled = gr.Checkbox(label="Enable Background Removal", value=True)
87
  blur_bg = gr.Checkbox(label="Blur Background")
88
  set_bg = gr.Checkbox(label="Custom Image Background")
89
- custom_image = gr.Image(label="Upload Custom Background")
90
  set_color = gr.Checkbox(label="Solid Color Background")
91
  bg_color = gr.Textbox(label="Background Color (R,G,B)", value="0,0,0")
92
  blur_intensity = gr.Slider(label="Blur Intensity", minimum=5, maximum=25, value=15, step=2)
@@ -95,8 +94,13 @@ with gr.Blocks() as demo:
95
  webcam.stream(
96
  fn=process_frame,
97
  inputs=[webcam, seg_enabled, blur_bg, set_bg, set_color, bg_color, blur_intensity, custom_image],
98
- outputs=[output_image, processing_time]
 
 
 
 
 
99
  )
100
 
101
  if __name__ == "__main__":
102
- demo.launch()
 
2
  import mediapipe as mp
3
  import numpy as np
4
  import gradio as gr
 
 
5
  import os
6
+ import time
7
 
8
  # Set MPLCONFIGDIR to avoid Matplotlib cache permission issues
9
  os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib-cache"
 
30
  "blur_bg": blur_bg,
31
  "set_bg": set_bg,
32
  "set_color": set_color,
33
+ "bg_color": tuple(map(int, bg_color.split(","))) if set_color and bg_color else (0, 0, 0),
34
  "blur_intensity": blur_intensity
35
  })
36
 
 
80
  gr.Markdown("# AI Background Remover")
81
  with gr.Row():
82
  with gr.Column():
83
+ webcam = gr.Image(sources=["webcam"], streaming=True, label="Live Video")
84
  output_image = gr.Image(label="Processed Output")
85
  seg_enabled = gr.Checkbox(label="Enable Background Removal", value=True)
86
  blur_bg = gr.Checkbox(label="Blur Background")
87
  set_bg = gr.Checkbox(label="Custom Image Background")
88
+ custom_image = gr.Image(label="Upload Custom Background", type="numpy")
89
  set_color = gr.Checkbox(label="Solid Color Background")
90
  bg_color = gr.Textbox(label="Background Color (R,G,B)", value="0,0,0")
91
  blur_intensity = gr.Slider(label="Blur Intensity", minimum=5, maximum=25, value=15, step=2)
 
94
  webcam.stream(
95
  fn=process_frame,
96
  inputs=[webcam, seg_enabled, blur_bg, set_bg, set_color, bg_color, blur_intensity, custom_image],
97
+ outputs=[output_image, processing_time],
98
+ _js="""async () => {
99
+ // Ensure webcam permissions are requested
100
+ await navigator.mediaDevices.getUserMedia({ video: true });
101
+ return true;
102
+ }"""
103
  )
104
 
105
  if __name__ == "__main__":
106
+ demo.launch(server_name="0.0.0.0", server_port=7860)