Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,19 +38,70 @@
|
|
| 38 |
# if __name__ == "__main__":
|
| 39 |
# demo.launch()
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
import gradio as gr
|
| 42 |
import cv2
|
| 43 |
import numpy as np
|
| 44 |
from ultralytics import YOLO
|
| 45 |
|
| 46 |
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
def process_image(image):
|
| 50 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
detected_faces = len(
|
|
|
|
| 54 |
|
| 55 |
if detected_faces == 1:
|
| 56 |
mask_tensor = results[0].masks.data[0].cpu().numpy()
|
|
@@ -65,13 +116,21 @@ def process_image(image):
|
|
| 65 |
rgba_image[:, :, 3] = mask
|
| 66 |
|
| 67 |
return rgba_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
# Return a blank image with error text
|
| 70 |
-
error_image = np.zeros((300, 500, 3), dtype=np.uint8)
|
| 71 |
-
error_message = "Error: More than one face detected."
|
| 72 |
-
cv2.putText(error_image, error_message, (20, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
|
| 73 |
|
| 74 |
-
return error_image
|
| 75 |
|
| 76 |
demo = gr.Interface(
|
| 77 |
fn=process_image,
|
|
|
|
| 38 |
# if __name__ == "__main__":
|
| 39 |
# demo.launch()
|
| 40 |
|
| 41 |
+
# import gradio as gr
|
| 42 |
+
# import cv2
|
| 43 |
+
# import numpy as np
|
| 44 |
+
# from ultralytics import YOLO
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# model = YOLO(r"best.pt")
|
| 48 |
+
|
| 49 |
+
# def process_image(image):
|
| 50 |
+
# image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 51 |
+
# results = model.predict(image, conf=0.15)
|
| 52 |
+
|
| 53 |
+
# detected_faces = len(results[0].boxes.cls) if results[0].boxes is not None else 0
|
| 54 |
+
|
| 55 |
+
# if detected_faces == 1:
|
| 56 |
+
# mask_tensor = results[0].masks.data[0].cpu().numpy()
|
| 57 |
+
# mask = (mask_tensor * 255).astype(np.uint8)
|
| 58 |
+
|
| 59 |
+
# mask = cv2.resize(mask, (image.shape[1], image.shape[0]))
|
| 60 |
+
# kernel = np.ones((5, 5), np.uint8)
|
| 61 |
+
# mask = cv2.dilate(mask, kernel, iterations=2)
|
| 62 |
+
# mask = cv2.erode(mask, kernel, iterations=2)
|
| 63 |
+
|
| 64 |
+
# rgba_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
|
| 65 |
+
# rgba_image[:, :, 3] = mask
|
| 66 |
+
|
| 67 |
+
# return rgba_image
|
| 68 |
+
|
| 69 |
+
# # Return a blank image with error text
|
| 70 |
+
# error_image = np.zeros((300, 500, 3), dtype=np.uint8)
|
| 71 |
+
# error_message = "Error: More than one face detected."
|
| 72 |
+
# cv2.putText(error_image, error_message, (20, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
|
| 73 |
+
|
| 74 |
+
# return error_image
|
| 75 |
+
|
| 76 |
+
# demo = gr.Interface(
|
| 77 |
+
# fn=process_image,
|
| 78 |
+
# inputs=gr.Image(type="numpy"),
|
| 79 |
+
# outputs=gr.Image(type="numpy"),
|
| 80 |
+
# title="Face Segmentation",
|
| 81 |
+
# description="Upload a singale face image"
|
| 82 |
+
# )
|
| 83 |
+
|
| 84 |
+
# if __name__ == "__main__":
|
| 85 |
+
# demo.launch()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
import gradio as gr
|
| 90 |
import cv2
|
| 91 |
import numpy as np
|
| 92 |
from ultralytics import YOLO
|
| 93 |
|
| 94 |
|
| 95 |
+
face_model = YOLO(r"face_det.pt")
|
| 96 |
+
seg_model = YOLO(r"seg_model.pt")
|
| 97 |
|
| 98 |
def process_image(image):
|
| 99 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 100 |
+
face_results = face_model.predict(image, conf=0.5, save=True)
|
| 101 |
+
results = seg_model.predict(image, conf=0.15)
|
| 102 |
|
| 103 |
+
detected_faces = len(face_results[0].boxes.cls) if results[0].boxes is not None else 0
|
| 104 |
+
print(detected_faces)
|
| 105 |
|
| 106 |
if detected_faces == 1:
|
| 107 |
mask_tensor = results[0].masks.data[0].cpu().numpy()
|
|
|
|
| 116 |
rgba_image[:, :, 3] = mask
|
| 117 |
|
| 118 |
return rgba_image
|
| 119 |
+
elif detected_faces >= 1:
|
| 120 |
+
# Return a blank image with error text
|
| 121 |
+
error_image = np.zeros((300, 500, 3), dtype=np.uint8)
|
| 122 |
+
error_message = "Error: More than one face detected."
|
| 123 |
+
cv2.putText(error_image, error_message, (20, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
|
| 124 |
+
return error_image
|
| 125 |
+
elif detected_faces == 0:
|
| 126 |
+
# Return a blank image with error text
|
| 127 |
+
error_image = np.zeros((300, 500, 3), dtype=np.uint8)
|
| 128 |
+
error_message = "Error: No face detected."
|
| 129 |
+
cv2.putText(error_image, error_message, (20, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
|
| 130 |
+
return error_image
|
| 131 |
+
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
|
|
|
| 134 |
|
| 135 |
demo = gr.Interface(
|
| 136 |
fn=process_image,
|