Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,6 +25,9 @@ except Exception as e:
|
|
| 25 |
raise RuntimeError(f"Error initializing TrOCR model: {e}")
|
| 26 |
|
| 27 |
def detect_license_plate(frame):
|
|
|
|
|
|
|
|
|
|
| 28 |
# Convert the frame to a PIL image
|
| 29 |
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 30 |
|
|
@@ -32,7 +35,7 @@ def detect_license_plate(frame):
|
|
| 32 |
inputs = detr_processor(images=pil_image, return_tensors="pt")
|
| 33 |
outputs = detr_model(**inputs)
|
| 34 |
|
| 35 |
-
# Get detected objects and filter for license plates
|
| 36 |
logits = outputs.logits
|
| 37 |
boxes = outputs.pred_boxes
|
| 38 |
probas = logits.softmax(-1)[0, :, :-1]
|
|
@@ -47,6 +50,9 @@ def detect_license_plate(frame):
|
|
| 47 |
return detected_boxes
|
| 48 |
|
| 49 |
def recognize_text(plate_image):
|
|
|
|
|
|
|
|
|
|
| 50 |
# Convert the license plate image to a PIL image
|
| 51 |
pil_image = Image.fromarray(cv2.cvtColor(plate_image, cv2.COLOR_BGR2RGB))
|
| 52 |
|
|
@@ -57,17 +63,23 @@ def recognize_text(plate_image):
|
|
| 57 |
|
| 58 |
return text.strip()
|
| 59 |
|
| 60 |
-
def
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
vehicle_data = {}
|
| 65 |
-
|
|
|
|
| 66 |
while cap.isOpened():
|
| 67 |
ret, frame = cap.read()
|
| 68 |
if not ret:
|
| 69 |
break
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Detect license plates
|
| 72 |
detected_boxes = detect_license_plate(frame)
|
| 73 |
|
|
@@ -93,13 +105,13 @@ def main():
|
|
| 93 |
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
| 94 |
cv2.putText(frame, license_plate, (x_min, y_min-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 95 |
|
| 96 |
-
# Display the frame
|
| 97 |
cv2.imshow('Vehicle Detection', frame)
|
| 98 |
|
| 99 |
# Break on 'q' key press
|
| 100 |
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 101 |
break
|
| 102 |
-
|
| 103 |
cap.release()
|
| 104 |
cv2.destroyAllWindows()
|
| 105 |
|
|
@@ -109,4 +121,5 @@ def main():
|
|
| 109 |
print(f"License Plate: {plate}, Entry Time: {times['entry_time']}, Exit Time: {times['exit_time']}")
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
| 112 |
-
|
|
|
|
|
|
| 25 |
raise RuntimeError(f"Error initializing TrOCR model: {e}")
|
| 26 |
|
| 27 |
def detect_license_plate(frame):
|
| 28 |
+
"""
|
| 29 |
+
Detect license plates in a video frame using DETR.
|
| 30 |
+
"""
|
| 31 |
# Convert the frame to a PIL image
|
| 32 |
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 33 |
|
|
|
|
| 35 |
inputs = detr_processor(images=pil_image, return_tensors="pt")
|
| 36 |
outputs = detr_model(**inputs)
|
| 37 |
|
| 38 |
+
# Get detected objects and filter for license plates
|
| 39 |
logits = outputs.logits
|
| 40 |
boxes = outputs.pred_boxes
|
| 41 |
probas = logits.softmax(-1)[0, :, :-1]
|
|
|
|
| 50 |
return detected_boxes
|
| 51 |
|
| 52 |
def recognize_text(plate_image):
|
| 53 |
+
"""
|
| 54 |
+
Recognize text from a license plate image using TrOCR.
|
| 55 |
+
"""
|
| 56 |
# Convert the license plate image to a PIL image
|
| 57 |
pil_image = Image.fromarray(cv2.cvtColor(plate_image, cv2.COLOR_BGR2RGB))
|
| 58 |
|
|
|
|
| 63 |
|
| 64 |
return text.strip()
|
| 65 |
|
| 66 |
+
def process_video(video_path, frame_skip=5):
|
| 67 |
+
"""
|
| 68 |
+
Process a video to detect license plates and log entry/exit times.
|
| 69 |
+
"""
|
| 70 |
+
cap = cv2.VideoCapture(video_path)
|
| 71 |
vehicle_data = {}
|
| 72 |
+
frame_count = 0
|
| 73 |
+
|
| 74 |
while cap.isOpened():
|
| 75 |
ret, frame = cap.read()
|
| 76 |
if not ret:
|
| 77 |
break
|
| 78 |
+
|
| 79 |
+
frame_count += 1
|
| 80 |
+
if frame_count % frame_skip != 0:
|
| 81 |
+
continue # Skip frames to optimize processing time
|
| 82 |
+
|
| 83 |
# Detect license plates
|
| 84 |
detected_boxes = detect_license_plate(frame)
|
| 85 |
|
|
|
|
| 105 |
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
| 106 |
cv2.putText(frame, license_plate, (x_min, y_min-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 107 |
|
| 108 |
+
# Display the frame (optional, can be removed for headless environments)
|
| 109 |
cv2.imshow('Vehicle Detection', frame)
|
| 110 |
|
| 111 |
# Break on 'q' key press
|
| 112 |
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 113 |
break
|
| 114 |
+
|
| 115 |
cap.release()
|
| 116 |
cv2.destroyAllWindows()
|
| 117 |
|
|
|
|
| 121 |
print(f"License Plate: {plate}, Entry Time: {times['entry_time']}, Exit Time: {times['exit_time']}")
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
+
# Replace 'road_video.mp4' with the path to your video file or use 0 for webcam
|
| 125 |
+
process_video("road_video.mp4", frame_skip=5)
|