Spaces:
Paused
Paused
Zhen Ye commited on
Commit ·
d2acfa9
1
Parent(s): 0effcd5
fix: Remove chat emojis, improve tracker GPT metadata sync
Browse files- Remove emojis from threat chat UI (use text labels YOU/TAC/SYS)
- Fix GPT reasoning to use track_id instead of index-based IDs
- Add inject_metadata() to ByteTracker for post-update GPT sync
- Add GPT_SYNC_KEYS for all threat intelligence fields
- Reset track ID counter on new ByteTracker instance
- Minor fixes to overlays, cards, and main.js
Files changed (8)
197 unmodified lines198199200201202203204205206207208209210197 unmodified lines<!-- Threat Chat Panel --><div class="panel panel-chat" id="chatPanel"><h3><span>🎖️ Threat Analyst Chat</span><button class="collapse-btn" id="chatToggle" style="font-size: 0.75rem;">▲ Close Chat</button></h3><div class="chat-container"><div class="chat-messages" id="chatMessages"><div class="chat-message chat-system"><span class="chat-icon">ℹ️</span><span class="chat-content">Run detection first, then ask questions about detected threats.</span></div></div>197 unmodified lines198199200201202203204205206207208209210197 unmodified lines<!-- Threat Chat Panel --><div class="panel panel-chat" id="chatPanel"><h3><span>Threat Analyst Chat</span><button class="collapse-btn" id="chatToggle" style="font-size: 0.75rem;">▲ Close Chat</button></h3><div class="chat-container"><div class="chat-messages" id="chatMessages"><div class="chat-message chat-system"><span class="chat-icon">SYS</span><span class="chat-content">Run detection first, then ask questions about detected threats.</span></div></div>
498 unmodified lines499500501502503504505498 unmodified linesfunction processFirstFrameDetections(dets) {state.detections = dets.map((d, i) => {const id = `T${String(i + 1).padStart(2, "0")}`;const ap = defaultAimpoint(d.label || d.class);const bbox = d.bbox? { x: d.bbox[0], y: d.bbox[1], w: d.bbox[2] - d.bbox[0], h: d.bbox[3] - d.bbox[1] }498 unmodified lines499500501502503504505498 unmodified linesfunction processFirstFrameDetections(dets) {state.detections = dets.map((d, i) => {const id = d.track_id || `T${String(i + 1).padStart(2, "0")}`;const ap = defaultAimpoint(d.label || d.class);const bbox = d.bbox? { x: d.bbox[0], y: d.bbox[1], w: d.bbox[2] - d.bbox[0], h: d.bbox[3] - d.bbox[1] }
16 unmodified lines1718192021222316 unmodified linesreturn;}dets.forEach((det, i) => {const id = det.id || `T${String(i + 1).padStart(2, '0')}`;let rangeStr = "---";16 unmodified lines17181920212223242516 unmodified linesreturn;}const sorted = [...dets].sort((a, b) => (b.threat_level_score || 0) - (a.threat_level_score || 0));sorted.forEach((det, i) => {const id = det.id || `T${String(i + 1).padStart(2, '0')}`;let rangeStr = "---";
109 unmodified lines110111112113114115116109 unmodified lines// Format content with line breaksconst formatted = content.replace(/\n/g, "<br>");const icon = role === "user" ? "👤" : role === "assistant" ? "🎖️" : "⚠️";msgDiv.innerHTML = `<span class="chat-icon">${icon}</span><span class="chat-content">${formatted}</span>`;chatMessages.appendChild(msgDiv);109 unmodified lines110111112113114115116109 unmodified lines// Format content with line breaksconst formatted = content.replace(/\n/g, "<br>");const icon = role === "user" ? "YOU" : role === "assistant" ? "TAC" : "SYS";msgDiv.innerHTML = `<span class="chat-icon">${icon}</span><span class="chat-content">${formatted}</span>`;chatMessages.appendChild(msgDiv);
87 unmodified lines88899091929336 unmodified lines13013113213313413513613713813914014114214387 unmodified linesctx.shadowBlur = isSel ? 18 : 10;roundRect(ctx, b.x, b.y, b.w, b.h, 10, false, true);// pseudo mask glowctx.shadowBlur = 0;const g = ctx.createRadialGradient(b.x + b.w * 0.5, b.y + b.h * 0.5, 10, b.x + b.w * 0.5, b.y + b.h * 0.5, Math.max(b.w, b.h) * 0.75);36 unmodified lines};APP.ui.overlays.renderFrameOverlay = function () {const { $ } = APP.core.utils;// User request: No overlays on first frame (Tab 1)const canvas = $("#frameOverlay");if (canvas) {const ctx = canvas.getContext("2d");ctx.clearRect(0, 0, canvas.width, canvas.height);}// APP.ui.overlays.render("frameOverlay", state.detections);};APP.ui.overlays.renderEngageOverlay = function () {87 unmodified lines88899091929394959697989910010110210310410510636 unmodified lines14314414514614714814915015115215315415515615715815916016116216316487 unmodified linesctx.shadowBlur = isSel ? 18 : 10;roundRect(ctx, b.x, b.y, b.w, b.h, 10, false, true);// Animated pulsing glow for selected boxif (isSel) {const pulse = Math.sin(t * 3) * 0.5 + 0.5; // 0..1 pulsingctx.save();ctx.strokeStyle = `rgba(34,211,238,${0.25 + pulse * 0.35})`;ctx.lineWidth = 6;ctx.shadowColor = `rgba(34,211,238,${0.3 + pulse * 0.4})`;ctx.shadowBlur = 28 + pulse * 20;roundRect(ctx, b.x, b.y, b.w, b.h, 10, false, true);ctx.restore();ctx.lineWidth = 3; // restore for subsequent drawing}// pseudo mask glowctx.shadowBlur = 0;const g = ctx.createRadialGradient(b.x + b.w * 0.5, b.y + b.h * 0.5, 10, b.x + b.w * 0.5, b.y + b.h * 0.5, Math.max(b.w, b.h) * 0.75);36 unmodified lines};APP.ui.overlays.renderFrameOverlay = function () {const { state } = APP.core;const { $ } = APP.core.utils;const canvas = $("#frameOverlay");if (!canvas) return;// Only show overlay for the selected detectionif (state.selectedId) {const sel = (state.detections || []).filter(d => d.id === state.selectedId);if (sel.length) {APP.ui.overlays.render("frameOverlay", sel);return;}}// Nothing selected — clearconst ctx = canvas.getContext("2d");ctx.clearRect(0, 0, canvas.width, canvas.height);};APP.ui.overlays.renderEngageOverlay = function () {
772 unmodified lines773774775776777778779326 unmodified lines11061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161772 unmodified lines# GPT returns { "T01": { "distance_m": ..., "direction": ... } }# Detections are list of dicts. We assume T01 maps to index 0, T02 to index 1...for i, det in enumerate(detections):# ID format matches what we constructed in gpt_reasoning.pyobj_id = f"T{str(i+1).zfill(2)}"if obj_id in gpt_results:info = gpt_results[obj_id]326 unmodified lines# Write next_idxp_frame, dets = buffer.pop(next_idx)# --- GPT ESTIMATION (Frame 0 Only) ---if next_idx == 0 and enable_gpt and dets:try:logging.info("Running GPT estimation for video start (Frame 0)...")with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:cv2.imwrite(tmp.name, p_frame) # Use processed frame (boxes not yet drawn)# Wait, p_frame might have heatmaps if depth enabled? No, draw_boxes comes later.# Actually, colorize_depth_map might have happened in worker.# But raw image is better? We don't have raw image here easily without stashing.# p_frame is 'processed'. If depth map enabled, it's a heatmap. Not good for GPT.# GPT needs RGB image.# Worker: processed = frame.copy() -> colorize -> draw_boxes (removed).# So processed is potentially modified.# Ideally we want original.# But let's assume for now processed is fine (if depth disabled) or GPT can handle it.# If depth is enabled, processed is a heatmap. GPT will fail to see car color/details.# FIX: We need access to original frame?# worker sends (idx, processed, detections).# It does NOT send original frame.# We should change worker to send original? Or assume GPT runs on processed?# If processed is heatmap, it's bad.# But User Objective says "legacy depth estimation" is optional/deprecated.# If depth_estimator_name is None, processed is just frame.gpt_res = estimate_distance_gpt(tmp.name, dets)os.remove(tmp.name)# Merge# Helper to match IDs?# estimate_distance_gpt expects us to pass detections list, output keyed by T01..# But detections don't have IDs yet! SimpleTracker assigns them.# We assign temporary IDs T01.. based on index for GPT matching?# gpt_distance.py generates IDs if not present.# Let's inspect gpt_distance.py... assume it matches by index T01, T02...for i, d in enumerate(dets):oid = f"T{str(i+1).zfill(2)}"if oid in gpt_res:d.update(gpt_res[oid])except Exception as e:logging.error("GPT failed for Frame 0: %s", e)# --- SEQUENTIAL TRACKING ---# Update tracker with current frame detections# ByteTracker returns the list of ACTIVE tracks with IDsdets = tracker.update(dets)speed_est.estimate(dets)# --- RENDER BOXES & OVERLAYS ---# We need to convert list of dicts back to boxes array for draw_boxes772 unmodified lines773774775776777778779780781782326 unmodified lines110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141772 unmodified lines# GPT returns { "T01": { "distance_m": ..., "direction": ... } }# Detections are list of dicts. We assume T01 maps to index 0, T02 to index 1...for i, det in enumerate(detections):# Index-based IDs are intentional here: no tracker runs for first-frame# preview, so GPT, inference merge, and frontend all use the same# index-based scheme (T01=index 0, T02=index 1, ...), keeping it# self-consistent. The video pipeline uses real ByteTracker IDs instead.obj_id = f"T{str(i+1).zfill(2)}"if obj_id in gpt_results:info = gpt_results[obj_id]326 unmodified lines# Write next_idxp_frame, dets = buffer.pop(next_idx)# --- SEQUENTIAL TRACKING ---# Run tracker FIRST so detections get real track_id from ByteTrackerdets = tracker.update(dets)speed_est.estimate(dets)# --- GPT ESTIMATION (Frame 0 Only) ---if next_idx == 0 and enable_gpt and dets:try:logging.info("Running GPT estimation for video start (Frame 0)...")with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:cv2.imwrite(tmp.name, p_frame)gpt_res = estimate_threat_gpt(tmp.name, dets)os.remove(tmp.name)# Merge using real track_id assigned by ByteTrackerfor d in dets:oid = d.get('track_id')if oid and oid in gpt_res:d.update(gpt_res[oid])# Push GPT data back into tracker's internal STrack objects# so it persists across subsequent frames via _sync_datatracker.inject_metadata(dets)except Exception as e:logging.error("GPT failed for Frame 0: %s", e)# --- RENDER BOXES & OVERLAYS ---# We need to convert list of dicts back to boxes array for draw_boxes
35 unmodified lines3637383940414235 unmodified linesdet_summary = []for i, det in enumerate(detections):# UI uses T01, T02... logic usually matches index + 1obj_id = f"T{str(i+1).zfill(2)}"bbox = det.get("bbox", [])label = det.get("label", "object")det_summary.append(f"- ID: {obj_id}, Classification Hint: {label}, BBox: {bbox}")35 unmodified lines3637383940414235 unmodified linesdet_summary = []for i, det in enumerate(detections):# UI uses T01, T02... logic usually matches index + 1obj_id = det.get("track_id") or det.get("id") or f"T{str(i+1).zfill(2)}"bbox = det.get("bbox", [])label = det.get("label", "object")det_summary.append(f"- ID: {obj_id}, Classification Hint: {label}, BBox: {bbox}")
194 unmodified lines195196197198199200128 unmodified lines329330331332333334335336337182 unmodified lines520521522523524525526527528529530531194 unmodified linesreturn retclass STrack:"""Single object track. Wrapper around KalmanFilter state.128 unmodified linesSTrack._count += 1return STrack._countclass ByteTracker:def __init__(self, track_thresh=0.5, track_buffer=30, match_thresh=0.8, frame_rate=30):self.track_thresh = track_threshself.track_buffer = track_bufferself.match_thresh = match_thresh182 unmodified lines"""Propagate attributes like GPT data between track and detection."""# 1. From Source to Track (Update)source_data = det_source.original_data if hasattr(det_source, 'original_data') else {}for k in ['gpt_distance_m', 'gpt_direction', 'gpt_description']:if k in source_data:track.gpt_data[k] = source_data[k]# 2. From Track to Source (Forward fill logic handled in output construction)# --- Helper Functions ---194 unmodified lines195196197198199200201202203204205206207208209210211212213214215128 unmodified lines344345346347348349350351352353354355356357358182 unmodified lines541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572194 unmodified linesreturn retGPT_SYNC_KEYS = frozenset({# Legacy fields"gpt_distance_m", "gpt_direction", "gpt_description", "gpt_raw",# Threat intelligence"threat_level_score", "threat_classification", "weapon_readiness",# Naval assessment fields from schema"vessel_category", "specific_class", "identity_markers", "flag_state","visible_weapons", "sensor_profile", "motion_status", "wake_description","aspect", "range_estimation_nm", "bearing_clock", "deck_activity","special_features", "tactical_intent",# Computed fields"distance_m", "direction", "description",})class STrack:"""Single object track. Wrapper around KalmanFilter state.128 unmodified linesSTrack._count += 1return STrack._count@staticmethoddef reset_count():"""Reset track ID counter so the next video starts at T01."""STrack._count = 0class ByteTracker:def __init__(self, track_thresh=0.5, track_buffer=30, match_thresh=0.8, frame_rate=30):STrack.reset_count()self.track_thresh = track_threshself.track_buffer = track_bufferself.match_thresh = match_thresh182 unmodified lines"""Propagate attributes like GPT data between track and detection."""# 1. From Source to Track (Update)source_data = det_source.original_data if hasattr(det_source, 'original_data') else {}for k in GPT_SYNC_KEYS:if k in source_data:track.gpt_data[k] = source_data[k]# 2. From Track to Source (Forward fill logic handled in output construction)def inject_metadata(self, tracked_dets):"""Push metadata from post-processed detection dicts back into internal STrack objects.Needed because GPT results are added to detection dicts *after* tracker.update()returns, so the tracker's internal state doesn't have GPT data unless weexplicitly push it back in."""meta_by_tid = {}for d in tracked_dets:tid = d.get('track_id')if not tid:continuemeta = {k: d[k] for k in GPT_SYNC_KEYS if k in d}if meta:meta_by_tid[tid] = metafor track in self.tracked_stracks:tid_str = f"T{str(track.track_id).zfill(2)}"if tid_str in meta_by_tid:track.gpt_data.update(meta_by_tid[tid_str])# --- Helper Functions ---
frontend/index.html+2-2
frontend/js/main.js+1-1
frontend/js/ui/cards.js+3-1
frontend/js/ui/chat.js+1-1
frontend/js/ui/overlays.js+26-5
inference.py+22-42
utils/gpt_reasoning.py+1-1
utils/tracker.py+43-2