import os import gradio as gr import requests import json import io from gradio.components import Image from PIL import Image as PILImage, ImageDraw, ImageFont # This import may be needed if you're processing images from PIL import Image def resize_image_with_aspect_ratio(image, target_height): # Get original dimensions original_width, original_height = image.size # Calculate the new width to maintain aspect ratio aspect_ratio = original_width / original_height new_width = int(target_height * aspect_ratio) # Resize the image resized_image = image.resize((new_width, target_height), Image.LANCZOS) return resized_image def draw_text_with_background(draw, text, position, font, text_color="red", bg_color="white", padding=2): # Get the bounding box of the text bbox = draw.textbbox(position, text, font=font) # Calculate the background rectangle coordinates bg_x0, bg_y0, bg_x1, bg_y1 = bbox[0] - padding, bbox[1] - padding, bbox[2] + padding, bbox[3] + padding # Draw the background rectangle draw.rectangle([bg_x0, bg_y0, bg_x1, bg_y1], fill=bg_color) # Draw the text on top of the background draw.text(position, text, fill=text_color, font=font) def process_image(image): # Convert PIL image to bytes to send in POST request img_bytes = io.BytesIO() image.save(img_bytes, format="JPEG") img_bytes.seek(0) url = "http://127.0.0.1:9000/process_image" files = {'image': img_bytes} result = requests.post(url=url, files=files) # Font setup (default font) try: font = ImageFont.truetype("arial.ttf", int(image.size[1] / 30)) except IOError: font = ImageFont.load_default() draw = ImageDraw.Draw(image) if result.ok: json_result = result.json() if json_result.get("resultCode") == "Error": return [image, {"resultCode": "Error", "result": "Failed to process image"}] plates = json_result.get("result", {}).get("plates", []) for plate in plates: number = plate.get("number") left = plate.get("left") top = plate.get("top") right = plate.get("right") bottom = plate.get("bottom") countryCode = plate.get('countryCode') car = plate.get('car') if car is not None: car_left = car.get('left') car_top = car.get('top') car_right = car.get('right') car_bottom = car.get('bottom') color = car.get('color') madeBy = car.get('madeBy') model = car.get('model') draw.rectangle([car_left, car_top, car_right, car_bottom], outline="white", width=3) draw_text_with_background(draw, color + "," + madeBy + "," + model, (car_left, car_top - 10), font, text_color="blue", bg_color="white") # Draw rectangle and plate number if left is not None: draw.rectangle([left, top, right, bottom], outline="green", width=3) draw_text_with_background(draw, number + "," + countryCode, (left, top - 10), font, text_color="green", bg_color="white") return [image, json_result] else: return [image, {"resultCode": "Error", "result": result.text}] with gr.Blocks() as demo: gr.Markdown( """
Opulentyn Logo

License Plate Recognition (ANPR/ALPR)

We offer on-premises OCR and liveness check solutions available with a perpetual license.

## 🤝 Talk to us
Website      Email      Slack
""" ) with gr.Row(): with gr.Column(): image_input = gr.Image(type='pil') gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg'], inputs=image_input) process_button = gr.Button("Process") with gr.Column(): with gr.TabItem("Image"): image_output = gr.Image(type="pil") with gr.TabItem("Json"): json_output = gr.JSON() process_button.click(process_image, inputs=[image_input], outputs=[image_output, json_output]) gr.HTML('') demo.launch(server_name="0.0.0.0", server_port=7860)