import gradio as gr import cv2 from ultralytics import YOLO #!pip install transformers #from transformers import AutoTokenizer, AutoModelForSequenceClassification import torch # Load Pretrained Model and Tokenizer model_path = "best.pt" #tokenizer = AutoTokenizer.from_pretrained(model) #model = AutoModelForSequenceClassification.from_pretrained(model) loaded_model = YOLO(model_path) # Define the Gradio Interface 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) # Use the easyocr reader for English language reader = easyocr.Reader(['en']) # Perform OCR on the input image result = reader.readtext(img,allowlist="0123456789") # Extract text and bounding box coordinates 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) # Launch the Gradio Interface demo.launch()