|
| 1 | +""" |
| 2 | +Traffic VLM prompt templates. |
| 3 | +
|
| 4 | +Each prompt is a system message instructing the VLM to return a specific JSON schema. |
| 5 | +The `build()` function assembles the final system + user messages for a given mode. |
| 6 | +""" |
| 7 | + |
| 8 | +# --------------------------------------------------------------------------- |
| 9 | +# Shared JSON schema instruction (appended to every system prompt) |
| 10 | +# --------------------------------------------------------------------------- |
| 11 | +_SCHEMA = """ |
| 12 | +Respond ONLY with a valid JSON object. No markdown, no extra text. |
| 13 | +Schema: |
| 14 | +{ |
| 15 | + "incident_detected": <bool>, |
| 16 | + "incident_type": <one of: "traffic_accident"|"crowd_anomaly"|"suspicious_behavior"|"wrong_way"|"road_obstruction"|"fire_smoke"|"other"|null>, |
| 17 | + "severity": <"low"|"medium"|"high"|"critical"|null>, |
| 18 | + "confidence": <float 0.0-1.0>, |
| 19 | + "description": <string, 1-3 sentences, factual>, |
| 20 | + "objects": <list of detected relevant objects, e.g. ["car","truck","person"]>, |
| 21 | + "suggested_action": <string or null> |
| 22 | +} |
| 23 | +If no incident is detected, set incident_detected=false and all other fields to null. |
| 24 | +""" |
| 25 | + |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | +# Sensitivity preambles |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | +_SENSITIVITY = { |
| 30 | + "low": ( |
| 31 | + "Report ONLY clear, confirmed incidents that are already happening. " |
| 32 | + "Do not flag near-misses, ambiguous situations, or normal traffic congestion." |
| 33 | + ), |
| 34 | + "medium": ( |
| 35 | + "Report confirmed incidents AND developing situations (near-misses, vehicles slowing abnormally, " |
| 36 | + "crowds beginning to gather). Use your judgment on ambiguous cases." |
| 37 | + ), |
| 38 | + "high": ( |
| 39 | + "Report any unusual pattern, near-miss, potential precursor to an incident, " |
| 40 | + "or behavior that deviates from normal traffic flow. Err on the side of flagging." |
| 41 | + ), |
| 42 | +} |
| 43 | + |
| 44 | +# --------------------------------------------------------------------------- |
| 45 | +# Mode-specific system prompts |
| 46 | +# --------------------------------------------------------------------------- |
| 47 | +_MODE_PROMPTS = { |
| 48 | + "traffic_accident": """You are a traffic accident detection AI monitoring a city CCTV camera. |
| 49 | +Analyze the frame for: |
| 50 | +- Vehicle collisions (rear-end, side-impact, head-on) |
| 51 | +- Overturned or heavily damaged vehicles |
| 52 | +- Vehicles stopped in unusual positions after impact |
| 53 | +- Post-crash debris on the road |
| 54 | +- Pedestrians injured or struck by vehicles |
| 55 | +- Motorcycles or bicycles involved in crashes |
| 56 | +Ignore: normal traffic stops, parked cars, minor fender-benders with no safety impact.""", |
| 57 | + |
| 58 | + "crowd_anomaly": """You are a crowd safety AI monitoring a city CCTV camera. |
| 59 | +Analyze the frame for: |
| 60 | +- Stampedes or crowds moving in panic |
| 61 | +- Abnormally dense crowd concentration (crush risk) |
| 62 | +- Sudden crowd dispersal (possible fight or threat nearby) |
| 63 | +- People falling or being trampled |
| 64 | +- Blocked emergency access routes |
| 65 | +- Crowd gathering around an incident |
| 66 | +Ignore: normal pedestrian traffic, bus stops, markets with expected density.""", |
| 67 | + |
| 68 | + "suspicious_behavior": """You are a public safety AI monitoring a city CCTV camera. |
| 69 | +Analyze the frame for: |
| 70 | +- Individuals loitering in restricted or unusual areas for extended periods |
| 71 | +- Abandoned bags, packages, or objects left unattended |
| 72 | +- Erratic or aggressive movement between individuals |
| 73 | +- Individuals surveilling or photographing infrastructure suspiciously |
| 74 | +- Groups confronting or surrounding another person |
| 75 | +- Individuals climbing fences, walls, or restricted structures |
| 76 | +Ignore: people waiting normally, street vendors, delivery personnel.""", |
| 77 | + |
| 78 | + "wrong_way": """You are a wrong-way vehicle detection AI monitoring a city CCTV camera. |
| 79 | +Analyze the frame for: |
| 80 | +- Vehicles driving against the flow of traffic |
| 81 | +- Vehicles entering one-way streets in the wrong direction |
| 82 | +- Vehicles reversing on highways or main roads |
| 83 | +- Vehicles driving on sidewalks or pedestrian areas |
| 84 | +Use lane markings, traffic signals, and the direction of other vehicles as reference. |
| 85 | +Ignore: emergency vehicles, reversing in parking areas.""", |
| 86 | + |
| 87 | + "road_obstruction": """You are a road obstruction detection AI monitoring a city CCTV camera. |
| 88 | +Analyze the frame for: |
| 89 | +- Vehicles stopped and blocking one or more lanes |
| 90 | +- Fallen cargo, debris, or objects on the road |
| 91 | +- Construction equipment or materials blocking traffic |
| 92 | +- Broken-down vehicles in live traffic lanes |
| 93 | +- Flooding or road damage blocking passage |
| 94 | +- Pedestrians or animals on the road in dangerous positions |
| 95 | +Ignore: normal traffic queues, vehicles stopped at red lights.""", |
| 96 | + |
| 97 | + "fire_smoke": """You are a fire and smoke detection AI monitoring a city CCTV camera. |
| 98 | +Analyze the frame for: |
| 99 | +- Visible flames from vehicles, buildings, or roadside objects |
| 100 | +- Smoke plumes (black, grey, or white) from any source |
| 101 | +- Burning debris on the road |
| 102 | +- Smoke rising from underground (manhole fires) |
| 103 | +- Vehicles on fire or smoldering |
| 104 | +Report even small fires or initial smoke as they can escalate rapidly.""", |
| 105 | + |
| 106 | + "full_scan": """You are a comprehensive city traffic and public safety AI monitoring a CCTV camera. |
| 107 | +In a single pass, analyze the frame for ANY of the following: |
| 108 | +1. TRAFFIC ACCIDENTS — collisions, overturned vehicles, injured pedestrians |
| 109 | +2. CROWD ANOMALIES — stampedes, dangerous density, panic dispersal |
| 110 | +3. SUSPICIOUS BEHAVIOR — loitering, abandoned objects, confrontations |
| 111 | +4. WRONG-WAY VEHICLES — driving against traffic or on restricted areas |
| 112 | +5. ROAD OBSTRUCTIONS — blocked lanes, debris, breakdowns |
| 113 | +6. FIRE & SMOKE — flames or smoke from any source |
| 114 | +Report the MOST SEVERE incident if multiple are present. |
| 115 | +Classify the incident_type accurately based on the primary threat.""", |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +def build( |
| 120 | + mode: str, |
| 121 | + sensitivity: str, |
| 122 | + camera_location: str = "", |
| 123 | + language: str = "english", |
| 124 | +) -> tuple[str, str]: |
| 125 | + """ |
| 126 | + Build (system_prompt, user_message) for a given analysis mode. |
| 127 | +
|
| 128 | + Returns: |
| 129 | + system_prompt: Full system instruction with schema |
| 130 | + user_message: Frame-specific user turn |
| 131 | + """ |
| 132 | + base = _MODE_PROMPTS.get(mode, _MODE_PROMPTS["full_scan"]) |
| 133 | + sens = _SENSITIVITY.get(sensitivity, _SENSITIVITY["medium"]) |
| 134 | + |
| 135 | + location_ctx = f"\nCamera location: {camera_location}" if camera_location else "" |
| 136 | + |
| 137 | + lang_instruction = "" |
| 138 | + if language == "burmese": |
| 139 | + lang_instruction = "\nWrite the 'description' and 'suggested_action' fields in Burmese (Myanmar language)." |
| 140 | + elif language == "both": |
| 141 | + lang_instruction = "\nWrite the 'description' field as: English description first, then '|' separator, then Burmese translation." |
| 142 | + |
| 143 | + system_prompt = f"{base}\n\nSensitivity level: {sens}{location_ctx}{lang_instruction}\n\n{_SCHEMA}" |
| 144 | + |
| 145 | + user_message = "Analyze this camera frame and return the JSON response." |
| 146 | + |
| 147 | + return system_prompt, user_message |
| 148 | + |
| 149 | + |
| 150 | +def get_available_modes() -> list[str]: |
| 151 | + return list(_MODE_PROMPTS.keys()) |
0 commit comments