Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,93 @@
|
|
| 1 |
from PIL import Image
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from ultralytics import YOLO
|
| 5 |
|
| 6 |
MODEL_PATH = "best.pt"
|
| 7 |
model = YOLO(MODEL_PATH)
|
| 8 |
|
| 9 |
-
def
|
| 10 |
if image is None:
|
| 11 |
return None
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
demo = gr.Interface(
|
| 18 |
-
fn=
|
| 19 |
inputs=[
|
| 20 |
-
gr.Image(type="pil", label="Input
|
| 21 |
-
gr.Slider(0.05, 0.9, value=0.25, step=0.05, label="Confidence"),
|
|
|
|
|
|
|
| 22 |
],
|
| 23 |
-
outputs=gr.Image(type="pil", label="
|
| 24 |
-
title="DogFLW
|
| 25 |
-
description="
|
| 26 |
)
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from PIL import Image
|
| 2 |
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
import gradio as gr
|
| 5 |
from ultralytics import YOLO
|
| 6 |
|
| 7 |
MODEL_PATH = "best.pt"
|
| 8 |
model = YOLO(MODEL_PATH)
|
| 9 |
|
| 10 |
+
def draw_pose_clean(image, conf=0.25, show_labels=False, point_size=3):
|
| 11 |
if image is None:
|
| 12 |
return None
|
| 13 |
+
|
| 14 |
+
img = np.array(image.convert("RGB"))
|
| 15 |
+
results = model.predict(source=img, conf=conf, imgsz=640, verbose=False)[0]
|
| 16 |
+
|
| 17 |
+
out = img.copy()
|
| 18 |
+
if results.keypoints is None or len(results.keypoints.xy) == 0:
|
| 19 |
+
return Image.fromarray(out)
|
| 20 |
+
|
| 21 |
+
kpts_xy = results.keypoints.xy.cpu().numpy()
|
| 22 |
+
kpts_conf = results.keypoints.conf.cpu().numpy() if results.keypoints.conf is not None else None
|
| 23 |
+
|
| 24 |
+
for det_i, det_kpts in enumerate(kpts_xy):
|
| 25 |
+
confs = kpts_conf[det_i] if kpts_conf is not None else np.ones(len(det_kpts))
|
| 26 |
+
for i, (x, y) in enumerate(det_kpts):
|
| 27 |
+
score = float(confs[i])
|
| 28 |
+
if score < conf:
|
| 29 |
+
continue
|
| 30 |
+
|
| 31 |
+
x_i, y_i = int(x), int(y)
|
| 32 |
+
cv2.circle(out, (x_i, y_i), int(point_size), (0, 255, 0), -1, lineType=cv2.LINE_AA)
|
| 33 |
+
|
| 34 |
+
# Optional: only label every 5th keypoint to reduce clutter
|
| 35 |
+
if show_labels and (i % 5 == 0):
|
| 36 |
+
cv2.putText(
|
| 37 |
+
out,
|
| 38 |
+
f"{i+1}",
|
| 39 |
+
(x_i + 4, y_i - 4),
|
| 40 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 41 |
+
0.35,
|
| 42 |
+
(255, 255, 0),
|
| 43 |
+
1,
|
| 44 |
+
cv2.LINE_AA,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return Image.fromarray(out)
|
| 48 |
|
| 49 |
demo = gr.Interface(
|
| 50 |
+
fn=draw_pose_clean,
|
| 51 |
inputs=[
|
| 52 |
+
gr.Image(type="pil", label="Input image"),
|
| 53 |
+
gr.Slider(0.05, 0.9, value=0.25, step=0.05, label="Confidence threshold"),
|
| 54 |
+
gr.Checkbox(value=False, label="Show keypoint index labels (sparser)"),
|
| 55 |
+
gr.Slider(1, 8, value=3, step=1, label="Point size"),
|
| 56 |
],
|
| 57 |
+
outputs=gr.Image(type="pil", label="Output"),
|
| 58 |
+
title="DogFLW Pose (Clean View)",
|
| 59 |
+
description="Less cluttered output: points only by default. Toggle sparse labels if needed.",
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
demo.launch()
|
| 64 |
+
|
| 65 |
+
# from PIL import Image
|
| 66 |
+
# import numpy as np
|
| 67 |
+
# import gradio as gr
|
| 68 |
+
# from ultralytics import YOLO
|
| 69 |
+
|
| 70 |
+
# MODEL_PATH = "best.pt"
|
| 71 |
+
# model = YOLO(MODEL_PATH)
|
| 72 |
+
|
| 73 |
+
# def predict(image, conf):
|
| 74 |
+
# if image is None:
|
| 75 |
+
# return None
|
| 76 |
+
# results = model.predict(source=np.array(image), conf=conf, imgsz=640, verbose=False)[0]
|
| 77 |
+
# plotted = results.plot() # BGR numpy array
|
| 78 |
+
# plotted = plotted[:, :, ::-1] # BGR -> RGB
|
| 79 |
+
# return Image.fromarray(plotted)
|
| 80 |
+
|
| 81 |
+
# demo = gr.Interface(
|
| 82 |
+
# fn=predict,
|
| 83 |
+
# inputs=[
|
| 84 |
+
# gr.Image(type="pil", label="Input Image"),
|
| 85 |
+
# gr.Slider(0.05, 0.9, value=0.25, step=0.05, label="Confidence"),
|
| 86 |
+
# ],
|
| 87 |
+
# outputs=gr.Image(type="pil", label="Pose Result"),
|
| 88 |
+
# title="DogFLW YOLOv8 Pose",
|
| 89 |
+
# description="Upload a dog image to detect 46 facial landmarks."
|
| 90 |
+
# )
|
| 91 |
+
|
| 92 |
+
# if __name__ == "__main__":
|
| 93 |
+
# demo.launch()
|