Hiridharan10's picture
Create app.py
fb12125 verified
raw
history blame
4.16 kB
# Smart Parking System - Gradio UI for Hugging Face Spaces
import torch
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from datetime import datetime, timedelta
import cv2
from transformers import AutoProcessor, AutoModelForCausalLM
import gradio as gr
import warnings
warnings.filterwarnings('ignore')
class StateOfTheArtParkingDetector:
def __init__(self, total_spaces=50):
self.total_spaces = total_spaces
self.device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"πŸš€ Loading State-of-the-Art Models... Device: {self.device}")
# Load Florence-2
try:
self.processor = AutoProcessor.from_pretrained(
"microsoft/Florence-2-large",
trust_remote_code=True
)
self.model = AutoModelForCausalLM.from_pretrained(
"microsoft/Florence-2-large",
torch_dtype=torch.float16 if self.device=="cuda" else torch.float32,
trust_remote_code=True
).to(self.device)
self.florence_available = True
except:
try:
self.processor = AutoProcessor.from_pretrained(
"microsoft/Florence-2-base",
trust_remote_code=True
)
self.model = AutoModelForCausalLM.from_pretrained(
"microsoft/Florence-2-base",
torch_dtype=torch.float16 if self.device=="cuda" else torch.float32,
trust_remote_code=True
).to(self.device)
self.florence_available = True
except:
self.florence_available = False
# Load YOLO-World
try:
from ultralytics import YOLO
self.yolo_world = YOLO('yolov8x-worldv2.pt')
self.yolo_world_available = True
except:
self.yolo_world_available = False
# Detection methods remain the same as your original code
# detect_with_florence, detect_with_yolo_world, advanced_cv_detection,
# merge_all_detections, calculate_iou, create_annotated_image, process_image
# Initialize detector
detector = StateOfTheArtParkingDetector()
# Gradio Interface
with gr.Blocks(
theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue"),
css="""
.gradio-container { font-family: 'Segoe UI', Arial, sans-serif; }
.main-header { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white; padding: 30px; border-radius: 10px; margin-bottom: 20px; }
"""
) as demo:
gr.HTML("""
<div class="main-header">
<h1 style="margin:0; font-size:42px;">πŸ…ΏοΈ Smart Parking Management System</h1>
<p style="margin:10px 0 0 0; font-size:18px; opacity:0.9;">Powered by AI Vision Transformers β€’ Real-time Detection</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### πŸ“€ Upload Parking Lot Image")
input_image = gr.Image(label="Parking Lot Image", type="pil", height=400)
total_spaces_slider = gr.Slider(minimum=10, maximum=200, value=50, step=5,
label="πŸ…ΏοΈ Total Parking Spaces",
info="Set the total capacity of your parking lot")
analyze_btn = gr.Button("πŸ” Analyze Parking Lot", variant="primary", size="lg")
with gr.Column(scale=1):
gr.Markdown("### 🎯 Detection Results")
output_image = gr.Image(label="Detected Vehicles", type="pil", height=400)
status_html = gr.HTML(label="Parking Status")
with gr.Row():
with gr.Column():
stats_output = gr.Textbox(label="πŸ“Š Detailed Statistics", lines=15, max_lines=20)
analyze_btn.click(fn=detector.process_image, inputs=[input_image, total_spaces_slider],
outputs=[output_image, status_html, stats_output])
demo.launch()