263 lines
7.9 KiB
Python
263 lines
7.9 KiB
Python
|
|
import cv2
|
||
|
|
import depthai as dai
|
||
|
|
import numpy as np
|
||
|
|
import time
|
||
|
|
import math
|
||
|
|
|
||
|
|
|
||
|
|
# =========================
|
||
|
|
# CONFIGURAÇÕES DO TESTE
|
||
|
|
# =========================
|
||
|
|
|
||
|
|
MIN_DEPTH_MM = 300 # ignora muito perto
|
||
|
|
MAX_DEPTH_MM = 3000 # só olha até 3 m
|
||
|
|
DANGER_DEPTH_MM = 1500 # abaixo disso marca perigo
|
||
|
|
|
||
|
|
MIN_AREA_PX = 450 # área mínima do blob
|
||
|
|
MAX_LOST_FRAMES = 10 # quantos frames mantém ID sem ver
|
||
|
|
TRACK_MAX_DIST = 90 # distância máxima em pixels para associar ID
|
||
|
|
|
||
|
|
ROI_TOP = 0.25 # começa em 25% da altura
|
||
|
|
ROI_BOTTOM = 0.95 # termina em 95% da altura
|
||
|
|
ROI_LEFT = 0.15 # ignora bordas
|
||
|
|
ROI_RIGHT = 0.85
|
||
|
|
|
||
|
|
|
||
|
|
# =========================
|
||
|
|
# TRACKER SIMPLES POR CENTRO
|
||
|
|
# =========================
|
||
|
|
|
||
|
|
class SimpleBlobTracker:
|
||
|
|
def __init__(self):
|
||
|
|
self.next_id = 1
|
||
|
|
self.tracks = {}
|
||
|
|
|
||
|
|
def update(self, detections):
|
||
|
|
# detections: lista de dicts com cx, cy, z_mm, bbox, area
|
||
|
|
updated = []
|
||
|
|
used_tracks = set()
|
||
|
|
|
||
|
|
for det in detections:
|
||
|
|
best_id = None
|
||
|
|
best_dist = 999999
|
||
|
|
|
||
|
|
for tid, tr in self.tracks.items():
|
||
|
|
if tid in used_tracks:
|
||
|
|
continue
|
||
|
|
|
||
|
|
dx = det["cx"] - tr["cx"]
|
||
|
|
dy = det["cy"] - tr["cy"]
|
||
|
|
dz = (det["z_mm"] - tr["z_mm"]) / 30.0 # peso leve para profundidade
|
||
|
|
dist = math.sqrt(dx * dx + dy * dy + dz * dz)
|
||
|
|
|
||
|
|
if dist < best_dist:
|
||
|
|
best_dist = dist
|
||
|
|
best_id = tid
|
||
|
|
|
||
|
|
if best_id is not None and best_dist < TRACK_MAX_DIST:
|
||
|
|
tid = best_id
|
||
|
|
used_tracks.add(tid)
|
||
|
|
|
||
|
|
self.tracks[tid].update(det)
|
||
|
|
self.tracks[tid]["lost"] = 0
|
||
|
|
self.tracks[tid]["age"] += 1
|
||
|
|
else:
|
||
|
|
tid = self.next_id
|
||
|
|
self.next_id += 1
|
||
|
|
|
||
|
|
self.tracks[tid] = dict(det)
|
||
|
|
self.tracks[tid]["lost"] = 0
|
||
|
|
self.tracks[tid]["age"] = 1
|
||
|
|
|
||
|
|
out = dict(self.tracks[tid])
|
||
|
|
out["id"] = tid
|
||
|
|
updated.append(out)
|
||
|
|
|
||
|
|
# envelhece tracks não usados
|
||
|
|
for tid in list(self.tracks.keys()):
|
||
|
|
if tid not in used_tracks and all(d.get("id") != tid for d in updated):
|
||
|
|
self.tracks[tid]["lost"] += 1
|
||
|
|
if self.tracks[tid]["lost"] > MAX_LOST_FRAMES:
|
||
|
|
del self.tracks[tid]
|
||
|
|
|
||
|
|
return updated
|
||
|
|
|
||
|
|
|
||
|
|
# =========================
|
||
|
|
# PIPELINE OAK-D LITE
|
||
|
|
# =========================
|
||
|
|
|
||
|
|
pipeline = dai.Pipeline()
|
||
|
|
|
||
|
|
# RGB
|
||
|
|
cam_rgb = pipeline.create(dai.node.ColorCamera)
|
||
|
|
cam_rgb.setPreviewSize(640, 400)
|
||
|
|
cam_rgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
|
||
|
|
cam_rgb.setInterleaved(False)
|
||
|
|
cam_rgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
|
||
|
|
cam_rgb.setFps(30)
|
||
|
|
|
||
|
|
# Mono stereo
|
||
|
|
mono_left = pipeline.create(dai.node.MonoCamera)
|
||
|
|
mono_right = pipeline.create(dai.node.MonoCamera)
|
||
|
|
|
||
|
|
mono_left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
|
||
|
|
mono_right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
|
||
|
|
|
||
|
|
mono_left.setBoardSocket(dai.CameraBoardSocket.CAM_B)
|
||
|
|
mono_right.setBoardSocket(dai.CameraBoardSocket.CAM_C)
|
||
|
|
|
||
|
|
# Depth
|
||
|
|
stereo = pipeline.create(dai.node.StereoDepth)
|
||
|
|
stereo.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.DEFAULT)
|
||
|
|
stereo.setDepthAlign(dai.CameraBoardSocket.CAM_A)
|
||
|
|
stereo.setSubpixel(True)
|
||
|
|
stereo.setLeftRightCheck(True)
|
||
|
|
|
||
|
|
mono_left.out.link(stereo.left)
|
||
|
|
mono_right.out.link(stereo.right)
|
||
|
|
|
||
|
|
# Outputs
|
||
|
|
xout_rgb = pipeline.create(dai.node.XLinkOut)
|
||
|
|
xout_depth = pipeline.create(dai.node.XLinkOut)
|
||
|
|
|
||
|
|
xout_rgb.setStreamName("rgb")
|
||
|
|
xout_depth.setStreamName("depth")
|
||
|
|
|
||
|
|
cam_rgb.preview.link(xout_rgb.input)
|
||
|
|
stereo.depth.link(xout_depth.input)
|
||
|
|
|
||
|
|
|
||
|
|
# =========================
|
||
|
|
# PROCESSAMENTO
|
||
|
|
# =========================
|
||
|
|
|
||
|
|
tracker = SimpleBlobTracker()
|
||
|
|
|
||
|
|
with dai.Device(pipeline) as device:
|
||
|
|
q_rgb = device.getOutputQueue("rgb", maxSize=4, blocking=False)
|
||
|
|
q_depth = device.getOutputQueue("depth", maxSize=4, blocking=False)
|
||
|
|
|
||
|
|
last = time.time()
|
||
|
|
fps = 0
|
||
|
|
|
||
|
|
while True:
|
||
|
|
in_rgb = q_rgb.get()
|
||
|
|
in_depth = q_depth.get()
|
||
|
|
|
||
|
|
frame = in_rgb.getCvFrame()
|
||
|
|
depth = in_depth.getFrame() # uint16 em mm
|
||
|
|
|
||
|
|
h, w = frame.shape[:2]
|
||
|
|
|
||
|
|
if depth.shape[:2] != (h, w):
|
||
|
|
depth = cv2.resize(depth, (w, h), interpolation=cv2.INTER_NEAREST)
|
||
|
|
|
||
|
|
now = time.time()
|
||
|
|
fps = 0.9 * fps + 0.1 * (1 / max(now - last, 1e-6))
|
||
|
|
last = now
|
||
|
|
|
||
|
|
# ROI
|
||
|
|
x1_roi = int(w * ROI_LEFT)
|
||
|
|
x2_roi = int(w * ROI_RIGHT)
|
||
|
|
y1_roi = int(h * ROI_TOP)
|
||
|
|
y2_roi = int(h * ROI_BOTTOM)
|
||
|
|
|
||
|
|
roi_depth = depth[y1_roi:y2_roi, x1_roi:x2_roi]
|
||
|
|
|
||
|
|
# Máscara de pixels com profundidade válida/próxima
|
||
|
|
mask = np.zeros_like(roi_depth, dtype=np.uint8)
|
||
|
|
mask[(roi_depth > MIN_DEPTH_MM) & (roi_depth < MAX_DEPTH_MM)] = 255
|
||
|
|
|
||
|
|
# Limpeza de ruído
|
||
|
|
kernel = np.ones((5, 5), np.uint8)
|
||
|
|
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
|
||
|
|
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
|
||
|
|
|
||
|
|
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||
|
|
|
||
|
|
detections = []
|
||
|
|
|
||
|
|
for cnt in contours:
|
||
|
|
area = cv2.contourArea(cnt)
|
||
|
|
if area < MIN_AREA_PX:
|
||
|
|
continue
|
||
|
|
|
||
|
|
x, y, bw, bh = cv2.boundingRect(cnt)
|
||
|
|
|
||
|
|
# Coordenadas globais
|
||
|
|
gx = x + x1_roi
|
||
|
|
gy = y + y1_roi
|
||
|
|
gcx = gx + bw // 2
|
||
|
|
gcy = gy + bh // 2
|
||
|
|
|
||
|
|
blob_depth = roi_depth[y:y + bh, x:x + bw]
|
||
|
|
valid = blob_depth[(blob_depth > MIN_DEPTH_MM) & (blob_depth < MAX_DEPTH_MM)]
|
||
|
|
|
||
|
|
if len(valid) < 50:
|
||
|
|
continue
|
||
|
|
|
||
|
|
z_mm = float(np.median(valid))
|
||
|
|
|
||
|
|
detections.append({
|
||
|
|
"bbox": (gx, gy, gx + bw, gy + bh),
|
||
|
|
"cx": gcx,
|
||
|
|
"cy": gcy,
|
||
|
|
"z_mm": z_mm,
|
||
|
|
"area": area
|
||
|
|
})
|
||
|
|
|
||
|
|
tracked = tracker.update(detections)
|
||
|
|
|
||
|
|
perigo = False
|
||
|
|
|
||
|
|
# Desenha ROI
|
||
|
|
cv2.rectangle(frame, (x1_roi, y1_roi), (x2_roi, y2_roi), (255, 255, 0), 1)
|
||
|
|
|
||
|
|
for obj in tracked:
|
||
|
|
x1, y1, x2, y2 = obj["bbox"]
|
||
|
|
z_m = obj["z_mm"] / 1000.0
|
||
|
|
center_percent = obj["cx"] / w * 100.0
|
||
|
|
|
||
|
|
danger = obj["z_mm"] < DANGER_DEPTH_MM
|
||
|
|
if danger:
|
||
|
|
perigo = True
|
||
|
|
|
||
|
|
color = (0, 0, 255) if danger else (0, 255, 0)
|
||
|
|
|
||
|
|
texto = (
|
||
|
|
f"ID {obj['id']} | Z={z_m:.2f}m | "
|
||
|
|
f"X={center_percent:.0f}% | area={int(obj['area'])}"
|
||
|
|
)
|
||
|
|
|
||
|
|
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
||
|
|
cv2.circle(frame, (obj["cx"], obj["cy"]), 4, (0, 255, 255), -1)
|
||
|
|
cv2.putText(frame, texto, (x1, max(20, y1 - 8)),
|
||
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 1)
|
||
|
|
|
||
|
|
cv2.putText(frame, f"FPS: {fps:.1f}", (10, 25),
|
||
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
||
|
|
|
||
|
|
cv2.putText(frame, f"Blobs: {len(tracked)}", (10, 52),
|
||
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
||
|
|
|
||
|
|
if perigo:
|
||
|
|
cv2.putText(frame, "PERIGO: obstaculo proximo - PARAR", (10, 82),
|
||
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
|
||
|
|
|
||
|
|
# Preview depth colorido
|
||
|
|
depth_vis = depth.copy()
|
||
|
|
depth_vis[depth_vis == 0] = MAX_DEPTH_MM
|
||
|
|
depth_vis = np.clip(depth_vis, MIN_DEPTH_MM, MAX_DEPTH_MM)
|
||
|
|
depth_vis = ((MAX_DEPTH_MM - depth_vis) / (MAX_DEPTH_MM - MIN_DEPTH_MM) * 255).astype(np.uint8)
|
||
|
|
depth_vis = cv2.applyColorMap(depth_vis, cv2.COLORMAP_JET)
|
||
|
|
|
||
|
|
cv2.imshow("OAK-D Lite - Depth Blob Tracker", frame)
|
||
|
|
cv2.imshow("Depth view", depth_vis)
|
||
|
|
cv2.imshow("Mask blobs", mask)
|
||
|
|
|
||
|
|
key = cv2.waitKey(1)
|
||
|
|
if key == ord("q") or key == 27:
|
||
|
|
break
|
||
|
|
|
||
|
|
cv2.destroyAllWindows()
|