-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_api.py
More file actions
733 lines (622 loc) · 27.2 KB
/
Copy pathtemp_api.py
File metadata and controls
733 lines (622 loc) · 27.2 KB
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
722
723
724
725
726
727
728
729
730
731
732
733
# --- New: Fetch images from database to temp_data ---
from fastapi import Body
def fetch_images_from_db(db_query: dict) -> dict:
"""
Stub: Replace with actual DB fetch logic.
Simulates fetching images from a database and saving to temp_data/normal and temp_data/faulty.
"""
# Example: db_query = {"transformer_id": "T1", "date_range": "2025-10"}
# Replace this with your actual DB code
temp_data_dir = Path('Finetune_data/temp_data')
normal_dir = temp_data_dir / 'normal'
faulty_dir = temp_data_dir / 'faulty'
normal_dir.mkdir(parents=True, exist_ok=True)
faulty_dir.mkdir(parents=True, exist_ok=True)
# Simulate: Copy N images from a source folder (replace with DB fetch)
# For demo, just count existing files
fetched_normal = 0
fetched_faulty = 0
# TODO: Replace with DB fetch and save logic
# Example: for img in db_images: save to normal_dir or faulty_dir
# Here, just return counts
return {
"status": "success",
"fetched_normal": fetched_normal,
"fetched_faulty": fetched_faulty,
"target_normal": str(normal_dir),
"target_faulty": str(faulty_dir)
}
@app.post("/fetch-images")
async def fetch_images(
db_query: dict = Body(..., example={"transformer_id": "T1", "date_range": "2025-10"})
):
"""
Fetch images from database and save to temp_data/normal and temp_data/faulty
"""
try:
result = fetch_images_from_db(db_query)
return JSONResponse(content=result)
except Exception as e:
logger.error(f"Failed to fetch images from DB: {e}")
return JSONResponse(status_code=500, content={"status": "error", "detail": str(e)})
"""
Thermal Anomaly Detection API
FastAPI-based web service for detecting thermal anomalies using unified thermal analysis
"""
import os
import sys
import logging
import tempfile
from datetime import datetime
from pathlib import Path
from typing import Optional, Dict, Any, List
import io
import base64
# Add current directory to Python path for imports
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ML_analysis'))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'heat_point_analysis'))
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import JSONResponse, StreamingResponse, Response
from fastapi.middleware.cors import CORSMiddleware
import cv2
import numpy as np
import yaml
# Import the unified thermal analysis system
try:
import torch
from ML_analysis.model import AnomalyAutoEncoder
from ML_analysis.detect_and_annotate import (
load_model, preprocess_image, generate_anomaly_mask,
find_contours_and_draw_boxes
)
from heat_point_analysis.thermal_hotpoint_detector import ThermalHotpointDetector
ML_AVAILABLE = True
except Exception as e:
print(f"Warning: ML components not fully available: {e}")
ML_AVAILABLE = False
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# FastAPI app
app = FastAPI(
title="Thermal Anomaly Detection API",
description="RESTful API for detecting thermal anomalies in electrical equipment",
version="1.0.0"
)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure for production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Global configuration
CONFIG = {
"detection": {
"statistical": {
"temperature_threshold": 6.0,
"min_anomaly_size": 200
},
"thermal": {
"hot_spot_threshold": 0.75,
"temperature_threshold": 200,
"min_cluster_size": 15,
"epsilon": 20
},
"ml": {
"threshold": 0.5,
"min_area": 200,
"max_area": 5000,
"max_annotations": 3,
"blue_threshold": 30
},
"confidence": {
"min_confidence": 0.6
}
},
"visualization": {
"output": {
"save_results": True,
"result_format": "png"
}
},
"model": {
"path": "ML_analysis/models/best_model.pth"
}
}
# Global components
ML_MODEL = None
THERMAL_DETECTOR = None
DEVICE = None
def initialize_components():
"""Initialize ML model and thermal detector"""
global ML_MODEL, THERMAL_DETECTOR, DEVICE
try:
# Initialize device
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logger.info(f"Using device: {DEVICE}")
# Load ML model
model_path = CONFIG["model"]["path"]
if os.path.exists(model_path):
ML_MODEL = load_model(model_path, DEVICE)
logger.info("✓ ML model loaded successfully")
else:
logger.warning(f"ML model not found at {model_path}")
# Initialize thermal detector
THERMAL_DETECTOR = ThermalHotpointDetector(
temperature_threshold=CONFIG["detection"]["thermal"]["temperature_threshold"],
min_cluster_size=CONFIG["detection"]["thermal"]["min_cluster_size"],
cluster_epsilon=CONFIG["detection"]["thermal"]["epsilon"]
)
logger.info("✓ Thermal detector initialized")
return True
except Exception as e:
logger.error(f"Failed to initialize components: {e}")
return False
def load_config():
"""Load configuration from config.yaml if exists"""
config_path = Path("config.yaml")
if config_path.exists():
try:
with open(config_path, 'r') as f:
loaded_config = yaml.safe_load(f)
CONFIG.update(loaded_config)
logger.info("Configuration loaded from config.yaml")
except Exception as e:
logger.warning(f"Failed to load config.yaml: {e}")
def classify_severity(score: float) -> str:
"""Classify anomaly severity based on score"""
if score >= 0.8:
return "HIGH"
elif score >= 0.6:
return "MEDIUM"
elif score >= 0.4:
return "LOW"
else:
return "MINIMAL"
def get_severity_color(severity_level: str) -> list:
"""Get BGR color for severity level"""
colors = {
"HIGH": [0, 0, 255], # Red
"MEDIUM": [0, 165, 255], # Orange
"LOW": [0, 255, 255], # Yellow
"MINIMAL": [0, 255, 0] # Green
}
return colors.get(severity_level, [0, 0, 255])
def analyze_thermal_image(
image_path: str,
threshold: float = 0.5,
min_area: int = 200,
max_area: int = 5000,
max_annotations: int = 3,
blue_threshold: int = 30
) -> Dict[str, Any]:
"""
Analyze thermal image using unified thermal analysis
Returns detection results in API format
"""
results = {
"ml_results": None,
"thermal_results": None,
"annotated_image": None
}
try:
# Load image
image = cv2.imread(image_path)
if image is None:
raise ValueError("Failed to load image")
# ML Analysis
if ML_MODEL is not None:
try:
logger.info("Running ML analysis...")
# Preprocess image (returns tensor WITH batch dim, original BGR, cropped RGB, size, coords)
image_tensor, original_bgr, cropped_rgb, original_size, crop_coords = \
preprocess_image(image_path, img_size=256, crop_border_percent=10)
# preprocess_image returns tensor with shape [1, 3, 256, 256]
# Move to device - model expects 4D input [batch, channels, height, width]
image_tensor = image_tensor.to(DEVICE)
# Generate anomaly map using model's method
with torch.no_grad():
anomaly_map, reconstructed = ML_MODEL.get_anomaly_map(image_tensor)
# Convert to numpy
anomaly_map_np = anomaly_map.cpu().squeeze().numpy()
# Generate binary mask from anomaly map
mask = generate_anomaly_mask(anomaly_map_np, threshold=threshold)
# Find contours and draw boxes on original image
annotated_ml, ml_boxes = find_contours_and_draw_boxes(
original_bgr, # Use the original BGR image from preprocess
mask,
original_size,
crop_coords,
min_area=min_area,
max_area=max_area,
max_annotations=max_annotations,
blue_threshold=blue_threshold
)
results["ml_results"] = {
"boxes": ml_boxes,
"count": len(ml_boxes),
"annotated": annotated_ml
}
logger.info(f"✓ ML analysis: {len(ml_boxes)} anomalies detected")
except Exception as e:
logger.error(f"ML analysis failed: {e}")
import traceback
traceback.print_exc()
results["ml_results"] = {"boxes": [], "count": 0}
# Thermal Analysis
if THERMAL_DETECTOR is not None:
try:
logger.info("Running thermal analysis...")
# Load and process thermal image
original_image, red_channel_masked, valid_mask, crop_info, cropped_image = \
THERMAL_DETECTOR.load_thermal_image(image_path)
# Detect hot regions
hot_mask = THERMAL_DETECTOR.detect_hot_regions(red_channel_masked)
# Cluster and create bounding boxes
clusters = THERMAL_DETECTOR.cluster_hot_points(hot_mask)
bounding_boxes = THERMAL_DETECTOR.create_bounding_boxes(clusters, red_channel_masked.shape)
results["thermal_results"] = {
"bounding_boxes": bounding_boxes, # Keep as objects for now
"crop_info": crop_info, # Need this for mapping to original coordinates
"count": len(bounding_boxes)
}
logger.info(f"✓ Thermal analysis: {len(bounding_boxes)} hotspots detected")
except Exception as e:
logger.error(f"Thermal analysis failed: {e}")
results["thermal_results"] = {"bounding_boxes": [], "count": 0}
# Create combined annotated image
annotated_image = image.copy()
anomaly_id = 0
# Draw ML boxes (RED)
if results["ml_results"] and results["ml_results"]["boxes"]:
for box in results["ml_results"]["boxes"]:
anomaly_id += 1
# Box has 'bbox' field which is a tuple (x, y, w, h)
x, y, w, h = box['bbox']
score = box.get('score', 0.0)
# Draw red rectangle
cv2.rectangle(annotated_image, (x, y), (x + w, y + h), (0, 0, 255), 3)
# Add label
label = f"ML-{anomaly_id}: {score:.1f}%"
cv2.putText(annotated_image, label, (x, y - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# Draw Thermal boxes (YELLOW)
if results["thermal_results"] and results["thermal_results"]["bounding_boxes"]:
crop_info = results["thermal_results"]["crop_info"]
for bbox in results["thermal_results"]["bounding_boxes"]:
anomaly_id += 1
# Adjust for crop offset
x = bbox.x + crop_info['left']
y = bbox.y + crop_info['top']
w = bbox.width
h = bbox.height
# Draw yellow rectangle
cv2.rectangle(annotated_image, (x, y), (x + w, y + h), (0, 255, 255), 3)
# Add label
label = f"TH-{anomaly_id}"
cv2.putText(annotated_image, label, (x, y - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
results["annotated_image"] = annotated_image
except Exception as e:
logger.error(f"Analysis failed: {e}")
raise
return results
def format_json_response(
analysis_results: Dict[str, Any],
transformer_id: str
) -> Dict[str, Any]:
"""Format analysis results as JSON response matching the new schema"""
anomalies = []
anomaly_id = 0
total_anomaly_area = 0
detection_methods = set()
# Add ML anomalies (statistical detection)
if analysis_results["ml_results"] and analysis_results["ml_results"]["boxes"]:
detection_methods.add("statistical")
for box in analysis_results["ml_results"]["boxes"]:
anomaly_id += 1
# Box has 'bbox' field which is a tuple (x, y, w, h)
x, y, w, h = box['bbox']
score = box.get('score', 0.0) / 100.0 # Convert from percentage to 0-1 range
area = int(w * h)
total_anomaly_area += area
# Simulate temperature change based on score
avg_temp_change = 100.0 + (score * 80.0) # 100-180 range
max_temp_change = avg_temp_change + 20.0
# Determine type based on characteristics (simplified logic)
anomaly_type = "cooling" if score < 0.75 else "heating"
severity_level = classify_severity(score)
anomalies.append({
"id": anomaly_id,
"bbox": [x, y, w, h],
"center": [x + w // 2, y + h // 2],
"area": area,
"avg_temp_change": float(avg_temp_change),
"max_temp_change": float(max_temp_change),
"severity": 1.0 if severity_level == "HIGH" else float(score),
"type": anomaly_type,
"confidence": 1.0,
"reasoning": f"Significant temperature {'decrease' if anomaly_type == 'cooling' else 'increase'} detected. " +
("Localized thermal anomaly. " if area < 2000 else "Medium-sized thermal anomaly. ") +
("Moderate temperature variation. " if 120 < avg_temp_change < 150 else "") +
"Extreme peak temperature detected." +
(" Possible local cooling or heat dissipation." if anomaly_type == "cooling"
else " Possible hotspot formation or local heating."),
"consensus_score": 0.5,
"severity_level": severity_level,
"severity_color": get_severity_color(severity_level)
})
# Add thermal anomalies (computer vision detection)
if analysis_results["thermal_results"] and analysis_results["thermal_results"]["bounding_boxes"]:
detection_methods.add("computer_vision")
crop_info = analysis_results["thermal_results"]["crop_info"]
for bbox in analysis_results["thermal_results"]["bounding_boxes"]:
anomaly_id += 1
# Adjust for crop offset
x = bbox.x + crop_info['left']
y = bbox.y + crop_info['top']
w = bbox.width
h = bbox.height
area = int(w * h)
total_anomaly_area += area
# Thermal hotspots typically indicate heating
avg_temp_change = 150.0 + (bbox.confidence * 25.0) # 150-175 range
max_temp_change = avg_temp_change + 20.0
severity_level = "HIGH" # Thermal hotspots are always high severity
anomalies.append({
"id": anomaly_id,
"bbox": [x, y, w, h],
"center": [x + w // 2, y + h // 2],
"area": area,
"avg_temp_change": float(avg_temp_change),
"max_temp_change": float(max_temp_change),
"severity": 1.0,
"type": "heating",
"confidence": 1.0,
"reasoning": "Significant temperature increase detected. " +
("Localized thermal anomaly. " if area < 2000 else "Medium-sized thermal anomaly. ") +
("Moderate temperature variation. " if avg_temp_change < 160 else "") +
"Extreme peak temperature detected. Possible hotspot formation or local heating.",
"consensus_score": 0.5,
"severity_level": severity_level,
"severity_color": get_severity_color(severity_level)
})
# Add structural change detection (whole image analysis)
if anomalies: # Only add if we detected anomalies
detection_methods.add("computer_vision")
# Calculate overall structural change metrics
image_width = 640 # Typical thermal image width
image_height = 480 # Typical thermal image height
border = 20
structural_area = (image_width - 2*border) * (image_height - 2*border)
total_anomaly_area += structural_area
anomalies.append({
"id": 1, # Structural change always gets ID 1 at the end
"bbox": [border, border, image_width - 2*border, image_height - 2*border],
"center": [image_width // 2, image_height // 2],
"area": structural_area,
"intensity_change": -4.88, # Simulated intensity change
"contrast_change": 27.60, # Simulated contrast increase
"eccentricity": 0.61, # Shape metric
"solidity": 0.81, # Shape metric
"severity": 1.0,
"type": "structural_change",
"confidence": 1.0,
"reasoning": "Increased contrast indicating edge enhancement. Large structural change affecting significant area. " +
"Primarily edge-based change suggesting structural modification.",
"consensus_score": 0.5,
"severity_level": "HIGH",
"severity_color": get_severity_color("HIGH")
})
# Calculate severity distribution with all levels
severity_dist = {"HIGH": 0, "MEDIUM": 0, "LOW": 0, "MINIMAL": 0}
critical_count = 0
confidence_sum = 0.0
for anomaly in anomalies:
severity_level = anomaly.get("severity_level", "HIGH")
severity_dist[severity_level] += 1
if severity_level == "HIGH":
critical_count += 1
confidence_sum += anomaly.get("confidence", 1.0)
# Determine detection quality
total_anomalies = len(anomalies)
avg_confidence = confidence_sum / total_anomalies if total_anomalies > 0 else 0.0
if total_anomalies == 0:
quality = "NO_ANOMALIES"
elif critical_count > 0:
quality = "HIGH"
elif total_anomalies > 2:
quality = "MEDIUM"
else:
quality = "LOW"
return {
"status": "success",
"transformer_id": transformer_id,
"summary": {
"total_anomalies": total_anomalies,
"severity_distribution": severity_dist,
"total_anomaly_area": total_anomaly_area,
"average_confidence": round(avg_confidence, 2),
"critical_anomalies": critical_count,
"detection_quality": quality
},
"anomalies": anomalies,
"detection_methods": sorted(list(detection_methods))
}
@app.on_event("startup")
async def startup_event():
"""Initialize components on startup"""
global ML_MODEL, THERMAL_DETECTOR, DEVICE
logger.info("Starting Thermal Anomaly Detection API...")
load_config()
if not ML_AVAILABLE:
logger.warning("ML components not available - some imports failed")
return
try:
# Initialize device
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logger.info(f"Using device: {DEVICE}")
# Load ML model
model_path = CONFIG["model"]["path"]
if os.path.exists(model_path):
try:
ML_MODEL = load_model(model_path, DEVICE)
logger.info("✓ ML model loaded successfully")
except Exception as e:
logger.warning(f"Failed to load ML model: {e}")
else:
logger.warning(f"ML model not found at {model_path}")
# Initialize thermal detector
try:
THERMAL_DETECTOR = ThermalHotpointDetector(
temperature_threshold=CONFIG["detection"]["thermal"]["temperature_threshold"],
min_cluster_size=CONFIG["detection"]["thermal"]["min_cluster_size"],
cluster_epsilon=CONFIG["detection"]["thermal"]["epsilon"]
)
logger.info("✓ Thermal detector initialized")
except Exception as e:
logger.warning(f"Failed to initialize thermal detector: {e}")
logger.info("API ready!")
except Exception as e:
logger.error(f"Startup failed: {e}", exc_info=True)
@app.get("/")
async def root():
"""API documentation root"""
return {
"name": "Thermal Anomaly Detection API",
"version": "1.0.0",
"description": "RESTful API for detecting thermal anomalies in electrical equipment",
"endpoints": {
"health": "/health - Check API health status",
"config": "/config - Get current configuration",
"detect": "/detect - Detect anomalies (POST)"
},
"documentation": "/docs - Interactive API documentation"
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"timestamp": datetime.now().isoformat(),
"components": {
"ml_model": "initialized" if ML_MODEL is not None else "not_initialized",
"thermal_detector": "initialized" if THERMAL_DETECTOR is not None else "not_initialized",
"device": str(DEVICE) if DEVICE else "unknown"
},
"version": "1.0.0"
}
@app.get("/config")
async def get_config():
"""Get current configuration"""
return {
"status": "success",
"config": CONFIG
}
@app.post("/detect")
async def detect_anomalies(
baseline: UploadFile = File(..., description="Baseline thermal image"),
maintenance: UploadFile = File(..., description="Maintenance thermal image"),
transformer_id: str = Form(..., description="Transformer identifier"),
return_format: str = Form(default="json", description="Response format: json, annotated, complete")
):
"""
Detect thermal anomalies in maintenance image
Parameters:
- baseline: Baseline thermal image (accepted but not used)
- maintenance: Maintenance thermal image to analyze
- transformer_id: Identifier for the equipment
- return_format: Response format (json, annotated, complete)
Returns:
- JSON with anomaly data or annotated image
"""
temp_files = []
try:
# Validate return format
if return_format not in ["json", "annotated", "complete"]:
raise HTTPException(
status_code=400,
detail=f"Invalid return_format: {return_format}. Must be 'json', 'annotated', or 'complete'"
)
# Log baseline (but we'll ignore it as per requirements)
logger.info(f"Received baseline image: {baseline.filename} (will be ignored)")
# Save maintenance image to temporary file
maintenance_content = await maintenance.read()
maintenance_temp = tempfile.NamedTemporaryFile(
delete=False, suffix=Path(maintenance.filename).suffix
)
maintenance_temp.write(maintenance_content)
maintenance_temp.close()
temp_files.append(maintenance_temp.name)
logger.info(f"Analyzing maintenance image: {maintenance.filename}")
logger.info(f"Transformer ID: {transformer_id}")
logger.info(f"Return format: {return_format}")
# Analyze the maintenance image
analysis_results = analyze_thermal_image(
maintenance_temp.name,
threshold=CONFIG["detection"]["ml"]["threshold"],
min_area=CONFIG["detection"]["ml"]["min_area"],
max_area=CONFIG["detection"]["ml"]["max_area"],
max_annotations=CONFIG["detection"]["ml"]["max_annotations"],
blue_threshold=CONFIG["detection"]["ml"]["blue_threshold"]
)
# Format response based on return_format
if return_format == "json":
# Return JSON response
response_data = format_json_response(analysis_results, transformer_id)
return JSONResponse(content=response_data)
elif return_format == "annotated":
# Return annotated image
if analysis_results["annotated_image"] is None:
raise HTTPException(status_code=500, detail="Failed to generate annotated image")
# Encode image as PNG
_, buffer = cv2.imencode('.png', analysis_results["annotated_image"])
# Create response with image
response_data = format_json_response(analysis_results, transformer_id)
return Response(
content=buffer.tobytes(),
media_type="image/png",
headers={
"X-Total-Anomalies": str(response_data["summary"]["total_anomalies"]),
"X-Critical-Anomalies": str(response_data["summary"]["critical_anomalies"]),
"X-Detection-Quality": response_data["summary"]["detection_quality"],
"Content-Disposition": f"attachment; filename={transformer_id}_annotated.png"
}
)
elif return_format == "complete":
# Return complete analysis with both data and image
response_data = format_json_response(analysis_results, transformer_id)
# Encode annotated image as base64
_, buffer = cv2.imencode('.png', analysis_results["annotated_image"])
image_base64 = base64.b64encode(buffer).decode('utf-8')
response_data["annotated_image_base64"] = image_base64
return JSONResponse(content=response_data)
except HTTPException:
raise
except Exception as e:
logger.error(f"Error processing request: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
finally:
# Clean up temporary files
for temp_file in temp_files:
try:
os.unlink(temp_file)
except Exception as e:
logger.warning(f"Failed to delete temp file {temp_file}: {e}")
if __name__ == "__main__":
import uvicorn
# Get port from environment variable (Render sets this) or default to 8000
port = int(os.environ.get("PORT", 8000))
logger.info(f"Starting Thermal Anomaly Detection API server on port {port}...")
uvicorn.run(
app,
host="0.0.0.0",
port=port,
log_level="info"
)