|
import gradio as gr |
|
import cv2 |
|
from ultralytics import YOLO |
|
|
|
|
|
import torch |
|
|
|
model_path = "best.pt" |
|
|
|
|
|
loaded_model = YOLO(model_path) |
|
|
|
import easyocr |
|
def image_classifier(img): |
|
|
|
res = loaded_model.predict(img,conf=0.25) |
|
box = res[0].boxes.xywh[0] |
|
bounding_box = box.cpu().numpy() |
|
x0 = bounding_box[0] - bounding_box[2] / 2 |
|
x1 = bounding_box[0] + bounding_box[2] / 2 |
|
y0 = bounding_box[1] - bounding_box[3] / 2 |
|
y1 = bounding_box[1] + bounding_box[3] / 2 |
|
|
|
start_point = (int(x0), int(y0)) |
|
end_point = (int(x1), int(y1)) |
|
cv2.rectangle(img, start_point, end_point, color=(0,255,0), thickness=2) |
|
|
|
reader = easyocr.Reader(['en']) |
|
|
|
|
|
result = reader.readtext(img,allowlist="0123456789") |
|
|
|
|
|
text_and_coordinates = [(entry[1], entry[0]) for entry in result] |
|
return text_and_coordinates |
|
|
|
demo = gr.Interface( |
|
fn=image_classifier, |
|
inputs=gr.Image(type="numpy"), |
|
outputs=gr.Textbox()) |
|
|
|
demo.launch(debug = True ,share = True) |
|
|
|
|
|
demo.launch() |