Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
SUB-SENTINEL backend β FastAPI application.
|
| 3 |
-
|
| 4 |
Enhanced Features Added:
|
| 5 |
β Sonar Mode
|
| 6 |
β Bioluminescence Mode
|
|
@@ -9,16 +8,22 @@ Enhanced Features Added:
|
|
| 9 |
β System Status Panel
|
| 10 |
"""
|
| 11 |
|
| 12 |
-
import cv2
|
| 13 |
-
import numpy as np
|
| 14 |
-
import base64
|
| 15 |
import os
|
| 16 |
import logging
|
| 17 |
|
| 18 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 19 |
from fastapi.middleware.cors import CORSMiddleware
|
| 20 |
|
| 21 |
-
from processing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
from sitrep import generate_sitrep
|
| 23 |
|
| 24 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -54,59 +59,11 @@ app.add_middleware(
|
|
| 54 |
)
|
| 55 |
|
| 56 |
# ---------------------------------------------------------------------------
|
| 57 |
-
# π§ HELPER FUNCTIONS
|
| 58 |
# ---------------------------------------------------------------------------
|
| 59 |
|
| 60 |
-
def generate_sonar(image_array):
|
| 61 |
-
h, w, _ = image_array.shape
|
| 62 |
-
sonar = np.zeros((h, w, 3), dtype=np.uint8)
|
| 63 |
-
|
| 64 |
-
center = (w // 2, h // 2)
|
| 65 |
-
|
| 66 |
-
# circular waves
|
| 67 |
-
for r in range(50, min(center), 40):
|
| 68 |
-
cv2.circle(sonar, center, r, (0, 255, 0), 1)
|
| 69 |
-
|
| 70 |
-
# scanning line
|
| 71 |
-
cv2.line(sonar, center, (w, h//2), (0, 255, 0), 2)
|
| 72 |
-
|
| 73 |
-
blended = cv2.addWeighted(image_array, 0.3, sonar, 0.7, 0)
|
| 74 |
-
|
| 75 |
-
_, buffer = cv2.imencode('.jpg', blended)
|
| 76 |
-
return base64.b64encode(buffer).decode('utf-8')
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
def apply_bioluminescence(image_array):
|
| 80 |
-
hsv = cv2.cvtColor(image_array, cv2.COLOR_BGR2HSV)
|
| 81 |
-
hsv[:, :, 2] = cv2.add(hsv[:, :, 2], 50)
|
| 82 |
-
|
| 83 |
-
glow = cv2.GaussianBlur(image_array, (0, 0), 15)
|
| 84 |
-
result = cv2.addWeighted(image_array, 0.6, glow, 0.8, 0)
|
| 85 |
-
|
| 86 |
-
_, buffer = cv2.imencode('.jpg', result)
|
| 87 |
-
return base64.b64encode(buffer).decode('utf-8')
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
def draw_detections(image, detections):
|
| 91 |
-
for det in detections:
|
| 92 |
-
x1, y1, x2, y2 = map(int, det["bbox"])
|
| 93 |
-
label = f"{det['mapped_label']} {int(det['confidence']*100)}%"
|
| 94 |
-
|
| 95 |
-
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 96 |
-
cv2.putText(
|
| 97 |
-
image,
|
| 98 |
-
label,
|
| 99 |
-
(x1, y1 - 10),
|
| 100 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
| 101 |
-
0.6,
|
| 102 |
-
(0, 0, 255),
|
| 103 |
-
2
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
return image
|
| 107 |
-
|
| 108 |
-
|
| 109 |
def transmission_status():
|
|
|
|
| 110 |
return {
|
| 111 |
"mode": "ACTIVE",
|
| 112 |
"type": "LOW BANDWIDTH",
|
|
@@ -144,19 +101,20 @@ async def process_image(file: UploadFile = File(...)) -> dict:
|
|
| 144 |
|
| 145 |
try:
|
| 146 |
# πΉ Core pipeline
|
| 147 |
-
enhanced_b64,
|
| 148 |
-
detections = run_detection(
|
| 149 |
-
heatmap_b64 = build_heatmap(
|
| 150 |
sitrep = generate_sitrep(detections)
|
| 151 |
|
| 152 |
-
# πΉ
|
| 153 |
-
sonar_b64 =
|
| 154 |
-
biolight_b64 =
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
boxed_b64 = base64.b64encode(buffer).decode('utf-8')
|
| 159 |
|
|
|
|
| 160 |
tx_status = transmission_status()
|
| 161 |
|
| 162 |
except Exception as exc:
|
|
@@ -170,6 +128,7 @@ async def process_image(file: UploadFile = File(...)) -> dict:
|
|
| 170 |
"sonar_base64": sonar_b64,
|
| 171 |
"biolight_base64": biolight_b64,
|
| 172 |
"boxed_image_base64": boxed_b64,
|
|
|
|
| 173 |
"detections": detections,
|
| 174 |
"sitrep_text": sitrep,
|
| 175 |
"transmission": tx_status,
|
|
|
|
| 1 |
"""
|
| 2 |
SUB-SENTINEL backend β FastAPI application.
|
|
|
|
| 3 |
Enhanced Features Added:
|
| 4 |
β Sonar Mode
|
| 5 |
β Bioluminescence Mode
|
|
|
|
| 8 |
β System Status Panel
|
| 9 |
"""
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
import os
|
| 12 |
import logging
|
| 13 |
|
| 14 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 15 |
from fastapi.middleware.cors import CORSMiddleware
|
| 16 |
|
| 17 |
+
# Import ALL needed functions from processing.py (use the fixed versions!)
|
| 18 |
+
from processing import (
|
| 19 |
+
enhance_image,
|
| 20 |
+
run_detection,
|
| 21 |
+
build_heatmap,
|
| 22 |
+
fuse_sonar_overlay,
|
| 23 |
+
generate_bioluminescence,
|
| 24 |
+
draw_detection_boxes,
|
| 25 |
+
generate_vector_sketch,
|
| 26 |
+
)
|
| 27 |
from sitrep import generate_sitrep
|
| 28 |
|
| 29 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
# ---------------------------------------------------------------------------
|
| 62 |
+
# π§ HELPER FUNCTIONS
|
| 63 |
# ---------------------------------------------------------------------------
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def transmission_status():
|
| 66 |
+
"""System transmission metadata"""
|
| 67 |
return {
|
| 68 |
"mode": "ACTIVE",
|
| 69 |
"type": "LOW BANDWIDTH",
|
|
|
|
| 101 |
|
| 102 |
try:
|
| 103 |
# πΉ Core pipeline
|
| 104 |
+
enhanced_b64, rgb_array = enhance_image(raw_bytes)
|
| 105 |
+
detections = run_detection(rgb_array)
|
| 106 |
+
heatmap_b64 = build_heatmap(rgb_array)
|
| 107 |
sitrep = generate_sitrep(detections)
|
| 108 |
|
| 109 |
+
# πΉ ADVANCED VISUALIZATION MODES (using fixed processing.py functions)
|
| 110 |
+
sonar_b64 = fuse_sonar_overlay(rgb_array, sonar_data={"angle": 0, "sweep": 30})
|
| 111 |
+
biolight_b64 = generate_bioluminescence(rgb_array)
|
| 112 |
+
boxed_b64 = draw_detection_boxes(rgb_array, detections)
|
| 113 |
|
| 114 |
+
# πΉ Low-bandwidth vector sketch
|
| 115 |
+
vector_sketch = generate_vector_sketch(detections, max_bytes=1024)
|
|
|
|
| 116 |
|
| 117 |
+
# πΉ Transmission status
|
| 118 |
tx_status = transmission_status()
|
| 119 |
|
| 120 |
except Exception as exc:
|
|
|
|
| 128 |
"sonar_base64": sonar_b64,
|
| 129 |
"biolight_base64": biolight_b64,
|
| 130 |
"boxed_image_base64": boxed_b64,
|
| 131 |
+
"vector_sketch": vector_sketch,
|
| 132 |
"detections": detections,
|
| 133 |
"sitrep_text": sitrep,
|
| 134 |
"transmission": tx_status,
|