Sanjayraju30 commited on
Commit
84dba68
·
verified ·
1 Parent(s): 408922d

Create utils/detector.py

Browse files
Files changed (1) hide show
  1. utils/detector.py +29 -0
utils/detector.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import DetrImageProcessor, DetrForObjectDetection
2
+ from PIL import Image
3
+ import torch
4
+ import numpy as np
5
+
6
+ processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
7
+ model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
8
+
9
+ def detect_faults(image):
10
+ inputs = processor(images=image, return_tensors="pt")
11
+ outputs = model(**inputs)
12
+ target_sizes = torch.tensor([image.size[::-1]])
13
+ results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
14
+
15
+ intrusion_detected = any(label == 1 for label in results["labels"].tolist())
16
+
17
+ # Simulated thermal detection (average red channel > 200 = overheat)
18
+ red_mean = np.array(image)[:, :, 0].mean()
19
+ overheating = red_mean > 200
20
+
21
+ # Simulated shade (brightness < 100 on average = dusty/shaded)
22
+ brightness = np.array(image).mean()
23
+ dusty = brightness < 100
24
+
25
+ return {
26
+ "Intrusion Detected": intrusion_detected,
27
+ "Overheating Panel": overheating,
28
+ "Dust/Shade Fault": dusty
29
+ }