Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import DetrImageProcessor, DetrForObjectDetection | |
import torch | |
import cv2 | |
import numpy as np | |
from PIL import Image | |
# Load Hugging Face object detection model | |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") | |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") | |
def detect_intrusion(video_path): | |
cap = cv2.VideoCapture(video_path) | |
alerts = [] | |
count = 0 | |
while cap.isOpened() and count < 20: | |
ret, frame = cap.read() | |
if not ret: | |
break | |
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) | |
inputs = processor(images=image, return_tensors="pt") | |
outputs = model(**inputs) | |
target_sizes = torch.tensor([image.size[::-1]]) | |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0] | |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): | |
label_name = model.config.id2label[label.item()] | |
if label_name == "person": | |
alerts.append(f"Frame {count}: π΄ Person Detected!") | |
break | |
count += 1 | |
cap.release() | |
return "\n".join(alerts) if alerts else "β No intrusion detected." | |
def detect_overheat(temp, humidity, solar_output): | |
if temp > 75: | |
return "π₯ Overheat Fault!" | |
elif humidity < 20 and solar_output < 300: | |
return "π«οΈ Dust/Shade Fault!" | |
else: | |
return "β All Good" | |
video_tab = gr.Interface(fn=detect_intrusion, | |
inputs=gr.Video(label="Upload Video"), | |
outputs=gr.Textbox(label="Intrusion Detection Alerts")) | |
sensor_tab = gr.Interface(fn=detect_overheat, | |
inputs=[gr.Number(label="Temperature (Β°C)"), | |
gr.Number(label="Humidity (%)"), | |
gr.Number(label="Solar Output (W)")], | |
outputs=gr.Textbox(label="Sensor Fault Detection")) | |
gr.TabbedInterface([video_tab, sensor_tab], ["Intrusion (Video)", "Sensor (Input)"]).launch() | |