File size: 26,675 Bytes
44f7c73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8eb31
44f7c73
 
 
 
4a8eb31
 
 
44f7c73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af13b2f
 
 
44f7c73
 
 
af13b2f
44f7c73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af13b2f
44f7c73
af13b2f
 
 
 
 
 
 
 
 
 
 
 
 
44f7c73
 
 
af13b2f
44f7c73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, JSONResponse
import json
import os
from datetime import datetime
from typing import List, Optional
import numpy as np
from PIL import Image
from io import BytesIO
import piexif
import uuid
from sqlalchemy import func
from dotenv import load_dotenv

# Load env before imports
load_dotenv()

# Import real YOLO ensemble detector
# Uses trained yolo11base.pt model for actual inference
try:
    from ..models.ensemble import EnsembleDetector, SeverityClassifier, TTAProcessor
    print("[OK] Loaded real YOLO ensemble detector")
except ImportError as e:
    print(f"[WARN] Real ensemble not found ({e}), using mocks")
    from ..models.mock_ensemble import EnsembleDetector, SeverityClassifier, TTAProcessor

# GPS Fallback: Near DJI Sample Location (DJI_202511261727_006)
# Original DJI GPS: 16.009719, 108.258302 - Fallback is 50m to the right
DEFAULT_LATITUDE = 16.009719
DEFAULT_LONGITUDE = 108.258852
DEFAULT_ALTITUDE = 68.5

from ..database.models import Detection, DetectionResult, Base
from ..database.db import SessionLocal, engine

# Create tables if they don't exist
Base.metadata.create_all(bind=engine)

app = FastAPI(
    title="Road Damage Detection API",
    description="AI-powered highway infrastructure monitoring",
    version="1.0.0"
)

# CORS configuration for web UI - MUST BE BEFORE static files mount
origins = os.getenv("ALLOWED_ORIGINS", "http://localhost:3000,http://localhost:3001").split(",")
if not origins or origins == [""]:
    origins = ["*"]
# Always include common localhost ports and production URLs
if "http://localhost:3001" not in origins:
    origins.append("http://localhost:3001")
if "http://localhost:3000" not in origins:
    origins.append("http://localhost:3000")
# Add Vercel production URL
if "https://road-damage-on5d.vercel.app" not in origins:
    origins.append("https://road-damage-on5d.vercel.app")
    
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    expose_headers=["*"],
)

# Custom image serving endpoint with CORS headers (StaticFiles doesn't properly apply CORS)
from starlette.responses import Response

@app.get("/uploads/{filename:path}")
async def serve_upload(filename: str):
    """Serve uploaded images with proper CORS headers."""
    filepath = os.path.join("data/uploads", filename)
    if not os.path.exists(filepath):
        raise HTTPException(status_code=404, detail="File not found")
    
    # Determine content type
    ext = os.path.splitext(filename)[1].lower()
    content_types = {
        '.jpg': 'image/jpeg',
        '.jpeg': 'image/jpeg',
        '.png': 'image/png',
        '.gif': 'image/gif',
        '.webp': 'image/webp',
    }
    content_type = content_types.get(ext, 'application/octet-stream')
    
    with open(filepath, 'rb') as f:
        content = f.read()
    
    return Response(
        content=content,
        media_type=content_type,
        headers={
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "GET, OPTIONS",
            "Access-Control-Allow-Headers": "*",
            "Cache-Control": "public, max-age=3600"
        }
    )


# Initialize models
print("[INFO] Loading models...")
ensemble = EnsembleDetector()
severity_classifier = SeverityClassifier()
tta_processor = TTAProcessor()

# Store detection results temporarily (for simple retrieval without DB)
DETECTION_CACHE = {}

# Ensure uploads directory exists
os.makedirs("data/uploads", exist_ok=True)

# ============================================
# 1. EXIF EXTRACTION FROM DRONE IMAGES
# ============================================

