Spaces:
Running
Running
File size: 1,244 Bytes
d053f30 |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import cv2
import easyocr
import gradio as gr
import base64
import json
def text_extraction(image):
# Convert base64 image to OpenCV format
image = base64.b64decode(image.split(",")[1])
nparr = np.frombuffer(image, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Instance text detector
reader = easyocr.Reader(['en'], gpu=False)
# Detect text on image
text_ = reader.readtext(img)
threshold = 0.25
# Draw bbox and text
for t_, t in enumerate(text_):
bbox, text, score = t
if score > threshold:
cv2.rectangle(img, tuple(map(int, bbox[0])), tuple(map(int, bbox[2])), (255, 0, 0), 2)
# Encode image to base64
retval, buffer = cv2.imencode('.jpg', img)
img_base64 = base64.b64encode(buffer).decode('utf-8')
# Create JSON response
response_json = {
'annotated_image_base64': img_base64,
'text_data': text_
}
# Convert the dictionary to a JSON string
response_json_str = json.dumps(response_json, default=str)
return response_json_str
# Define Gradio interface
iface = gr.Interface(
fn=text_extraction,
inputs=gr.Image(),
outputs=["image", "json"]
)
# Launch the Gradio interface
iface.launch()
|