For a text image input, I need to break the text into segments using the OPENCV library
Let’s say the image has 4 lines of text, I need to write a function that breaks down and cuts the lines and creates 4 new images for each line respectively.
Then a function that receives a line/sentence that consists of several words and cuts images of the words separately.
Then a function that receives an image of a word and decomposes it into letters, for each letter its own image will be created.
The function below cutting only one word to letters
# Cutting one word to letters
def image_to_text(image_file_path):
img = cv2.imread(image_file_path, cv2.IMREAD_COLOR)
extracted_text = ""
if img is not None:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for idx, contour in enumerate(contours):
x, y, w, h = cv2.boundingRect(contour)
letter_img = thresh[y:y + h, x:x + w]
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
letter_img = cv2.filter2D(letter_img, -1, kernel)
letter_img = cv2.copyMakeBorder(letter_img, 4, 4, 4, 4, cv2.BORDER_CONSTANT, value=[0, 0, 0])
letter_img = cv2.resize(letter_img, (28, 28), interpolation=cv2.INTER_AREA)
# Display the image
plt.imshow(letter_img, cmap='gray')
plt.axis('off')
plt.show()
letter_img = letter_img.reshape(1, 28, 28, 1).astype('float32') / 255
prediction = loaded_model.predict(letter_img)
# Print the predicted class and its corresponding label
predicted_class = np.argmax(prediction)
predicted_label = class_labels[predicted_class]
print(f"Predicted class for the letter: {predicted_class} ({predicted_label})")
extracted_text += predicted_label
extracted_text = extracted_text[::-1]
print('Extracted text:', extracted_text)
return extracted_text
text = image_to_text(r"C:UsersstudentDesktopFinalProjectFlaskimagetest3IMG.jpg")
input:
output:
I am limited in the pictures I can upload to the forum, so I will only upload two examples of the letters B and E
Letter B from ‘Because’
Letter C from ‘Because’
The rest of the letters were extracted in the same way..
enter image description here
the question is how can i handle an input like this:
enter image description here
I’m trying for days and I’m getting frustrated.. will be glad for help
Rozira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.