Spaces:
Runtime error
Runtime error
File size: 3,318 Bytes
5d151a0 d42a735 5d151a0 9c4bed0 d42a735 5d151a0 9c4bed0 5d151a0 9c4bed0 5d151a0 9c4bed0 5d151a0 d42a735 a33c5c3 d42a735 5d151a0 d42a735 5d151a0 d42a735 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
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()
|