制作数据集时报错AttributeError: 'str' object has no attribute 'write'

weixin_46105101 weixin_46105101 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下
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里面

weixin_38608189

python系列(亲测有效):AttributeError: module ‘fcntl‘ has no attribute ‘F_GETFL‘

AttributeError: module ‘fcntl‘ has no attribute ‘F_GETFL‘

坦笑&&life

解决运行django程序出错问题 'str'object has no attribute'_meta'

主要介绍了解决运行django程序出错问题 'str'object has no attribute'_meta',具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

weixin_38603704

详解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文件读写及字

weixin_38677227

解决python3 urllib中urlopen报错的问题

主要介绍了关于解决python3 urllib中urlopen报错问题的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。

weixin_38563176
评论 5
收藏 已收藏
分享
返回顶部
weixin_46105101
脚本语言 37,738 成员 34,208 内容