Hi @ajndossi
Here is how you can make input parameters specifiable through command line:
import argparse
import degirum as dg
import degirum_tools
import matplotlib.pyplot as plt
def display_images(images, title="Images", figsize=(15, 5)):
num_images = len(images)
fig, axes = plt.subplots(1, num_images, figsize=figsize)
if num_images == 1:
axes = [axes]
for ax, image in zip(axes, images):
image_rgb = image[:, :, ::-1] # Convert BGR to RGB
ax.imshow(image_rgb)
ax.axis('off')
fig.suptitle(title, fontsize=16)
plt.tight_layout()
plt.show()
def crop_images(image, results):
cropped_images = []
for result in results:
bbox = result.get('bbox')
if not bbox or len(bbox) != 4:
continue
x_min, y_min, x_max, y_max = map(int, bbox)
x_min = max(0, x_min)
y_min = max(0, y_min)
x_max = min(image.shape[1], x_max)
y_max = min(image.shape[0], y_max)
cropped = image[y_min:y_max, x_min:x_max]
cropped_images.append(cropped)
return cropped_images
def rearrange_detections(detections):
detections_sorted = sorted(detections, key=lambda det: det["bbox"][0])
return "".join([det["label"] for det in detections_sorted])
def main(args):
# Load models
det_model = dg.load_model(
model_name=args.det_model,
inference_host_address=args.inference_host,
zoo_url=args.zoo_url,
token=args.token or degirum_tools.get_token(),
overlay_color=[(255, 255, 0), (0, 255, 0)]
)
ocr_model = dg.load_model(
model_name=args.ocr_model,
inference_host_address=args.inference_host,
zoo_url=args.zoo_url,
token=args.token or degirum_tools.get_token(),
output_use_regular_nms=False,
output_confidence_threshold=0.1
)
# Run detection
det_results = det_model(args.image)
# OCR
if det_results.results:
cropped = crop_images(det_results.image, det_results.results)
for idx, plate_img in enumerate(cropped):
ocr_results = ocr_model.predict(plate_img)
label = rearrange_detections(ocr_results.results)
det_results.results[idx]["label"] = label
# Display
display_images([det_results.image_overlay], title="License Plate Recognition Result")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="License Plate Recognition with OCR")
parser.add_argument("image", help="Path to input image")
parser.add_argument("--inference-host", default="@cloud", help="Inference host address (e.g., @cloud or @local)")
parser.add_argument("--zoo-url", default="degirum/models_hailort", help="Model zoo URL")
parser.add_argument("--token", default="", help="Access token (optional, auto-resolved if blank)")
parser.add_argument("--det-model", default="yolov8n_relu6_lp--640x640_quant_hailort_hailo8l_1", help="Detection model name")
parser.add_argument("--ocr-model", default="yolov8n_relu6_lp_ocr--256x128_quant_hailort_hailo8l_1", help="OCR model name")
args = parser.parse_args()
main(args)