def extract_gps_from_image(image_bytes: bytes) -> dict:
    """
    Extract GPS coordinates from DJI image EXIF data.
    """
    try:
        image = Image.open(BytesIO(image_bytes))
        
        try:
            exif_dict = piexif.load(image_bytes)
            print(f"[GPS] EXIF loaded, keys: {list(exif_dict.keys())}")
        except Exception as e:
            # Maybe PNG or no exif
            print(f"[WARN] EXIF load failed: {e}")
            return {
                "latitude": DEFAULT_LATITUDE,
                "longitude": DEFAULT_LONGITUDE,
                "altitude": DEFAULT_ALTITUDE,
                "timestamp": datetime.now().isoformat(),
                "drone_model": "Unknown",
                "success": False
            }
        
        gps_info = exif_dict.get("GPS", {})
        print(f"[GPS] GPS info keys: {list(gps_info.keys())}")
        
        # Helper to convert rational to float
        def convert_to_degrees(value):
            if not value: return 0.0
            d = value[0][0] / value[0][1]
            m = value[1][0] / value[1][1] / 60.0
            s = value[2][0] / value[2][1] / 3600.0
            return d + m + s
        
        if piexif.GPSIFD.GPSLatitude in gps_info and piexif.GPSIFD.GPSLongitude in gps_info:
            lat = convert_to_degrees(gps_info[piexif.GPSIFD.GPSLatitude])
            lon = convert_to_degrees(gps_info[piexif.GPSIFD.GPSLongitude])
            print(f"[GPS] GPS found: raw lat={lat}, lon={lon}")
            
            # Adjust for direction
            if piexif.GPSIFD.GPSLatitudeRef in gps_info and gps_info[piexif.GPSIFD.GPSLatitudeRef] == b'S':
                lat = -lat
            if piexif.GPSIFD.GPSLongitudeRef in gps_info and gps_info[piexif.GPSIFD.GPSLongitudeRef] == b'W':
                lon = -lon
            
            print(f"[GPS] GPS after direction: lat={lat}, lon={lon}")
                
            # Altitude - safely handle different data formats
            try:
                alt_data = gps_info.get(piexif.GPSIFD.GPSAltitude)
                if alt_data and isinstance(alt_data, tuple) and len(alt_data) >= 1:
                    if isinstance(alt_data[0], tuple) and len(alt_data[0]) >= 2:
                        altitude = alt_data[0][0] / alt_data[0][1]
                    elif isinstance(alt_data[0], (int, float)):
                        altitude = float(alt_data[0])
                    else:
                        altitude = 0
                else:
                    altitude = 0
            except Exception:
                altitude = 0
        else:
            # Fallback to Da Nang if GPS not in EXIF
            print(f"[WARN] GPS not found in EXIF! Using default Da Nang coords")
            lat = DEFAULT_LATITUDE
            lon = DEFAULT_LONGITUDE
            altitude = DEFAULT_ALTITUDE

        # Timestamp - safely extract
        try:
            timestamp_data = exif_dict.get("0th", {}).get(piexif.ImageIFD.DateTime)
            if timestamp_data:
                if isinstance(timestamp_data, bytes):
                    timestamp = timestamp_data.decode()
                elif isinstance(timestamp_data, str):
                    timestamp = timestamp_data
                else:
                    timestamp = datetime.now().isoformat()
            else:
                timestamp = datetime.now().isoformat()
        except Exception:
            timestamp = datetime.now().isoformat()
        
        # Drone model - safely extract
        try:
            model_data = exif_dict.get("0th", {}).get(piexif.ImageIFD.Model)
            if model_data:
                if isinstance(model_data, bytes):
                    drone_model = model_data.decode()
                elif isinstance(model_data, str):
                    drone_model = model_data
                else:
                    drone_model = "Unknown"
            else:
                drone_model = "Unknown"
        except Exception:
            drone_model = "Unknown"
        
        return {
            "latitude": float(lat),
            "longitude": float(lon),
            "altitude": float(altitude),
            "timestamp": timestamp,
            "drone_model": drone_model,
            "success": True
        }
    
    except Exception as e:
        # Fallback to Da Nang coordinates
        print(f"[WARN] EXIF extraction failed: {str(e)}")
        return {
            "latitude": DEFAULT_LATITUDE,
            "longitude": DEFAULT_LONGITUDE,
            "altitude": DEFAULT_ALTITUDE,
            "timestamp": datetime.now().isoformat(),
            "drone_model": "Unknown",
            "success": False,
            "error": str(e)
        }

