def main(input_path, DEBUG): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, ) classes = load_coco_names(FLAGS.class_names) frozenGraph = load_graph(FLAGS.frozen_model) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) boxes_list = [] with tf.Session(graph=frozenGraph, config=config) as sess: for item in input_path: start = clock() FLAGS.input_img = item img = Image.open(FLAGS.input_img) img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128) img_resized = img_resized.astype(np.float32) detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) boxes_list.append(filtered_boxes) if DEBUG: draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size), True) print(filtered_boxes) print("Execution Time : {} / #Symbols : {} / Path : {}".format( clock() - start, len(filtered_boxes), item)) sess.close() tf.reset_default_graph() return boxes_list, classes, FLAGS.size
def detection(path): image = Image.open(path) img_resized = utils.letter_box_image(image, input_size, input_size, 128) img_resized = img_resized.astype(np.float32) boxes, inputs = utils.get_boxes_and_inputs_pb(frozenGraph) t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) filtered_boxes = utils.non_max_suppression(detected_boxes, confidence_threshold=conf_threshold, iou_threshold=iou_threshold) print("Predictions found in {:.2f}s".format(time.time() - t0)) if filtered_boxes: # if len(filtered_boxes[0][:]) == 1: img, region, score, box = utils.draw_boxes(filtered_boxes, image, classes, (input_size, input_size), True) # box = np.array(box) # print(box) if score > 0.90: person_image_height = box[0][3] - box[0][1] # region.save(out_image) print(person_image_height) # 计算当前用户身高 # 可根据参照物(本例采用椅子作为参照物,其实际高度为96cm,在固定距离下该参照物在图像中像素值为230)实际高度与图像高度像素, # 获取人物图像像素高度。具体调参需在具体环境下进行调参 # 此方法存在较大的误差,故结果仅供趣味输出,追求准确仍需具体输入准确值 person_height = (person_image_height * 96) / 230 print("person_height: %.2fcm \n" % (person_height))
def main(argv=None): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, ) img = Image.open(FLAGS.input_img) img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128) img_resized = img_resized.astype(np.float32) classes = load_coco_names(FLAGS.class_names) if FLAGS.frozen_model: t0 = time.time() frozenGraph = load_graph(FLAGS.frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) #print(frozenGraph.inputs) #print(frozenGraph.outputs) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) with tf.Session(graph=frozenGraph, config=config) as sess: t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) else: if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny elif FLAGS.spp: model = yolo_v3.yolo_v3_spp else: model = yolo_v3.yolo_v3 boxes, inputs = get_boxes_and_inputs(model, len(classes), FLAGS.size, FLAGS.data_format) saver = tf.train.Saver(var_list=tf.global_variables(scope='detector')) with tf.Session(config=config) as sess: t0 = time.time() saver.restore(sess, FLAGS.ckpt_file) print('Model restored in {:.2f}s'.format(time.time() - t0)) t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) print("Predictions found in {:.2f}s".format(time.time() - t0)) draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size), True) img.save(FLAGS.output_img)
def main(argv=None): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, ) classes = load_coco_names(FLAGS.class_names) t0 = time.time() frozenGraph = load_graph(FLAGS.frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) with tf.Session(graph=frozenGraph, config=config) as sess: t0 = time.time() print(FLAGS.input_img) cap = cv2.VideoCapture(FLAGS.input_img) # cap = cv2.VideoCapture(0) fps = cap.get(cv2.CAP_PROP_FPS) width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) videoWriter = cv2.VideoWriter( "output.mp4", cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, (int(width), int(height))) while (cap.isOpened()): ret, frame = cap.read() if ret == True: frame = cv2.flip(frame, 0) img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128) img_resized = img_resized.astype(np.float32) detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) print("Predictions found in {:.2f}s".format(time.time() - t0)) draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size), True) fimg = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) cv2.imshow("show", fimg) videoWriter.write(fimg) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() videoWriter.release()
def get_classification(self, cv_image): """Determines the color of the traffic light in the image Args: image (cv::Mat): image containing the traffic light Returns: int: ID of traffic light color (specified in styx_msgs/TrafficLight) """ #TODO implement light color prediction image = Image.fromarray(cv_image) img_resized = letter_box_image(image, options['image_size'], options['image_size'], 128) img_resized = img_resized.astype(np.float32) boxes, inputs = get_boxes_and_inputs_pb(self.frozenGraph) # with tf.Session(graph=self.frozenGraph, config=self.config) as sess: t0 = time.time() detected_boxes = self.sess.run(boxes, feed_dict={inputs: [img_resized]}) filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=options['thresh'], iou_threshold=options['iou']) print("Predictions found in {:.2f}s".format(time.time() - t0)) inp = filtered_boxes.get(9) inp_new = dict() inp_new[9] = inp if (inp_new[9] != None): if (len(inp_new[9]) > 0): for cls, bboxs in inp_new.items(): for box, score in bboxs: box = convert_to_original_size( box, (options['image_size'], options['image_size']), np.array(image.size), True) # print(inp_new) a = analyze_color(inp_new, cv_image) # print(a) light_color = state_predict(a) print("the light color is {}".format(light_color)) if light_color: if light_color == 'YELLOW': return TrafficLight.YELLOW elif light_color == 'RED': return TrafficLight.RED elif light_color == 'GREEN': return TrafficLight.GREEN return TrafficLight.UNKNOWN
def main(argv=None): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, # inter_op_parallelism_threads=0, # intra_op_parallelism_threads=0, # device_count={"CPU": 6} ) cap = cv2.VideoCapture(FLAGS.video_path) classes = utils.load_names(FLAGS.class_names) frozenGraph = utils.load_graph(FLAGS.frozen_model) boxes, inputs = utils.get_boxes_and_inputs_pb(frozenGraph) with tf.Session(graph=frozenGraph, config=config) as sess: while True: ret, frame = cap.read() if ret: t1 = time.time() frame1 = frame[:, :, ::-1] # from BGR to RGB # frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) print('\'BGR2RGB\' time consumption:', time.time() - t1) img_resized = utils.resize_cv2( frame1, (FLAGS.size, FLAGS.size), keep_aspect_ratio=FLAGS.keep_aspect_ratio) img_resized = img_resized[np.newaxis, :] t0 = time.time() detected_boxes = sess.run( boxes, feed_dict={inputs: img_resized }) # get the boxes whose confidence > 0.005 filtered_boxes = utils.non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold)[ 0] # boxes' filter by NMS print('\'detection\' time consumption:', time.time() - t0) utils.draw_boxes_cv2(filtered_boxes, frame, classes, (FLAGS.size, FLAGS.size), FLAGS.keep_aspect_ratio) print('\n\n\n') cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows()
def main(argv=None): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, # inter_op_parallelism_threads=0, # intra_op_parallelism_threads=0, # device_count={"CPU": 6} ) classes = utils.load_names(FLAGS.class_names) input_size = (FLAGS.size, FLAGS.size) img_pathes = [path for path in os.listdir(FLAGS.input_imgpath) if path.endswith(('.jpg', '.png', '.bmp'))] num_imgs = len(img_pathes) batch_size = FLAGS.batch_size img_list = [] img_batch_all = np.zeros((num_imgs, FLAGS.size, FLAGS.size, 3)) for k in range(num_imgs): img_array = cv2.imread(os.path.join(FLAGS.input_imgpath, img_pathes[k])) img_list.append(img_array) img_batch_all[k] = utils.resize_cv2(img_array, input_size)[:, :, ::-1] frozenGraph = utils.load_graph(FLAGS.frozen_model) boxes, inputs = utils.get_boxes_and_inputs_pb(frozenGraph) with tf.Session(graph=frozenGraph, config=config) as sess: for i in range(0, num_imgs, batch_size): if i < num_imgs - batch_size: img_batch = img_batch_all[i:i + batch_size] else: img_batch = img_batch_all[i:] detected_boxes = sess.run(boxes, feed_dict={inputs: img_batch}) filtered_boxes = utils.non_max_suppression(detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) for n, bboxes in enumerate(filtered_boxes): img = img_list[i + n] img_name = img_pathes[i + n] utils.draw_boxes_cv2(bboxes, img, classes, input_size, keep_aspect_ratio=FLAGS.keep_aspect_ratio) # cv2.imshow('image_{}'.format(img_name), img) cv2.imwrite(os.path.join(FLAGS.output_imgpath, 'out_' + img_name), img) print('{} has been processed !'.format(img_name)) print('#'*20)
def main(argv=None): img = Image.open('out/images/19.png') # img = Image.open('city.png') img_resized = letter_box_image(img, size, size, 128) img_resized = img_resized.astype(np.float32) classes = load_coco_names('coco.names') if frozen_model: t0 = time.time() frozenGraph = load_graph(frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) with tf.Session(graph=frozenGraph) as sess: t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) else: if tiny: model = yolo_v3_tiny.yolo_v3_tiny else: model = yolo_v3.yolo_v3 boxes, inputs = get_boxes_and_inputs(model, len(classes), size, data_format) saver = tf.train.Saver(var_list=tf.global_variables(scope='detector')) with tf.Session() as sess: t0 = time.time() saver.restore(sess, ckpt_file) print('Model restored in {:.2f}s'.format(time.time() - t0)) t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) filtered_boxes = non_max_suppression(detected_boxes, confidence_threshold=conf_threshold, iou_threshold=iou_threshold) print("Predictions found in {:.2f}s".format(time.time() - t0)) draw_boxes(filtered_boxes, img, classes, (size, size), True) img.save('out_check.png')
def main(argv=None): #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) gpu_options = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, ) img = Image.open(FLAGS.input_img) classes = load_coco_names(FLAGS.class_names) if FLAGS.frozen_model: t0 = time.time() frozenGraph = load_graph(FLAGS.frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) with tf.Session(graph=frozenGraph, config=config) as sess: t0 = time.time() show_camera(sess, boxes, inputs) else: if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny else: model = yolo_v3.yolo_v3 boxes, inputs = get_boxes_and_inputs(model, len(classes), FLAGS.size, FLAGS.data_format) saver = tf.train.Saver(var_list=tf.global_variables(scope='detector')) with tf.Session(config=config) as sess: t0 = time.time() saver.restore(sess, FLAGS.ckpt_file) print('Model restored in {:.2f}s'.format(time.time() - t0)) t0 = time.time()
def main(argv=None): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, ) cap = cv2.VideoCapture() cap.open(0) classes = load_coco_names(FLAGS.class_names) if FLAGS.frozen_model: t0 = time.time() frozenGraph = load_graph(FLAGS.frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) with tf.Session(graph=frozenGraph, config=config) as sess: while True: ret, img = cap.read() img_resized = cv2.resize(img, (int(416), int(416))) t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) print("Predictions found in {:.2f}s".format(time.time() - t0)) # img = draw_boxes(filtered_boxes, img, classes, (416, 416), True) print(filtered_boxes) cv2.imshow("Output", img)
def main(argv=None): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, # inter_op_parallelism_threads=0, # intra_op_parallelism_threads=0, # device_count={"CPU": 6} ) img = Image.open(FLAGS.input_img) if FLAGS.keep_aspect_ratio: img_resized = utils.letter_box_image(img, FLAGS.size, FLAGS.size, 128) img_resized = img_resized.astype(np.float32) else: img_resized = img.resize((FLAGS.size, FLAGS.size), Image.BILINEAR) img_resized = np.asarray(img_resized, dtype=np.float32) classes = utils.load_names(FLAGS.class_names) frozenGraph = utils.load_graph(FLAGS.frozen_model) boxes, inputs = utils.get_boxes_and_inputs_pb(frozenGraph) with tf.Session(graph=frozenGraph, config=config) as sess: t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) print("Predictions found in {:.2f}s".format(time.time() - t0)) filtered_boxes = utils.non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold)[0] utils.draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size), FLAGS.keep_aspect_ratio) img.save(FLAGS.output_img)
def get_score_from_image(img_fp, gpu_options, config, model): img = Image.open(img_fp) img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128) img_resized = img_resized.astype(np.float32) classes = load_coco_names(FLAGS.class_names) inference_start_time = time.time() if FLAGS.frozen_model: boxes, inputs = get_boxes_and_inputs_pb(model) with tf.Session(graph=model, config=config) as sess: detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) else: if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny elif FLAGS.spp: model = yolo_v3.yolo_v3_spp else: model = yolo_v3.yolo_v3 boxes, inputs = get_boxes_and_inputs(model, len(classes), FLAGS.size, FLAGS.data_format) saver = tf.train.Saver(var_list=tf.global_variables(scope='detector')) with tf.Session(config=config) as sess: saver.restore(sess, FLAGS.ckpt_file) detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) total_inference_time = time.time() - inference_start_time filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) return get_person_scores(filtered_boxes, classes), round(total_inference_time * 1000, 3)
def main(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, ) #----------- Initialization -------------- # Settings data+ following initializations classes = load_coco_names(class_names) cap = cv2.VideoCapture('video.avi') ret, _ = cap.read() plt.ion() frame_index = 0 # defining model if frozen_model: #The protobuf file contains the graph definition as well as the weights of the model. t0 = time.time() # loading model and related weights frozenGraph = load_graph(frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) with tf.device("/GPU:0"): with tf.Session(graph=frozenGraph, config=config) as sess: # Is there any frame to read? while ret: frame_index += 1 ret, frame = cap.read() # applying transformation and apropriate changes to frame to feed the loaded model img = Image.fromarray( cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img_resized = letter_box_image(img, size, size, 128) img_resized = img_resized.astype(np.float32) t0 = time.time() # feeding tensor to loaded model detected_boxes = sess.run( boxes, feed_dict={inputs: [img_resized]}) #obtaining the bounding boxes of detected objects filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=conf_threshold, iou_threshold=iou_threshold) print("Predictions found in {:.2f}s".format(time.time() - t0)) #croping and extracting bounding boxes of detected objects in frame rois = draw_boxes(filtered_boxes, img, classes, (size, size), True) if len(rois) > 0: for i in range(len(rois)): # saving the cropped images in Hard Disk = './extracted_regions/' Directory rois[i].save('./extracted_regions/frame' + str(frame_index) + '_ExtObj_' + str(i) + '.jpg') plt.imshow(np.array(img)) plt.pause(0.02) plt.show() else: # using ckpt file for loading the model weights #----------- Initialization -------------- saver = tf.train.Saver(var_list=tf.global_variables(scope='detector')) cap = cv2.VideoCapture('video.avi') ret, _ = cap.read() plt.ion() t0 = time.time() frame_index = 0 # loading model and related weights if tiny: model = yolo_v3_tiny.yolo_v3_tiny else: model = yolo_v3.yolo_v3 boxes, inputs = get_boxes_and_inputs(model, len(classes), size, data_format) t0 = time.time() saver.restore(sess, ckpt_file) print('Model restored in {:.2f}s'.format(time.time() - t0)) with tf.Session(config=config) as sess: # is there any frame to read? while ret: frame_index += 1 ret, frame = cap.read() # applying transformation and apropriate changes to frame to feed the loaded model img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) img_resized = letter_box_image(img, size, size, 128) img_resized = img_resized.astype(np.float32) t0 = time.time() # feeding tensor to loaded model detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) #obtaining the bounding boxes of detected objects filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=conf_threshold, iou_threshold=iou_threshold) print("Predictions found in {:.2f}s".format(time.time() - t0)) #croping and extracting bounding boxes of detected objects rois = draw_boxes(filtered_boxes, img, classes, (size, size), True) if len(rois) > 0: for i in range(len(rois)): # saving the cropped images in Hard Disk = './extracted_regions/' Directory rois[i].save('./extracted_regions/frame' + str(frame_index) + '_ExtObj_' + str(i) + '.jpg') plt.imshow(np.array(img)) plt.pause(0.02) plt.show()
def main(argv=None): img = Image.open('city.png') img_resized = letter_box_image(img, size, size, 128) img_resized = img_resized.astype(np.float32) classes = load_coco_names('coco.names') fake_boxes = {2: [(np.array([300, 200, 370, 250]), 1.)]} generated_boxes, g_indices = generate_ground_truth(fake_boxes, size, 0.4) draw_boxes(copy.deepcopy(generated_boxes), img, classes, (size, size), True) draw_boxes(copy.deepcopy(fake_boxes), img, classes, (size, size), True) # draw_boxes(filtered_boxes, img, classes, (size, size), True) img.save('out_fakeboxes.jpg') mask = np.zeros([1, 10647]) for cls, indices in g_indices.items(): mask[0, indices] = 1 gt_tensor = np.zeros([1, 10647, 4 + 1 + len(classes)]) for cls, boxes in generated_boxes.items(): for i, box in enumerate(boxes): class_mask = np.zeros([len(classes)]) class_mask[cls] = 1 gt_row = [*np.asarray(box[0]), 1., *class_mask] gt_tensor[0, g_indices[cls][i]] = gt_row if frozen_model: t0 = time.time() frozenGraph = load_graph(frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) with frozenGraph.as_default(): fake_gt = tf.constant(gt_tensor, dtype=tf.float32) mask_tensor = tf.constant(mask, dtype=tf.float32) fake_loss = mse(fake_gt, boxes) * mask_tensor fake_loss = tf.reduce_mean(fake_loss, axis=-1) grad_op = tf.gradients(fake_loss, inputs) with tf.Session(graph=frozenGraph) as sess: t0 = time.time() for iters in range(num_iterations): grads = sess.run(grad_op, feed_dict={inputs: [img_resized]}) grad = grads[0][0] sigma = (iters * 4.0) / num_iterations + 0.5 grad_smooth1 = gaussian_filter(grad, sigma=sigma) grad_smooth2 = gaussian_filter(grad, sigma=sigma * 2) grad_smooth3 = gaussian_filter(grad, sigma=sigma * 0.5) grad = (grad_smooth1 + grad_smooth2 + grad_smooth3) step_size_scaled = step_size / (np.std(grad) + 1e-8) # Update the image by following the gradient. mod = grad * step_size_scaled grad_img = Image.fromarray(np.uint8(mod + 128)) grad_img.save('out/grads/{}.png'.format(iters)) img_resized = np.clip(img_resized - mod, 0, 255) new_img = Image.fromarray(np.uint8(img_resized)) new_img.save('out/images/{}.png'.format(iters)) else: if tiny: model = yolo_v3_tiny.yolo_v3_tiny else: model = yolo_v3.yolo_v3 boxes, inputs = get_boxes_and_inputs(model, len(classes), size, data_format) saver = tf.train.Saver(var_list=tf.global_variables(scope='detector')) with tf.Session() as sess: t0 = time.time() saver.restore(sess, ckpt_file) print('Model restored in {:.2f}s'.format(time.time() - t0)) t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})
if cap.isOpened(): window_handle = cv2.namedWindow('CSI Camera', cv2.WINDOW_AUTOSIZE) # Create session and load graph # Configure the tensorflow session, especially with allow_growth, so it doesnt fails to get memory gpu_options = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False) # Initialize the session: ACHTUNG!! This is the more efficient way, in comparison to with tf.Session as sess: #I am not sure why, but that way freezes the Nanoboard and make the loading process really slow tf_sess = tf.Session(graph=frozenGraph,config=config) # Get the names of the inputs and outputs of the networks boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) # Saving data to debug ant test algorithms #f = open('data.txt','w') #f.write('X0,Y0,X1,Y1,Indice\n') tracker = TrackingAlgorithm() # While you get something from the camera while cv2.getWindowProperty('CSI Camera', 0) >= 0: ret_val, img = cap.read() # Prepare the image num_rows, num_cols = img.shape[:2] rotation_matrix = cv2.getRotationMatrix2D((num_cols / 2, num_rows / 2), 180, 1)
classes = utils.load_coco_names(class_names) out_image = './person.jpg' t0 = time.time() frozenGraph = utils.load_graph(frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) sess = tf.Session(graph=frozenGraph) # image = cv2.imread(input_image) # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # image = Image.fromarray(image.astype('uint8')).convert('RGB') # 上面三步等同于下面的Image.open()操作 image = Image.open(input_image) img_resized = utils.letter_box_image(image, input_size, input_size, 128) img_resized = img_resized.astype(np.float32) boxes, inputs = utils.get_boxes_and_inputs_pb(frozenGraph) t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) filtered_boxes = utils.non_max_suppression(detected_boxes, confidence_threshold=conf_threshold, iou_threshold=iou_threshold) print("Predictions found in {:.2f}s".format(time.time() - t0)) if filtered_boxes: # if len(filtered_boxes[0][:]) == 1: img, region, score, box = utils.draw_boxes(filtered_boxes, image, classes, (input_size, input_size), True) # box = np.array(box) # print(box) if score > 0.90: person_image_height = box[0][3] - box[0][1]
def main(argv=None): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, ) # img = Image.open(FLAGS.input_img) # img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128) # img_resized = img_resized.astype(np.float32) classes = load_coco_names(FLAGS.class_names) # if FLAGS.frozen_model: t0 = time.time() frozenGraph = load_graph(FLAGS.frozen_model) print("Loaded graph in {:.2f}s".format(time.time() - t0)) boxes, inputs = get_boxes_and_inputs_pb(frozenGraph) ### Start inference on Video cap = cv2.VideoCapture(FLAGS.input_video) cap.open(FLAGS.input_video) # Grab the shape of the input width = int(cap.get(3)) height = int(cap.get(4)) with tf.Session(graph=frozenGraph, config=config) as sess: while cap.isOpened(): flag, img = cap.read() if not flag: break key_pressed = cv2.waitKey(27) img = cv2.cvtColor( img, cv2.COLOR_BGR2RGB) #Image.open(FLAGS.input_video) # convert from cv2 image to PIL image img = Image.fromarray(img) img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128) img_resized = img_resized.astype(np.float32) classes = load_coco_names(FLAGS.class_names) t0 = time.time() detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]}) infer_time = time.time() - t0 filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size), True) img = np.asarray(img) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) cv2.putText( img, "infer time= " + str('{:.1f}'.format(infer_time * 1000)) + " ms", (80, 40), 0, 0.5, (250, 0, 0), 1) ### Send the frame to the FFMPEG server ### sys.stdout.buffer.write(img) sys.stdout.flush() # Break if escape key pressed if key_pressed == 27: break # Release the out capture, and destroy any OpenCV windows cap.release() cv2.destroyAllWindows()