制作数据集时报错AttributeError: 'str' object has no attribute 'write'
import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
#cwd='./data/train/'
cwd='./data/test/'
classes={'mask','nomask'} #人为设定2类
#writer= tf.python_io.TFRecordWriter("mask_and_nomask_test.tfrecords") #要生成的文件
writer= tf.compat.v1.python_io.TFRecordWriter=("mask_and_nomask_test.tfrecords") #要生成的文件
for index,name in enumerate(classes):
class_path=cwd+name+'/'
for img_name in os.listdir(class_path):
img_path=class_path+img_name #每一个图片的地址
img=Image.open(img_path)
img= img.resize((128,128))
print(np.shape(img))
img_raw=img.tobytes()
example = tf.train.Example(features=tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
}))
writer.write(example.SerializeToString())
writer.close()
报错如下
writer.write(example.SerializeToString()) #序列化为字符串
AttributeError: 'str' object has no attribute 'write'
谢谢各位大佬
相关推荐
TypeError object of type ‘type’ has no len()—Python报错问题:
翻译过来是类型为“type”的TypeError对象没有len(),我报错的代码是: #coding=utf-8 print(请输入一个字符串:) a = input('') #回文的长度至少为2 if len(str) < 2: print('请不要输入空字符串!') a = input('请重新输入一个字符串:') b = reversed(list(a)) if list(b) == list(a): print(a,'是回文') else: print(a,'不是回文') 运行结果: 将if len(a) < 2 里面的a错写成了python里面
python系列(亲测有效):AttributeError: module ‘fcntl‘ has no attribute ‘F_GETFL‘
AttributeError: module ‘fcntl‘ has no attribute ‘F_GETFL‘
解决运行django程序出错问题 'str'object has no attribute'_meta'
主要介绍了解决运行django程序出错问题 'str'object has no attribute'_meta',具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
详解Python中的编码问题(encoding与decode、str与bytes)
1 引言 在文件读写及字符操作时,我们经常会出现下面这几种错误: TypeError: write() argument must be str, not bytes AttributeError: ‘URLError’ object has no attribute ‘code’ UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa0’ inposition 5747: illegal multibyte sequence 这些错误一看就是编码问题, 本篇博文总结一下Python3文件读写及字
解决python3 urllib中urlopen报错的问题
主要介绍了关于解决python3 urllib中urlopen报错问题的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。


