Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from PIL import Image, ImageDraw
|
5 |
from transformers import DetrImageProcessor, DetrForObjectDetection, TrOCRProcessor, VisionEncoderDecoderModel
|
|
|
6 |
import torch
|
|
|
7 |
|
8 |
# Load Models
|
9 |
detr_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
@@ -11,20 +12,14 @@ detr_model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
|
11 |
trocr_processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
12 |
trocr_model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
st.write("This app uses Hugging Face Transformers, OpenCV, and Streamlit for detecting and recognizing car number plates in real-time.")
|
17 |
-
|
18 |
-
# Authorized Car Database
|
19 |
-
authorized_cars = {"KA01AB1234", "MH12XY5678", "DL8CAF9090"} # Dummy data for verification
|
20 |
|
21 |
# Detect License Plates
|
22 |
def detect_license_plate(frame):
|
23 |
-
pil_image = Image.fromarray(
|
24 |
inputs = detr_processor(images=pil_image, return_tensors="pt")
|
25 |
outputs = detr_model(**inputs)
|
26 |
-
|
27 |
-
# Post-process outputs to get bounding boxes
|
28 |
target_sizes = torch.tensor([pil_image.size[::-1]])
|
29 |
results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)
|
30 |
return results[0]["boxes"], pil_image
|
@@ -35,56 +30,35 @@ def recognize_text_from_plate(cropped_plate):
|
|
35 |
outputs = trocr_model.generate(**inputs)
|
36 |
return trocr_processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
37 |
|
38 |
-
# Verify Plate
|
39 |
def verify_plate(plate_text):
|
40 |
if plate_text in authorized_cars:
|
41 |
return f"✅ Access Granted: {plate_text}"
|
42 |
else:
|
43 |
return f"❌ Access Denied: {plate_text}"
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
st.error("Unable to access the camera.")
|
50 |
-
return
|
51 |
-
|
52 |
-
stframe = st.image([]) # Placeholder for video feed
|
53 |
-
|
54 |
-
while True:
|
55 |
-
ret, frame = cap.read()
|
56 |
-
if not ret:
|
57 |
-
st.error("Failed to capture frame from the camera. Exiting...")
|
58 |
-
break
|
59 |
-
|
60 |
-
# Detect plates
|
61 |
boxes, pil_image = detect_license_plate(frame)
|
62 |
draw = ImageDraw.Draw(pil_image)
|
63 |
|
64 |
recognized_plates = []
|
65 |
for box in boxes:
|
66 |
-
# Crop and recognize plate
|
67 |
cropped_plate = pil_image.crop((box[0], box[1], box[2], box[3]))
|
68 |
plate_text = recognize_text_from_plate(cropped_plate)
|
69 |
recognized_plates.append(plate_text)
|
70 |
-
|
71 |
-
# Draw box and label
|
72 |
draw.rectangle(box.tolist(), outline="red", width=3)
|
73 |
draw.text((box[0], box[1]), plate_text, fill="red")
|
74 |
|
75 |
-
#
|
76 |
-
processed_frame =
|
77 |
-
|
78 |
-
# Stream video to Streamlit
|
79 |
-
stframe.image(processed_frame, channels="BGR", use_column_width=True)
|
80 |
-
|
81 |
-
# Display results
|
82 |
for plate_text in recognized_plates:
|
83 |
st.write(verify_plate(plate_text))
|
84 |
-
|
85 |
-
cap.release()
|
86 |
-
cv2.destroyAllWindows()
|
87 |
|
88 |
# Streamlit UI
|
89 |
-
|
90 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase
|
3 |
+
import av
|
|
|
4 |
from transformers import DetrImageProcessor, DetrForObjectDetection, TrOCRProcessor, VisionEncoderDecoderModel
|
5 |
+
from PIL import Image, ImageDraw
|
6 |
import torch
|
7 |
+
import numpy as np
|
8 |
|
9 |
# Load Models
|
10 |
detr_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
|
|
12 |
trocr_processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
13 |
trocr_model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
14 |
|
15 |
+
# Authorized car database
|
16 |
+
authorized_cars = {"KA01AB1234", "MH12XY5678", "DL8CAF9090"}
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Detect License Plates
|
19 |
def detect_license_plate(frame):
|
20 |
+
pil_image = Image.fromarray(frame)
|
21 |
inputs = detr_processor(images=pil_image, return_tensors="pt")
|
22 |
outputs = detr_model(**inputs)
|
|
|
|
|
23 |
target_sizes = torch.tensor([pil_image.size[::-1]])
|
24 |
results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)
|
25 |
return results[0]["boxes"], pil_image
|
|
|
30 |
outputs = trocr_model.generate(**inputs)
|
31 |
return trocr_processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
32 |
|
33 |
+
# Verify Plate
|
34 |
def verify_plate(plate_text):
|
35 |
if plate_text in authorized_cars:
|
36 |
return f"✅ Access Granted: {plate_text}"
|
37 |
else:
|
38 |
return f"❌ Access Denied: {plate_text}"
|
39 |
|
40 |
+
# Custom Video Processor
|
41 |
+
class LicensePlateProcessor(VideoProcessorBase):
|
42 |
+
def recv(self, frame: av.VideoFrame):
|
43 |
+
frame = frame.to_ndarray(format="bgr24")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
boxes, pil_image = detect_license_plate(frame)
|
45 |
draw = ImageDraw.Draw(pil_image)
|
46 |
|
47 |
recognized_plates = []
|
48 |
for box in boxes:
|
|
|
49 |
cropped_plate = pil_image.crop((box[0], box[1], box[2], box[3]))
|
50 |
plate_text = recognize_text_from_plate(cropped_plate)
|
51 |
recognized_plates.append(plate_text)
|
|
|
|
|
52 |
draw.rectangle(box.tolist(), outline="red", width=3)
|
53 |
draw.text((box[0], box[1]), plate_text, fill="red")
|
54 |
|
55 |
+
# Return processed frame
|
56 |
+
processed_frame = np.array(pil_image)
|
|
|
|
|
|
|
|
|
|
|
57 |
for plate_text in recognized_plates:
|
58 |
st.write(verify_plate(plate_text))
|
59 |
+
return av.VideoFrame.from_ndarray(processed_frame, format="bgr24")
|
|
|
|
|
60 |
|
61 |
# Streamlit UI
|
62 |
+
st.title("Real-Time Car Number Plate Recognition")
|
63 |
+
st.write("Streamlit with WebRTC for camera streaming.")
|
64 |
+
webrtc_streamer(key="plate-recognition", video_processor_factory=LicensePlateProcessor)
|