├── ObjectDectectDemo ├── nms.pyc ├── tools.pyc ├── ckpt │ ├── checkpoint │ ├── tipooling.ckpt.index │ └── tipooling.ckpt.meta ├── image │ ├── input.JPG │ └── result.png ├── toolstrain.pyc ├── rot_mnist12K_model.pyc ├── nms.py ├── tools.py ├── toolstrain.py ├── rot_mnist12K_model.py ├── rot_mnist12K.py └── Jiayoujidemo.py └── README.md /ObjectDectectDemo/nms.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/HEAD/ObjectDectectDemo/nms.pyc -------------------------------------------------------------------------------- /ObjectDectectDemo/tools.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/HEAD/ObjectDectectDemo/tools.pyc -------------------------------------------------------------------------------- /ObjectDectectDemo/ckpt/checkpoint: -------------------------------------------------------------------------------- 1 | model_checkpoint_path: "tipooling.ckpt" 2 | all_model_checkpoint_paths: "tipooling.ckpt" 3 | -------------------------------------------------------------------------------- /ObjectDectectDemo/image/input.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/HEAD/ObjectDectectDemo/image/input.JPG -------------------------------------------------------------------------------- /ObjectDectectDemo/toolstrain.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/HEAD/ObjectDectectDemo/toolstrain.pyc -------------------------------------------------------------------------------- /ObjectDectectDemo/image/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/HEAD/ObjectDectectDemo/image/result.png -------------------------------------------------------------------------------- /ObjectDectectDemo/rot_mnist12K_model.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/HEAD/ObjectDectectDemo/rot_mnist12K_model.pyc -------------------------------------------------------------------------------- /ObjectDectectDemo/ckpt/tipooling.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/HEAD/ObjectDectectDemo/ckpt/tipooling.ckpt.index -------------------------------------------------------------------------------- /ObjectDectectDemo/ckpt/tipooling.ckpt.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/HEAD/ObjectDectectDemo/ckpt/tipooling.ckpt.meta -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PlaneObjectDetect 2 | 使用教老的Two Stage策略进行飞机识别,本识别代码可以轻松移植到任何的目标检测任务中,只需要你重新训练CNN网络 3 | # 效果 4 | 输入图像: 5 | ![Aaron Swartz](https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/master/ObjectDectectDemo/image/input.JPG) 6 | 结果图像: 7 | ![Aaron Swartz](https://raw.githubusercontent.com/jiangruoqiao/PlaneObjectDetect/master/ObjectDectectDemo/image/result.png) 8 | # 说明 9 | 使用Seletive Search进行图像分割,对于遥感图像目标来说由于具有旋转,所以使用TI-Pooling网络进行目标分类 10 | 同时为了解决Selective Search算法使用单一的sigam值无法完美将目标分割出来的问题,本项目使用多线程同时对在多个敏感尺度和高斯核下进行图像分割,完美解决这一问题,如有需要可以再开多几个线程进行图像分割 11 | # 使用 12 | 直接运行jiayoujidemo.py即可运行,使用前请确定图片目录正确 13 | # 软件版本 14 | Python 2.7 15 | Tensorflow-gpu 1.4 16 | Opencv 17 | Pillow 18 | Skimage 19 | # 扩展 20 | 如果你需要将此扩展到其他图像目标的识别,请重新训练CNN模型,并进行更换,对于遥感图像目标检测推荐识别旋转不变性网络进行训练,如有需要请参考本人另外一个Github项目,复现西北工业大学自动化学院老师的RICNN网络 21 | -------------------------------------------------------------------------------- /ObjectDectectDemo/nms.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def nonMaximumSuppression(boxes, overlapThresh): 4 | # If there are no boxes, return an empty list 5 | if len(boxes) == 0: 6 | return [] 7 | 8 | # Initialize the picked bounding boxes list 9 | pick = [] 10 | 11 | # Get the coordinates of the bounding boxes 12 | x1 = boxes[:,0] 13 | y1 = boxes[:,1] 14 | x2 = boxes[:,2] 15 | y2 = boxes[:,3] 16 | 17 | # Compute the area of the bounding boxes 18 | area = (x2-x1+1) * (y2-y1+1) 19 | # Sort the bounding boxes by the bottom-right y-coordinate of the bounding box 20 | idxs = np.argsort(y2) 21 | 22 | # Loop over the indexes list 23 | while len(idxs) > 0: 24 | # Get the last index and put it into pick list as the first value to compare 25 | last = len(idxs) - 1 26 | i = idxs[last] 27 | pick.append(i) 28 | # Initialize suppression list 29 | suppress = [last] 30 | 31 | # Loop over all of the indexes in the idxs list 32 | for pos in range(0, last): 33 | # Get the current index 34 | j = idxs[pos] 35 | 36 | # Find the largest (x,y) coordinates for the start of the bounding box and 37 | # smallest (x,y) coordinates for the end of the bounding box 38 | xx1 = max(x1[i], x1[j]) 39 | yy1 = max(y1[i], y1[j]) 40 | xx2 = min(x2[i], x2[j]) 41 | yy2 = min(y2[i], y2[j]) 42 | 43 | # Compute the width and height of the bounding box 44 | w = max(0, xx2-xx1+1) 45 | h = max(0, yy2-yy1+1) 46 | 47 | # Compute the ratio of overlap between the computed bounding box and the 48 | # bounding box in the area list 49 | overlap = float(w*h)/area[j] 50 | 51 | # If there is sufficient overlap, suppress the current bounding box 52 | if overlap > overlapThresh: 53 | suppress.append(pos) 54 | 55 | # Delete all of the indexes in the suppression list 56 | idxs = np.delete(idxs, suppress) 57 | 58 | # Return the picked bounding boxes 59 | return boxes[pick] 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /ObjectDectectDemo/tools.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import math 3 | from scipy.ndimage.interpolation import rotate 4 | 5 | class DataLoader: 6 | def __init__(self, 7 | name, 8 | number_of_classes, 9 | number_of_transformations, 10 | loaded_size, 11 | desired_size, 12 | max_size=None): 13 | loaded = np.loadtxt(name) 14 | if max_size is not None: 15 | subset = np.random.choice(loaded.shape[0], max_size, replace=False) 16 | loaded = loaded[subset, :] 17 | padded_x = self._pad(loaded[:, :-1], loaded_size, desired_size) 18 | self._x = self._transform(padded_x, number_of_transformations) 19 | self._y = self._int_labels_to_one_hot(loaded[:, -1], number_of_classes) 20 | self._completed_epochs = -1 21 | self._new_epoch = False 22 | self._start_new_epoch() 23 | 24 | def _pad(self, loaded_x, loaded_size, desired_size): 25 | padding_size = (desired_size - loaded_size) / 2 26 | padding_list = [[0, 0], 27 | [padding_size, padding_size], 28 | [padding_size, padding_size], 29 | [0, 0]] 30 | return np.pad(np.reshape(loaded_x, [-1, loaded_size, loaded_size, 1]), 31 | padding_list, 32 | 'constant', 33 | constant_values=0) 34 | 35 | def _transform(self, padded, number_of_transformations): 36 | tiled = np.tile(np.expand_dims(padded, 4), [number_of_transformations]) 37 | for transformation_index in xrange(number_of_transformations): 38 | angle = 360.0 * transformation_index / float(number_of_transformations) 39 | tiled[:, :, :, :, transformation_index] = rotate( 40 | tiled[:, :, :, :, transformation_index], 41 | angle, 42 | axes=[1, 2], 43 | reshape=False) 44 | print('finished transforming') 45 | return tiled 46 | 47 | def _int_labels_to_one_hot(self, int_labels, number_of_classes): 48 | offsets = np.arange(self._size()) * number_of_classes 49 | one_hot_labels = np.zeros((self._size(), number_of_classes)) 50 | flat_iterator = one_hot_labels.flat 51 | for index in xrange(self._size()): 52 | flat_iterator[offsets[index] + int(int_labels[index])] = 1 53 | return one_hot_labels 54 | 55 | def _size(self): 56 | return self._x.shape[0] 57 | 58 | def _start_new_epoch(self): 59 | permuted_indexes = np.random.permutation(self._size()) 60 | self._x = self._x[permuted_indexes, :] 61 | self._y = self._y[permuted_indexes] 62 | self._completed_epochs += 1 63 | self._index = 0 64 | self._new_epoch = True 65 | 66 | def get_completed_epochs(self): 67 | return self._completed_epochs 68 | 69 | def is_new_epoch(self): 70 | return self._new_epoch 71 | 72 | def next_batch(self, batch_size): 73 | if (self._new_epoch): 74 | self._new_epoch = False 75 | start = self._index 76 | end = start + batch_size 77 | if (end > self._size()): 78 | assert batch_size <= self._size() 79 | self._start_new_epoch() 80 | start = 0 81 | end = start + batch_size 82 | self._index += batch_size 83 | return self._x[start:end, :], self._y[start:end] 84 | 85 | def all(self): 86 | return self._x, self._y 87 | -------------------------------------------------------------------------------- /ObjectDectectDemo/toolstrain.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import math 3 | from scipy.ndimage.interpolation import rotate 4 | import h5py 5 | 6 | class DataLoader: 7 | def __init__(self, 8 | name, 9 | number_of_classes, 10 | number_of_transformations, 11 | loaded_size, 12 | desired_size, 13 | max_size=None): 14 | #loaded = np.loadtxt(name) 15 | 16 | 17 | file = h5py.File(name, 'r') 18 | loaded = file['data'][:] 19 | label = file['label'][:] 20 | print loaded.shape 21 | print label.shape 22 | 23 | if max_size is not None: 24 | subset = np.random.choice(loaded.shape[0], max_size, replace=False) 25 | loaded = loaded[subset, :] 26 | 27 | self._x = loaded 28 | self._y = self._int_labels_to_one_hot(label, number_of_classes) 29 | self._completed_epochs = -1 30 | self._new_epoch = False 31 | self._start_new_epoch() 32 | 33 | def _pad(self, loaded_x, loaded_size, desired_size): 34 | padding_size = (desired_size - loaded_size) / 2 35 | padding_list = [[0, 0], 36 | [padding_size, padding_size], 37 | [padding_size, padding_size], 38 | [0, 0]] 39 | return np.pad(np.reshape(loaded_x, [-1, loaded_size, loaded_size, 1]), 40 | padding_list, 41 | 'constant', 42 | constant_values=0) 43 | 44 | def _transform(self, padded, number_of_transformations): 45 | tiled = np.tile(np.expand_dims(padded, 4), [number_of_transformations]) 46 | for transformation_index in xrange(number_of_transformations): 47 | angle = 360.0 * transformation_index / float(number_of_transformations) 48 | tiled[:, :, :, :, transformation_index] = rotate( 49 | tiled[:, :, :, :, transformation_index], 50 | angle, 51 | axes=[1, 2], 52 | reshape=False) 53 | print('finished transforming') 54 | return tiled 55 | 56 | def _int_labels_to_one_hot(self, int_labels, number_of_classes): 57 | offsets = np.arange(self._size()) * number_of_classes 58 | one_hot_labels = np.zeros((self._size(), number_of_classes)) 59 | flat_iterator = one_hot_labels.flat 60 | for index in xrange(self._size()): 61 | flat_iterator[offsets[index] + int(int_labels[index])] = 1 62 | return one_hot_labels 63 | 64 | def _size(self): 65 | return self._x.shape[0] 66 | 67 | def _start_new_epoch(self): 68 | permuted_indexes = np.random.permutation(self._size()) 69 | self._x = self._x[permuted_indexes, :] 70 | self._y = self._y[permuted_indexes] 71 | self._completed_epochs += 1 72 | self._index = 0 73 | self._new_epoch = True 74 | 75 | def get_completed_epochs(self): 76 | return self._completed_epochs 77 | 78 | def is_new_epoch(self): 79 | return self._new_epoch 80 | 81 | def next_batch(self, batch_size): 82 | if (self._new_epoch): 83 | self._new_epoch = False 84 | start = self._index 85 | end = start + batch_size 86 | if (end > self._size()): 87 | assert batch_size <= self._size() 88 | self._start_new_epoch() 89 | start = 0 90 | end = start + batch_size 91 | self._index += batch_size 92 | return self._x[start:end, :], self._y[start:end] 93 | 94 | def all(self): 95 | return self._x, self._y 96 | -------------------------------------------------------------------------------- /ObjectDectectDemo/rot_mnist12K_model.py: -------------------------------------------------------------------------------- 1 | import math 2 | import tensorflow as tf 3 | 4 | def conv2d(x, W): 5 | return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 6 | 7 | def max_pool_2x2(x): 8 | return tf.nn.max_pool(x, 9 | ksize=[1, 2, 2, 1], 10 | strides=[1, 2, 2, 1], 11 | padding='SAME') 12 | 13 | # xavier-like initializer 14 | def weights_biases(kernel_shape, bias_shape): 15 | in_variables = 1 16 | for index in xrange(len(kernel_shape) - 1): 17 | in_variables *= kernel_shape[index] 18 | stdv = 1.0 / math.sqrt(in_variables) 19 | weights = tf.get_variable( 20 | 'weights', 21 | kernel_shape, 22 | initializer=tf.random_uniform_initializer(-stdv, stdv)) 23 | biases = tf.get_variable( 24 | 'biases', 25 | bias_shape, 26 | initializer=tf.random_uniform_initializer(-stdv, stdv)) 27 | return weights, biases 28 | 29 | def conv_relu_maxpool(input, kernel_shape, bias_shape): 30 | weights, biases = weights_biases(kernel_shape, bias_shape) 31 | return max_pool_2x2(tf.nn.relu(conv2d(input, weights) + biases)) 32 | 33 | def fc_relu(input, kernel_shape, bias_shape): 34 | weights, biases = weights_biases(kernel_shape, bias_shape) 35 | return tf.nn.relu(tf.matmul(input, weights) + biases) 36 | 37 | def fc(input, kernel_shape, bias_shape): 38 | weights, biases = weights_biases(kernel_shape, bias_shape) 39 | return tf.matmul(input, weights) + biases 40 | 41 | # x should already be reshaped as a 32x32x1 image 42 | def single_branch(x, number_of_filters, number_of_fc_features): 43 | with tf.variable_scope('conv1'): 44 | max_pool1 = conv_relu_maxpool(x, 45 | [3, 46 | 3, 47 | 3, 48 | number_of_filters], 49 | [number_of_filters]) 50 | with tf.variable_scope('conv2'): 51 | max_pool2 = conv_relu_maxpool(max_pool1, 52 | [3, 53 | 3, 54 | number_of_filters, 55 | 2 * number_of_filters], 56 | [2 * number_of_filters]) 57 | with tf.variable_scope('conv3'): 58 | max_pool3 = conv_relu_maxpool(max_pool2, 59 | [3, 60 | 3, 61 | 2 * number_of_filters, 62 | 4 * number_of_filters], 63 | [4 * number_of_filters]) 64 | flattened_size = ((32 / 8) ** 2) * 4 * number_of_filters 65 | flattened = tf.reshape(max_pool3, [-1, flattened_size]) 66 | with tf.variable_scope('fc1'): 67 | fc1 = fc_relu(flattened, 68 | [flattened_size, number_of_fc_features], 69 | [number_of_fc_features]) 70 | return fc1 71 | 72 | # x are batches nx32x32x1xnumber_of_transformations 73 | def define_model(x, 74 | keep_prob, 75 | number_of_classes, 76 | number_of_filters, 77 | number_of_fc_features): 78 | splitted = tf.unstack(x, axis=4) 79 | branches = [] 80 | with tf.variable_scope('branches') as scope: 81 | for index, tensor_slice in enumerate(splitted): 82 | branches.append(single_branch(splitted[index], 83 | number_of_filters, 84 | number_of_fc_features)) 85 | if (index == 0): 86 | scope.reuse_variables() 87 | concatenated = tf.stack(branches, axis=2) 88 | ti_pooled = tf.reduce_max(concatenated, reduction_indices=[2]) 89 | drop = tf.nn.dropout(ti_pooled, keep_prob) 90 | with tf.variable_scope('fc2'): 91 | logits = fc(drop, 92 | [number_of_fc_features, number_of_classes], 93 | [number_of_classes]) 94 | return logits 95 | -------------------------------------------------------------------------------- /ObjectDectectDemo/rot_mnist12K.py: -------------------------------------------------------------------------------- 1 | #ecoding:utf-8 2 | import tools 3 | import rot_mnist12K_model 4 | import tensorflow as tf 5 | import sys 6 | import numpy as np 7 | import toolstrain 8 | import os 9 | 10 | os.environ["CUDA_VISIBLE_DEVICES"] = "2" 11 | TRAIN_FILENAME = '/media/ubuntu/2c0e4fe1-8b1d-4526-b308-a7173c23f87e/NWPUTrain.h5' 12 | TEST_FILENAME = '/media/ubuntu/2c0e4fe1-8b1d-4526-b308-a7173c23f87e/NWPUTest.h5' 13 | LOADED_SIZE = 28 14 | DESIRED_SIZE = 32 15 | # model constants 16 | NUMBER_OF_CLASSES = 11 17 | NUMBER_OF_FILTERS = 40 18 | NUMBER_OF_FC_FEATURES = 5120 19 | NUMBER_OF_TRANSFORMATIONS = 8 20 | # optimization constants 21 | BATCH_SIZE = 64 22 | TEST_CHUNK_SIZE = 517 23 | ADAM_LEARNING_RATE = 1e-4 24 | PRINTING_INTERVAL = 10 25 | # set seeds 26 | np.random.seed(100) 27 | tf.set_random_seed(100) 28 | # set up training graph 29 | x = tf.placeholder(tf.float32, shape=[None, 30 | DESIRED_SIZE, 31 | DESIRED_SIZE, 32 | 3, 33 | NUMBER_OF_TRANSFORMATIONS]) 34 | y_gt = tf.placeholder(tf.float32, shape=[None, NUMBER_OF_CLASSES]) 35 | keep_prob = tf.placeholder(tf.float32) 36 | logits = rot_mnist12K_model.define_model(x, 37 | keep_prob, 38 | NUMBER_OF_CLASSES, 39 | NUMBER_OF_FILTERS, 40 | NUMBER_OF_FC_FEATURES) 41 | cross_entropy = tf.reduce_mean( 42 | tf.nn.softmax_cross_entropy_with_logits(logits= logits, labels= y_gt)) 43 | train_step = tf.train.AdamOptimizer(ADAM_LEARNING_RATE).minimize(cross_entropy) 44 | correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y_gt, 1)) 45 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 46 | 47 | # run training 48 | session = tf.Session() 49 | session.run(tf.initialize_all_variables()) 50 | train_data_loader = toolstrain.DataLoader(TRAIN_FILENAME, 51 | NUMBER_OF_CLASSES, 52 | NUMBER_OF_TRANSFORMATIONS, 53 | LOADED_SIZE, 54 | DESIRED_SIZE) 55 | test_data_loader = toolstrain.DataLoader(TEST_FILENAME, 56 | NUMBER_OF_CLASSES, 57 | NUMBER_OF_TRANSFORMATIONS, 58 | LOADED_SIZE, 59 | DESIRED_SIZE) 60 | test_size = test_data_loader.all()[1].shape[0] 61 | print "aa" 62 | print test_size 63 | print test_size % TEST_CHUNK_SIZE 64 | assert test_size % TEST_CHUNK_SIZE == 0 65 | number_of_test_chunks = test_size / TEST_CHUNK_SIZE 66 | saver = tf.train.Saver() 67 | while (True): 68 | batch = train_data_loader.next_batch(BATCH_SIZE) 69 | if (train_data_loader.is_new_epoch()): 70 | train_accuracy = session.run(accuracy, feed_dict={x : batch[0], 71 | y_gt : batch[1], 72 | keep_prob : 1.0}) 73 | print("completed_epochs %d, training accuracy %g" % 74 | (train_data_loader.get_completed_epochs(), train_accuracy)) 75 | saver.save(session, 'ckpt/tipooling.ckpt') 76 | sys.stdout.flush() 77 | if (train_data_loader.get_completed_epochs() % PRINTING_INTERVAL == 0): 78 | sum = 0.0 79 | for chunk_index in xrange(number_of_test_chunks): 80 | chunk = test_data_loader.next_batch(TEST_CHUNK_SIZE) 81 | sum += session.run(accuracy, feed_dict={x : chunk[0], 82 | y_gt : chunk[1], 83 | keep_prob : 1.0}) 84 | test_accuracy = sum / number_of_test_chunks 85 | print("testing accuracy %g" % test_accuracy) 86 | sys.stdout.flush() 87 | session.run(train_step, feed_dict={x : batch[0], 88 | y_gt : batch[1], 89 | keep_prob : 0.5}) 90 | 91 | 92 | -------------------------------------------------------------------------------- /ObjectDectectDemo/Jiayoujidemo.py: -------------------------------------------------------------------------------- 1 | # ecoding:utf-8 2 | import Tkinter as tk 3 | import tkMessageBox 4 | import matplotlib.pyplot as plt 5 | import matplotlib.patches as mpatches 6 | import selectivesearch 7 | import cv2 8 | import numpy as np 9 | import nms 10 | import multiprocessing 11 | import tensorflow as tf 12 | from PIL import Image 13 | from skimage import io 14 | import rot_mnist12K_model 15 | import sys 16 | import numpy as np 17 | import os 18 | import random 19 | 20 | from scipy.ndimage.interpolation import rotate 21 | 22 | 23 | def image_to_matrix(image): 24 | image_data = image.getdata() 25 | image_data = np.matrix(image_data, dtype='float') / 255.0 26 | new_image_matrix = np.resize(image_data, (32, 32, 3)) 27 | return new_image_matrix 28 | 29 | 30 | def matrix_to_image(matrix): 31 | matrix = matrix * 255 32 | new = Image.fromarray(matrix.astype(np.uint8)) 33 | return new 34 | 35 | 36 | def IOU(Reframe, GTframe): 37 | label = GTframe[4] 38 | x1 = Reframe[0] 39 | y1 = Reframe[1] 40 | width1 = Reframe[2] - Reframe[0] 41 | height1 = Reframe[3] - Reframe[1] 42 | x2 = GTframe[0] 43 | y2 = GTframe[1] 44 | width2 = GTframe[2] - GTframe[0] 45 | height2 = GTframe[3] - GTframe[1] 46 | endx = max(x1 + width1, x2 + width2) 47 | startx = min(x1, x2) 48 | width = width1 + width2 - (endx - startx) 49 | endy = max(y1 + height1, y2 + height2) 50 | starty = min(y1, y2) 51 | height = height1 + height2 - (endy - starty) 52 | if width <= 0 or height <= 0: 53 | ratio = 0 # 重叠率为 0 54 | else: 55 | Area = width * height # 两矩形相交面积 56 | Area1 = width1 * height1 57 | Area2 = width2 * height2 58 | ratio = Area * 1. / (Area1 + Area2 - Area) 59 | return ratio, Reframe, GTframe, label 60 | 61 | 62 | def region_from_selective_search(img, value_scale, value_sigma): 63 | img_lbl, regions = selectivesearch.selective_search(img, scale=value_scale, sigma=value_sigma, min_size=100) 64 | return regions 65 | 66 | 67 | def make_corr(line): 68 | resultLine = [] 69 | numMat = eval(line) 70 | x1 = numMat[0][0] 71 | y1 = numMat[0][1] 72 | x2 = numMat[1][0] 73 | y2 = numMat[1][1] 74 | label = numMat[2] 75 | resultLine.append([x1, y1, x2, y2, label]) 76 | return resultLine 77 | 78 | 79 | def make_negative_region(imageFileNamePath, groundTruthFileNamePath, image_index): 80 | img = io.imread(imageFileNamePath) 81 | image = Image.open(imageFileNamePath) 82 | loadGrondTruth = open(groundTruthFileNamePath, 'r') 83 | labelResutlAll = [] 84 | numberOfLineInImage = 0 85 | for line in loadGrondTruth.readlines(): 86 | resultOfLabel = make_corr(line) 87 | labelResutlAll.extend(resultOfLabel) 88 | numberOfLineInImage = numberOfLineInImage + 1 89 | corrdGruondTruth = np.zeros((len(labelResutlAll), 5)) # 用来存放GroundTruth的坐标和标签 90 | number1 = 0 91 | fig1, ax1 = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) 92 | for x, y, w, h, label in labelResutlAll: 93 | corrdGruondTruth[number1, :] = ([int(x), int(y), int(w), int(h), int(label)]) 94 | rect = mpatches.Rectangle( 95 | (x, y), w, h, fill=False, edgecolor='red') 96 | ax1.add_patch(rect) 97 | number1 = number1 + 1 98 | reg_1 = [] 99 | reg_2 = [] 100 | reg_3 = [] 101 | reg_4 = [] 102 | reg_5 = [] 103 | reg_6 = [] 104 | pool = multiprocessing.Pool(processes=4) 105 | reg_1.append(pool.apply_async(region_from_selective_search, (img, 1000, 0.9))) 106 | reg_2.append(pool.apply_async(region_from_selective_search, (img, 1000, 0.8))) 107 | reg_3.append(pool.apply_async(region_from_selective_search, (img, 1000, 0.7))) 108 | reg_4.append(pool.apply_async(region_from_selective_search, (img, 800, 0.9))) 109 | reg_5.append(pool.apply_async(region_from_selective_search, (img, 800, 0.7))) 110 | reg_6.append(pool.apply_async(region_from_selective_search, (img, 800, 0.6))) 111 | pool.close() 112 | pool.join() 113 | regions_1 = [] 114 | regions_2 = [] 115 | regions_3 = [] 116 | regions_4 = [] 117 | regions_5 = [] 118 | regions_6 = [] 119 | for res_1 in reg_1: 120 | regions_1 = res_1.get() 121 | for res_2 in reg_2: 122 | regions_2 = res_2.get() 123 | for res_3 in reg_3: 124 | regions_3 = res_3.get() 125 | for res_4 in reg_4: 126 | regions_4 = res_4.get() 127 | for res_5 in reg_5: 128 | regions_5 = res_5.get() 129 | for res_6 in reg_6: 130 | regions_6 = res_6.get() 131 | regions = regions_1 + regions_2 + regions_3 + regions_4 + regions_5 + regions_6 132 | candidates = set() # 创建一个无序不重复元素集,可进行关系测试,可以删除重复数据,还可以计算交集等 133 | for r in regions: 134 | if r['rect'] in candidates: 135 | print(r['rect']) 136 | continue 137 | if r["size"] < 100: 138 | continue 139 | if r["size"] > 2000: 140 | continue 141 | x, y, w, h = r['rect'] 142 | if w == 0 or h == 0: 143 | continue 144 | if w / h > 1.5 or h / w > 1.5: 145 | continue 146 | candidates.add(r['rect']) 147 | fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) 148 | corrd = np.zeros((len(candidates), 4)) 149 | number = 0 150 | for x, y, w, h in candidates: 151 | corrd[number, :] = ([int(x), int(y), int(w + x), int(h + y)]) 152 | rect = mpatches.Rectangle( 153 | (x, y), w, h, fill=False, edgecolor='red') 154 | ax.add_patch(rect) 155 | number = number + 1 156 | return corrd, image 157 | 158 | 159 | def _int_labels_to_one_hot(int_labels, number_of_classes): 160 | offsets = np.arange(2) * number_of_classes 161 | one_hot_labels = np.zeros((2, number_of_classes)) 162 | flat_iterator = one_hot_labels.flat 163 | for index in xrange(2): 164 | flat_iterator[offsets[index] + int(int_labels[index])] = 1 165 | return one_hot_labels 166 | 167 | 168 | def _transform(padded, number_of_transformations): 169 | tiled = np.tile(np.expand_dims(padded, 4), [number_of_transformations]) 170 | for transformation_index in range(number_of_transformations): 171 | angle = 360.0 * transformation_index / float(number_of_transformations) 172 | tiled[:, :, :, :, transformation_index] = rotate( 173 | tiled[:, :, :, :, transformation_index], 174 | angle, 175 | axes=[1, 2], 176 | reshape=False) 177 | return tiled 178 | 179 | 180 | ''' 181 | top = tk.Tk() 182 | ##添加一个Label控件 183 | tk.Label(top, text = "图片加载路径:").grid(row = 0) 184 | e = tk.Entry(top) 185 | e.grid(row = 0, column = 1) 186 | e.delete(0, tk.END) 187 | e.insert(0, "添加图片路径") 188 | print e.get() 189 | B = tk.Button(top, text="加油机识别", command=helloCallBack) 190 | B.grid(row = 4, column = 1) 191 | top.mainloop() 192 | ''' 193 | 194 | LOADED_SIZE = 28 195 | DESIRED_SIZE = 32 196 | # model constants 197 | NUMBER_OF_CLASSES = 11 198 | NUMBER_OF_FILTERS = 40 199 | NUMBER_OF_FC_FEATURES = 5120 200 | NUMBER_OF_TRANSFORMATIONS = 8 201 | # optimization constants 202 | BATCH_SIZE = 64 203 | # set seeds 204 | np.random.seed(100) 205 | tf.set_random_seed(100) 206 | # set up training graph 207 | x = tf.placeholder(tf.float32, shape=[None, 208 | DESIRED_SIZE, 209 | DESIRED_SIZE, 210 | 3, 211 | NUMBER_OF_TRANSFORMATIONS]) 212 | y_gt = tf.placeholder(tf.float32, shape=[None, NUMBER_OF_CLASSES]) 213 | keep_prob = tf.placeholder(tf.float32) 214 | logits = rot_mnist12K_model.define_model(x, 215 | keep_prob, 216 | NUMBER_OF_CLASSES, 217 | NUMBER_OF_FILTERS, 218 | NUMBER_OF_FC_FEATURES) 219 | # run training 220 | fileName = '/home/yx/桌面/IMG_3460.JPG' 221 | # 求ground truth 222 | groundTruthFile = '/home/yx/桌面/NWPU VHR-10 dataset/ground truth/ 26.txt' 223 | ang, img = make_negative_region(fileName, groundTruthFile, 26) 224 | image = cv2.imread(fileName) 225 | session = tf.Session() 226 | saver = tf.train.Saver() 227 | saver.restore(session, "ckpt/tipooling.ckpt") 228 | result_set = [] 229 | for index in range(0, len(ang), 1): 230 | y1 = int(ang[index][1]) 231 | y2 = int(ang[index][3]) 232 | x1 = int(ang[index][0]) 233 | x2 = int(ang[index][2]) 234 | label = 1 235 | if y2 - y1 > x2 - x1: 236 | cropped = img.crop((x1, y1, x1 + x2 - x1, y1 + x2 - x1)) 237 | else: 238 | cropped = img.crop((x1, y1, x1 + y2 - y1, y1 + y2 - y1)) 239 | newImage = cropped.resize((32, 32)) 240 | newMatrix = image_to_matrix(newImage) 241 | newMatrix = np.resize(newMatrix, (32, 32, 3)) 242 | tem = np.zeros([2, 32, 32, 3]) 243 | tem[1, :, :, :] = newMatrix 244 | inputx = _transform(tem, 8) 245 | inputx = inputx[1, :, :, :, :] 246 | inputx = np.resize(inputx, (1, 32, 32, 3, 8)) 247 | temlabel = np.zeros([2]) 248 | temlabel[0] = label 249 | inputlabal = _int_labels_to_one_hot(temlabel, 11) 250 | inputlabal = inputlabal[0] 251 | inputlabal = np.resize(inputx, (1, 11)) 252 | train_accuracy = session.run(tf.argmax(logits, 1), feed_dict={x: inputx, 253 | y_gt: inputlabal, 254 | keep_prob: 1.0}) 255 | if train_accuracy[0] == 1: 256 | result_set.append([int(x1), int(y1), int(x2), int(y2)]) 257 | print train_accuracy 258 | sys.stdout.flush() 259 | 260 | length = len(result_set) 261 | corrd = np.zeros((len(result_set), 4)) 262 | for index2 in range(0, length): 263 | corrd[index2, 0] = result_set[index2][0] 264 | corrd[index2, 1] = result_set[index2][1] 265 | corrd[index2, 2] = result_set[index2][2] 266 | corrd[index2, 3] = result_set[index2][3] 267 | 268 | orig = image.copy() 269 | for (startX, startY, endX, endY) in corrd: 270 | cv2.rectangle(orig, (int(startX), int(startY)), (int(endX), int(endY)), 271 | (int(random.uniform(0, 255)), int(random.uniform(0, 255)), int(random.uniform(0, 255)) 272 | ), 2) 273 | pick = nms.nonMaximumSuppression(corrd, 0.4) 274 | for (startX, startY, endX, endY) in pick: 275 | cv2.rectangle(image, (int(startX), int(startY)), (int(endX), int(endY)), (0, 255, 0), 2) 276 | 277 | cv2.imshow("Original", orig) 278 | cv2.imshow("After NMS", image) 279 | cv2.waitKey(1000000) 280 | --------------------------------------------------------------------------------