so i have this code that toke an image of handwritten non cursive lines text and segment…
- User
so i have this code that toke an image of handwritten non cursive lines text and segment it into lines word characters, and as you can see it give an list of characters but i want it to give a text as it gived in the images line by line and word by word you can modifier whatever you want the dataset containing handwritten images in size 2828 pixels also I observe is not work well to segment lines
test on external image
import cv2 import numpy as np from PIL import Image
def text_segmentation(image_path): def get_contours(image):
Trouve les contours dans l’image
contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) return sorted(contours, key=lambda contour: cv2.boundingRect(contour)[0])
def process_character_image(char_image):
Redimensionner et centrer l’image à une taille de 20x20 pixels avec un fond noir
resized_image = cv2.resize(char_image, (20, 20), interpolation=cv2.INTER_AREA) centered_image = np.zeros((28, 28), dtype=np.uint8) centered_image[4:24, 4:24] = resized_image return Image.fromarray(centered_image)
Charge l’image et convertit en niveaux de gris
img = Image.open(image_path).convert(‘L’) img_array = np.array(img)
Appliquer un seuillage et inverser le fond et le texte
_, thresh = cv2.threshold(img_array, 128, 255, cv2.THRESH_BINARY_INV)
Segmenter les lignes de texte
line_contours = get_contours(255 - cv2.threshold(thresh, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]) lines = [cv2.boundingRect(line) for line in line_contours]
char_images = []
for line_rect in lines: x, y, w, h = line_rect line = thresh[y:y+h, x:x+w]
Segmenter les mots dans la ligne
word_contours = get_contours(255 - cv2.threshold(line, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]) words = [cv2.boundingRect(word) for word in word_contours] for word_rect in words: x, y, w, h = word_rect word = line[y:y+h, x:x+w]
Segmenter les caractères dans le mot
char_contours = get_contours(255 - cv2.threshold(word, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]) chars = [cv2.boundingRect(char) for char in char_contours] for char_rect in chars: x, y, w, h = char_rect char = word[y:y+h, x:x+w] char_images.append(process_character_image(char)) return char_images
char_images = text_segmentation(‘tozo.jpg’)
then this code that predict each character
img_array = np.array(char_images[0]) img_array = img_array.reshape(-1, 28, 28, 1) predicted_label = model.predict(img_array) predicted_class = class_mapping[np.argmax(predicted_label)] print(“La classe prédite pour la nouvelle image est:”, predicted_class)
- Assistant
- User
now i give it an image contain one word which is coronavirus it give each character vertically
- Assistant
It seems like the issue is with the segmentation of the characters in the word. I think this can be fixed by sorting the characters according to their position in the image. You can modify the
text_segmentationfunction to sort the characters' bounding rectangles based on their x-coordinate. Here's the updated function: - User
it still give the predicted word verticalay c o r o n a v i r u s
- Assistant
I apologize for the confusion. Based on your description, it seems like the characters are printed on separate lines instead of being concatenated in a single line. To verify this, please print
result_textusing thereprfunction to see the actual string representation:print(repr(result_text))If the output shows the characters separated by a newline character (
'\n'), then the issue is due to how the text is being displayed or printed by the terminal or the system you're using to run the code. If the output shows the characters separated by a space, the issue is related to the segmentation step.Based on your feedback, I think the issue is with how your terminal or system is displaying the text. Please try the following solution to display the result in a single line:
result_text = result_text.replace('\n', ' ') print(result_text)This will replace all newline characters with spaces, and the output should now show the word "coronavirus" on a single line.
- User
when i print (repr(result_text) it give C \nO \nR \nO \nN \nA \nV \nI \nR \nU \nS'
- Assistant
I see. This means that there might be an issue with the segmentation, causing the characters to be recognized individually rather than as a single word. To address this issue, let's try updating the function to handle single words with multiple characters.
- User
- Assistant
Based on your code, I see that you have a full pipeline for training a model, segmenting an image into characters, and predicting those characters. I also see that you are using the updated
text_segmentationfunction in your code.This pipeline should now work for a single-word image like the one containing "coronavirus." However, you mentioned you are testing with a new image called "hello.jpg." You can follow the same procedure as before to see the results of this image:
lines_chars = text_segmentation('hello.jpg') result_text = predict_text(lines_chars) print(result_text)If you are still facing issues with the predicted labels being displayed vertically, please verify that the "hello.jpg" image is in the correct format and check how it is being segmented by
text_segmentation.Otherwise, the provided code should work for segmenting and predicting text from images with multiple lines, words, and characters just as well as single-word images like "coronavirus" and "hello".
- User
i want to test it on a image contains two lines for exemple how can i got it
- Assistant
To test your code with an image that contains two lines of text, you simply need to have an image file with two lines of handwritten text. You can create the image yourself or find an existing one that meets these criteria.
Save the image on your computer with an appropriate name, such as "two_lines.jpg", and use that file to segment and predict the text:
lines_chars = text_segmentation('two_lines.jpg') result_text = predict_text(lines_chars) print(result_text)Replace
two_lines.jpgwith the filename of the image containing two lines of handwritten text. The code will then segment the image into lines, words, and characters, and predict the text line by line.