我是Python新手。我试图识别来自TensorFlow的图像中的文本。当我尝试在终端中运行python gen.py
时,我收到了TypeError
。
Traceback (most recent call last): File "gen.py", line 287, in for img_idx, (im, c, p) in enumerate(im_gen): File "gen.py", line 277, in generate_ims yield generate_im(random.choice(char_ims), num_bg_images) File "gen.py", line 239, in generate_im bg = generate_bg(num_bg_images) File "gen.py", line 226, in generate_bg bg = cv2.imread(fname, cv2.IMREAD_GRAYSCALE) / 255. TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'
答案 0 :(得分:0)
看起来您的代码无法在该路径上打开图片
bg = cv2.imread(fname, cv2.IMREAD_GRAYSCALE) / 255
cv2.imread方法返回None。
最好先拍摄图像
image = cv2.imread(fname, cv2.IMREAD_GRAYSCALE)
if image is not None:
bg = image / 255
您也可以在开头导入os模块以检查该文件是否存在
import os
# <your code...>
if !os.path.isfile(fname):
raise Exception("File doesn't exist")
image = cv2.imread(fname, cv2.IMREAD_GRAYSCALE)
if image is not None:
bg = image / 255