Upload folder using huggingface_hub
Browse files- segment.py +15 -32
segment.py
CHANGED
|
@@ -1,54 +1,37 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
-
import torch
|
| 4 |
from ultralytics import YOLO
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# 1. LOAD THE SKELETON WITH 8 CLASSES
|
| 8 |
-
# By passing the dictionary with 8 names, YOLO automatically sets nc=8
|
| 9 |
-
custom_names = {
|
| 10 |
-
0: "so101_base", 1: "so101_shoulder", 2: "so101_upper_arm",
|
| 11 |
-
3: "so101_lower_arm", 4: "so101_wrist", 5: "so101_gripper",
|
| 12 |
-
6: "so101_camera_mount", 7: "so101_moving_jaw"
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
# This builds the 'Small' architecture specifically for 8 classes
|
| 16 |
-
model = YOLO("yolo11s-seg.yaml", task="segment")
|
| 17 |
-
model.model.nc = 8
|
| 18 |
-
model.names = custom_names
|
| 19 |
-
|
| 20 |
-
# 2. INJECT THE WEIGHTS
|
| 21 |
-
# Now that the 'Head' is the right size (8), this will not error
|
| 22 |
-
safetensors_path = "weights/best.safetensors"
|
| 23 |
-
weights = load_file(safetensors_path)
|
| 24 |
-
model.model.load_state_dict(weights)
|
| 25 |
-
|
| 26 |
-
# 3. VIDEO SETUP
|
| 27 |
cap = cv2.VideoCapture("test_video.mp4")
|
|
|
|
| 28 |
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 29 |
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 30 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 31 |
|
| 32 |
-
# 'avc1' for VS Code player compatibility
|
| 33 |
fourcc = cv2.VideoWriter_fourcc(*"avc1")
|
| 34 |
out = cv2.VideoWriter("comparison_output.mp4", fourcc, fps, (w * 2, h))
|
| 35 |
|
| 36 |
-
print(
|
| 37 |
|
| 38 |
while cap.isOpened():
|
| 39 |
ret, frame = cap.read()
|
| 40 |
if not ret:
|
| 41 |
break
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
black_bg = np.zeros_like(frame)
|
|
|
|
| 48 |
right_side = results[0].plot(img=black_bg, boxes=False, labels=True)
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
combined_frame = np.hstack((
|
|
|
|
| 52 |
out.write(combined_frame)
|
| 53 |
|
| 54 |
cap.release()
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
+
|
| 5 |
+
model = YOLO("weights/best.pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
cap = cv2.VideoCapture("test_video.mp4")
|
| 7 |
+
|
| 8 |
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 9 |
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 10 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 11 |
|
|
|
|
| 12 |
fourcc = cv2.VideoWriter_fourcc(*"avc1")
|
| 13 |
out = cv2.VideoWriter("comparison_output.mp4", fourcc, fps, (w * 2, h))
|
| 14 |
|
| 15 |
+
print("Generating side-by-side video...")
|
| 16 |
|
| 17 |
while cap.isOpened():
|
| 18 |
ret, frame = cap.read()
|
| 19 |
if not ret:
|
| 20 |
break
|
| 21 |
|
| 22 |
+
results = model(frame)
|
| 23 |
+
|
| 24 |
+
# LEFT SIDE: Original frame
|
| 25 |
+
left_side = frame
|
| 26 |
+
|
| 27 |
+
# RIGHT SIDE: Masks on a black background
|
| 28 |
black_bg = np.zeros_like(frame)
|
| 29 |
+
# img=black_bg tells YOLO to draw segments onto the black canvas
|
| 30 |
right_side = results[0].plot(img=black_bg, boxes=False, labels=True)
|
| 31 |
+
|
| 32 |
+
# Combine them horizontally
|
| 33 |
+
combined_frame = np.hstack((left_side, right_side))
|
| 34 |
+
|
| 35 |
out.write(combined_frame)
|
| 36 |
|
| 37 |
cap.release()
|