利用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 ==