File size: 4,793 Bytes
5b1e428
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# Crowd Analysis: OpenVINO Runtime sample.
# See README.md for prerequisites and inputs. Run: python3 openvino_sample.py

import cv2
import numpy as np
import openvino as ov

PERSON_CLASS_ID = 0
CONF_THRESHOLD = 0.4
# Select the model: "yolo26n" (default) or "detr-resnet-50".
MODEL = "yolo26n"
INPUT_SIZE = 800 if MODEL.startswith("detr") else 640

# Crowd-density thresholds (person count per frame).
DENSITY_LOW_MAX = 10      # fewer than 10 -> LOW
DENSITY_MEDIUM_MAX = 25   # 10-25 -> MEDIUM, more than 25 -> HIGH

# Movement tracking.
IOU_MATCH_THRESHOLD = 0.3
MAX_MISSED_FRAMES = 15


def density_level(count):
    if count < DENSITY_LOW_MAX:
        return "LOW", (0, 200, 0)
    if count <= DENSITY_MEDIUM_MAX:
        return "MEDIUM", (0, 200, 255)
    return "HIGH", (0, 0, 255)


def iou(box_a, box_b):
    ax1, ay1, ax2, ay2 = box_a
    bx1, by1, bx2, by2 = box_b
    ix1, iy1 = max(ax1, bx1), max(ay1, by1)
    ix2, iy2 = min(ax2, bx2), min(ay2, by2)
    inter = max(0, ix2 - ix1) * max(0, iy2 - iy1)
    if inter == 0:
        return 0.0
    area_a = max(0, ax2 - ax1) * max(0, ay2 - ay1)
    area_b = max(0, bx2 - bx1) * max(0, by2 - by1)
    return inter / float(area_a + area_b - inter)


class CentroidTracker:
    """Minimal IoU tracker that records each track's last centroid so we can
    estimate per-frame movement (flow) vectors."""

    def __init__(self):
        self._next_id = 1
        self._tracks = {}  # id -> {"box", "centroid", "missed"}

    def update(self, boxes):
        unmatched = set(self._tracks)
        assignments, moves = [], []
        for box in boxes:
            cx = (box[0] + box[2]) / 2.0
            cy = (box[1] + box[3]) / 2.0
            best_id, best_iou = None, IOU_MATCH_THRESHOLD
            for tid in unmatched:
                score = iou(box, self._tracks[tid]["box"])
                if score > best_iou:
                    best_id, best_iou = tid, score
            if best_id is not None:
                tid = best_id
                unmatched.discard(tid)
                pcx, pcy = self._tracks[tid]["centroid"]
                moves.append((cx - pcx, cy - pcy))
            else:
                tid = self._next_id
                self._next_id += 1
            self._tracks[tid] = {"box": box, "centroid": (cx, cy), "missed": 0}
            assignments.append((box, tid))
        for tid in unmatched:
            self._tracks[tid]["missed"] += 1
            if self._tracks[tid]["missed"] > MAX_MISSED_FRAMES:
                del self._tracks[tid]
        return assignments, moves


core = ov.Core()
model = core.read_model(f"{MODEL}_openvino_model/{MODEL}.xml")
compiled = core.compile_model(model, "CPU")  # or "GPU", "NPU"

cap = cv2.VideoCapture("test_video.mp4")
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
writer = cv2.VideoWriter(
    "output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))

tracker = CentroidTracker()

while True:
    ok, frame = cap.read()
    if not ok:
        break
    h0, w0 = frame.shape[:2]
    sx, sy = w0 / INPUT_SIZE, h0 / INPUT_SIZE

    blob = cv2.resize(frame, (INPUT_SIZE, INPUT_SIZE))
    blob = cv2.cvtColor(blob, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
    blob = blob.transpose(2, 0, 1)[np.newaxis, ...]  # NCHW

    # End-to-end output: [1, N, 6] = [x1, y1, x2, y2, confidence, class_id]
    # (N = 300 for YOLO26, 100 for DETR)
    output = compiled([blob])[compiled.output(0)][0]
    mask = (output[:, 4] >= CONF_THRESHOLD) & (output[:, 5].astype(int) == PERSON_CLASS_ID)
    dets = output[mask]

    boxes = [(d[0] * sx, d[1] * sy, d[2] * sx, d[3] * sy) for d in dets]
    assignments, moves = tracker.update(boxes)

    for box, _tid in assignments:
        x1, y1, x2, y2 = (int(v) for v in box)
        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)

    count = len(boxes)
    level, color = density_level(count)
    cv2.putText(frame, f"Crowd: {count} ({level})", (10, 40),
                cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2)

    # Movement: mean of all per-track displacements -> dominant flow arrow.
    if moves:
        mdx = float(np.mean([m[0] for m in moves]))
        mdy = float(np.mean([m[1] for m in moves]))
        ox, oy = width // 2, height - 40
        cv2.arrowedLine(frame, (ox, oy),
                        (int(ox + mdx * 10), int(oy + mdy * 10)),
                        (255, 0, 0), 3, tipLength=0.3)
        cv2.putText(frame, f"Flow dx={mdx:+.1f} dy={mdy:+.1f}", (10, 75),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)

    writer.write(frame)

cap.release()
writer.release()
print("Saved: output_openvino.mp4")