我想用名为“S.png”的图像测试我的OCR代码,该图像位于与python文件相同的文件夹中。
我试过这段代码:
import numpy as np
import os
import string
import sys
from PIL import Image, ImageFont, ImageDraw
from skimage.io import imread
from sklearn.model_selection import ShuffleSplit
from TFANN import ANNC
def ImageToString(I):
'''
Uses OCR to transform an image into a string
'''
SI = DivideIntoSubimages(I)
YH = cnnc.predict(SI)
ss = SubimageShape(I)
return JoinStrings(YH, ss)
#OCR code...
for img in sys.argv[1:]:
print(sys.argv[1:]) #I get: [] I don't know why it doesn't show S.png
I = imread(img)
S = ImageToString(I)
print("S= "+str(S))
它不会产生错误,也不会产生任何结果。然后我尝试了这段代码:
img = Image.open("S.png")
print("img= "+str(img)) #=> img= <PIL.PngImagePlugin.PngImageFile image
mode=RGB size=969x1261 at 0x1D9994C0F28>
I = imread(img, 'RGB') #this line generates the error desvribed below
S = ImageToString(I)
print("S= "+str(S))
这是打印错误:
File "C:/Users/Hounaîda/Documents/pfe/ANN_OCR/DeepOCR.py", line 203, in <module>
I = imread(img)
File "C:\Users\Hounaîda\Anaconda3\lib\site-packages\skimage\io\_io.py", line 61, in imread
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
File "C:\Users\Hounaîda\Anaconda3\lib\site-packages\skimage\io\manage_plugins.py", line 211, in call_plugin
return func(*args, **kwargs)
File "C:\Users\Hounaîda\Anaconda3\lib\site-packages\skimage\io\_plugins\pil_plugin.py", line 43, in imread
im = Image.open(fname)
File "C:\Users\Hounaîda\Anaconda3\lib\site-packages\PIL\Image.py", line 2486, in open
prefix = fp.read(16)
AttributeError: 'PngImageFile' object has no attribute 'read'
答案 0 :(得分:0)
我使用以下代码解决了这个问题:
if __name__ == "__main__":
I = imread("S.png")
S = ImageToString(I)
print("S= "+str(S))