Hiridharan10 commited on
Commit
fb12125
Β·
verified Β·
1 Parent(s): 8f2e1a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Smart Parking System - Gradio UI for Hugging Face Spaces
2
+
3
+ import torch
4
+ import numpy as np
5
+ from PIL import Image
6
+ import matplotlib.pyplot as plt
7
+ import matplotlib.patches as patches
8
+ from datetime import datetime, timedelta
9
+ import cv2
10
+ from transformers import AutoProcessor, AutoModelForCausalLM
11
+ import gradio as gr
12
+ import warnings
13
+ warnings.filterwarnings('ignore')
14
+
15
+ class StateOfTheArtParkingDetector:
16
+ def __init__(self, total_spaces=50):
17
+ self.total_spaces = total_spaces
18
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
19
+
20
+ print(f"πŸš€ Loading State-of-the-Art Models... Device: {self.device}")
21
+
22
+ # Load Florence-2
23
+ try:
24
+ self.processor = AutoProcessor.from_pretrained(
25
+ "microsoft/Florence-2-large",
26
+ trust_remote_code=True
27
+ )
28
+ self.model = AutoModelForCausalLM.from_pretrained(
29
+ "microsoft/Florence-2-large",
30
+ torch_dtype=torch.float16 if self.device=="cuda" else torch.float32,
31
+ trust_remote_code=True
32
+ ).to(self.device)
33
+ self.florence_available = True
34
+ except:
35
+ try:
36
+ self.processor = AutoProcessor.from_pretrained(
37
+ "microsoft/Florence-2-base",
38
+ trust_remote_code=True
39
+ )
40
+ self.model = AutoModelForCausalLM.from_pretrained(
41
+ "microsoft/Florence-2-base",
42
+ torch_dtype=torch.float16 if self.device=="cuda" else torch.float32,
43
+ trust_remote_code=True
44
+ ).to(self.device)
45
+ self.florence_available = True
46
+ except:
47
+ self.florence_available = False
48
+
49
+ # Load YOLO-World
50
+ try:
51
+ from ultralytics import YOLO
52
+ self.yolo_world = YOLO('yolov8x-worldv2.pt')
53
+ self.yolo_world_available = True
54
+ except:
55
+ self.yolo_world_available = False
56
+
57
+ # Detection methods remain the same as your original code
58
+ # detect_with_florence, detect_with_yolo_world, advanced_cv_detection,
59
+ # merge_all_detections, calculate_iou, create_annotated_image, process_image
60
+
61
+ # Initialize detector
62
+ detector = StateOfTheArtParkingDetector()
63
+
64
+ # Gradio Interface
65
+ with gr.Blocks(
66
+ theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue"),
67
+ css="""
68
+ .gradio-container { font-family: 'Segoe UI', Arial, sans-serif; }
69
+ .main-header { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
70
+ color: white; padding: 30px; border-radius: 10px; margin-bottom: 20px; }
71
+ """
72
+ ) as demo:
73
+
74
+ gr.HTML("""
75
+ <div class="main-header">
76
+ <h1 style="margin:0; font-size:42px;">πŸ…ΏοΈ Smart Parking Management System</h1>
77
+ <p style="margin:10px 0 0 0; font-size:18px; opacity:0.9;">Powered by AI Vision Transformers β€’ Real-time Detection</p>
78
+ </div>
79
+ """)
80
+
81
+ with gr.Row():
82
+ with gr.Column(scale=1):
83
+ gr.Markdown("### πŸ“€ Upload Parking Lot Image")
84
+ input_image = gr.Image(label="Parking Lot Image", type="pil", height=400)
85
+
86
+ total_spaces_slider = gr.Slider(minimum=10, maximum=200, value=50, step=5,
87
+ label="πŸ…ΏοΈ Total Parking Spaces",
88
+ info="Set the total capacity of your parking lot")
89
+
90
+ analyze_btn = gr.Button("πŸ” Analyze Parking Lot", variant="primary", size="lg")
91
+
92
+ with gr.Column(scale=1):
93
+ gr.Markdown("### 🎯 Detection Results")
94
+ output_image = gr.Image(label="Detected Vehicles", type="pil", height=400)
95
+ status_html = gr.HTML(label="Parking Status")
96
+
97
+ with gr.Row():
98
+ with gr.Column():
99
+ stats_output = gr.Textbox(label="πŸ“Š Detailed Statistics", lines=15, max_lines=20)
100
+
101
+ analyze_btn.click(fn=detector.process_image, inputs=[input_image, total_spaces_slider],
102
+ outputs=[output_image, status_html, stats_output])
103
+
104
+ demo.launch()