# ============================================
# 2. DETECTION API ENDPOINT
# ============================================

@app.post("/detect")
async def detect_damage(file: UploadFile = File(...)):
    print(f"\n{'='*60}")
    print(f"[DETECT] Received file: {file.filename}")
    print(f"{'='*60}")
    try:
        # Read image
        contents = await file.read()
        print(f"[DETECT] File size: {len(contents)} bytes")
        image = Image.open(BytesIO(contents))
        image_array = np.array(image)
        print(f"[DETECT] Image shape: {image_array.shape}")
        
        # Save for static serving
        filename = f"{datetime.now().strftime('%Y%m%d%H%M%S')}_{file.filename}"
        filepath = os.path.join("data/uploads", filename)
        with open(filepath, "wb") as f:
            f.write(contents)
        print(f"[DETECT] Saved to: {filepath}")
        
        image_url = f"/uploads/{filename}"

        # Extract GPS from EXIF
        gps_data = extract_gps_from_image(contents)
        base_lat = gps_data["latitude"]
        base_lon = gps_data["longitude"]
        print(f"[DETECT] GPS: lat={base_lat}, lon={base_lon}")
        
        # Run AI inference
        print("[DETECT] Running YOLO inference...")
        ensemble_results = ensemble.predict(image_array)
        print(f"[DETECT] Ensemble returned {len(ensemble_results)} detections")
        tta_results = tta_processor.predict(image_array)
        print(f"[DETECT] TTA returned {len(tta_results)} detections")
        all_detections = ensemble_results + tta_results
        print(f"[DETECT] Total raw detections: {len(all_detections)}")
        
        # Process and classify
        detections_with_severity = []
        for det in all_detections:
            x1, y1, x2, y2 = det["box"]
            bbox_area = (x2 - x1) * (y2 - y1)
            image_area = image.size[0] * image.size[1]
            area_ratio = bbox_area / image_area
            
            severity = severity_classifier.classify(area_ratio, det["confidence"])
            
            # Add slight GPS offset to separate markers if multiple damages in one image
            detection_idx = len(detections_with_severity)
            det_lat = base_lat + (detection_idx * 0.0001)
            det_lon = base_lon + (detection_idx * 0.0001)
            
            detections_with_severity.append({
                "x1": int(x1), "y1": int(y1), "x2": int(x2), "y2": int(y2),
                "class_name": det["class_name"],
                "class_id": det["class_id"],
                "confidence": float(det["confidence"]),
                "severity": severity,
                "area_ratio": float(area_ratio),
                "votes": det.get("votes", 2),
                "latitude": float(det_lat),
                "longitude": float(det_lon),
                "image_url": image_url
            })
        
        # Save detection result
        detection_id = str(uuid.uuid4())
        DETECTION_CACHE[detection_id] = {
            "detections": detections_with_severity,
            "gps_data": gps_data,
            "timestamp": datetime.now().isoformat(),
            "image_filename": file.filename,
            "total_count": len(detections_with_severity),
            "image_url": image_url
        }
        
        # DB Storage
        db = SessionLocal()
        try:
            db_detection = Detection(
                detection_id=detection_id,
                image_filename=file.filename,
                gps_latitude=base_lat,
                gps_longitude=base_lon,
                total_detections=len(detections_with_severity),
                created_at=datetime.now()
            )
            db.add(db_detection)
            
            for det in detections_with_severity:
                db_result = DetectionResult(
                    detection_id=detection_id,
                    class_name=det["class_name"],
                    confidence=det["confidence"],
                    severity=det["severity"],
                    gps_latitude=det["latitude"],
                    gps_longitude=det["longitude"],
                    bbox_x1=det["x1"], bbox_y1=det["y1"],
                    bbox_x2=det["x2"], bbox_y2=det["y2"]
                )
                db.add(db_result)
            db.commit()
        except Exception as db_error:
            print(f"[WARN] Database error: {str(db_error)}")
            db.rollback()
        finally:
            db.close()
        
        print(f"[DETECT] SUCCESS: Returning {len(detections_with_severity)} detections")
        for i, det in enumerate(detections_with_severity):
            print(f"[DETECT]   {i+1}. {det['class_name']} - conf: {det['confidence']:.3f}")
        print(f"{'='*60}")
        
        return JSONResponse({
            "success": True,
            "detection_id": detection_id,
            "detections": detections_with_severity,
            "gps_data": gps_data,
            "total_count": len(detections_with_severity),
            "image_url": image_url
        })
    
    except Exception as e:
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/results/{detection_id}")
async def get_results(detection_id: str):
    if detection_id in DETECTION_CACHE:
        return DETECTION_CACHE[detection_id]
    
    # Try DB
    db = SessionLocal()
    try:
        d = db.query(Detection).filter(Detection.detection_id == detection_id).first()
        if not d:
             raise HTTPException(status_code=404, detail="Detection not found")
        
        results = db.query(DetectionResult).filter(DetectionResult.detection_id == detection_id).all()
        
        return {
            "detections": [
                {
                    "class_name": r.class_name,
                    "confidence": r.confidence,
                    "severity": r.severity,
                    "latitude": r.gps_latitude,
                    "longitude": r.gps_longitude,
                    "x1": r.bbox_x1, "y1": r.bbox_y1, "x2": r.bbox_x2, "y2": r.bbox_y2,
                    "image_url": f"/uploads/{d.image_filename}" # Assumption: filename is unique/enough
                } for r in results
            ],
            "timestamp": d.created_at.isoformat(),
            "image_filename": d.image_filename
        }
    finally:
        db.close()

