简体   繁体   中英

How to read text from an image and save it to a text file

I used

image_path= "image.jpg"
reader = easyocr.Reader(['en'], gpu=False)
result= reader.readtext(image_path)
print(result)
print("text printed")

output:

Using CPU. Note: This module is much faster with a GPU.
[([[707, 587], [999, 587], [999, 719], [707, 719]], 'Dream', 0.930219167433956), ([[998, 546], [1246, 546], [1246, 770], [998, 770]], 'big!', 0.8384830355644226)]
text printed

But the run time is very large I don't know why

If I want to write an API for the same is it possible to show the file path as output? Any suggestions would be very helpful. Thanks

This is the answer:

    import pytesseract as tess
    from PIL import Image

    tess.pytesseract.tesseract_cmd= r"D:\softies\Python Packages\Tesseract-OCR\tesseract.exe"
    img_path = "img.jpg"
    img = Image.open(path)
    text= tess.image_to_string(img)
    print(text)
    with open(r"C:\Users\a\Desktop\save.txt",'w') as f:
        print(text,file=f)
#Depends on what you need. Here I am appending cords and text.

#If your running to whole folder run this.

reader = easyocr.Reader(["en"], gpu = False)

text = []
cords= []
for files in glob.glob("*.jpg"):
  #print(files)
  images = cv2.imread(files)
  bounds = reader.readtext(images)
  for bound in bounds:
    text.append(bound[1])
    cords.append(bound[0])

#Creating the dataframe and transfer to text file
df_ocr = pd.DataFrame({"cords": cords,"text": text})
df_ocr.to_csv('result.txt', sep='\t', index=False)


#If your runnning to single file 
bound = reader.readtext(image)
df_ocr = pd.DataFrame({"cords": bound[0],"text": bound[1})
df_ocr.to_csv('result.txt', sep='\t', index=False)

You can use EasyOCR in this way:

img_path = "img.jpg"
img = Image.open(img_path)    
with open("save.txt", "w") as text_file:
    reader = easyocr.Reader(['en'])
    result = reader.readtext(img)
    for (bbox, text, prob) in result:
        if prob >= 0.5:
            # display 
            print(f'Detected text: {text} (Probability: {prob:.2f})')
            text_file.write(f"{str(text)}\n")
    text_file.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM