vikramjeetthakur commited on
Commit
3ae443a
·
verified ·
1 Parent(s): 5728a5f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import DetrImageProcessor, DetrForObjectDetection, TrOCRProcessor, VisionEncoderDecoderModel
2
+ import cv2
3
+ from PIL import Image, ImageDraw
4
+ import torch
5
+ import streamlit as st
6
+
7
+ # Load Hugging Face Models
8
+ detr_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
9
+ detr_model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
10
+ trocr_processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
11
+ trocr_model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
12
+
13
+ # Detect license plates
14
+ def detect_license_plate(frame):
15
+ pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
16
+ inputs = detr_processor(images=pil_image, return_tensors="pt")
17
+ outputs = detr_model(**inputs)
18
+
19
+ target_sizes = torch.tensor([pil_image.size[::-1]])
20
+ results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)
21
+
22
+ return results[0]["boxes"], pil_image
23
+
24
+ # Recognize text
25
+ def recognize_text_from_plate(cropped_plate):
26
+ inputs = trocr_processor(images=cropped_plate, return_tensors="pt")
27
+ outputs = trocr_model.generate(**inputs)
28
+ return trocr_processor.batch_decode(outputs, skip_special_tokens=True)[0]
29
+
30
+
31
+
32
+
33
+ # Streamlit configuration
34
+ st.title("Real-Time Car Number Plate Recognition")
35
+ st.text("This application uses Hugging Face Transformers to detect and recognize car plates.")
36
+
37
+ # Authorized car database
38
+ authorized_cars = {"KA01AB1234", "MH12XY5678", "DL8CAF9090"}
39
+
40
+ # Verification function
41
+ def verify_plate(plate_text):
42
+ if plate_text in authorized_cars:
43
+ return f"✅ Access Granted: {plate_text}"
44
+ else:
45
+ return f"❌ Access Denied: {plate_text}"
46
+
47
+
48
+ # Live video feed and processing
49
+ def live_feed():
50
+ cap = cv2.VideoCapture(0) # Open the webcam
51
+ stframe = st.empty() # Streamlit frame for displaying video
52
+
53
+ while cap.isOpened():
54
+ ret, frame = cap.read()
55
+ if not ret:
56
+ break
57
+
58
+ # Detect license plates
59
+ boxes, pil_image = detect_license_plate(frame)
60
+ draw = ImageDraw.Draw(pil_image)
61
+
62
+ recognized_plates = []
63
+ for box in boxes:
64
+ # Crop the detected plate
65
+ cropped_plate = pil_image.crop((box[0], box[1], box[2], box[3]))
66
+
67
+ # Recognize text
68
+ plate_text = recognize_text_from_plate(cropped_plate)
69
+ recognized_plates.append(plate_text)
70
+
71
+ # Draw bounding box and text
72
+ draw.rectangle(box.tolist(), outline="red", width=2)
73
+ draw.text((box[0], box[1]), plate_text, fill="red")
74
+
75
+ # Convert PIL image back to OpenCV format
76
+ processed_frame = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
77
+
78
+ # Stream the video to Streamlit
79
+ stframe.image(processed_frame, channels="BGR")
80
+
81
+ # Show results
82
+ for plate_text in recognized_plates:
83
+ st.write(verify_plate(plate_text))
84
+
85
+ cap.release()
86
+ cv2.destroyAllWindows()
87
+
88
+ if st.button("Start Camera"):
89
+ live_feed()
90
+