Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from models.object_detection import detect_faults
|
3 |
+
from models.thermal_anomaly_detection import detect_thermal_anomalies
|
4 |
+
from models.energy_anomaly_detection import detect_energy_anomalies
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
@app.post("/detect_faults/")
|
11 |
+
async def detect_faults_from_video(file: UploadFile = File(...)):
|
12 |
+
# Save the uploaded file temporarily
|
13 |
+
file_location = f"data/{file.filename}"
|
14 |
+
with open(file_location, "wb") as buffer:
|
15 |
+
buffer.write(file.file.read())
|
16 |
+
|
17 |
+
# Read video or image for object detection
|
18 |
+
video = cv2.VideoCapture(file_location)
|
19 |
+
result = detect_faults(video)
|
20 |
+
return {"faults_detected": result}
|
21 |
+
|
22 |
+
@app.post("/detect_thermal_anomalies/")
|
23 |
+
async def detect_thermal_anomalies_from_image(file: UploadFile = File(...)):
|
24 |
+
# Save the uploaded thermal image
|
25 |
+
file_location = f"data/{file.filename}"
|
26 |
+
with open(file_location, "wb") as buffer:
|
27 |
+
buffer.write(file.file.read())
|
28 |
+
|
29 |
+
# Load image and run thermal anomaly detection
|
30 |
+
result = detect_thermal_anomalies(file_location)
|
31 |
+
return {"thermal_anomalies": result}
|
32 |
+
|
33 |
+
@app.post("/detect_energy_anomalies/")
|
34 |
+
async def detect_energy_anomalies_from_data(file: UploadFile = File(...)):
|
35 |
+
# Save energy data file (CSV, JSON, etc.)
|
36 |
+
file_location = f"data/{file.filename}"
|
37 |
+
with open(file_location, "wb") as buffer:
|
38 |
+
buffer.write(file.file.read())
|
39 |
+
|
40 |
+
# Run energy anomaly detection
|
41 |
+
result = detect_energy_anomalies(file_location)
|
42 |
+
return {"energy_anomalies": result}
|