Spaces:
Runtime error
Runtime error
samusander
commited on
Commit
•
234b29c
1
Parent(s):
ad67cff
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import mediapipe as mp
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
mp_selfie_segmentation = mp.solutions.selfie_segmentation
|
7 |
+
BG_COLOR = (255, 255, 255)
|
8 |
+
|
9 |
+
def selfie_segmentation(image):
|
10 |
+
# Wenden Sie das Modell auf das Eingabebild an
|
11 |
+
with mp_selfie_segmentation.SelfieSegmentation(
|
12 |
+
model_selection=0) as selfie_segmentation:
|
13 |
+
image = cv2.imread(image)
|
14 |
+
image_height, image_width, _ = image.shape
|
15 |
+
|
16 |
+
# Convert the BGR image to RGB before processing.
|
17 |
+
results = selfie_segmentation.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
18 |
+
|
19 |
+
condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.1
|
20 |
+
bg_image = np.zeros(image.shape, dtype=np.uint8)
|
21 |
+
bg_image[:] = BG_COLOR
|
22 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
23 |
+
return np.where(condition, image, bg_image)
|
24 |
+
#im = Image.fromarray(output_image)
|
25 |
+
#img_var /= im.save("example.png")
|
26 |
+
#bla = "/content/example.png"
|
27 |
+
#bla = Image.open("/content/example.png")
|
28 |
+
#return img_var
|
29 |
+
|
30 |
+
#pil_image = Image.fromarray(output_image)
|
31 |
+
#byte_io = BytesIO()
|
32 |
+
#pil_image.save(byte_io, format='PNG')
|
33 |
+
#byte_io.seek(0)
|
34 |
+
#return byte_io.read()
|
35 |
+
|
36 |
+
|
37 |
+
# Start a interface in gradio
|
38 |
+
iface = gr.Interface(fn=selfie_segmentation,
|
39 |
+
inputs="image",
|
40 |
+
outputs="image")
|
41 |
+
|
42 |
+
# starten Sie die Schnittstelle
|
43 |
+
iface.launch()
|