# ============================================
# PDF REPORT GENERATION
# ============================================

from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
from reportlab.lib.units import inch
from reportlab.lib import colors

@app.get("/download-pdf/{detection_id}")
async def download_pdf(detection_id: str):
    # Fetch data (from cache or DB)
    data = await get_results(detection_id)
    detections = data["detections"]
    
    pdf_filename = f"report_{detection_id}.pdf"
    pdf_path = f"/tmp/{pdf_filename}"
    if os.name == 'nt': # Windows fix
         pdf_path = f"report_{detection_id}.pdf"

    doc = SimpleDocTemplate(pdf_path, pagesize=letter)
    story = []
    styles = getSampleStyleSheet()
    
    story.append(Paragraph("Road Damage Detection Report", styles['Title']))
    story.append(Spacer(1, 0.2*inch))
    
    table_data = [["Class", "Severity", "Confidence", "GPS"]]
    for det in detections:
        table_data.append([
            det["class_name"],
            det["severity"],
            f"{det['confidence']:.2f}",
            f"{det['latitude']:.4f}, {det['longitude']:.4f}"
        ])
        
    t = Table(table_data)
    t.setStyle(TableStyle([
        ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
        ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
        ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
        ('GRID', (0, 0), (-1, -1), 1, colors.black)
    ]))
    story.append(t)
    
    doc.build(story)
    return FileResponse(pdf_path, filename=pdf_filename)

@app.get("/health")
async def health_check():
    try:
        # Check DB connection
        db = SessionLocal()
        db.execute(func.now())
        db.close()
        db_status = "connected"
    except Exception as e:
        db_status = f"error: {str(e)}"

    return {
        "status": "healthy",
        "models_loaded": True,
        "database": db_status,
        "timestamp": datetime.now().isoformat()
    }

# ============================================
# 3. BATCH DETECTION ENDPOINT
# ============================================

# Request model for analysis
from pydantic import BaseModel

