Object Detection API谷歌

该文章部分参考别的大佬的,由于忘了内容出处,所以没有加转载链接,请谅解,有原创作者看到可以联系我添加。

========转载请注明出处==========

此python文件放在dataset_tools下面

生成自己训练的数据集主要看个人annotation文件是什么格式的。我这里的每张图都有自己的annotation文件,例如:

图片xxx.jpg,其annotation文件为xxx.box

box文件内容为:

Xmin Ymin Xmax Ymax  label  如下图:如果有多个label ,可以继续追加在下一行:

Xmin Ymin Xmax Ymax  label \n

Xmin Ymin Xmax Ymax  label

from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport hashlib
import io
import os
import PIL.Image
import tensorflow as tf
import pandas as pd
import cv2
from functools import reduce
import operator
from object_detection.utils import dataset_utilflags = tf.app.flags
flags.DEFINE_string('train_imgs_dir', '/home/ai/Downloads/competition_change_box_img/img', 'Root directory to bc train dataset.')
flags.DEFINE_string('train_labels', '/home/ai/Downloads/competition_change_box_img/box','(Relative) path to annotations directory.')
flags.DEFINE_string('train_output', '../All_tf_record/competition_img_test.record', 'Path to output TFRecord')
FLAGS = flags.FLAGSdef create_coordinate_info_of_content_list(image_dir,label_dir):content_list_all = []for item,file_name in enumerate(os.listdir(label_dir)):img = cv2.imread(os.path.join(image_dir,file_name.replace('.box','.jpg')))height = img.shape[0]width = img.shape[1]deepth = img.shape[2]content_list = [[file_name.replace('.box', '.jpg'), height, width, deepth]]with open(os.path.join(label_dir,file_name), 'r') as f: lines = f.readlines()for line in lines:new_line = line.split(' ')[:]content_one = [new_line[0],new_line[1],new_line[2],new_line[3],new_line[4]]content_list.append(content_one)a = reduce(operator.add,content_list)content_list_all.append(a)return content_list_alldef create_tf_example(content_list, imgs_dir):height = int(content_list[1])width = int(content_list[2])filename = content_list[0]img_path = os.path.join(imgs_dir, filename)with tf.gfile.GFile(img_path, 'rb') as fid:encoded_jpg = fid.read()encoded_jpg_io = io.BytesIO(encoded_jpg)image = PIL.Image.open(encoded_jpg_io)if image.format != 'JPEG':raise ValueError('Image format not JPEG')key = hashlib.sha256(encoded_jpg).hexdigest()xmin = []ymin = []xmax = []ymax = []classes = []classes_text = []box_num = int((len(content_list) - 4) / 5)    #一张图上可能有多个labelfor i in range(box_num):xmin.append(float(content_list[5 * i + 4 + 0]) / width)ymin.append(float(content_list[5 * i + 4 + 1]) / height)xmax.append(float(content_list[5 * i + 4 + 2]) / width)ymax.append(float(content_list[5 * i + 4 + 3]) / height)classes_text.append(content_list[5 * i + 4 + 4].encode('utf8'))classes.append(classMap[content_list[5 * i + 4 + 4]])print('the class id is {} '.format(classMap[content_list[5 * i + 4 + 4]]))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.encode('utf8')),'image/source_id': dataset_util.bytes_feature(filename.encode('utf8')),'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),'image/encoded': dataset_util.bytes_feature(encoded_jpg),'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),'image/object/bbox/xmin': dataset_util.float_list_feature(xmin),'image/object/bbox/xmax': dataset_util.float_list_feature(xmax),'image/object/bbox/ymin': dataset_util.float_list_feature(ymin),'image/object/bbox/ymax': dataset_util.float_list_feature(ymax),'image/object/class/text': dataset_util.bytes_list_feature(classes_text),'image/object/class/label': dataset_util.int64_list_feature(classes),}))return exampledef main(_):# train tfrecord generateprint("Reading from {}".format(FLAGS.train_imgs_dir))writer = tf.python_io.TFRecordWriter(FLAGS.train_output)content_list_all = create_coordinate_info_of_content_list(FLAGS.train_imgs_dir, FLAGS.train_labels)for line in content_list_all:content_list = linetf_example = create_tf_example(content_list, FLAGS.train_imgs_dir)writer.write(tf_example.SerializeToString())writer.close()if __name__ == '__main__':tf.app.run()

