Object Detection Based On Yolo(一)

第一次写博客啦~

萌新请多指教

yolo是一个在速度上很优秀的object detection模型。

对yolo的算法理解请前往:https://blog.csdn.net/u014380165/article/details/72616238

我们的目标是要做到这个样子:

 

 

这回,我们先对label进行预处理吧orz

我们先对yolo的label进行预处理

raw_data的label就差不多长这个样子啦

如:

88.82,98.33,98.82,191.42,456.95,184.42,447.95,99.33,薄利汽配

代表四个坐标值,每个坐标值就是框住object的四个顶点啦

label是一个13x13x6的格式

其中,6即(类别概率,x,y,w,h,置信度)

图片的预训练打算用inception v4,处理成17x17x1024的矩阵

这个也基本完工了

时间太紧,暂且这么多吧

代码如下:

然后就是 

只要你有label和对应的图片

换成你的路径

就能生成yolo所需要的label格式啦

写的不太好请见谅啊~

import numpy as np
import os
import cv2
#运行该程序前请新建一个文件夹把generate_label_array放进去
class label_preprocessing:
def __init__(self):
self.raw_images_path='./raw_data/image_9000/' #换成你自己的图片路径
self.raw_label_path='./raw_data/txt_9000/' #换成你自己的label路径
self.generate_data_path_from_filter_label_1='./labels/'
self.labels_matrix_path='./label_matrix/'
self.image_size=299
self.cell_size=13
def filter_label_1(self):
f=os.listdir(self.raw_label_path)
for fs in f:
print('*' * 20)
with open(fs,'r',encoding='utf-8') as file:
f_line=''
for line in file:
if '#' not in line.replace('\n','').split(',')[-1]:
f_line=f_line+line+' '
with open(self.generate_data_path_from_filter_label_1+fs,'w',encoding='utf-8') as label:
label.write(f_line)
print(fs,'done')
print('*' * 20)
def generate_float_label(self,label_):
with open(self.generate_data_path_from_filter_label_1+label_,'r',encoding='utf-8') as labels:
label_all_locations=[]
for label_txt in labels:
lo_str=label_txt.replace(' ', '').replace('\n', '').split(',')[:-1]
if len(lo_str)!=0:
locations=[float(t) for t in lo_str]
label_all_locations.append(locations)
return np.array(label_all_locations)
def load_annotation(self):
imname = os.listdir(self.raw_images_path)
# modify and delete
for i_name in imname:
try:
im = cv2.imread(self.raw_images_path+i_name)
im.shape
except:
continue
h_ratio = 1.0 * self.image_size / im.shape[0]
w_ratio = 1.0 * self.image_size / im.shape[1]
label = np.zeros((self.cell_size, self.cell_size, 6))

locations_matrix=self.generate_float_label(i_name[:-4]+'.txt')

for location in locations_matrix:
xmin=location[0]
ymin=location[1]
xmax=location[4]
ymax=location[5]
x1 = max(min((xmin - 1) * w_ratio, self.image_size - 1), 0)
y1 = max(min((ymin - 1) * h_ratio, self.image_size - 1), 0)
x2 = max(min((xmax - 1) * w_ratio, self.image_size - 1), 0)
y2 = max(min((ymax - 1) * h_ratio, self.image_size - 1), 0)
boxes = [(x2 + x1) / 2.0, (y2 + y1) / 2.0, x2 - x1, y2 - y1]
x_ind = int(boxes[0] * self.cell_size / self.image_size)
y_ind = int(boxes[1] * self.cell_size / self.image_size)
if label[y_ind, x_ind, 0] == 1:
continue
label[y_ind, x_ind, 0] = 1
label[y_ind, x_ind, 1:5] = boxes
label[y_ind, x_ind, 5] = 1
yield label,i_name
def check_LabelsFile_exist(self):
build_filter_label=False
try:
os.listdir(self.generate_data_path_from_filter_label_1)
print('[info] ./labels exist,checking finished!')
except:
build_filter_label=True
if build_filter_label:
self.filter_label_1()
def save_labels_matrix_13x13(self):
label=self.load_annotation()
for label_matrix,label_file_name in label:
np.save(self.labels_matrix_path+label_file_name+'.npy',label_matrix)
print('[info] ','done===>',label_file_name)
if __name__ == '__main__':
print('''
┏┓ ┏┓
┏┛┻━━━━━━┛┻┓
┃ ┃
┃ ┳┛ ┗┳ ┃
┃ ┻ ┃
┗━ ━┛
''')
lp=label_preprocessing()
lp.check_LabelsFile_exist()
try: #modify
os.listdir(lp.labels_matrix_path) #modify
except: #modify
os.mkdir(lp.labels_matrix_path) #modify
if len(os.listdir(lp.labels_matrix_path))==8878:
print('[info] ','The file called labels_matrix has been build and the data has been generated, escape the generating process...')
else:
print('[info] ','Start to build and generate...')
lp.save_labels_matrix_13x13()
下回写写自己对yolo的理解吧。。。
posted @ 2018-04-07 22:33  窝宅  阅读(142)  评论(0)    收藏  举报