Spaces:
Runtime error
Runtime error
initial segmentation code
Browse files- README.md +12 -0
- app.py +21 -4
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: SelfieSegmentation
|
| 3 |
+
emoji: 📐
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.17.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -7,16 +7,33 @@ import numpy as np
|
|
| 7 |
mp_drawing = mp.solutions.drawing_utils
|
| 8 |
mp_selfie_segmentation = mp.solutions.selfie_segmentation
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def snap(video):
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
demo = gr.Interface(
|
| 17 |
fn=snap,
|
| 18 |
-
inputs=gr.
|
| 19 |
-
outputs=
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
if __name__ == "__main__":
|
|
|
|
| 7 |
mp_drawing = mp.solutions.drawing_utils
|
| 8 |
mp_selfie_segmentation = mp.solutions.selfie_segmentation
|
| 9 |
|
| 10 |
+
seg = mp_selfie_segmentation.SelfieSegmentation()
|
| 11 |
+
|
| 12 |
+
BG_COLOR = (0, 0, 0)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def close_segmentation_model():
|
| 16 |
+
print("Closing segmentation model")
|
| 17 |
+
seg.close()
|
| 18 |
+
|
| 19 |
|
| 20 |
def snap(video):
|
| 21 |
+
image = cv2.cvtColor(video, cv2.COLOR_RGB2BGR)
|
| 22 |
+
results = seg.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 23 |
+
condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.5
|
| 24 |
+
|
| 25 |
+
bg_image = np.zeros(image.shape, dtype=np.uint8)
|
| 26 |
+
bg_image[:] = BG_COLOR
|
| 27 |
+
output_image = np.where(condition, video, bg_image)
|
| 28 |
+
|
| 29 |
+
return output_image
|
| 30 |
|
| 31 |
|
| 32 |
demo = gr.Interface(
|
| 33 |
fn=snap,
|
| 34 |
+
inputs=gr.Image(sources=["webcam"], streaming=True),
|
| 35 |
+
outputs="image",
|
| 36 |
+
live=True,
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
mediapipe
|
| 3 |
+
opencv-python
|