tensorflow使用object detection完成目标检测的实例——无数的坑超详细吐血整理

在搭建完object detection环境之后(参考文章:https://blog.csdn.net/qq_17854471/article/details/89764428)
我便开始着手参照文章做一个自己的小应用,目标是通过训练图片,让机器学习检测图片中是否含有武大靖这个人。
参考文章:
https://blog.csdn.net/dy_guox/article/details/79111949
这个博主还录制了视频:
https://www.bilibili.com/video/av21539370/?p=2
另外还参考了:
https://blog.csdn.net/csdn_6105/article/details/82933628

1、在网上收集武大靖的照片,一共100张,文件名1-100.jpg
在models\research\object_detection目录下面新建images文件夹,存放图片。新建连个子文件夹 train和test,分别存放训练集图片和测试集图片。
2、使用下载LabelImg软件,对图片进行标注:
在这里插入图片描述
自定义标签 wdj,每个图片保存之后会生成一个.xml文件,保存标签的位置信息。
3、将xml文件转换成csv文件:

# -*- coding: utf-8 -*-
"""
Created on Fri May  3 08:24:30 2019

@author: jack
"""

import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET


def xml_to_csv(path):
    xml_list = []
    for xml_file in glob.glob(path + '\\*.xml'):
        print(xml_file)
        tree = ET.parse(xml_file)
        root = tree.getroot()
        for member in root.findall('object'):
            value = (root.find('filename').text,
                     int(root.find('size')[0].text),
                     int(root.find('size')[1].text),
                     member[0].text,
                     int(member[4][0].text),
                     int(member[4][1].text),
                     int(member[4][2].text),
                     int(member[4][3].text)
                     )
            xml_list.append(value)
    column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
    xml_df = pd.DataFrame(xml_list, columns=column_name)
    return xml_df


def main():
    print(os.getcwd())
    for folder in ['train','test']:
        image_path = os.path.join('C:\\Users\\jack\\models\\research\\object_detection\\', ('images\\' + folder))  
        print(image_path)
#这里就是需要访问的.xml的存放地址
        xml_df = xml_to_csv(image_path)                              
        #object_detection/images/train or test
        xml_df.to_csv(('C:\\Users\\jack\\models\\research\\object_detection\\images\\' + folder + '_labels.csv'), index=None)
        print('Successfully converted xml to csv.')

main()

image_path这里容易出错,我索性改成了绝对路径。执行成功之后images文件夹下面就有两个新的csv文件了。文件内容如下:
在这里插入图片描述
4、将csv文件生成tfrecord格式:这一步把我折磨惨了。。。。一直报错。
程序如下:

"""
Usage:
  # From tensorflow/models/
  # Create train data:
  python generate_tfrecord.py --csv_input=images/train_labels.csv --image_dir=images/train --output_path=train.record


  python generate_tfrecord.py --csv_input=images/test_labels.csv  --image_dir=images/test --output_path=test.record
"""
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

from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict

flags = tf.app.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('image_dir', '', 'Path to the image directory')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS

def class_text_to_int(row_label):
    if row_label == 'wdj':  #这里需要自己修改,有多少个类就该多少个!!!!
        return 1
    else:
        None


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):
    with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_j
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值