Update app.py
Browse files
app.py
CHANGED
|
@@ -13,9 +13,8 @@ transforms = torch.hub.load("isl-org/MiDaS", "transforms", trust_repo=True)
|
|
| 13 |
transform = transforms.small_transform
|
| 14 |
|
| 15 |
# -------- Load YOLOv8 Pose Model ----------
|
| 16 |
-
# Using ultralytics hub (needs internet)
|
| 17 |
from ultralytics import YOLO
|
| 18 |
-
pose_model = YOLO("yolov8n-pose.pt") # small model
|
| 19 |
|
| 20 |
def run_pose_depth(image: Image.Image):
|
| 21 |
# Convert PIL to OpenCV RGB
|
|
@@ -25,7 +24,7 @@ def run_pose_depth(image: Image.Image):
|
|
| 25 |
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 26 |
h, w, _ = img_rgb.shape
|
| 27 |
|
| 28 |
-
# --- 1. Depth ---
|
| 29 |
input_batch = transform(img_rgb).to(device)
|
| 30 |
with torch.no_grad():
|
| 31 |
prediction = midas(input_batch)
|
|
@@ -36,6 +35,8 @@ def run_pose_depth(image: Image.Image):
|
|
| 36 |
align_corners=False
|
| 37 |
).squeeze()
|
| 38 |
depth_map = prediction.cpu().numpy()
|
|
|
|
|
|
|
| 39 |
depth_norm = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
| 40 |
depth_img = (depth_norm * 255).astype(np.uint8)
|
| 41 |
depth_img = cv2.applyColorMap(depth_img, cv2.COLORMAP_MAGMA)
|
|
@@ -48,17 +49,20 @@ def run_pose_depth(image: Image.Image):
|
|
| 48 |
if results.keypoints is not None:
|
| 49 |
for person in results.keypoints:
|
| 50 |
joints = []
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
x, y, conf = kp
|
| 54 |
# Clamp x,y
|
| 55 |
px = int(np.clip(x, 0, w-1))
|
| 56 |
py = int(np.clip(y, 0, h-1))
|
| 57 |
-
z = depth_map[py, px] # Z from MiDaS
|
| 58 |
-
joints.append({"x": float(x), "y": float(y), "z":
|
| 59 |
keypoints_list.append(joints)
|
| 60 |
|
| 61 |
-
# --- 3. Print keypoints ---
|
| 62 |
for i, joints in enumerate(keypoints_list):
|
| 63 |
print(f"Person {i+1}:")
|
| 64 |
for j, kp in enumerate(joints):
|
|
@@ -72,7 +76,7 @@ iface = gr.Interface(
|
|
| 72 |
inputs=gr.Image(type="pil"),
|
| 73 |
outputs=[gr.Image(type="pil"), gr.JSON()],
|
| 74 |
title="YOLO Pose + MiDaS Depth",
|
| 75 |
-
description="Upload an image
|
| 76 |
)
|
| 77 |
|
| 78 |
iface.launch()
|
|
|
|
| 13 |
transform = transforms.small_transform
|
| 14 |
|
| 15 |
# -------- Load YOLOv8 Pose Model ----------
|
|
|
|
| 16 |
from ultralytics import YOLO
|
| 17 |
+
pose_model = YOLO("yolov8n-pose.pt") # small model for speed
|
| 18 |
|
| 19 |
def run_pose_depth(image: Image.Image):
|
| 20 |
# Convert PIL to OpenCV RGB
|
|
|
|
| 24 |
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 25 |
h, w, _ = img_rgb.shape
|
| 26 |
|
| 27 |
+
# --- 1. Depth Estimation ---
|
| 28 |
input_batch = transform(img_rgb).to(device)
|
| 29 |
with torch.no_grad():
|
| 30 |
prediction = midas(input_batch)
|
|
|
|
| 35 |
align_corners=False
|
| 36 |
).squeeze()
|
| 37 |
depth_map = prediction.cpu().numpy()
|
| 38 |
+
|
| 39 |
+
# Normalize for visualization
|
| 40 |
depth_norm = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
| 41 |
depth_img = (depth_norm * 255).astype(np.uint8)
|
| 42 |
depth_img = cv2.applyColorMap(depth_img, cv2.COLORMAP_MAGMA)
|
|
|
|
| 49 |
if results.keypoints is not None:
|
| 50 |
for person in results.keypoints:
|
| 51 |
joints = []
|
| 52 |
+
person_array = person.cpu().numpy() # convert tensor to numpy
|
| 53 |
+
for kp in person_array:
|
| 54 |
+
kp = np.squeeze(kp) # flatten if necessary
|
| 55 |
+
if len(kp) < 3:
|
| 56 |
+
continue
|
| 57 |
x, y, conf = kp
|
| 58 |
# Clamp x,y
|
| 59 |
px = int(np.clip(x, 0, w-1))
|
| 60 |
py = int(np.clip(y, 0, h-1))
|
| 61 |
+
z = float(depth_map[py, px]) # Z from MiDaS
|
| 62 |
+
joints.append({"x": float(x), "y": float(y), "z": z, "confidence": float(conf)})
|
| 63 |
keypoints_list.append(joints)
|
| 64 |
|
| 65 |
+
# --- 3. Print keypoints in console ---
|
| 66 |
for i, joints in enumerate(keypoints_list):
|
| 67 |
print(f"Person {i+1}:")
|
| 68 |
for j, kp in enumerate(joints):
|
|
|
|
| 76 |
inputs=gr.Image(type="pil"),
|
| 77 |
outputs=[gr.Image(type="pil"), gr.JSON()],
|
| 78 |
title="YOLO Pose + MiDaS Depth",
|
| 79 |
+
description="Upload an image to see the depth map and keypoints X,Y,Z for each person."
|
| 80 |
)
|
| 81 |
|
| 82 |
iface.launch()
|