vikramjeetthakur commited on
Commit
e92cb9b
·
verified ·
1 Parent(s): 316033d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -31
app.py CHANGED
@@ -1,43 +1,44 @@
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}"
@@ -45,46 +46,45 @@ def verify_plate(plate_text):
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
 
 
 
 
 
1
+ import streamlit as st
2
  import cv2
3
+ import numpy as np
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")
10
  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
+ # Streamlit App Configuration
15
+ st.title("Real-Time Car Number Plate Recognition")
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
+
22
+ # Detect License Plates
23
  def detect_license_plate(frame):
24
  pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
25
  inputs = detr_processor(images=pil_image, return_tensors="pt")
26
  outputs = detr_model(**inputs)
27
 
28
+ # Post-process outputs to get bounding boxes
29
  target_sizes = torch.tensor([pil_image.size[::-1]])
30
  results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)
 
31
  return results[0]["boxes"], pil_image
32
 
33
+
34
+ # Recognize Text from Plates
35
  def recognize_text_from_plate(cropped_plate):
36
  inputs = trocr_processor(images=cropped_plate, return_tensors="pt")
37
  outputs = trocr_model.generate(**inputs)
38
  return trocr_processor.batch_decode(outputs, skip_special_tokens=True)[0]
39
 
40
 
41
+ # Verify Plate Text
 
 
 
 
 
 
 
 
 
42
  def verify_plate(plate_text):
43
  if plate_text in authorized_cars:
44
  return f"✅ Access Granted: {plate_text}"
 
46
  return f"❌ Access Denied: {plate_text}"
47
 
48
 
49
+ # Real-Time Video Processing with OpenCV
50
  def live_feed():
51
+ cap = cv2.VideoCapture(0) # Open webcam
52
+ stframe = st.empty() # Placeholder for video stream
53
 
54
  while cap.isOpened():
55
  ret, frame = cap.read()
56
  if not ret:
57
  break
58
 
59
+ # Detect plates
60
  boxes, pil_image = detect_license_plate(frame)
61
  draw = ImageDraw.Draw(pil_image)
62
 
63
  recognized_plates = []
64
  for box in boxes:
65
+ # Crop and recognize plate
66
  cropped_plate = pil_image.crop((box[0], box[1], box[2], box[3]))
 
 
67
  plate_text = recognize_text_from_plate(cropped_plate)
68
  recognized_plates.append(plate_text)
69
 
70
+ # Draw box and label
71
+ draw.rectangle(box.tolist(), outline="red", width=3)
72
  draw.text((box[0], box[1]), plate_text, fill="red")
73
 
74
+ # Convert back to OpenCV format
75
  processed_frame = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
76
 
77
+ # Stream video to Streamlit
78
+ stframe.image(processed_frame, channels="BGR", use_column_width=True)
79
 
80
+ # Display results
81
  for plate_text in recognized_plates:
82
  st.write(verify_plate(plate_text))
83
 
84
  cap.release()
85
  cv2.destroyAllWindows()
86
 
 
 
87
 
88
+ # Streamlit UI
89
+ if st.button("Start Camera"):
90
+ live_feed()