File size: 2,149 Bytes
cc6d392
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()