-
-
Save 2qp/35537e8eebfa45cbd80a01ecc59fd218 to your computer and use it in GitHub Desktop.
Real-time license plate recognition with 'openalpr' using a video file as input. Please check out my "Building and Testing 'openalpr' on Jetson TX2" post for more information: https://jkjung-avt.github.io/openalpr-on-tx2/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
from openalpr import Alpr | |
RTSP_SOURCE = 'rtsp://face:Face12345@10.15.19.201:554/live.sdp' | |
WINDOW_NAME = 'openalpr' | |
FRAME_SKIP = 15 | |
def open_cam_rtsp(uri, width=1280, height=720, latency=2000): | |
gst_str = ('rtspsrc location={} latency={} ! ' | |
'rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! ' | |
'video/x-raw, width=(int){}, height=(int){}, format=(string)BGRx ! ' | |
'videoconvert ! appsink').format(uri, latency, width, height) | |
return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) | |
def main(): | |
alpr = Alpr('tw', 'tx2.conf', '/usr/local/share/openalpr/runtime_data') | |
if not alpr.is_loaded(): | |
print('Error loading OpenALPR') | |
sys.exit(1) | |
alpr.set_top_n(3) | |
#alpr.set_default_region('new') | |
cap = open_cam_rtsp(RTSP_SOURCE) | |
if not cap.isOpened(): | |
alpr.unload() | |
sys.exit('Failed to open video file!') | |
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_AUTOSIZE) | |
cv2.setWindowTitle(WINDOW_NAME, 'OpenALPR video test') | |
_frame_number = 0 | |
while True: | |
ret_val, frame = cap.read() | |
if not ret_val: | |
print('VidepCapture.read() failed. Exiting...') | |
break | |
_frame_number += 1 | |
if _frame_number % FRAME_SKIP != 0: | |
continue | |
cv2.imshow(WINDOW_NAME, frame) | |
results = alpr.recognize_ndarray(frame) | |
for i, plate in enumerate(results['results']): | |
best_candidate = plate['candidates'][0] | |
print('Plate #{}: {:7s} ({:.2f}%)'.format(i, best_candidate['plate'].upper(), best_candidate['confidence'])) | |
if cv2.waitKey(1) == 27: | |
break | |
cv2.destroyAllWindows() | |
cap.release() | |
alpr.unload() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment