Spaces:
Paused
Paused
Zhen Ye commited on
Commit ·
55e372a
1
Parent(s): 58bb3a4
GPT reasoning: add in-memory frame encoding, image_b64 support, use gpt-4o-mini for relevance
Browse filesFiles changed (2)
- utils/gpt_reasoning.py+31-10
- utils/relevance.py+1-1
14 unmodified lines15161718192021222324252627282989 unmodified lines11912012111311412212312412512612712812913012113113213313413513613713813914014114214314412 unmodified lines15715815916016116214614714814915016316416516616716816917017117217330 unmodified lines20420520618720720820921021116 unmodified lines22822923021023123223323414 unmodified lines return base64.b64encode(image_file.read()).decode('utf-8')
def encode_frame_to_b64(frame) -> str: """Encode an OpenCV BGR frame to a base64 JPEG string in memory (no disk I/O).""" import cv2 success, buf = cv2.imencode('.jpg', frame) if not success: raise ValueError("Failed to encode frame to JPEG") return base64.b64encode(buf.tobytes()).decode('utf-8')
_DOMAIN_ROLES = { "NAVAL": "Naval Intelligence Officer and Maritime Threat Analyst", "GROUND": "Ground Surveillance Intelligence Officer",89 unmodified lines
def estimate_threat_gpt( image_path: str, detections: List[Dict[str, Any]], image_path: Optional[str] = None, detections: Optional[List[Dict[str, Any]]] = None, mission_spec=None, # Optional[MissionSpecification] image_b64: Optional[str] = None,) -> Dict[str, Any]: """ Perform Threat Assessment on detected objects using GPT-4o.
Args: image_path: Path to the image file. image_path: Path to the image file (mutually exclusive with image_b64). detections: List of detection dicts (bbox, label, etc.). mission_spec: Optional MissionSpecification for domain-aware assessment. image_b64: Pre-encoded base64 JPEG string (avoids disk round-trip).
Returns: Dict mapping object ID (e.g., T01) to threat assessment dict. """ if detections is None: detections = []
api_key = os.environ.get("OPENAI_API_KEY") if not api_key: logger.warning("OPENAI_API_KEY not set. Skipping GPT threat assessment.")12 unmodified lines if not det_text: return {}
# 2. Encode image (prefer pre-encoded b64 to avoid disk I/O) if image_b64: base64_image = image_b64 # 2. Encode image try: base64_image = encode_image(image_path) except Exception as e: logger.error(f"Failed to encode image for GPT: {e}") elif image_path: try: base64_image = encode_image(image_path) except Exception as e: logger.error(f"Failed to encode image for GPT: {e}") return {} else: logger.error("estimate_threat_gpt: no image_path or image_b64 provided") return {}
# 3. Domain-aware prompt selection (INV-7)30 unmodified lines { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}" "url": f"data:image/jpeg;base64,{base64_image}", "detail": "low" } } ]16 unmodified lines headers=headers, method="POST" ) with urllib.request.urlopen(req) as response: with urllib.request.urlopen(req, timeout=30) as response: resp_data = json.loads(response.read().decode('utf-8')) content = resp_data['choices'][0]['message'].get('content')107 unmodified lines108109110111111112113114107 unmodified lines )
payload = { "model": "gpt-4o", "model": "gpt-4o-mini", "temperature": 0.0, "max_tokens": 200, "response_format": {"type": "json_object"},utils/gpt_reasoning.py+31-10
utils/relevance.py+1-1