Spaces:
Sleeping
Sleeping
Update detector/yolo_pipeline.py
Browse files๋ชจ๋ธ ๋ก๋ฉ ๋ฐฉ์ ๊ฐ์
- ์ ์ญ ํจ์ get_yolo_model()์ ํตํด ๋ชจ๋ธ ์ฑ๊ธํค ๊ด๋ฆฌ
- ํด๋์ค ์ธ์คํด์ค ์์ฑ ์ ๋ชจ๋ธ ์ฐธ์กฐ ๋์ ํจ์ ํธ์ถ ์ฌ์ฉ
์ง๋ ฌํ ๋ฌธ์ ํด๊ฒฐ
- YOLO3DReconstructor ํด๋์ค๊ฐ ์ง์ ๋ชจ๋ธ ์ธ์คํด์ค๋ฅผ ๋ณด์ ํ์ง ์์
- ์ ์ญ ๋ชจ๋ธ ์บ์ฑ์ผ๋ก ์ธํ ํผํด๋ง ๋ฌธ์ ๋ฐฉ์ง
๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ ์ต์ ํ
- ์ฌ๋ฌ ์ธ์คํด์ค ์์ฑ ์์๋ ๋ชจ๋ธ์ ํ ๋ฒ๋ง ๋ก๋
- GPU ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ๊ฐ์
- detector/yolo_pipeline.py +26 -21
detector/yolo_pipeline.py
CHANGED
|
@@ -1,48 +1,53 @@
|
|
| 1 |
-
from ultralytics import YOLO
|
| 2 |
-
from utils.pointcloud_tools import PointCloudProcessor # ์ถ๊ฐ
|
| 3 |
from spaces import GPU
|
|
|
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
import trimesh
|
|
|
|
|
|
|
| 7 |
|
| 8 |
@GPU()
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
class YOLO3DReconstructor:
|
| 11 |
-
_model_instance = None # ํด๋์ค ๋ ๋ฒจ์์ ๋ชจ๋ธ ์บ์ฑ
|
| 12 |
-
|
| 13 |
def __init__(self):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
self.class_mapping = {
|
| 18 |
-
0: 'person',
|
| 19 |
7: 'forklift',
|
| 20 |
63: 'conveyor'
|
| 21 |
}
|
| 22 |
-
|
| 23 |
def process_frames(self, frame_paths):
|
| 24 |
point_cloud = []
|
| 25 |
-
detections = {v:0 for v in self.
|
| 26 |
-
|
| 27 |
-
for frame_idx,
|
| 28 |
-
img = cv2.imread(
|
| 29 |
if img is None:
|
| 30 |
continue
|
| 31 |
-
|
| 32 |
results = self.model(img)[0]
|
| 33 |
for box in results.boxes.xywh.cpu().numpy():
|
| 34 |
cls_id = int(box[5])
|
| 35 |
-
if cls_id in self.
|
| 36 |
-
label = self.
|
| 37 |
detections[label] += 1
|
| 38 |
x, y, w, h = box[:4]
|
| 39 |
point_cloud.append([x, y, frame_idx * 50])
|
| 40 |
-
|
| 41 |
if not point_cloud:
|
| 42 |
return None
|
| 43 |
-
|
| 44 |
points = np.array(point_cloud)
|
| 45 |
points = PointCloudProcessor.filter_outliers(points)
|
| 46 |
points = PointCloudProcessor.normalize_points(points)
|
| 47 |
|
| 48 |
-
return trimesh.points.PointCloud(points), detections
|
|
|
|
|
|
|
|
|
|
| 1 |
from spaces import GPU
|
| 2 |
+
import os
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
import trimesh
|
| 6 |
+
from ultralytics import YOLO
|
| 7 |
+
from utils.pointcloud_tools import PointCloudProcessor
|
| 8 |
|
| 9 |
@GPU()
|
| 10 |
+
# YOLO ๋ชจ๋ธ ์ฑ๊ธํค ์ธ์คํด์ค
|
| 11 |
+
_MODEL = None
|
| 12 |
+
|
| 13 |
+
def get_yolo_model():
|
| 14 |
+
global _MODEL
|
| 15 |
+
if _MODEL is None:
|
| 16 |
+
_MODEL = YOLO('yolov8m.pt')
|
| 17 |
+
return _MODEL
|
| 18 |
+
|
| 19 |
class YOLO3DReconstructor:
|
|
|
|
|
|
|
| 20 |
def __init__(self):
|
| 21 |
+
self.model = get_yolo_model()
|
| 22 |
+
self.class_map = {
|
| 23 |
+
0: 'person',
|
|
|
|
|
|
|
| 24 |
7: 'forklift',
|
| 25 |
63: 'conveyor'
|
| 26 |
}
|
| 27 |
+
|
| 28 |
def process_frames(self, frame_paths):
|
| 29 |
point_cloud = []
|
| 30 |
+
detections = {v:0 for v in self.class_map.values()}
|
| 31 |
+
|
| 32 |
+
for frame_idx, path in enumerate(frame_paths):
|
| 33 |
+
img = cv2.imread(path)
|
| 34 |
if img is None:
|
| 35 |
continue
|
| 36 |
+
|
| 37 |
results = self.model(img)[0]
|
| 38 |
for box in results.boxes.xywh.cpu().numpy():
|
| 39 |
cls_id = int(box[5])
|
| 40 |
+
if cls_id in self.class_map:
|
| 41 |
+
label = self.class_map[cls_id]
|
| 42 |
detections[label] += 1
|
| 43 |
x, y, w, h = box[:4]
|
| 44 |
point_cloud.append([x, y, frame_idx * 50])
|
| 45 |
+
|
| 46 |
if not point_cloud:
|
| 47 |
return None
|
| 48 |
+
|
| 49 |
points = np.array(point_cloud)
|
| 50 |
points = PointCloudProcessor.filter_outliers(points)
|
| 51 |
points = PointCloudProcessor.normalize_points(points)
|
| 52 |
|
| 53 |
+
return (trimesh.points.PointCloud(points), detections)
|