class AnalysisRequest(BaseModel):
    filenames: List[str]

@app.post("/analyze-survey")
async def analyze_survey(request: AnalysisRequest):
    print(f"\n{'='*60}")
    print(f"[ANALYZE] Starting analysis of {len(request.filenames)} files")
    print(f"{'='*60}")
    results = []
    
    for idx, filename in enumerate(request.filenames):
        print(f"\n[ANALYZE] [{idx+1}/{len(request.filenames)}] Processing: {filename}")
        # Check if file exists in uploads
        filepath = os.path.join("data/uploads", filename)
        if not os.path.exists(filepath):
            print(f"[ANALYZE] ERROR: File not found: {filepath}")
            continue
            
        try:
            # Read image
            with open(filepath, "rb") as f:
                contents = f.read()
            print(f"[ANALYZE] File size: {len(contents)} bytes")
            
            image = Image.open(BytesIO(contents))
            image_array = np.array(image)
            print(f"[ANALYZE] Image shape: {image_array.shape}")
            image_url = f"/uploads/{filename}"
            
            # Extract GPS (re-extract or we could cache it, but re-extract is safer/stateless)
            gps_data = extract_gps_from_image(contents)
            base_lat = gps_data["latitude"]
            base_lon = gps_data["longitude"]
            print(f"[ANALYZE] GPS: lat={base_lat}, lon={base_lon}")

            # Run API
            print(f"[ANALYZE] Running YOLO inference...")
            ensemble_results = ensemble.predict(image_array)
            print(f"[ANALYZE] Detected {len(ensemble_results)} objects")
            detections_processed = []

            # If no detections, return clean status
            if not ensemble_results:
                 results.append({
                    "x1": 0, "y1": 0, "x2": 0, "y2": 0,
                    "class_name": "No Damage",
                    "class_id": -1,
                    "confidence": 1.0,
                    "severity": "none",
                    "latitude": float(base_lat),
                    "longitude": float(base_lon),
                    "image_url": image_url,
                    "filename": filename
                })
            
            for det in ensemble_results:
                detections_processed.append({
                    "x1": int(det["box"][0]), "y1": int(det["box"][1]),
                    "x2": int(det["box"][2]), "y2": int(det["box"][3]),
                    "class_name": det["class_name"],
                    "class_id": det.get("class_id", 0),
                    "confidence": float(det["confidence"]),
                    "severity": "medium", # Simplified
                    "latitude": float(base_lat),
                    "longitude": float(base_lon),
                    "image_url": image_url,
                    "filename": filename
                })
            results.extend(detections_processed)

        except Exception as e:
            print(f"[ERROR] Error analyzing {filename}: {e}")
            continue

    return {
        "success": True,
        "total_analyzed": len(request.filenames),
        "detections": results
    }

# ============================================
# 4. UPLOAD-ONLY ENDPOINT (Map first, analyze later)
# ============================================

