def demo_MBNet(self, opt, val_data, weight_path, out_path): self.model_all.load_weights(weight_path, by_name=True) print('load weights from {}'.format(weight_path)) for f in range(len(val_data)): img_name = os.path.join(out_path, val_data[f] + '_visible.png') if not img_name.lower().endswith(('.jpg', '.png')): continue print(img_name) img = cv2.imread(img_name) img_name_lwir = os.path.join(out_path, val_data[f] + '_lwir.png') print(img_name_lwir) img_lwir = cv2.imread(img_name_lwir) x_in = bbox_process.format_img(img, opt) x_in_lwir = bbox_process.format_img(img_lwir, opt) Y = self.model_all.predict([x_in, x_in_lwir]) proposals = bbox_process.pred_pp_1st(self.anchors, Y[0], Y[1], opt) bbx, scores = bbox_process.pred_det(proposals, Y[2], Y[3], opt, step=2) for ind in range(len(bbx)): if scores[ind][0] < 0.5: continue (x1, y1, x2, y2) = bbx[ind, :] cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) #cv2.putText(img, str(scores[ind][0]), (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.3, (0, 0, 255), 1) cv2.rectangle(img_lwir, (x1, y1), (x2, y2), (0, 255, 0), 2) #cv2.putText(img_lwir, str(scores[ind][0]), (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.3, (0, 0, 255), 1) img_concat = np.concatenate([img, img_lwir], axis=1) cv2.imwrite(os.path.join(out_path, val_data[f] + '.png'), img_concat)
def demo_video_MBNet(self, opt, test_file, lwir_test_file, weight_path): self.model_all.load_weights(weight_path, by_name=True) print('loaded weights from {}'.format(weight_path)) print('loaded visible video from :' + test_file) print('loaded lwir video from :' + lwir_test_file) vid = cv2.VideoCapture(test_file) lwir_vid = cv2.VideoCapture(lwir_test_file) fps = vid.get(cv2.CAP_PROP_FPS) frame_all = vid.get(cv2.CAP_PROP_FRAME_COUNT) print("[INFO] 视频FPS: {}".format(fps)) print("[INFO] 视频总帧数: {}".format(frame_all)) print("[INFO] 视频时长: {}s".format(frame_all / fps)) frame_width = int(vid.get(3)) frame_height = int(vid.get(4)) out_vid = cv2.VideoWriter('./output_videos/output_visible.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (frame_width, frame_height)) out_lwir_vid = cv2.VideoWriter( './output_videos/output_lwir.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (frame_width, frame_height)) idx = 0 while idx < frame_all: #防止越界 ret, frame = vid.read() lwir_ret, lwir_frame = lwir_vid.read() x_in = bbox_process.format_img(frame, opt) x_in_lwir = bbox_process.format_img(lwir_frame, opt) Y = self.model_all.predict([x_in, x_in_lwir]) proposals = bbox_process.pred_pp_1st(self.anchors, Y[0], Y[1], opt) bbx, scores = bbox_process.pred_det(proposals, Y[2], Y[3], opt, step=2) for ind in range(len(bbx)): if scores[ind][0] < 0.5: continue (x1, y1, x2, y2) = bbx[ind, :] cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) # cv2.putText(img, str(scores[ind][0]), (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.3, (0, 0, 255), 1) cv2.rectangle(lwir_frame, (x1, y1), (x2, y2), (0, 255, 0), 2) out_vid.write(frame) out_lwir_vid.write(lwir_frame) idx += 1 vid.release() lwir_vid.release() out_vid.release() out_lwir_vid.release() cv2.destroyAllWindows()
def test_MBNet(self, opt, data_path, val_data, weight_path): self.model_all.load_weights(weight_path, by_name=True) print('load weights from {}'.format(weight_path)) for f in range(len(val_data)): img_name = os.path.join(data_path, val_data[f]) if not img_name.lower().endswith(('.jpg', '.png')): continue print(img_name) img = cv2.imread(img_name) img_name_lwir = os.path.join(data_path[:-7] + 'lwir', val_data[f][:-11] + 'lwir.png') print(img_name_lwir) img_lwir = cv2.imread(img_name_lwir) start_time = time.time() x_in = bbox_process.format_img(img, opt) x_in_lwir = bbox_process.format_img(img_lwir, opt) Y = self.model_all.predict([x_in, x_in_lwir]) proposals = bbox_process.pred_pp_1st(self.anchors, Y[0], Y[1], opt) bbx, scores = bbox_process.pred_det(proposals, Y[2], Y[3], opt, step=2) print('Test time: %.4f s' % (time.time() - start_time)) image_name_save, png = val_data[f].split('.') image_name_save = image_name_save[:-8] #lwir result_path = './data/result' if not os.path.exists(result_path): os.makedirs(result_path) image_set_file = os.path.join(result_path, image_name_save + '.txt') list_file = open(image_set_file, 'w') for i in range(len(bbx)): image_write_txt = 'person' + ' ' + str(np.round(bbx[i][0], 4)) + ' ' + str(np.round(bbx[i][1], 4)) + ' ' \ + str(np.round(bbx[i][2], 4)) + ' ' + str(np.round(bbx[i][3], 4)) + ' ' + str(round(float(scores[i]), 8)) list_file.write(image_write_txt) list_file.write('\n') list_file.close()