Spaces:
Runtime error
Runtime error
| import torch # Import PyTorch for tensor operations | |
| import numpy as np # Import NumPy for array handling | |
| # Function to detect faults in solar panels | |
| def detect_faults_solar(model, image): | |
| img_tensor = torch.from_numpy(image).permute(2, 0, 1).float() / 255.0 # Convert image to tensor | |
| img_tensor = img_tensor.unsqueeze(0) # Add batch dimension | |
| results = model.predict(img_tensor, verbose=False) # Perform inference | |
| faults = [] # List to store detected faults | |
| for detection in results[0].boxes: # Iterate over detected objects | |
| class_id = int(detection.cls) | |
| if class_id == 0: | |
| faults.append({"type": "Crack", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())}) | |
| elif class_id == 1: | |
| faults.append({"type": "Damage", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())}) | |
| elif class_id == 4: | |
| faults.append({"type": "Hotspot", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())}) | |
| return faults # Return list of faults | |
| # Function to detect faults in windmills | |
| def detect_faults_windmill(model, image): | |
| img_tensor = torch.from_numpy(image).permute(2, 0, 1).float() / 255.0 # Convert image to tensor | |
| img_tensor = img_tensor.unsqueeze(0) # Add batch dimension | |
| results = model.predict(img_tensor, verbose=False) # Perform inference | |
| faults = [] # List to store detected faults | |
| for detection in results[0].boxes: # Iterate over detected objects | |
| class_id = int(detection.cls) | |
| if class_id == 2: | |
| faults.append({"type": "Blade Damage", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())}) | |
| elif class_id == 3: | |
| faults.append({"type": "Motor Fault", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())}) | |
| return faults # Return list of faults |