Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

Prepost (#132)

Rory Clear1e5d0f662cf0
diff --git a/clearcam.py b/clearcam.py
index 16537ba..ed23720 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -489,10 +489,8 @@ class VideoCapture:
def run_inference(self, frame):
frame = Tensor(frame)
- pre = model.preprocess(frame)
- preds = model(pre).numpy()
+ preds = model(frame).numpy()
thresh = (self.settings.get("threshold") if self.settings else 0.5) or 0.5 #todo clean!
- preds = model.scale_boxes(pre.shape[:2], preds, frame.shape)
online_targets = tracker.update(preds, thresh)
if type(model) == RFDETR: # RF-DETR has different class_ids
for j in range(len(online_targets)): online_targets[j].class_id = detr_to_yolo[int(online_targets[j].class_id)]
diff --git a/detection/rfdetr.py b/detection/rfdetr.py
index fa1d969..0716bcd 100644
--- a/detection/rfdetr.py
+++ b/detection/rfdetr.py
@@ -609,8 +609,9 @@ class RFDETR():
load_state_dict(self, state_dict)
@TinyJit
- def __call__(self, processed_images):
- predictions = self.predict(processed_images)
+ def __call__(self, frame):
+ pre = self.preprocess(frame)
+ predictions = self.predict(pre)
out_logits, out_bbox = predictions
prob = out_logits.sigmoid()
topk_values, topk_indexes = Tensor.topk(prob.view(out_logits.shape[0], -1), 300, dim=1)
@@ -619,6 +620,7 @@ class RFDETR():
boxes = box_cxcywh_to_xyxy(out_bbox)
boxes = Tensor.gather(boxes, 1, topk_boxes.unsqueeze(-1).repeat(1,1,4))
ret = Tensor.cat(boxes.squeeze(0), topk_values.squeeze(0).unsqueeze(1), labels.squeeze(0).unsqueeze(1), dim=1)
+ ret = self.scale_boxes(pre.shape[:2], ret, frame.shape)
return ret
def predict(self, samples, targets=None):
@@ -636,7 +638,6 @@ class RFDETR():
outputs_class = self.class_embed(hs)[-1]
return outputs_class, outputs_coord[-1]
- @TinyJit
def preprocess(self, img):
img = img.cast(dtypes.float32)
img /= 255.0
@@ -648,7 +649,7 @@ class RFDETR():
return img
def scale_boxes(self, img1_shape, predictions, img0_shape):
- predictions[:,:4] *= [img0_shape[1], img0_shape[0], img0_shape[1], img0_shape[0]]
+ predictions[:,:4] *= Tensor([img0_shape[1], img0_shape[0], img0_shape[1], img0_shape[0]])
return predictions
def compute_iou_matrix(boxes):
diff --git a/detection/yolov9.py b/detection/yolov9.py
index 623cf02..fbad543 100644
--- a/detection/yolov9.py
+++ b/detection/yolov9.py
@@ -372,8 +372,9 @@ class YOLOv9():
load_state_dict(self, state_dict)
@TinyJit
- def __call__(self, x):
- x = x.unsqueeze(0)
+ def __call__(self, frame):
+ pre = self.preprocess(frame)
+ x = pre.unsqueeze(0)
x = x.permute(0, 3, 1, 2)
x = x / 255.0
y = [] # outputs
@@ -382,10 +383,10 @@ class YOLOv9():
if m.f != -1: x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f]
x = m(x)
y.append(x)
-
- return postprocess(x[0])[0]
+ preds = postprocess(x[0])[0]
+ preds = self.scale_boxes(pre.shape[:2], preds, frame.shape)
+ return preds
- @TinyJit
def preprocess(self, image, new_shape=None, auto=True, scaleFill=False, scaleup=True, stride=32) -> Tensor:
if new_shape is None: new_shape = self.res
shape = image.shape[:2]
@@ -403,21 +404,21 @@ class YOLOv9():
return image
def scale_boxes(self, img1_shape, predictions, img0_shape):
- gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])
- pad = ((img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2)
- for pred in predictions:
- boxes_np = pred[:4].numpy() if isinstance(pred[:4], Tensor) else pred[:4]
- boxes_np[..., [0, 2]] -= pad[0]
- boxes_np[..., [1, 3]] -= pad[1]
- boxes_np[..., :4] /= gain
- boxes_np = clip_boxes(boxes_np, img0_shape)
- pred[:4] = boxes_np
- return predictions
+ gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])
+ pad_x = (img1_shape[1] - img0_shape[1] * gain) / 2
+ pad_y = (img1_shape[0] - img0_shape[0] * gain) / 2
+ boxes = predictions[:, :4].contiguous()
+ boxes[:, [0, 2]] -= pad_x
+ boxes[:, [1, 3]] -= pad_y
+ boxes /= gain
+ boxes = clip_boxes(boxes, img0_shape)
+ predictions[:, :4] = boxes
+ return predictions
def clip_boxes(boxes, shape):
- boxes[..., [0, 2]] = np.clip(boxes[..., [0, 2]], 0, shape[1]) # x1, x2
- boxes[..., [1, 3]] = np.clip(boxes[..., [1, 3]], 0, shape[0]) # y1, y2
- return boxes
+ boxes[..., [0, 2]] = boxes[..., [0, 2]].clip(0, shape[1])
+ boxes[..., [1, 3]] = boxes[..., [1, 3]].clip(0, shape[0])
+ return boxes
def resize(img, new_size):
img = img.permute(2,0,1)
diff --git a/test/run_mot.py b/test/run_mot.py
index ae7ed0a..ee3e340 100644
--- a/test/run_mot.py
+++ b/test/run_mot.py
@@ -31,9 +31,7 @@ if __name__ == "__main__":
if not ret: break
im = im0
im = Tensor(im).cast(dtypes.float32)
- im = model.preprocess(im)
pred = model(im).numpy()
- pred = model.scale_boxes(im.shape[:2], pred, im0.shape)
online_targets = ocs_tracker.update(pred, 0.25)
if type(model) == RFDETR: # RF-DETR has different class_ids
for j in range(len(online_targets)): online_targets[j].class_id = detr_to_yolo[int(online_targets[j].class_id)]