【发布时间】:2020-06-07 19:29:36
【问题描述】:
我正在尝试将 Tfrecord 文件转换为 JPEG,但我不知道如何修复此错误。我是 Python 新手,如果我做错了什么,我很抱歉。如果有人可以帮助我,请提前谢谢 给出的错误 Tensor("DecodeJpeg:0", shape=(?, ?, 1), dtype=uint8) 预期图像(JPEG、PNG 或 GIF),得到空文件 [[node DecodeJpeg(定义于:38)]]
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from google.colab import drive
drive.mount('/content/drive')
c = 0
totalFile=0
tfrco="/content/drive/My Drive/ColabNotebooks/ddsm- mammography/training10_0/training10_0.tfrecords"
output_path = "/content/drive/My Drive/ColabNotebooks/ddsm-mammography/training10_0/Images10_0"
for record in tf.python_io.tf_record_iterator(tfrco):
c += 1
totalFiles=c
tf.reset_default_graph()
fq = tf.train.string_input_producer([tfrco], num_epochs=totalFiles)
reader = tf.TFRecordReader()
_, v = reader.read(fq)
fk = {
'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''),
'image/class/synset': tf.FixedLenFeature([], tf.string, default_value=''),
'image/filename': tf.FixedLenFeature([], tf.string, default_value='')
}
ex = tf.parse_single_example(v, fk)
imagem = tf.image.decode_jpeg(ex['image/encoded'], channels=1)
label = tf.cast(ex['image/class/synset'], tf.string)
fileName = tf.cast(ex['image/filename'], tf.string)
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
sess = tf.Session()
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord, sess = sess)
num_images=c
print("VAI RESTAURAR {} ARQUIVOS ".format(num_images))
for i in range(num_images):
try:
im_,lbl,fName = sess.run([imagem,label,fileName])
except Exception as e:
print(e)
break
lbl_=lbl.decode("utf-8")
savePath=os.path.join(output_path,lbl_)
if not os.path.exists(savePath):
os.makedirs(savePath)
fName_=os.path.join(savePath, fName.decode("utf-8").split('_')[1])
cv2.imwrite(fName_ , im_)
print(fName)
coord.request_stop()
coord.join(threads)
你能帮帮我吗?
【问题讨论】:
标签: python tensorflow jpeg tfrecord