把faster-rcnn检测出来的结果保存成txt,再转成xml

这篇博客介绍了如何使用Faster R-CNN模型进行图像检测,并将检测结果保存为TXT文件。接着,博主分享了一段Python代码实现此功能,并指出需要修改的部分。此外,还提供了MATLAB代码将TXT转换为XML,特别指出如果图片格式为JPEG,需要额外调整代码中的两个地方。
摘要由CSDN通过智能技术生成

利用faster-rcnn检测图片,先把结果保存成txt,就像下面这样

利用下面这段代码就可以做到,把这段代码保存成XX.py,再运行。代码里需要改的地方都注释了,不知道怎么上传源码的,将就着用

#!/usr/bin/env python
# -*- coding: UTF-8 -*- 
# --------------------------------------------------------
# Faster R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

"""
Demo script showing detections in sample images.

See README.md for installation instructions before running.
"""

import _init_paths
from fast_rcnn.config import cfg
from fast_rcnn.test import im_detect
from fast_rcnn.nms_wrapper import nms
from utils.timer import Timer
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import caffe, os, sys, cv2
import argparse

CLASSES = ('__background__',
           'aeroplane', 'bicycle', 'bird', 'boat',
           'bottle', 'bus', 'car', 'cat', 'chair',
           'cow', 'diningtable', 'dog', 'horse',
           'motorbike', 'person', 'pottedplant',
           'sheep', 'sofa', 'train', 'tvmonitor')                                     #改成你的类别

NETS = {'vgg16': ('VGG16',
                  'VGG16_faster_rcnn_final.caffemodel'),
        'zf': ('ZF',
                  'ZF_faster_rcnn_final.caffemodel')}


def vis_detections(image_name, class_name, dets, thresh=0.5):
    """Draw detected bounding boxes."""
    inds = np.where(dets[:, -1] >= thresh)[0]
    if len(inds) == 0:
        return

    for i in inds:
        bbox = dets[i, :4]
        score = dets[i, -1]
	if(class_name == '__background__'):
            fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')                                                             #最终的txt保存在这个路径下,下面的都改
	    fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')
            #                    图片名                 标签名                          四个坐标
	    fw.close()
	elif(class_name == 'aeroplane'):
            fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')
	    fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')
	    fw.close()
        elif(class_name == 'bicycle'):    
            fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')
	    fw.write(str(image_name)+' '+'n02835271'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  #双人自行车
	    fw.close()
        elif(class_name == 'bird'):      
            fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')
	    fw.write(str(image_name)+' '+'n01833805'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')   #蜂鸟
	    fw.close()
        elif(class_name == 'boat'):
            fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')
	    fw.write(str(image_name)+' '+'n04273569'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')    #快艇
	    fw.close()
        elif(class_name == 'bottle'):
            fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')
	    fw.write(str(image_name)+' '+'n04557648'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')    #水瓶
	    fw.close()
        elif(class_name == 
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值