You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

from collections import defaultdict, deque
def distance_risk(items, thr=80):
centers = [((it["x1"]+it["x2"])/2, (it["y1"]+it["y2"])/2) for it in items]
n = len(centers)
if n < 2:
return 0.0
close_pairs = 0
for i in range(n):
cx1, cy1 = centers[i]
for j in range(i+1, n):
cx2, cy2 = centers[j]
dist = ((cx1-cx2)**2 + (cy1-cy2)**2)**0.5
if dist < thr:
close_pairs += 1
return min(close_pairs / 30.0, 1.0)
prev_centers = {}
trajectories = defaultdict(lambda: deque(maxlen=10))
last_seen = {}
KEEP_FRAMES = 15
frame_counter = 0
def motion_risk_and_path(items):
global prev_centers, trajectories, last_seen, frame_counter
frame_counter += 1
fid = frame_counter
curr = {}
speeds = []
for it in items:
if it["tid"] == 65535:
continue
tid = it["tid"]
cx = (it["x1"] + it["x2"]) / 2
cy = (it["y1"] + it["y2"]) / 2
curr[tid] = (cx, cy)
trajectories[tid].append((cx, cy))
last_seen[tid] = fid
if tid in prev_centers:
px, py = prev_centers[tid]
spd = ((cx - px)**2 + (cy - py)**2)**0.5
speeds.append(spd)
prev_centers = curr
for tid in list(trajectories.keys()):
if tid not in curr:
if fid - last_seen.get(tid, fid) > KEEP_FRAMES:
del trajectories[tid]
del last_seen[tid]
if not speeds:
risk = 0.0
else:
risk = min((sum(speeds) / len(speeds)) / 20.0, 1.0)
return risk, trajectories