Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
@@ -10,7 +51,9 @@ def process_image(image):
|
|
10 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
11 |
results = model.predict(image, conf=0.15)
|
12 |
|
13 |
-
|
|
|
|
|
14 |
mask_tensor = results[0].masks.data[0].cpu().numpy()
|
15 |
mask = (mask_tensor * 255).astype(np.uint8)
|
16 |
|
@@ -23,9 +66,10 @@ def process_image(image):
|
|
23 |
rgba_image[:, :, 3] = mask
|
24 |
|
25 |
return rgba_image
|
26 |
-
|
27 |
return "Error: Uploaded image has more than one face. Please upload a different image."
|
28 |
-
|
|
|
29 |
|
30 |
demo = gr.Interface(
|
31 |
fn=process_image,
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# import cv2
|
3 |
+
# import numpy as np
|
4 |
+
# from ultralytics import YOLO
|
5 |
+
|
6 |
+
|
7 |
+
# model = YOLO(r"best.pt")
|
8 |
+
|
9 |
+
# def process_image(image):
|
10 |
+
# image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
11 |
+
# results = model.predict(image, conf=0.15)
|
12 |
+
|
13 |
+
# if len(results[0].boxes.cls) == 1:
|
14 |
+
# mask_tensor = results[0].masks.data[0].cpu().numpy()
|
15 |
+
# mask = (mask_tensor * 255).astype(np.uint8)
|
16 |
+
|
17 |
+
# mask = cv2.resize(mask, (image.shape[1], image.shape[0]))
|
18 |
+
# kernel = np.ones((5, 5), np.uint8)
|
19 |
+
# mask = cv2.dilate(mask, kernel, iterations=2)
|
20 |
+
# mask = cv2.erode(mask, kernel, iterations=2)
|
21 |
+
|
22 |
+
# rgba_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
|
23 |
+
# rgba_image[:, :, 3] = mask
|
24 |
+
|
25 |
+
# return rgba_image
|
26 |
+
# else:
|
27 |
+
# return "Error: Uploaded image has more than one face. Please upload a different image."
|
28 |
+
|
29 |
+
|
30 |
+
# demo = gr.Interface(
|
31 |
+
# fn=process_image,
|
32 |
+
# inputs=gr.Image(type="numpy"),
|
33 |
+
# outputs=gr.Image(type="numpy"),
|
34 |
+
# title="Face Segmentation",
|
35 |
+
# description="Upload an image"
|
36 |
+
# )
|
37 |
+
|
38 |
+
# if __name__ == "__main__":
|
39 |
+
# demo.launch()
|
40 |
+
|
41 |
+
|
42 |
import gradio as gr
|
43 |
import cv2
|
44 |
import numpy as np
|
|
|
51 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
52 |
results = model.predict(image, conf=0.15)
|
53 |
|
54 |
+
detected_faces = len(results[0].boxes.cls) if results[0].boxes is not None else 0
|
55 |
+
|
56 |
+
if detected_faces == 1:
|
57 |
mask_tensor = results[0].masks.data[0].cpu().numpy()
|
58 |
mask = (mask_tensor * 255).astype(np.uint8)
|
59 |
|
|
|
66 |
rgba_image[:, :, 3] = mask
|
67 |
|
68 |
return rgba_image
|
69 |
+
elif detected_faces > 1:
|
70 |
return "Error: Uploaded image has more than one face. Please upload a different image."
|
71 |
+
else:
|
72 |
+
return "Error: No face detected. Please upload a valid image."
|
73 |
|
74 |
demo = gr.Interface(
|
75 |
fn=process_image,
|