将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练。

import xml.etree.ElementTree as ET
import numpy as np
import os
import tensorflow as tf
from PIL import Image classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return [x, y, w, h] def convert_annotation(image_id):
in_file = open('F:/xml/%s.xml'%(image_id)) tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
bboxes = []
for i, obj in enumerate(root.iter('object')):
if i > 29:
break
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert((w, h), b) + [cls_id]
bboxes.extend(bb)
if len(bboxes) < 30*5:
bboxes = bboxes + [0, 0, 0, 0, 0]*(30-int(len(bboxes)/5)) return np.array(bboxes, dtype=np.float32).flatten().tolist() def convert_img(image_id):
image = Image.open('F:/snow leopard/test_im/%s.jpg' % (image_id))
resized_image = image.resize((416, 416), Image.BICUBIC)
image_data = np.array(resized_image, dtype='float32')/255
img_raw = image_data.tobytes()
return img_raw filename = os.path.join('test'+'.tfrecords')
writer = tf.python_io.TFRecordWriter(filename)
# image_ids = open('F:/snow leopard/test_im/%s.txt' % (
# year, year, image_set)).read().strip().split() image_ids = os.listdir('F:/snow leopard/test_im/')
# print(filename)
for image_id in image_ids:
print (image_id)
image_id = image_id.split('.')[0]
print (image_id) xywhc = convert_annotation(image_id)
img_raw = convert_img(image_id) example = tf.train.Example(features=tf.train.Features(feature={
'xywhc':
tf.train.Feature(float_list=tf.train.FloatList(value=xywhc)),
'img':
tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),
}))
writer.write(example.SerializeToString())
writer.close()

  

Python读取文件夹下图片的两种方法:

import os
imagelist = os.listdir('./images/') #读取images文件夹下所有文件的名字
import glob
imagelist= sorted(glob.glob('./images/' + 'frame_*.png')) #读取带有相同关键字的图片名字,比上一中方法好

参考:

https://blog.csdn.net/CV_YOU/article/details/80778392

https://github.com/raytroop/YOLOv3_tf

目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练的更多相关文章

  1. 训练自己数据-xml文件转voc格式

    首先我们有一堆xml文件 笔者是将mask-rcnn得到的json标注文件转为xml的 批量json转xml方法:https://www.cnblogs.com/bob-jianfeng/p/1112 ...

  2. yolo系列目标检测+自标注数据集进行目标识别

    1. yolov1的识别原理 参考:https://blog.csdn.net/u010712012/article/details/85116365 https://blog.csdn.net/gb ...

  3. [AI开发]目标检测之素材标注

    算力和数据是影响深度学习应用效果的两个关键因素,在算力满足条件的情况下,为了到达更好的效果,我们需要将海量.高质量的素材数据喂给神经网络,训练出高精度的网络模型.吴恩达在深度学习公开课中提到,在算力满 ...

  4. (转)如何用TensorLayer做目标检测的数据增强

    数据增强在机器学习中的作用不言而喻.和图片分类的数据增强不同,训练目标检测模型的数据增强在对图像做处理时,还需要对图片中每个目标的坐标做相应的处理.此外,位移.裁剪等操作还有可能使得一些目标在处理后只 ...

  5. 第三十二节,使用谷歌Object Detection API进行目标检测、训练新的模型(使用VOC 2012数据集)

    前面已经介绍了几种经典的目标检测算法,光学习理论不实践的效果并不大,这里我们使用谷歌的开源框架来实现目标检测.至于为什么不去自己实现呢?主要是因为自己实现比较麻烦,而且调参比较麻烦,我们直接利用别人的 ...

  6. 【目标检测实战】目标检测实战之一--手把手教你LMDB格式数据集制作!

    文章目录 1 目标检测简介 2 lmdb数据制作 2.1 VOC数据制作 2.2 lmdb文件生成 lmdb格式的数据是在使用caffe进行目标检测或分类时,使用的一种数据格式.这里我主要以目标检测为 ...

  7. 平均精度均值(mAP)——目标检测模型性能统计量

    在机器学习领域,对于大多数常见问题,通常会有多个模型可供选择.当然,每个模型会有自己的特性,并会受到不同因素的影响而表现不同. 每个模型的好坏是通过评价它在某个数据集上的性能来判断的,这个数据集通常被 ...

  8. 目标检测模型的性能评估--MAP(Mean Average Precision)

    目标检测模型中性能评估的几个重要参数有精确度,精确度和召回率.本文中我们将讨论一个常用的度量指标:均值平均精度,即MAP. 在二元分类中,精确度和召回率是一个简单直观的统计量,但是在目标检测中有所不同 ...

  9. 【目标检测】SSD:

    slides 讲得是相当清楚了: http://www.cs.unc.edu/~wliu/papers/ssd_eccv2016_slide.pdf 配合中文翻译来看: https://www.cnb ...

随机推荐

  1. c/c++ 函数指针的用法

    [目录] 基本定义 c 函数指针使用举例 c++ 函数指针使用举例 函数指针作为函数参数 函数指针作为函数返回值 函数指针数组 typedef 简化函数指针操作 c语言函数指针的定义形式:返回类型 ( ...

  2. Mina.Net实现的断线重连

    using Mina.Filter.Codec; using Mina.Filter.Codec.TextLine; using System; using System.Collections.Ge ...

  3. SHELL 循环获取日期以及FOR使用

    ;i<=;i++)); do PYTHONPATH=lib/ bin/cupid -c conf/config.cfg -u http://shop33220311.taobao.com/?tb ...

  4. java中经常使用的快捷键

    Eclipse(MyEclipse) 经常使用快捷键Eclipse 的编辑功能很强大.掌握了 Eclipse 快捷键功能,能够大大提高开发效率. Eclipse中有例如以下一些和编辑相关的快捷键.1. ...

  5. 〖Linux〗Ubuntu中使用KVM安装虚拟机

    1. 安装软件: sudo apt-get install libvirt0 libvirt-bin libvirt-dev virt-manager qemu-system 2. 配置网桥: # i ...

  6. 【Linux】找出文件之间的差异

    使用命令comm可以找出2个文件之间的差异 现在有文件如下: Linux:/qinys # cat A.txt apple lemon onion orange pear Linux:/qinys # ...

  7. django之创建第10个项目-图片上传方式1

    1.upload.HTMl <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang=& ...

  8. python模块之importlib(py3中功能有明显加强)

    # -*- coding: utf-8 -*-#python 27#xiaodeng#python模块之importlib(py3中功能有明显加强)

  9. 图解Win7如何手动添加受信任证书

    点击开始—>运行,如下图所示:   弹出“控制台”窗口如下,如下图所示:   点击“文件—添加/删除管理单元”,如下图所示:   选择“证书”,并点击“添加”,如下图所示:   在弹出的窗口上选 ...

  10. 彻底抛弃脚本录制,LR脚本之使用web_custom_request函数自定义

    原文  http://www.cnblogs.com/Bonnie83/p/3525200.html 初学性能测试时候,第一步必学脚本录制,但一路下来各种录制失败.回放脚本失败的问题层出不穷,究其原因 ...