Spaces:
Runtime error
Runtime error
Create thermal_anomaly_detection.py
Browse files
models/thermal_anomaly_detection.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torchvision import models, transforms
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load a pretrained ResNet50 model for thermal anomaly detection
|
7 |
+
model = models.resnet50(pretrained=True)
|
8 |
+
model.eval()
|
9 |
+
|
10 |
+
def detect_thermal_anomalies(image_path):
|
11 |
+
"""
|
12 |
+
Detect thermal anomalies in solar panels using thermal images.
|
13 |
+
Args:
|
14 |
+
- image_path (str): Path to the thermal image file
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
- anomaly (str): Description of the detected thermal anomaly
|
18 |
+
"""
|
19 |
+
# Image preprocessing for ResNet
|
20 |
+
transform = transforms.Compose([
|
21 |
+
transforms.Resize(256),
|
22 |
+
transforms.CenterCrop(224),
|
23 |
+
transforms.ToTensor(),
|
24 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
25 |
+
])
|
26 |
+
image = Image.open(image_path)
|
27 |
+
image_tensor = transform(image).unsqueeze(0)
|
28 |
+
|
29 |
+
with torch.no_grad():
|
30 |
+
output = model(image_tensor)
|
31 |
+
_, predicted_class = torch.max(output, 1)
|
32 |
+
|
33 |
+
# Simulate anomaly detection (you can replace this with actual anomaly labels)
|
34 |
+
anomaly = "Overheating detected" if predicted_class == 0 else "Normal condition"
|
35 |
+
return anomaly
|