Spaces:
Runtime error
Runtime error
| import cv2 | |
| import gradio as gr | |
| import os | |
| import datetime | |
| import pandas as pd | |
| from PIL import Image | |
| from pathlib import Path | |
| import torch | |
| # π§ Setup | |
| os.makedirs("logs", exist_ok=True) | |
| # π§ Load YOLOv5 Model | |
| model = torch.hub.load('ultralytics/yolov5', 'yolov5s', trust_repo=True) | |
| # π Fake GPS Location | |
| def get_fake_gps_location(): | |
| return "28.6139Β° N, 77.2090Β° E" # Delhi (demo) | |
| # π Simulated Voice Alert (just returns text) | |
| def voice_alert(text): | |
| print(f"[VOICE ALERT] {text}") | |
| return f"π Voice: {text}" | |
| # π― Detection Function | |
| def detect_luggage(image_path, status_label): | |
| image = Image.open(image_path) | |
| results = model(image) | |
| annotated_image = results.render()[0] | |
| annotated_pil = Image.fromarray(annotated_image) | |
| # πΊοΈ Fake GPS | |
| gps = get_fake_gps_location() | |
| # π Voice Message | |
| alert_text = f"Luggage {status_label} at {gps}" | |
| voice = voice_alert(alert_text) | |
| # π Logging | |
| time_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| log_path = "luggage_log.csv" | |
| entry = {"timestamp": time_now, "status": status_label, "gps": gps} | |
| df = pd.DataFrame([entry]) | |
| if not os.path.exists(log_path): | |
| df.to_csv(log_path, index=False) | |
| else: | |
| df.to_csv(log_path, mode='a', header=False, index=False) | |
| # πΎ Save Image | |
| img_name = f"{status_label}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" | |
| annotated_pil.save(f"logs/{img_name}") | |
| return annotated_pil, f"{voice} | π Location: {gps}" | |
| # π€ Demo Function | |
| def run_demo(status_label): | |
| test_image_path = "assets/test_luggage.jpg" | |
| return detect_luggage(test_image_path, status_label) | |
| # π¨ Gradio UI | |
| demo = gr.Interface( | |
| fn=run_demo, | |
| inputs=gr.Radio(["Loaded", "Dispatched"], label="Select Luggage Status", value="Loaded"), | |
| outputs=[ | |
| gr.Image(label="Detected Luggage"), | |
| gr.Text(label="Detection Report") | |
| ], | |
| title="π Luggage Tracking with Voice + GPS", | |
| description="YOLO-based luggage detection with simulated GPS & voice alerts. Ready for Hugging Face!" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |