我是机器学习和对象检测的新手,我正在尝试加载自己的数据集以进行对象检测。当我尝试生成tfrecords时,出现错误“ OSError:无法识别图像文件<_io.BytesIO对象位于0x1347afa40>”。我正在使用以下代码:https://github.com/wpilibsuite/DetectCoral/blob/local/mobilenet/dataset/scripts/generate_tfrecord.py,其脚本中编写了经过修改的路径和函数。
问题摘录
def create_tf_example(group, path, labels):
with tf.io.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
# The problem occurs on the line below
image = Image.open(encoded_jpg_io)
完整代码
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import io
import pandas as pd
import tensorflow as tf
import json
import glob
from os.path import join
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict
def get_labels():
f = None
base = "/Users/kabirkumar/Desktop/full_data/meta.json"
for file in glob.glob('/Users/kabirkumar/Desktop/full_data/meta.json'):
f = file
with open(f, 'r') as meta:
return [label["title"] for label in json.load(meta)["classes"]]
def class_text_to_int(label, labels):
return labels.index(label)
def split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def create_tf_example(group, path, labels):
with tf.io.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
# the problem occurs for the line below
image = Image.open(encoded_jpg_io)
width, height = image.size
filename = group.filename.encode('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for index, row in group.object.iterrows():
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class'], labels))
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))
return tf_example
input_csvv = '/Users/kabirkumar/Desktop/Object-detection/data/train.csv'
output_tfrecordd = '/Users/kabirkumar/Desktop/Object-detection/data/train.record'
def main(input_csv, output_tfrecord):
writer = tf.io.TFRecordWriter(output_tfrecord)
path = '/Users/kabirkumar/Desktop/Object-detection/Filming Day 2 Video/imgtrain'
examples = pd.read_csv(input_csv)
grouped = split(examples, 'filename')
labels = get_labels()
for group in grouped:
tf_example = create_tf_example(group, path, labels)
writer.write(tf_example.SerializeToString())
writer.close()
print(end='.\nSuccessfully created the TFRecords: {}'.format(output_tfrecord))
main('/Users/kabirkumar/Desktop/Object-detection/data/train.csv', join('/Users/kabirkumar/Desktop/Object-detection/data/train.record'))
我修改的路径是csv输入路径,tfrecord输出路径和图像路径。