如何检测边界框中的另一个对象,然后在原始图像中围绕检测到的对象创建边界框?

时间:2019-07-28 14:24:33

标签: tensorflow object-detection object-detection-api

我正在为两个轮车头盔检测项目使用tensorflow对象检测API。

我为程序设计了以下工作流程:-

  1. 检测框架中的所有两个惠勒并在它们周围绘制边界框。
  2. 现在将所有边界框视为单独的图像,并使用另一个分类器来检测边界框内的骑手是否戴着头盔。

到目前为止,我已经设法使第1步起作用。假设我有一个坐标为(ymin,xmin,ymax,xmax)的边界框。如何在此框中检测头盔,并在原始框架中的头盔周围绘制另一个边界框。

下面是我的代码。

def get_coordinates_of_box(im_width, im_height, coordinates_from_dictionary):
real_coordinates = []

left = int(coordinates_from_dictionary[1]*im_width) 
right = int(coordinates_from_dictionary[3]*im_width)
top = int(coordinates_from_dictionary[0]*im_height)
bottom = int(coordinates_from_dictionary[2]*im_height)

real_coordinates.append(left)
real_coordinates.append(top)
real_coordinates.append(right)
real_coordinates.append(bottom)

#print(real_coordinates)
return real_coordinates  #upper corner -> (left,top), lower corner --> (right,bottom)
##This function returns the list in the order that is required for the crop function. [left,top,right,bottom]

image_counter = 1
for image_path in TEST_IMAGE_PATHS:
  image = Image.open(image_path)
  image_name = 'image' + str(image_counter)
  im_width, im_height = image.size
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  # Actual detection.
  output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks'),
      use_normalized_coordinates=True,
      line_thickness=8,
      max_boxes_to_draw = 100)

  boxes = output_dict['detection_boxes']

  max_boxes_to_draw = boxes.shape[0] ##100

  scores = output_dict['detection_scores']

  min_score_thresh=.5

  real_num_detections = 0

  for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    #the above for loop is for detecting what detection boxes will be used.
    if scores is None or scores[i] > min_score_thresh:
        real_num_detections = real_num_detections + 1 
        # boxes[i] is the box which will be drawn.
        class_name = category_index[output_dict['detection_classes'][i]]['name']

        coordinates_of_box = get_coordinates_of_box(im_width,im_height,boxes[i])

        cropped = image.crop(coordinates_of_box) 
        ##Cropped is the image of two wheeler captured from the frame.




        ##need to apply another detector on the cropped image. 

        cropped.save('bounding_box_images/'+ image_name +'box' + str(i) +'.jpg' )

  print('Number of detections in ' + image_name + ' is', real_num_detections)
  image_counter = image_counter + 1 

  plt.figure(figsize=IMAGE_SIZE)
  plt.imshow(image_np)
  print(type(output_dict['detection_scores']))
  plt.show()

0 个答案:

没有答案