Skip to content

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

Moretiny (#81)

Rory Cleara8a99d3c0321
diff --git a/clearcam.py b/clearcam.py
index 14b099e..cfbd0ac 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -30,6 +30,13 @@ import platform
import ctypes
import zlib
+def resize(img, new_size):
+ img = img.permute(2,0,1)
+ img = Tensor.interpolate(img, size=(new_size[1], new_size[0]), mode='linear', align_corners=False)
+ img = img.permute(1, 2, 0)
+ return img
+
+@TinyJit
def preprocess(image, new_shape=1280, auto=True, scaleFill=False, scaleup=True, stride=32) -> Tensor:
shape = image.shape[:2] # current shape [height, width]
new_shape = (new_shape, new_shape) if isinstance(new_shape, int) else new_shape
@@ -41,11 +48,11 @@ def preprocess(image, new_shape=1280, auto=True, scaleFill=False, scaleup=True,
new_unpad = (new_shape[1], new_shape[0]) if scaleFill else new_unpad
dw /= 2
dh /= 2
- image = resize(image, new_unpad) if shape[::-1] != new_unpad else image
+ image = resize(image, new_unpad)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
image = copy_make_border(image, top, bottom, left, right, value=(114,114,114))
- return Tensor(image)
+ return image
def dist2bbox(distance, anchor_points, xywh=True, dim=-1):
lt, rb = distance.chunk(2, dim)
@@ -76,55 +83,7 @@ def make_anchors(feats, strides, grid_cell_offset=0.5):
return anchor_points, stride_tensor
def copy_make_border(img, top, bottom, left, right, value=(0, 0, 0)):
- if img.ndim == 2:
- h, w = img.shape
- c = 1
- else:
- h, w, c = img.shape
- new_h = h + top + bottom
- new_w = w + left + right
- if c == 1:
- out = np.full((new_h, new_w), value if not isinstance(value, tuple) else value[0], dtype=img.dtype)
- else:
- out = np.full((new_h, new_w, c), value, dtype=img.dtype)
- out[top:top+h, left:left+w, ...] = img
- return out
-
-
-def resize(img, new_size):
- h, w = img.shape[:2]
- new_w, new_h = new_size
-
- if (w, h) == (new_w, new_h): return img
-
- y = np.linspace(0, h - 1, new_h)
- x = np.linspace(0, w - 1, new_w)
- xv, yv = np.meshgrid(x, y)
-
- x0 = np.floor(xv).astype(int)
- x1 = np.clip(x0 + 1, 0, w - 1)
- y0 = np.floor(yv).astype(int)
- y1 = np.clip(y0 + 1, 0, h - 1)
-
- wx = xv - x0
- wy = yv - y0
-
- Ia = img[y0, x0]
- Ib = img[y1, x0]
- Ic = img[y0, x1]
- Id = img[y1, x1]
-
- wa = (1 - wx) * (1 - wy)
- wb = (1 - wx) * wy
- wc = wx * (1 - wy)
- wd = wx * wy
-
- out = (Ia * wa[..., None] +
- Ib * wb[..., None] +
- Ic * wc[..., None] +
- Id * wd[..., None])
-
- return out.astype(img.dtype)
+ return img.pad(((top,top),(left,left),(0,0)))
# this function is from the original implementation
def autopad(k, p=None, d=1): # kernel, padding, dilation
@@ -502,8 +461,8 @@ class VideoCapture:
self.object_set = set()
self.src = src
- self.width = 1280 # todo 1080?
- self.height = 720
+ self.width = 1920 # todo 1080?
+ self.height = 1080
self.proc = None
self.running = True
@@ -682,6 +641,7 @@ class VideoCapture:
with self.lock:
frame = self.raw_frame.copy() if self.raw_frame is not None else None
if frame is not None:
+ frame = Tensor(frame)
pre = preprocess(frame)
preds = do_inf(pre).numpy()
if track: