Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
3 |
+
from PIL import Image
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
import time
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Device setup (GPU or CPU)
|
10 |
+
device = 'cpu'
|
11 |
+
if torch.cuda.is_available():
|
12 |
+
device = torch.device('cuda')
|
13 |
+
elif torch.backends.mps.is_available():
|
14 |
+
device = torch.device('mps')
|
15 |
+
|
16 |
+
# Load pre-trained model and image processor from Hugging Face
|
17 |
+
ckpt = 'yainage90/fashion-object-detection'
|
18 |
+
image_processor = AutoImageProcessor.from_pretrained(ckpt)
|
19 |
+
model = AutoModelForObjectDetection.from_pretrained(ckpt).to(device)
|
20 |
+
|
21 |
+
def detect_objects(frame):
|
22 |
+
"""Detect objects in the video frame."""
|
23 |
+
# Convert the frame to PIL image
|
24 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
25 |
+
|
26 |
+
# Prepare inputs for the model
|
27 |
+
with torch.no_grad():
|
28 |
+
inputs = image_processor(images=[image], return_tensors="pt")
|
29 |
+
outputs = model(**inputs.to(device))
|
30 |
+
target_sizes = torch.tensor([[image.size[1], image.size[0]]])
|
31 |
+
results = image_processor.post_process_object_detection(outputs, threshold=0.4, target_sizes=target_sizes)[0]
|
32 |
+
|
33 |
+
# Extract the detected items
|
34 |
+
items = []
|
35 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
36 |
+
score = score.item()
|
37 |
+
label = label.item()
|
38 |
+
box = [i.item() for i in box]
|
39 |
+
print(f"{model.config.id2label[label]}: {round(score, 3)} at {box}")
|
40 |
+
items.append((score, label, box))
|
41 |
+
|
42 |
+
return items
|
43 |
+
|
44 |
+
def process_image(image):
|
45 |
+
"""Process the image uploaded via Gradio and return the result."""
|
46 |
+
# Convert the image to numpy array
|
47 |
+
frame = np.array(image)
|
48 |
+
|
49 |
+
# Detect objects (e.g., helmets) in the frame
|
50 |
+
items = detect_objects(frame)
|
51 |
+
|
52 |
+
if items: # If objects are detected, save the data
|
53 |
+
save_data(frame, items)
|
54 |
+
|
55 |
+
return {"items_detected": items}
|
56 |
+
|
57 |
+
def save_data(frame, items):
|
58 |
+
"""Save image and extract plate number."""
|
59 |
+
filename = f"helmet_violation_{int(time.time())}.jpg"
|
60 |
+
cv2.imwrite(filename, frame)
|
61 |
+
|
62 |
+
# Here, you'd extract plate numbers or process further
|
63 |
+
plate_number = extract_plate_number(frame)
|
64 |
+
save_to_database(filename, plate_number, items)
|
65 |
+
|
66 |
+
def extract_plate_number(frame):
|
67 |
+
"""Extract license plate number (simplified)."""
|
68 |
+
plate_number = "XYZ 1234" # Replace with an actual license plate recognition method
|
69 |
+
return plate_number
|
70 |
+
|
71 |
+
def save_to_database(image_filename, plate_number, items):
|
72 |
+
"""Save the data (for simplicity, we just print it here)."""
|
73 |
+
print(f"Plate Number: {plate_number}, Image saved as {image_filename}")
|
74 |
+
print("Detected items:", items)
|
75 |
+
|
76 |
+
# Define the Gradio interface
|
77 |
+
interface = gr.Interface(fn=process_image,
|
78 |
+
inputs=gr.inputs.Image(type="pil"),
|
79 |
+
outputs=gr.outputs.JSON(),
|
80 |
+
live=True)
|
81 |
+
|
82 |
+
# Launch the Gradio app
|
83 |
+
interface.launch(debug=True)
|