File size: 1,052 Bytes
8011742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import gradio as gr
import pytesseract
from PIL import Image, ImageDraw

def extract_text_and_boxes(image):
    data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
    draw = ImageDraw.Draw(image)
    
    boxes_and_words = []
    
    for i in range(len(data['text'])):
        if data['text'][i].strip() != '':  # Filters out empty text results
            x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
            word = data['text'][i]
            boxes_and_words.append({'box': (x, y, w, h), 'word': word})
            draw.rectangle([x, y, x + w, y + h], outline='red')
    
    return image, boxes_and_words

iface = gr.Interface(fn=extract_text_and_boxes,
                     inputs=gr.Image(type='pil'),
                     outputs=[gr.Image(type='pil', label="Image with Bounding Boxes"),
                              gr.JSON(label="Extracted Words and Boxes")],
                     title="Test Tesseract",
                     description="Test PyTesseract.")

iface.launch()