Tensorflow Object Detection API生成自己的tfrecord训练数据集相关推荐

  1. Tensorflow object detection API 搭建自己的目标检测模型并迁移到Android上

    参考链接:https://blog.csdn.net/dy_guox/article/details/79111949 之前参考上述一系列博客在Windows10下面成功运行了TensorFlow A ...

  2. ssd目标检测训练自己的数据_目标检测Tensorflow object detection API之训练自己的数据集...

    构建自己的模型之前,推荐先跑一下Tensorflow object detection API的demo JustDoIT:目标检测Tensorflow object detection API​zh ...

  3. 使用tensorflow object detection API 训练自己的目标检测模型 (三)

    在上一篇博客"使用tensorflow object detection API 训练自己的目标检测模型 (二)"中介绍了如何使用LabelImg标记数据集,生成.xml文件,经过 ...

  4. 建立自己的数据集 并用Tensorflow object detection API进行训练

    ps: 欢迎大家光临我的博客 建立数据集 标注工具: ubuntu 图像标注工具labelImg sudo apt-get install pyqt5-dev-tools sudo pip3 inst ...

  5. TensorFlow Object Detection API 超详细教程和踩坑过程

    安装Anacond:https://blog.csdn.net/ITLearnHall/article/details/81708148 安装Pycharm:https://blog.csdn.net ...

  6. TensorFlow Object Detection API入门例子 (小浣熊检测上)

    玩了一周的TensorFlow Object Detection API了,是时候记录一下,踩过的坑都快忘记了~ 首先,总结一下检测的流程,实验分以下几步完成: 收集并标注数据 数据格式转换 确定训练 ...

  7. TensorFlow Object Detection API 技术手册(5)——制作自己的目标检测数据集

    TensorFlow Object Detection API 技术手册(5)--制作自己的目标检测数据集 (一)收集图片 (二)安装图像打标工具labelImg (三)将XML文件转化为CSV文件 ...

  8. Tensorflow object detection API 搭建属于自己的物体识别模型(转载修改)

    Tensorflow object detection API 搭建属于自己的物体识别模型 电脑配置信息 开始搭建环境 测试自带案例 准备训练图片 配置文件与模型 开始训练模型 生成最终的训练文件 测 ...

  9. 基于TensorFlow Object Detection API训练自己的目标识别模型

    基于TensorFlow Object Detection API训练自己的目标识别模型 环境 Windows10 CUDA_9 Cudnn_9.0 Anaconda3-5.2.0 Tensorflo ...

最新文章

  1. 【Linux】在虚拟机上安装CentOS7
  2. [NET] 如何从 Winform 移植到 Webform [自己搞定HTTP协议]
  3. python数据处理实例-入门Python数据分析最好的实战项目(一)
  4. 尝试为文件附加自动命名的数据库,但失败。已存在同名的数据库,或指定的文件无法打开或位于 UNC 共享目录中...
  5. unity安装,sdk,jdk问题
  6. 力扣刷题常用数据结构和方法(java版本)
  7. 可以部署在广域网执行QQ高仿版 GG2014 (源代码)
  8. html 只能输入正数,vue 限制input只能输入正数
  9. SPOJ COT Count on a tree(主席树+倍增lca)
  10. Kaggle 数据清洗挑战 Day 3 - 快速解析日期(date)数据
  11. 嵌入html_音视频格式转换神器与html视频元素加字幕——零基础自学网页制作
  12. If you already have a 64-bit JDK installed。。。。
  13. ARM与x86的战争史诗(连载1):Wintel帝国(ZZ)
  14. 优化模型验证关键代码06:多行程旅行商问题(mTSP)
  15. 系统性能统计(CPU占用率,内存占用率,系统平均负载)
  16. 台式计算机启动时 每次按f1,开机按f1的解决方法_电脑开机每次都要按F1,怎么解决...
  17. 将腾讯视频QLV格式转换为MP4格式
  18. python进阶——AI视觉实现口罩检测实时语音报警系统
  19. 中国软件外包企业的出路
  20. 搭建个人家用NAS网络存储服务器

热门文章

  1. win7下卸载oracle10g
  2. 世界大学排名(前100名)一览表
  3. Python编程快速上手——让繁琐工作自动化
  4. ElasticSearch倒排索引详解
  5. 【好书推荐】推荐一份从入门到进阶的机器学习书单
  6. 制作OSGB数据索引
  7. 电子对抗中的烧穿距离
  8. 3.1.1 存储器的分类3.1.2. 存储器的性能指标
  9. 带你认识闻名遐迩的ZBrush
  10. 英特尔Nick McKeown:定义边缘 引领前沿