【发布时间】:2019-08-31 13:54:04
【问题描述】:
我正在创建一个用于文本识别的管道,我想使用 Tensorflow Dtatasets 通过 OpenCV 的一些预处理来加载数据
我正在学习本教程 https://www.tensorflow.org/guide/datasets#applying_arbitrary_python_logic_with_tfpy_func 我有这个预处理功能:
def preprocess(path, imgSize=(1024, 64), dataAugmentation=False):
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
kernel = np.ones((3, 3), np.uint8)
th, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV +
cv2.THRESH_OTSU)
img = cv2.dilate(img, kernel, iterations=1)
# create target image and copy sample image into it
(wt, ht) = imgSize
(h, w) = img.shape
fx = w / wt
fy = h / ht
f = max(fx, fy)
newSize = (max(min(wt, int(w / f)), 1),
max(min(ht, int(h / f)), 1)) # scale according to f (result at
least 1 and at most wt or ht)
img = cv2.resize(img, newSize)
# add random padding to fit the target size if data augmentation is true
# otherwise add padding to the right
if newSize[1] == ht:
if dataAugmentation:
padding_width_left = np.random.random_integers(0, wt-newSize[0])
img = cv2.copyMakeBorder(img, 0, 0, padding_width_left, wt-newSize[0]-padding_width_left, cv2.BORDER_CONSTANT, None, (0, 0))
else:
img = cv2.copyMakeBorder(img, 0, 0, 0, wt - newSize[0], cv2.BORDER_CONSTANT, None, (0, 0))
else:
img = cv2.copyMakeBorder(img, int(np.floor((ht - newSize[1])/2)), int(np.ceil((ht - newSize[1])/2)), 0, 0, cv2.BORDER_CONSTANT, None, (0, 0))
# transpose for TF
img = cv2.transpose(img)
return img
但是如果我用这个
list_images = os.listdir(images_path)
image_paths = []
for i in range(len(list_images)):
image_paths.append("iam-database/images/" + list_images[i])
dataset = tf.data.Dataset.from_tensor_slices(image_paths)
dataset = dataset.map(lambda filename: tuple(tf.py_function(preprocess, [filename], [tf.uint8])))
print(dataset)
我的形状未知,似乎没有解析预处理函数。我该怎么办?
【问题讨论】:
-
你在哪里迭代你的数据?
-
你得到什么错误?这行
dataset = dataset.map(lambda filename: tuple(tf.py_func(preprocess, [filename], [tf.uint8])))没有显示任何错误。我执行了您的代码,但没有看到错误。你的意思是<MapDataset shapes: (<unknown>,), types: (tf.uint8,)>吗? -
@borarak 在底部脚本中。我不需要一个包含 tf.data.Dataset.from_tensor_slices() 中路径的数组吗?
-
@MohanRadhakrishnan 我没有收到任何错误。但是,如果我打印数据集,我会得到未知的形状,并且不会解析预处理函数
标签: python opencv tensorflow tensorflow-datasets