简体   繁体   English

如何从图像中读取文本并将其保存到文本文件

[英]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: 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?如果我想写一个 API 是否可以将文件路径显示为 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:你可以这样使用 EasyOCR:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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