ny = npr.randint(0, height - size) crop_box = np.array([nx, ny, nx + size, ny + size]) Iou = IoU(crop_box, boxes) cropped_im = img[ny:ny + size, nx:nx + size, :] resized_im = cv2.resize(cropped_im, (stdsize, stdsize), interpolation=cv2.INTER_LINEAR) if np.max(Iou) < 0.3: # Iou with all gts must below 0.3 '''负样本的标签为 0''' item_id += 1 ''' size 12''' if (lmdb_id == 0): mtcnn_datum = data_tran_tool.array_to_mtcnndatum( resized_im, 0, [-1.0, -1.0, -1.0, -1.0]) keystr = '{:0>8d}'.format(item_id) lmdb_txn_12.put(keystr, mtcnn_datum.SerializeToString()) # write batch if (item_id) % batch_size == 0: if (lmdb_id == 0): lmdb_txn_12.commit() lmdb_txn_12 = lmdb_env_12.begin(write=True) n_idx += 1 neg_num += 1 # pos part for box in boxes: # box (x_left, y_top, x_right, y_bottom) x1, y1, x2, y2 = box w = x2 - x1 + 1 h = y2 - y1 + 1
interpolation=cv2.INTER_LINEAR) else: resized_im48 = cv2.resize(cropped_im, (48, 48), interpolation=cv2.INTER_LINEAR) box_ = box.reshape(1, -1) if IoU(crop_box, box_) >= 0.65: #save_file = os.path.join(pos_save_dir, "%s.jpg"%p_idx) #cv2.imwrite(save_file, resized_im) #f1.write(str(stdsize)+"/positive/%s"%p_idx + ' 1 %f %f %f %f\n'%(offset_x1, offset_y1, offset_x2, offset_y2)) '''正样本的标签为 1''' item_id += 1 ''' size 12''' if (lmdb_id == 0): mtcnn_datum = data_tran_tool.array_to_mtcnndatum( resized_im12, 1, [offset_x1, offset_y1, offset_x2, offset_y2]) keystr = '{:0>8d}'.format(item_id) lmdb_txn_12.put(keystr, mtcnn_datum.SerializeToString()) elif (lmdb_id == 1): mtcnn_datum = data_tran_tool.array_to_mtcnndatum( resized_im24, 1, [offset_x1, offset_y1, offset_x2, offset_y2]) keystr = '{:0>8d}'.format(item_id) lmdb_txn_24.put(keystr, mtcnn_datum.SerializeToString()) else: mtcnn_datum = data_tran_tool.array_to_mtcnndatum(
# 打乱样本顺序 random.shuffle(list_item) for ann in list_item: line = ann.strip().split(' ') item_id += 1 # 读入图像 im_path = rootDir + line[0] img = cv2.imread(im_path) label = int(line[1]) nx1 = float(line[2]) ny1 = float(line[3]) nx2 = float(line[4]) ny2 = float(line[5]) mtcnn_datum = data_tran_tool.array_to_mtcnndatum(img, label, [nx1, ny1, nx2, ny2]) keystr = '{:0>8d}'.format(item_id) lmdb_txn_12.put(keystr, mtcnn_datum.SerializeToString()) # write batch if(item_id) % batch_size == 0: if(lmdb_id == 0): lmdb_txn_12.commit() lmdb_txn_12 = lmdb_env_12.begin(write=True) if (item_id+1) % batch_size != 0: if(lmdb_id == 0): lmdb_txn_12.commit() lmdb_env_12.close() print 'last batch' print "There are %d images in total" % item_id