Skip to main content
 首页 » 编程设计

python之如何在队列中解码 Tensorflow 中的 pfm 文件

2025年02月15日25EasonJim

我制作了一个文件名队列,这些文件是 *.pfm 文件。然后我编写了一个转换函数readPFM() 将*.pfm 文件转换为ndarray。

我想做的是,当一个文件从队列中出队时,我将使用该函数将其转换为 numpy ndarray。然后它将被输入到图表中。但是代码不起作用。

def disparity(batch_size, path, LR, epochs=2): 
    filenames = file_name(path, LR, 'pfm') 
    filenames = sorted(filenames) 
 
    filename_queue = tf.train.string_input_producer(filenames, shuffle=False, num_epochs=epochs) 
    reader = tf.WholeFileReader() 
    key, img_bytes = reader.read(filename_queue) 
    disparity, _ = readPFM(img_bytes) 
 
    return tf.train.batch([disparity], batch_size, dynamic_pad=True) 

pfm文件读取函数在这里。

def readPFM(file): 
    file = open(file, 'rb') 
 
    color = None 
    width = None 
    height = None 
    scale = None 
    endian = None 
 
    header = file.readline().rstrip() 
    if header == 'PF': 
        color = True 
    elif header == 'Pf': 
        color = False 
    else: 
        raise Exception('Not a PFM file.') 
 
    dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline()) 
    if dim_match: 
        width, height = map(int, dim_match.groups()) 
    else: 
        raise Exception('Malformed PFM header.') 
 
    scale = float(file.readline().rstrip()) 
    if scale < 0:  # little-endian 
        endian = '<' 
        scale = -scale 
    else: 
        endian = '>'  # big-endian 
 
    data = np.fromfile(file, endian + 'f') 
    shape = (height, width, 3) if color else (height, width) 
 
    data = np.reshape(data, shape) 
    data = np.flipud(data) 
    return data, scale 
 
 
def writePFM(file, image, scale=1): 
    file = open(file, 'wb') 
 
    color = None 
 
    if image.dtype.name != 'float32': 
        raise Exception('Image dtype must be float32.') 
 
    image = np.flipud(image) 
 
    if len(image.shape) == 3 and image.shape[2] == 3:  # color image 
        color = True 
    elif len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1:  # greyscale 
        color = False 
    else: 
        raise Exception('Image must have H x W x 3, H x W x 1 or H x W dimensions.') 
 
    file.write('PF\n' if color else 'Pf\n') 
    file.write('%d %d\n' % (image.shape[1], image.shape[0])) 
 
    endian = image.dtype.byteorder 
 
    if endian == '<' or endian == '=' and sys.byteorder == 'little': 
        scale = -scale 
 
    file.write('%f\n' % scale) 
 
    image.tofile(file) 

错误消息显示我的函数无法处理张量,因为它只能处理 *.pfm 文件。

有什么解决办法吗?

请您参考如下方法:

您不能像在 tensorflow 中那样使用 readPFM 函数,您需要用 tf.py_func 将其包装起来。

# helper function 
def decode_pfm(path): 
    data, _ = load_pfm(open(path, 'rb')) 
 
    # http://netpbm.sourceforge.net/doc/pfm.html 
    # pfm stores the data bottom-to-top, need to reverse 
    data = np.flipud(data) 
    data = np.expand_dims(data, 2) 
    return data 
 
def read_and_decode(path): 
    image_decoded = tf.py_func(decode_pfm, [path], tf.float32) 
 
    # py_func does not set the shape, you might need to explictly 
    # set it 
    image_decoded.set_shape((H, W, channels))