@app.post("/upload-survey")
async def upload_survey(files: List[UploadFile] = File(...)):
    """
    Upload survey images and extract GPS data only.
    Returns 'Pending' points for map display.
    Use /analyze-survey to run AI detection later.
    """
    results = []
    
    print(f"[UPLOAD] Uploading {len(files)} survey files...")
    
    for file in files:
        # Skip non-image files
        if not file.content_type or not file.content_type.startswith('image/'):
            print(f"[SKIP] Skipping non-image: {file.filename}")
            continue
            
        try:
            # Read content
            contents = await file.read()
            image = Image.open(BytesIO(contents))
            
            # Extract just the base filename (remove folder path if present)
            base_filename = os.path.basename(file.filename) if file.filename else f"upload_{uuid.uuid4().hex[:8]}.jpg"
            
            # Save for static serving with unique name
            unique_name = f"{datetime.now().strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}_{base_filename}"
            filepath = os.path.join("data/uploads", unique_name)
            
            # Ensure uploads directory exists
            os.makedirs("data/uploads", exist_ok=True)
            
            with open(filepath, "wb") as f:
                f.write(contents)
            
            image_url = f"/uploads/{unique_name}"

            # Extract GPS
            gps_data = extract_gps_from_image(contents)
            base_lat = gps_data["latitude"]
            base_lon = gps_data["longitude"]
            print(f"[GPS] GPS from {file.filename}: lat={base_lat}, lon={base_lon}, success={gps_data.get('success')}")
            
            # Return "Pending" point for map
            results.append({
                "x1": 0, "y1": 0, "x2": 0, "y2": 0,
                "class_name": "Pending",
                "class_id": -2,
                "confidence": 0.0,
                "severity": "none",
                "latitude": float(base_lat),
                "longitude": float(base_lon),
                "image_url": image_url,
                "filename": unique_name
            })
            print(f"[OK] Uploaded: {file.filename} -> {unique_name}")
            
        except Exception as e:
            import traceback
            print(f"[ERROR] Error uploading {file.filename}: {e}")
            traceback.print_exc()
            continue

    return {
        "success": True,
        "total_files": len(files),
        "total_uploaded": len(results),
        "detections": results
    }

@app.post("/detect-batch")
async def detect_batch(files: List[UploadFile] = File(...), analyze: Optional[str] = Form("true")):
    # Convert string to bool (FormData sends strings)
    should_analyze = analyze.lower() in ("true", "1", "yes")
    results = []
    
    print(f"[BATCH] Processing batch of {len(files)} files (Analyze={should_analyze})...")
    
    for file in files:
        # Skip non-image files
        if not file.content_type.startswith('image/'):
            continue
            
        try:
            # Read content
            contents = await file.read()
            image = Image.open(BytesIO(contents))
            
            # Save for static serving
            unique_name = f"{datetime.now().strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}_{file.filename}"
            filepath = os.path.join("data/uploads", unique_name)
            with open(filepath, "wb") as f:
                f.write(contents)
            
            image_url = f"/uploads/{unique_name}"

            # Extract GPS
            gps_data = extract_gps_from_image(contents)
            base_lat = gps_data["latitude"]
            base_lon = gps_data["longitude"]
            
            # IF NOT ANALYZING: Return "Pending" point
            if not should_analyze:
                 results.append({
                    "x1": 0, "y1": 0, "x2": 0, "y2": 0,
                    "class_name": "Pending", # Special class for UI
                    "class_id": -2,
                    "confidence": 0.0,
                    "severity": "none",
                    "latitude": float(base_lat),
                    "longitude": float(base_lon),
                    "image_url": image_url,
                    "filename": unique_name # Return the saved filename for later analysis
                })
                 continue

            # IF ANALYZING (Legacy flow or manual trigger if we used this endpoint?)
            image_array = np.array(image)
            ensemble_results = ensemble.predict(image_array)
            detections_processed = []
            
            if not ensemble_results:
                results.append({
                    "x1": 0, "y1": 0, "x2": 0, "y2": 0,
                    "class_name": "No Damage",
                    "class_id": -1,
                    "confidence": 1.0,
                    "severity": "none",
                    "latitude": float(base_lat),
                    "longitude": float(base_lon),
                    "image_url": image_url,
                    "filename": unique_name
                })
            
            for det in ensemble_results:
                detections_processed.append({
                    "x1": int(det["box"][0]), "y1": int(det["box"][1]),
                    "x2": int(det["box"][2]), "y2": int(det["box"][3]),
                    "class_name": det["class_name"],
                    "class_id": det.get("class_id", 0),
                    "confidence": float(det["confidence"]),
                    "severity": "medium", 
                    "latitude": float(base_lat),
                    "longitude": float(base_lon),
                    "image_url": image_url,
                    "filename": unique_name
                })
            
            results.extend(detections_processed)
            
        except Exception as e:
            print(f"[ERROR] Error processing {file.filename}: {e}")
            continue

    return {
        "success": True,
        "total_files": len(files),
        "total_detections": len(results),
        "detections": results
    }