bonrix's picture
Update app.py (#2)
a33c5c3
import gradio as gr
import cv2
import numpy as np
import pytesseract
import time
def text1(image):
start_time = time.time()
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding to preprocess the image
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
# Perform OCR on the thresholded image
text = pytesseract.image_to_string(thresh, lang='eng')
# Split the OCR output into a list of lines
lines = [line.strip() for line in text.split('\n') if line.strip()]
end_time = time.time()
time_taken = end_time - start_time
return {
'text': lines,
'time_taken': time_taken
}
def text2(image):
start_time = time.time()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
inverted = cv2.bitwise_not(gray)
# apply thresholding to preprocess the image
thresh = cv2.threshold(inverted, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
# text = pytesseract.image_to_string(plate_roi, lang='eng', config='--psm 6')
text = pytesseract.image_to_string(thresh, lang='eng')
# split the OCR output into a list of lines
lines = [line.strip() for line in text.split('\n') if line.strip()]
end_time = time.time()
time_taken = end_time - start_time
return {
'text': lines,
'time_taken': time_taken
}
def text3(image):
start_time = time.time()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
inverted = cv2.bitwise_not(gray)
# apply thresholding to preprocess the image
thresh = cv2.threshold(inverted, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
# text = pytesseract.image_to_string(plate_roi, lang='eng', config='--psm 6')
text = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6 -c tessedit_char_whitelist=0123456789.')
# split the OCR output into a list of lines
lines = [line.strip() for line in text.split('\n') if line.strip()]
end_time = time.time()
time_taken = end_time - start_time
return {
'text': lines,
'time_taken': time_taken
}
with gr.Blocks() as demo:
gr.Markdown("Various OCR (Optical Character Recognition) tools can convert images into text form. Some popular methods include Tesseract OCR, Google Cloud Vision API, and Microsoft Azure Cognitive Services OCR. These tools use advanced algorithms to analyze images, recognize characters, and convert them into editable text, enabling efficient data extraction and document digitization.")
with gr.Tab("OCR Function 1"):
image_input1 = gr.inputs.Image()
text_output1 = gr.outputs.Textbox()
button1 = gr.Button("Perform OCR")
with gr.Tab("OCR Function 2"):
image_input2 = gr.inputs.Image()
text_output2 = gr.outputs.Textbox()
button2 = gr.Button("Perform OCR")
with gr.Tab("OCR Function 3"):
image_input3 = gr.inputs.Image()
text_output3 = gr.outputs.Textbox()
button3 = gr.Button("Perform OCR")
button1.click(text1, inputs=image_input1, outputs=text_output1)
button2.click(text2, inputs=image_input2, outputs=text_output2)
button3.click(text3, inputs=image_input3, outputs=text_output3)
demo.launch()