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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -18,7 +18,6 @@ st.write("This app uses Hugging Face Transformers, OpenCV, and Streamlit for det
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))
@@ -30,14 +29,12 @@ def detect_license_plate(frame):
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:
@@ -45,15 +42,19 @@ def verify_plate(plate_text):
45
  else:
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
@@ -84,7 +85,6 @@ def live_feed():
84
  cap.release()
85
  cv2.destroyAllWindows()
86
 
87
-
88
  # Streamlit UI
89
  if st.button("Start Camera"):
90
  live_feed()
 
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(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
 
29
  results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)
30
  return results[0]["boxes"], pil_image
31
 
 
32
  # Recognize Text from Plates
33
  def recognize_text_from_plate(cropped_plate):
34
  inputs = trocr_processor(images=cropped_plate, return_tensors="pt")
35
  outputs = trocr_model.generate(**inputs)
36
  return trocr_processor.batch_decode(outputs, skip_special_tokens=True)[0]
37
 
 
38
  # Verify Plate Text
39
  def verify_plate(plate_text):
40
  if plate_text in authorized_cars:
 
42
  else:
43
  return f"❌ Access Denied: {plate_text}"
44
 
 
45
  # Real-Time Video Processing with OpenCV
46
  def live_feed():
47
  cap = cv2.VideoCapture(0) # Open webcam
48
+ if not cap.isOpened():
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
 
85
  cap.release()
86
  cv2.destroyAllWindows()
87
 
 
88
  # Streamlit UI
89
  if st.button("Start Camera"):
90
  live_feed()