from fastapi import FastAPI, UploadFile, File, HTTPException from models.object_detection import detect_faults_from_huggingface from models.thermal_anomaly_detection import detect_thermal_anomalies from models.energy_anomaly_detection import detect_energy_anomalies import cv2 import numpy as np import os app = FastAPI() # Ensure the data directory exists os.makedirs('data', exist_ok=True) @app.post("/detect_faults/") async def detect_faults_from_video(file: UploadFile = File(...)): try: # Save the uploaded image for object detection file_location = f"data/{file.filename}" with open(file_location, "wb") as buffer: buffer.write(await file.read()) # Run object detection on image using Hugging Face model result = detect_faults_from_huggingface(file_location) return {"faults_detected": result} except Exception as e: raise HTTPException(status_code=500, detail=f"Error processing fault detection: {str(e)}") @app.post("/detect_thermal_anomalies/") async def detect_thermal_anomalies_from_image(file: UploadFile = File(...)): try: # Save the uploaded thermal image file_location = f"data/{file.filename}" with open(file_location, "wb") as buffer: buffer.write(await file.read()) # Load image and run thermal anomaly detection result = detect_thermal_anomalies(file_location) return {"thermal_anomalies": result} except Exception as e: raise HTTPException(status_code=500, detail=f"Error processing thermal anomaly detection: {str(e)}") @app.post("/detect_energy_anomalies/") async def detect_energy_anomalies_from_data(file: UploadFile = File(...)): try: # Save energy data file (CSV, JSON, etc.) file_location = f"data/{file.filename}" with open(file_location, "wb") as buffer: buffer.write(await file.read()) # Run energy anomaly detection result = detect_energy_anomalies(file_location) return {"energy_anomalies": result} except Exception as e: raise HTTPException(status_code=500, detail=f"Error processing energy anomaly detection: {str(e)}")