Update demo.py
Browse files
demo.py
CHANGED
|
@@ -8,31 +8,6 @@ from PIL import Image as PILImage, ImageDraw, ImageFont # This import may be ne
|
|
| 8 |
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
-
def resize_image_with_aspect_ratio(image, target_height):
|
| 12 |
-
# Get original dimensions
|
| 13 |
-
original_width, original_height = image.size
|
| 14 |
-
|
| 15 |
-
# Calculate the new width to maintain aspect ratio
|
| 16 |
-
aspect_ratio = original_width / original_height
|
| 17 |
-
new_width = int(target_height * aspect_ratio)
|
| 18 |
-
|
| 19 |
-
# Resize the image
|
| 20 |
-
resized_image = image.resize((new_width, target_height), Image.LANCZOS)
|
| 21 |
-
return resized_image
|
| 22 |
-
|
| 23 |
-
def draw_text_with_background(draw, text, position, font, text_color="red", bg_color="white", padding=2):
|
| 24 |
-
# Get the bounding box of the text
|
| 25 |
-
bbox = draw.textbbox(position, text, font=font)
|
| 26 |
-
|
| 27 |
-
# Calculate the background rectangle coordinates
|
| 28 |
-
bg_x0, bg_y0, bg_x1, bg_y1 = bbox[0] - padding, bbox[1] - padding, bbox[2] + padding, bbox[3] + padding
|
| 29 |
-
|
| 30 |
-
# Draw the background rectangle
|
| 31 |
-
draw.rectangle([bg_x0, bg_y0, bg_x1, bg_y1], fill=bg_color)
|
| 32 |
-
|
| 33 |
-
# Draw the text on top of the background
|
| 34 |
-
draw.text(position, text, fill=text_color, font=font)
|
| 35 |
-
|
| 36 |
def process_image(image):
|
| 37 |
# Convert PIL image to bytes to send in POST request
|
| 38 |
img_bytes = io.BytesIO()
|
|
@@ -54,39 +29,11 @@ def process_image(image):
|
|
| 54 |
if result.ok:
|
| 55 |
json_result = result.json()
|
| 56 |
if json_result.get("resultCode") == "Error":
|
| 57 |
-
return [
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# for plate in plates:
|
| 61 |
-
# number = plate.get("number")
|
| 62 |
-
# left = plate.get("left")
|
| 63 |
-
# top = plate.get("top")
|
| 64 |
-
# right = plate.get("right")
|
| 65 |
-
# bottom = plate.get("bottom")
|
| 66 |
-
# countryCode = plate.get('countryCode')
|
| 67 |
-
# car = plate.get('car')
|
| 68 |
-
|
| 69 |
-
# if car is not None:
|
| 70 |
-
# car_left = car.get('left')
|
| 71 |
-
# car_top = car.get('top')
|
| 72 |
-
# car_right = car.get('right')
|
| 73 |
-
# car_bottom = car.get('bottom')
|
| 74 |
-
# color = car.get('color')
|
| 75 |
-
# madeBy = car.get('madeBy')
|
| 76 |
-
# model = car.get('model')
|
| 77 |
-
# draw.rectangle([car_left, car_top, car_right, car_bottom], outline="white", width=3)
|
| 78 |
-
# draw_text_with_background(draw, color + "," + madeBy + "," + model, (car_left, car_top - 10), font, text_color="blue", bg_color="white")
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# # Draw rectangle and plate number
|
| 82 |
-
# if left is not None:
|
| 83 |
-
# draw.rectangle([left, top, right, bottom], outline="green", width=3)
|
| 84 |
-
# draw_text_with_background(draw, number + "," + countryCode, (left, top - 10), font, text_color="green", bg_color="white")
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
return [image, json_result]
|
| 88 |
else:
|
| 89 |
-
return [
|
| 90 |
|
| 91 |
with gr.Blocks() as demo:
|
| 92 |
gr.Markdown(
|
|
@@ -128,12 +75,9 @@ with gr.Blocks() as demo:
|
|
| 128 |
inputs=image_input)
|
| 129 |
process_button = gr.Button("Process")
|
| 130 |
with gr.Column():
|
| 131 |
-
|
| 132 |
-
image_output = gr.Image(type="pil")
|
| 133 |
-
with gr.TabItem("Json"):
|
| 134 |
-
json_output = gr.JSON()
|
| 135 |
|
| 136 |
-
process_button.click(process_image, inputs=[image_input], outputs=[
|
| 137 |
|
| 138 |
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fopulentyn%2FLicensePlateRecognition"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fopulentyn%2FLicensePlateRecognition&countColor=%23263759" /></a>')
|
| 139 |
|
|
|
|
| 8 |
|
| 9 |
from PIL import Image
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def process_image(image):
|
| 12 |
# Convert PIL image to bytes to send in POST request
|
| 13 |
img_bytes = io.BytesIO()
|
|
|
|
| 29 |
if result.ok:
|
| 30 |
json_result = result.json()
|
| 31 |
if json_result.get("resultCode") == "Error":
|
| 32 |
+
return [{"resultCode": "Error", "result": "Failed to process image"}]
|
| 33 |
+
|
| 34 |
+
return [json_result]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
else:
|
| 36 |
+
return [{"resultCode": "Error", "result": result.text}]
|
| 37 |
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
gr.Markdown(
|
|
|
|
| 75 |
inputs=image_input)
|
| 76 |
process_button = gr.Button("Process")
|
| 77 |
with gr.Column():
|
| 78 |
+
json_output = gr.JSON()
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
process_button.click(process_image, inputs=[image_input], outputs=[json_output])
|
| 81 |
|
| 82 |
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fopulentyn%2FLicensePlateRecognition"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fopulentyn%2FLicensePlateRecognition&countColor=%23263759" /></a>')
|
| 83 |
|