精华内容
下载资源
问答
  • 训练后,我们对未标记的数据集B进行预测,然后保存预测(每个图像的边界框和标签)。 现在,这是该存储库的来源! 从图像中我们裁剪出从预测中收到的所有边界框,并使用在单独环境中训练的辅助分类器模型对它们...
  • 分析多只鼠标进行方向盘检测和识别任务 从此存储库下载所有功能和代码 确保它在matlab路径上 首先打开load_data_functions.m代码 在对话框中选择鼠标数据的位置(文件夹为M123,M124等的文件夹) exp类型无关紧要,...
  • 任务描述:用API检测出来的结果是一整张图片,想要把检测出的bounding box部分单独截取出来并保存

    任务描述:用Tensorflow object detection API检测出来的结果是一整张图片,想要把检测出的bounding box部分单独截取出来并保存
    运行环境:spyder
    效果展示
    测试图片:test_images --> 检测图片:testsave_images --> 裁剪bounding box:test_cropped

    在这里插入图片描述
    在这里插入图片描述

    1. 我的修改过后的test文件
    
    # coding: utf-8
    
    # # Object Detection Demo
    # Welcome to the object detection inference walkthrough!  This notebook will walk you step by step through the process of using a pre-trained model to detect objects in an image. Make sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md) before you start.
    
    # # Imports
    
    # In[19]:
    from skimage import data_dir
    import skimage.io as io
    import numpy as np
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import zipfile
    
    from collections import defaultdict
    from io import StringIO
    from matplotlib import pyplot as plt
    from PIL import Image
    
    # This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("..")
    from object_detection.utils import ops as utils_ops
    
    if tf.__version__ < '1.4.0':
      raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')
    
    
    # ## Env setup
    
    # In[20]:
    
    
    # This is needed to display the images.
    #get_ipython().run_line_magic('matplotlib', 'inline')  我的电脑上这句如果不注释掉会报错
    
    
    # ## Object detection imports
    # Here are the imports from the object detection module.
    
    # In[21]:
    
    
    from utils import label_map_util
    
    from utils import visualization_utils as vis_util #主要就是用到了utils目录下的visualization_utils文件
    
    
    # # Model preparation 
    
    # ## Variables
    # 
    # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.  
    # 
    # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.
    
    # In[22]:
    
    
    # What model to download.
    MODEL_NAME = 'headshoulder0603'  #我的模型名字
    
    
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
    
    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'headshoulder.pbtxt')  #我的模型相关文件
    
    NUM_CLASSES = 1 #我的任务是检测头肩,只有一类
    
    
    # ## Download Model
    
    # ## Load a (frozen) Tensorflow model into memory.
    
    # In[ ]:
    
    
    detection_graph = tf.Graph()
    with detection_graph.as_default():
      od_graph_def = tf.GraphDef()
      with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')
    
    
    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    
    # In[ ]:
    
    
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    
    
    # ## Helper code
    
    # In[23]:
    
    #自己在实践中发现,图片经常需要在Image和numpy array两种格式中切换,这个函数就是将Image格式转换成numpy array
    def load_image_into_numpy_array(image):
      (im_width, im_height) = image.size
      return np.array(image.getdata()).reshape(
          (im_height, im_width, 3)).astype(np.uint8)
    
    
    # # Detection
    
    # In[24]:
    
    
    # For the sake of simplicity we will use only 2 images:
    # image1.jpg
    # image2.jpg
    # If you want to test the code with Aayour images, just add path to the images to the TEST_IMAGE_PATHS.
    
    PATH_TO_TEST_IMAGES_DIR = os.getcwd()+'\\test_images'   #存放测试图片的目录路径
    os.chdir(PATH_TO_TEST_IMAGES_DIR)  
    TEST_IMAGE_PATHS = os.listdir(PATH_TO_TEST_IMAGES_DIR) 
    # Size, in inches, of the output images.
    IMAGE_SIZE = (12, 8)
    
    
    # In[25]:
    
    
    def run_inference_for_single_image(image, graph):
      with graph.as_default():
        with tf.Session() as sess:
          # Get handles to input and output tensors
          ops = tf.get_default_graph().get_operations()
          all_tensor_names = {output.name for op in ops for output in op.outputs}
          tensor_dict = {}
          for key in [
              'num_detections', 'detection_boxes', 'detection_scores',
              'detection_classes', 'detection_masks'
          ]:
            tensor_name = key + ':0'
            if tensor_name in all_tensor_names:
              tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
                  tensor_name)
          if 'detection_masks' in tensor_dict:
            # The following processing is only for single image
            detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
            detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
            # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
            real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
            detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
            detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
            detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
                detection_masks, detection_boxes, image.shape[0], image.shape[1])
            detection_masks_reframed = tf.cast(
                tf.greater(detection_masks_reframed, 0.5), tf.uint8)
            # Follow the convention by adding back the batch dimension
            tensor_dict['detection_masks'] = tf.expand_dims(
                detection_masks_reframed, 0)
          image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
    
          # Run inference
          output_dict = sess.run(tensor_dict,
                                 feed_dict={image_tensor: np.expand_dims(image, 0)})
    
          # all outputs are float32 numpy arrays, so convert types as appropriate
          output_dict['num_detections'] = int(output_dict['num_detections'][0])
          output_dict['detection_classes'] = output_dict[
              'detection_classes'][0].astype(np.uint8)
          output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
          output_dict['detection_scores'] = output_dict['detection_scores'][0]
          if 'detection_masks' in output_dict:
            output_dict['detection_masks'] = output_dict['detection_masks'][0]
      return output_dict
    
    
    # In[26]:
    
    
    for image_path in TEST_IMAGE_PATHS:
        image= Image.open(image_path) #注意这里的image_path是个路径,也就是说是个字符串str,下文会用到
      # 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, detection_graph)
      # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          image_path, #原有的代码没有这一行,但是我需要传递测试图片image的文件名给visualize_utils文件中,所以加上,对应的visualize_utils中的visualize_boxes_and_labels_on_image_array函数也要加上这个参数
          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=1, #默认的框到粗细是8,但是实在太粗了
          )#the width of bounding box,default is 8
        plt.figure(figsize=IMAGE_SIZE)
        plt.imshow(image_np)
        #save_dir = TEST_IMAGE_PATHS + '{}.jpg'.format(image_path)
        PATH_TO_TEST_IMAGES_1_DIR = r'C:\\models\\research\\object_detection\\testsave_images' #这里可能要加上r,不然可能会有编码ucf8错误
        save_dir = os.path.join(PATH_TO_TEST_IMAGES_1_DIR, image_path)
        vis_util.save_image_array_as_png(image_np,save_dir) #我将检测出来的图片保存在testsave_images文件夹下
         
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    
    
    
    1. 修改过后的visualization_utils.py文件中 visualize_boxes_and_labels_on_image_array 函数
    def visualize_boxes_and_labels_on_image_array(
        image,
        image_path, #我添加的,因为要用到测试图片的文件名
        boxes,
        classes,
        scores,
        category_index,
        instance_masks=None,
        instance_boundaries=None,
        keypoints=None,
        use_normalized_coordinates=False,
        max_boxes_to_draw=20,
        min_score_thresh=.5,
        agnostic_mode=False,
        line_thickness=1,
        groundtruth_box_visualization_color='black',
        skip_scores=False,
        skip_labels=False,
        ):
      """Overlay labeled boxes on an image with formatted scores and label names.
    
      This function groups boxes that correspond to the same location
      and creates a display string for each detection and overlays these
      on the image. Note that this function modifies the image in place, and returns
      that same image.
    
      Args:
        image: uint8 numpy array with shape (img_height, img_width, 3)
        boxes: a numpy array of shape [N, 4]
        classes: a numpy array of shape [N]. Note that class indices are 1-based,
          and match the keys in the label map.
        scores: a numpy array of shape [N] or None.  If scores=None, then
          this function assumes that the boxes to be plotted are groundtruth
          boxes and plot all boxes as black with no classes or scores.
        category_index: a dict containing category dictionaries (each holding
          category index `id` and category name `name`) keyed by category indices.
        instance_masks: a numpy array of shape [N, image_height, image_width] with
          values ranging between 0 and 1, can be None.
        instance_boundaries: a numpy array of shape [N, image_height, image_width]
          with values ranging between 0 and 1, can be None.
        keypoints: a numpy array of shape [N, num_keypoints, 2], can
          be None
        use_normalized_coordinates: whether boxes is to be interpreted as
          normalized coordinates or not.
        max_boxes_to_draw: maximum number of boxes to visualize.  If None, draw
          all boxes.
        min_score_thresh: minimum score threshold for a box to be visualized
        agnostic_mode: boolean (default: False) controlling whether to evaluate in
          class-agnostic mode or not.  This mode will display scores but ignore
          classes.
        line_thickness: integer (default: 4) controlling line width of the boxes.
        groundtruth_box_visualization_color: box color for visualizing groundtruth
          boxes
        skip_scores: whether to skip score when drawing a single detection
        skip_labels: whether to skip label when drawing a single detection
    
      Returns:
        uint8 numpy array with shape (img_height, img_width, 3) with overlaid boxes.
      """
      # Create a display string (and color) for every box location, group any boxes
      # that correspond to the same location.
    
      box_to_display_str_map = collections.defaultdict(list)
      box_to_color_map = collections.defaultdict(str)
      box_to_instance_masks_map = {}
      box_to_instance_boundaries_map = {}
    #  box_to_keypoints_map = collections.defaultdict(list)
      if not max_boxes_to_draw:
        max_boxes_to_draw = boxes.shape[0]
      for i in range(min(max_boxes_to_draw, boxes.shape[0])):
        if scores is None or scores[i] > min_score_thresh:
          box = tuple(boxes[i].tolist())
          if instance_masks is not None:
            box_to_instance_masks_map[box] = instance_masks[i]
          if instance_boundaries is not None:
            box_to_instance_boundaries_map[box] = instance_boundaries[i]
         
          if scores is None:
            box_to_color_map[box] = groundtruth_box_visualization_color
          else:
            display_str = ''
            if not skip_labels:
              if not agnostic_mode:
                if classes[i] in category_index.keys():
                  class_name = category_index[classes[i]]['name']
                else:
                  class_name = 'N/A'
                display_str = str(class_name)
            if not skip_scores:
              if not display_str:
                display_str = '{}%'.format(int(100*scores[i]))
              else:
                display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
            box_to_display_str_map[box].append(display_str)
            if agnostic_mode:
              box_to_color_map[box] = 'DarkOrange'
            else:
              box_to_color_map[box] = STANDARD_COLORS[
                  classes[i] % len(STANDARD_COLORS)]
    #==================================================================================#
    #===============crop bounding box images===========================================#
    #==================================================================================# 	
      t = 0 #这行记得是放在下面的循环体外的
      for box, color in box_to_color_map.items():
        ymin, xmin, ymax, xmax = box  #前文已经得到bounding box的坐标,但是,由于使用了use_normalized_coordinates,将坐标归一化了,所以要映射回来
        img = Image.fromarray(np.uint8(image))
        im_width, im_height = img.size
        print('im_width, im_height:', im_width, im_height)
        new_xmin = int(xmin * im_width)
        new_xmax = int(xmax * im_width)
        new_ymin = int(ymin * im_height)
        new_ymax = int(ymax * im_height)
    #    print('xmin,xmax,ymin,ymax:',xmin,xmax,ymin,ymax)
        print('new_xmin,new_xmax,new_ymin,new_ymax:',new_xmin,new_xmax,new_ymin,new_ymax)
    
        image_ = image[new_ymin:new_ymax,new_xmin:new_xmax] #将测试图片做裁剪,只剩bounding box部分,注意此处的image_命名,之前我写的是image,会出错。而且要注意的是,由于代码中用的是PIL图片格式,原点在左上角,而此处用numpy的方式裁剪图片,原点在左下角,所以x和y是反过来的。
        
        plt.imshow(image_)
        plt.show()
    #       
        PATH_TO_crop_DIR = r'C:\\models\\research\\object_detection\\test_cropped' #保存的裁剪图片位置
        image_ = Image.fromarray(np.uint8(image_))
        t+=1
    #    image_.save( 'C:\\models\\research\\object_detection\\test_cropped\\cropped_images.jpg')
    #    image_.save(os.path.join(PATH_TO_crop_DIR, str(t)+'.jpg'))
        image_.save(os.path.join(PATH_TO_crop_DIR, (str(t)+'_')+os.path.basename(image_path))) #这里用上了image_path,因为os.path.join需要字符串的参数,由于一张测试图片可能会检测出多个bounding box,所以在保存命名的时候加上了t这个参数(这一步应该会有更优雅的做法,但是我懒得去试了)
    ####################################################################################
      # Draw all boxes onto image.把框和文件画到测试图片上
      for box, color in box_to_color_map.items():
        ymin, xmin, ymax, xmax = box
        draw_bounding_box_on_image_array(
            image,
            ymin,
            xmin,
            ymax,
            xmax,
            color=color,
            thickness=line_thickness,
            display_str_list=box_to_display_str_map[box],
            use_normalized_coordinates=use_normalized_coordinates)、
      return image
    
    展开全文
  • 修改Darknet源码,使其可以保存Bounding-box坐标信息

    千次阅读 热门讨论 2019-04-07 21:55:18
    因为使用Darknet进行目标检测任务时,框架只会输出图片形式的检测结果(即将bounding-box显式的画在原图上),但并不会输出单独的坐标信息,这给我们的进一步分析带来很多不便。所以我们就要对框架源码进行一定的...

    因为使用Darknet进行目标检测任务时,框架只会输出图片形式的检测结果(即将bounding-box显式的画在原图上),但并不会输出单独的坐标信息,这给我们的进一步分析带来很多不便。所以我们就要对框架源码进行一定的修改,使其能保存图片中目标的bounding-box坐标到txt文件中。

    Step 1

    首先我们要找到源码中画框的函数,因为画框是需要坐标信息的,我们可以在这个函数中找到bounding-box的坐标信息。

    在源码中,进行画框任务的函数为 ./darknet/src/image.c 里面的 draw_detections() 函数,考虑到框架中多个文件会调用此函数,所以我们不直接对函数进行修改,而是在 image.c 文件中新增一个函数,命名为 draw_detections_in_txt(),这个函数在 draw_detections() 的基础上新增了一个参数 filename ,这个参数用来将 txt 保存成和被检测图片一致的文件名。整个函数如下:

    //JiaXuejian Modified//
    /Added an argument:filename
    void draw_detections_in_txt(image im, char *filename, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
    {
        int i,j;
    
    	char *output = filename;
        //output = strcat(output, ".txt");
        //output = strcat("./", output); 
        //printf("PATH:%s", output); 
    	int namecount = 0;
    	for (namecount = strlen(filename)-1;namecount>=0 ; namecount--)
    	{
    		if ((filename[namecount]!='j')&&(filename[namecount]!='p')&&(filename[namecount]!='g')&&(filename[namecount]!='.'))
    		{
    			break;
    		}
    		else{
    			output[namecount] = '\0';
    		}
    	}
    	output = strcat(filename, ".txt");
    
        for(i = 0; i < num; ++i){
            char labelstr[4096] = {0};
            int class = -1;
            for(j = 0; j < classes; ++j){
                if (dets[i].prob[j] > thresh){
                    if (class < 0) {
                        strcat(labelstr, names[j]);
                        class = j;
                    } else {
                        strcat(labelstr, ", ");
                        strcat(labelstr, names[j]);
                    }
                    printf("%s: %.0f%%\n", names[j], dets[i].prob[j]*100);
                }
            }
            if(class >= 0){
                int width = im.h * .006;
    
                /*
                   if(0){
                   width = pow(prob, 1./2.)*10+1;
                   alphabet = 0;
                   }
                 */
    
                //printf("%d %s: %.0f%%\n", i, names[class], prob*100);
                int offset = class*123457 % classes;
                float red = get_color(2,offset,classes);
                float green = get_color(1,offset,classes);
                float blue = get_color(0,offset,classes);
                float rgb[3];
    
                //width = prob*20+2;
    
                rgb[0] = red;
                rgb[1] = green;
                rgb[2] = blue;
                box b = dets[i].bbox;
                //printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);
    
                int left  = (b.x-b.w/2.)*im.w;
                int right = (b.x+b.w/2.)*im.w;
                int top   = (b.y-b.h/2.)*im.h;
                int bot   = (b.y+b.h/2.)*im.h;
    
                if(left < 0) left = 0;
                if(right > im.w-1) right = im.w-1;
                if(top < 0) top = 0;
                if(bot > im.h-1) bot = im.h-1;
    	    ///JiaXuejian asdded//
                //printf("%d %d %d %d\n", left, right, top, bot);
    	    ///
    
    	    //JiaXuejian added
    	        FILE* box_coordinate = fopen(output, "a+");
    	        fprintf(box_coordinate, "Class:%s, Box: %d %d %d %d\n", names[class], left, right, top, bot);
    	        fclose(box_coordinate);
    	    ///
    
                draw_box_width(im, left, top, right, bot, width, red, green, blue);
                if (alphabet) {
                    image label = get_label(alphabet, labelstr, (im.h*.03));
                    draw_label(im, top + width, left, label, rgb);
                    free_image(label);
                }
                if (dets[i].mask){
                    image mask = float_to_image(14, 14, 1, dets[i].mask);
                    image resized_mask = resize_image(mask, b.w*im.w, b.h*im.h);
                    image tmask = threshold_image(resized_mask, .5);
                    embed_image(tmask, im, left, top);
                    free_image(mask);
                    free_image(resized_mask);
                    free_image(tmask);
                }
                ///JiaXuejian added//
    	    // printf("%s:", label)
    	    printf("%d %d %d %d\n", left, right, top, bot);
     	    
            }
        }
    }
    
    /Above is JiaXuejian Added/

    新增的部分为:

    int namecount = 0;
    for (namecount = strlen(filename)-1;namecount>=0 ; namecount--){
    	if ((filename[namecount]!='j')&&(filename[namecount]!='p')&&(filename[namecount]!='g')&&(filename[namecount]!='.')){
    		break;
    	}
    	else{
    		output[namecount] = '\0';
    	}
    }
    output = strcat(filename, ".txt");
    
    
    FILE* box_coordinate = fopen(output, "a+");
    fprintf(box_coordinate, "Class:%s, Box: %d %d %d %d\n", names[class], left, right, top, bot);
    fclose(box_coordinate);

    由于是C++代码,所以需要在头文件中声明一下新增的函数,darknet中大部分的函数声明在 ./darknet/include/darknet.h 文件中,打开该文件,在735行可以看到 draw_detections 的函数声明,所以我们要输入以下代码:

    void draw_detections_in_txt(image im, char *filename, detection *dets, int num, float thresh, char **names, image **alphabet, int classes);

    Step 2

    在进行完函数修改之后,我们要找到调用该函数的地方。

    ./darknet/example/detector.c 文件中的 test_detector() 函数调用了draw_detections() 函数,所以我们要将之前的调用改为我们新建的函数的调用,并传入一个 filename 参数,通过对代码的分析,发现 test_detector() 函数中的 input 变量为处理过后的图片名,所以将 input 传入我们新建的函数中,并进行调用,并将以前对 draw_detections() 的调用删掉。代码如下:

    draw_detections_in_txt(im, input, dets, nboxes, thresh, names, alphabet, l.classes);

    至此就完成了对源码的修改,此时就可以将bounding-box的坐标保存进相应文件名的txt中了。

     

    Reference

    https://blog.csdn.net/zhangxinyu11021130/article/details/76473193

    展开全文
  • 任务描述:用Tensorflow object detection API检测出来的结果是一整张图片,想要把检测出的bounding box部分单独截取出来并保存 运行环境:spyder 效果展示: 测试图片:test_images --> 检测图片:testsave_...

    任务描述:用Tensorflow object detection API检测出来的结果是一整张图片,想要把检测出的bounding box部分单独截取出来并保存
    运行环境:spyder
    效果展示:
    测试图片:test_images --> 检测图片:testsave_images --> 裁剪bounding box:test_cropped

    1. 我的修改过后的test文件
    
    # coding: utf-8
    
    # # Object Detection Demo
    # Welcome to the object detection inference walkthrough!  This notebook will walk you step by step through the process of using a pre-trained model to detect objects in an image. Make sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md) before you start.
    
    # # Imports
    
    # In[19]:
    from skimage import data_dir
    import skimage.io as io
    import numpy as np
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import zipfile
    
    from collections import defaultdict
    from io import StringIO
    from matplotlib import pyplot as plt
    from PIL import Image
    
    # This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("..")
    from object_detection.utils import ops as utils_ops
    
    if tf.__version__ < '1.4.0':
      raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')
    
    
    # ## Env setup
    
    # In[20]:
    
    
    # This is needed to display the images.
    #get_ipython().run_line_magic('matplotlib', 'inline')  我的电脑上这句如果不注释掉会报错
    
    
    # ## Object detection imports
    # Here are the imports from the object detection module.
    
    # In[21]:
    
    
    from utils import label_map_util
    
    from utils import visualization_utils as vis_util #主要就是用到了utils目录下的visualization_utils文件
    
    
    # # Model preparation 
    
    # ## Variables
    # 
    # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.  
    # 
    # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.
    
    # In[22]:
    
    
    # What model to download.
    MODEL_NAME = 'headshoulder0603'  #我的模型名字
    
    
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
    
    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'headshoulder.pbtxt')  #我的模型相关文件
    
    NUM_CLASSES = 1 #我的任务是检测头肩,只有一类
    
    
    # ## Download Model
    
    # ## Load a (frozen) Tensorflow model into memory.
    
    # In[ ]:
    
    
    detection_graph = tf.Graph()
    with detection_graph.as_default():
      od_graph_def = tf.GraphDef()
      with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')
    
    
    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    
    # In[ ]:
    
    
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    
    
    # ## Helper code
    
    # In[23]:
    
    #自己在实践中发现,图片经常需要在Image和numpy array两种格式中切换,这个函数就是将Image格式转换成numpy array
    def load_image_into_numpy_array(image):
      (im_width, im_height) = image.size
      return np.array(image.getdata()).reshape(
          (im_height, im_width, 3)).astype(np.uint8)
    
    
    # # Detection
    
    # In[24]:
    
    
    # For the sake of simplicity we will use only 2 images:
    # image1.jpg
    # image2.jpg
    # If you want to test the code with Aayour images, just add path to the images to the TEST_IMAGE_PATHS.
    
    PATH_TO_TEST_IMAGES_DIR = os.getcwd()+'\\test_images'   #存放测试图片的目录路径
    os.chdir(PATH_TO_TEST_IMAGES_DIR)  
    TEST_IMAGE_PATHS = os.listdir(PATH_TO_TEST_IMAGES_DIR) 
    # Size, in inches, of the output images.
    IMAGE_SIZE = (12, 8)
    
    
    # In[25]:
    
    
    def run_inference_for_single_image(image, graph):
      with graph.as_default():
        with tf.Session() as sess:
          # Get handles to input and output tensors
          ops = tf.get_default_graph().get_operations()
          all_tensor_names = {output.name for op in ops for output in op.outputs}
          tensor_dict = {}
          for key in [
              'num_detections', 'detection_boxes', 'detection_scores',
              'detection_classes', 'detection_masks'
          ]:
            tensor_name = key + ':0'
            if tensor_name in all_tensor_names:
              tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
                  tensor_name)
          if 'detection_masks' in tensor_dict:
            # The following processing is only for single image
            detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
            detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
            # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
            real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
            detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
            detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
            detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
                detection_masks, detection_boxes, image.shape[0], image.shape[1])
            detection_masks_reframed = tf.cast(
                tf.greater(detection_masks_reframed, 0.5), tf.uint8)
            # Follow the convention by adding back the batch dimension
            tensor_dict['detection_masks'] = tf.expand_dims(
                detection_masks_reframed, 0)
          image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
    
          # Run inference
          output_dict = sess.run(tensor_dict,
                                 feed_dict={image_tensor: np.expand_dims(image, 0)})
    
          # all outputs are float32 numpy arrays, so convert types as appropriate
          output_dict['num_detections'] = int(output_dict['num_detections'][0])
          output_dict['detection_classes'] = output_dict[
              'detection_classes'][0].astype(np.uint8)
          output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
          output_dict['detection_scores'] = output_dict['detection_scores'][0]
          if 'detection_masks' in output_dict:
            output_dict['detection_masks'] = output_dict['detection_masks'][0]
      return output_dict
    
    
    # In[26]:
    
    
    for image_path in TEST_IMAGE_PATHS:
        image= Image.open(image_path) #注意这里的image_path是个路径,也就是说是个字符串str,下文会用到
      # 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, detection_graph)
      # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          image_path, #原有的代码没有这一行,但是我需要传递测试图片image的文件名给visualize_utils文件中,所以加上,对应的visualize_utils中的visualize_boxes_and_labels_on_image_array函数也要加上这个参数
          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=1, #默认的框到粗细是8,但是实在太粗了
          )#the width of bounding box,default is 8
        plt.figure(figsize=IMAGE_SIZE)
        plt.imshow(image_np)
        #save_dir = TEST_IMAGE_PATHS + '{}.jpg'.format(image_path)
        PATH_TO_TEST_IMAGES_1_DIR = r'C:\\models\\research\\object_detection\\testsave_images' #这里可能要加上r,不然可能会有编码ucf8错误
        save_dir = os.path.join(PATH_TO_TEST_IMAGES_1_DIR, image_path)
        vis_util.save_image_array_as_png(image_np,save_dir) #我将检测出来的图片保存在testsave_images文件夹下
         
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    2、修改过后的visualization_utils.py文件中 visualize_boxes_and_labels_on_image_array 函数

    def visualize_boxes_and_labels_on_image_array(
        image,
        image_path, #我添加的,因为要用到测试图片的文件名
        boxes,
        classes,
        scores,
        category_index,
        instance_masks=None,
        instance_boundaries=None,
        keypoints=None,
        use_normalized_coordinates=False,
        max_boxes_to_draw=20,
        min_score_thresh=.5,
        agnostic_mode=False,
        line_thickness=1,
        groundtruth_box_visualization_color='black',
        skip_scores=False,
        skip_labels=False,
        ):
      """Overlay labeled boxes on an image with formatted scores and label names.
    
      This function groups boxes that correspond to the same location
      and creates a display string for each detection and overlays these
      on the image. Note that this function modifies the image in place, and returns
      that same image.
    
      Args:
        image: uint8 numpy array with shape (img_height, img_width, 3)
        boxes: a numpy array of shape [N, 4]
        classes: a numpy array of shape [N]. Note that class indices are 1-based,
          and match the keys in the label map.
        scores: a numpy array of shape [N] or None.  If scores=None, then
          this function assumes that the boxes to be plotted are groundtruth
          boxes and plot all boxes as black with no classes or scores.
        category_index: a dict containing category dictionaries (each holding
          category index `id` and category name `name`) keyed by category indices.
        instance_masks: a numpy array of shape [N, image_height, image_width] with
          values ranging between 0 and 1, can be None.
        instance_boundaries: a numpy array of shape [N, image_height, image_width]
          with values ranging between 0 and 1, can be None.
        keypoints: a numpy array of shape [N, num_keypoints, 2], can
          be None
        use_normalized_coordinates: whether boxes is to be interpreted as
          normalized coordinates or not.
        max_boxes_to_draw: maximum number of boxes to visualize.  If None, draw
          all boxes.
        min_score_thresh: minimum score threshold for a box to be visualized
        agnostic_mode: boolean (default: False) controlling whether to evaluate in
          class-agnostic mode or not.  This mode will display scores but ignore
          classes.
        line_thickness: integer (default: 4) controlling line width of the boxes.
        groundtruth_box_visualization_color: box color for visualizing groundtruth
          boxes
        skip_scores: whether to skip score when drawing a single detection
        skip_labels: whether to skip label when drawing a single detection
    
      Returns:
        uint8 numpy array with shape (img_height, img_width, 3) with overlaid boxes.
      """
      # Create a display string (and color) for every box location, group any boxes
      # that correspond to the same location.
    
      box_to_display_str_map = collections.defaultdict(list)
      box_to_color_map = collections.defaultdict(str)
      box_to_instance_masks_map = {}
      box_to_instance_boundaries_map = {}
    #  box_to_keypoints_map = collections.defaultdict(list)
      if not max_boxes_to_draw:
        max_boxes_to_draw = boxes.shape[0]
      for i in range(min(max_boxes_to_draw, boxes.shape[0])):
        if scores is None or scores[i] > min_score_thresh:
          box = tuple(boxes[i].tolist())
          if instance_masks is not None:
            box_to_instance_masks_map[box] = instance_masks[i]
          if instance_boundaries is not None:
            box_to_instance_boundaries_map[box] = instance_boundaries[i]
         
          if scores is None:
            box_to_color_map[box] = groundtruth_box_visualization_color
          else:
            display_str = ''
            if not skip_labels:
              if not agnostic_mode:
                if classes[i] in category_index.keys():
                  class_name = category_index[classes[i]]['name']
                else:
                  class_name = 'N/A'
                display_str = str(class_name)
            if not skip_scores:
              if not display_str:
                display_str = '{}%'.format(int(100*scores[i]))
              else:
                display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
            box_to_display_str_map[box].append(display_str)
            if agnostic_mode:
              box_to_color_map[box] = 'DarkOrange'
            else:
              box_to_color_map[box] = STANDARD_COLORS[
                  classes[i] % len(STANDARD_COLORS)]
    #==================================================================================#
    #===============crop bounding box images===========================================#
    #==================================================================================# 	
      t = 0 #这行记得是放在下面的循环体外的
      for box, color in box_to_color_map.items():
        ymin, xmin, ymax, xmax = box  #前文已经得到bounding box的坐标,但是,由于使用了use_normalized_coordinates,将坐标归一化了,所以要映射回来
        img = Image.fromarray(np.uint8(image))
        im_width, im_height = img.size
        print('im_width, im_height:', im_width, im_height)
        new_xmin = int(xmin * im_width)
        new_xmax = int(xmax * im_width)
        new_ymin = int(ymin * im_height)
        new_ymax = int(ymax * im_height)
    #    print('xmin,xmax,ymin,ymax:',xmin,xmax,ymin,ymax)
        print('new_xmin,new_xmax,new_ymin,new_ymax:',new_xmin,new_xmax,new_ymin,new_ymax)
    
        image_ = image[new_ymin:new_ymax,new_xmin:new_xmax] #将测试图片做裁剪,只剩bounding box部分,注意此处的image_命名,之前我写的是image,会出错。而且要注意的是,由于代码中用的是PIL图片格式,原点在左上角,而此处用numpy的方式裁剪图片,原点在左下角,所以x和y是反过来的。
        
        plt.imshow(image_)
        plt.show()
    #       
        PATH_TO_crop_DIR = r'C:\\models\\research\\object_detection\\test_cropped' #保存的裁剪图片位置
        image_ = Image.fromarray(np.uint8(image_))
        t+=1
    #    image_.save( 'C:\\models\\research\\object_detection\\test_cropped\\cropped_images.jpg')
    #    image_.save(os.path.join(PATH_TO_crop_DIR, str(t)+'.jpg'))
        image_.save(os.path.join(PATH_TO_crop_DIR, (str(t)+'_')+os.path.basename(image_path))) #这里用上了image_path,因为os.path.join需要字符串的参数,由于一张测试图片可能会检测出多个bounding box,所以在保存命名的时候加上了t这个参数(这一步应该会有更优雅的做法,但是我懒得去试了)
    ####################################################################################
      # Draw all boxes onto image.把框和文件画到测试图片上
      for box, color in box_to_color_map.items():
        ymin, xmin, ymax, xmax = box
        draw_bounding_box_on_image_array(
            image,
            ymin,
            xmin,
            ymax,
            xmax,
            color=color,
            thickness=line_thickness,
            display_str_list=box_to_display_str_map[box],
            use_normalized_coordinates=use_normalized_coordinates)、
      return image

     

    转自:

    Tensorflow object detection API--修改visualization_utils文件,裁剪并保存bounding box部分

    展开全文
  • 一般对于某些站点附件、应用系统生成的报表文件,管理员都需要进行生命周期的管理,将一部分年份很久的移到别处保存以使系统获得更好的运行效率。如果这些文件分散在很多目录或目录级下,手工作业过于繁琐,此工具...
  • 这个高级任务管理器被作者制作成了文件绿色版软件,这就意味着,Task Manager DeLuxe无需安装,也可以存放到任何地方运行,甚至是U盘中。 这个软件的体积为1.1M,十分的小,兼容2000、XP、2003、Vista、Win7、...
  • 单例模式:简单的说,一个对象只负责一个特定的任务。 单例类: 1.构造函数需要标记为private,单例类不能再其他类中实例化,只能被其自身实例化 2.拥有一个保存类的实例静态成员变量 3.拥有一个访问这个实例的...
  • 7、执行各种操作时,在任务栏按钮上同步显示操作进度。 8、支持通过鼠标拖拽加载虚拟硬盘。 9、支持通过鼠标从资源管理器拖拽的方式复制文件到分区。 10、支持通过右键菜单复制磁盘及分区信息中的文字。 11、发现...
  • 修正文件管理器显示模式状态不能保存问题 优化其他一些功能 iTools 2011 Preview2 0508 增加设备导入导出墙纸 增加设备导入导出铃声,自动MP3 WAV 转换成 M4R 格式 增加SHSH备份以及管理,支持本地模拟SHSH 服务器...
  • 学习单73 动态创建数据表 学习单74 在程序中设置ODBC数据源 学习单75 自动检测、建立数据库别名和数据表 任 务 篇任务单76 绘制图形 任务单77 图像编辑器 任务单78 制作mp3播放器 任务单79 多媒体播放器 任务单80 ...
  • 爬虫系统的设计

    千次阅读 2018-04-02 00:01:28
    爬虫系统的组成部分 爬虫的组件 下载任务 解析任务 检测任务 ...任务队列 ...代理任务 ... 从上面的图片中可以清晰的看出整个爬虫系统 在...保存未消费的任务状态,记录已经消费的任务状态,这样当项目重启的时候,能...

    爬虫系统的组成部分

    这里写图片描述

    爬虫的组件

    • 下载任务

    • 解析任务

    • 检测任务

    • 调度中心

    • 任务队列

    • 数据仓库

    • 代理任务

    从上面的图片中可以清晰的看出整个爬虫系统 在单机的状态是如何工作的,其实整个系统看起来就是消费者和生产者的关系,所以需要一个装载任务的容器,那么这个容器要有基本的要求:断点续传,能够在项目意外暂停的时候,保存未消费的任务状态,记录已经消费的任务状态,这样当项目重启的时候,能够加载未消费的任务然后继续消费?给出两种方案:
    一、通过数据库记录每一条任务的状态,比如添加一个selected字段标识这条任务是否在队列,添加status字段标识这条任务是否被消费过,一旦任务消费了,立马改变status字段状态,selected状态,这样系统突然停止的话,根据selected标识还未被消费的状态,然后把这部分数据添加到系统的任务队列之中,这样的策略虽然能够达到要求,但是不足的地方要不断的和数据库进行通信,要经受大量的写请求,那么要求数据库对表级锁的支持要必须好,性能方面不适合大规模的抓取任务。
    二、通过文件存储来实现对任务状态的记录,每次任务队列中拿到的数据都会存储到一个文件中,按照文件大小做rollingFile,那么每一个新的任务加进来,都会首先被放到一个head文件,当head文件不停增长的时候,到一定大小的时候,一个新的head文件就会被加进来,同时有一个checkpoint的文件,记录任务的消费状态,难么当系统异常重启的时候,通过checkpoint文件定位到已经消费到的文件位置,然后把对应位置以后的所有任务都添加到任务队列中,达到记录任务的状态与持久化存储。

    展开全文
  • 协程IO多路复用

    2018-10-30 16:07:00
    提高效率任务切换 + 保存状态并行:多核cpu,真正的同时执行串行:一个任务执行完在执行另外一个任务多线程多进程下的任务切换+保存状态是操作系统协程:线程下实现并发,最大化线程的效率,检测IO并自动切换,程序级别的...
  • 由此项目,可延伸泛化到「工业零件检测计数」『人流量统计』等领域应用。 另外,还有丰富奖品等着大家噢:蓝牙键盘、飞桨充电宝、京东电子购物卡、百度网盘超级会员、飞桨鸭舌帽、飞桨帆布袋等等~ 具体抽奖规则请...
  • 今日概要: 1 gevent模块 协程:线程下实现并发(并发指的是看起来同时运行,实现方式:切换+保存状态) ... 2、检测单线程下任务的IO,实现遇到IO自动切换 2 IO模型 3 socketserver 4 subpr...
  • 协程

    2017-12-07 21:33:43
    17年12 月5日 协程:线程实现并发。 ps:并非所有的并发对效率都有意义。 1 gevent模块 协程:线程下实现并发(并发指的是看...2、检测单线程下任务的IO,实现遇到IO自动切换 只有大招:(yield,Greenlet不太会)
  • Python-4.29 协程

    2021-02-22 22:10:15
    在应用程序里控制多个任务的切换+保存状态 优点: 应用程序级别速度要远远高于操作系统的切换 缺点: 多个任务一旦有一个阻塞没有切,整个线程都阻塞在原地 该线程内的其他的任务都不能执行了 一旦引入...
  • python-study-36

    2018-07-17 20:54:00
    python并发编程之协程 1、协程: 线程实现并发 在应用程序里控制多个任务的切换+保存状态 ... 应用程序级别速度要远远高于操作系统的切换 ... 多个任务一旦有一个阻塞没有... 一旦引入协程,就需要检测单线程下...
  • python并发编程之协程

    2018-07-17 20:29:00
    1、协程: 线程实现并发 在应用程序里控制多个任务的切换+保存状态 优点: 应用程序级别速度要远远高于操作系统的切换 缺点: 多个任务一旦有一个阻塞没有切,整个线程都阻塞在原地 该线程内的其他的任务都不能...
  • 协程与IO模型

    2018-07-17 20:00:08
    在应用程序里控制多个任务的切换+保存状态 优点: 应用程序级别速度要远远高于操作系统的切换 缺点: 多个任务一旦有一个阻塞没有切,整个线程都阻塞在原地 该线程内的其他的任务都不能执行了 一旦引入协程...
  • python中的协程(二)

    2018-12-10 12:55:00
    协程 1、协程: 线程实现并发 ...一旦引入协程,就需要检测单线程下所有的IO行为, 实现遇到IO就切换,少一个都不行,以为一旦一个任务阻塞了,整个线程就阻塞了, 其他的任务即便是可以计算,但是也无法...
  • 协程和I/O模型

    2019-09-26 12:24:07
    1、协程: 线程实现并发 在应用程序里控制多个任务的切换+保存状态 优点: 应用程序级别速度要远远高于操作系统的切换 缺点: 多个任务一旦有一个阻塞没有切换,整个线程都阻塞在原地 该线程内的其他的任务都不能...
  • 协程的本质就是在线程下,由用户自己控制一个任务遇到io阻塞了就切换另外一个任务去执行,以此来提升效率。为了实现它,我们需要找寻一种可以同时满足以下条件的解决方案: 1. 可以控制多个任务之间的切换,切换...
  • 并发编程之 协程

    2019-10-08 02:22:07
    程序员自己通过代码自己检测程序中的IO 一旦遇到了IO自己通过代码切换 给操作系统的感觉就是你这个线程没有任何的IO 从而提升代码的运行效率 切换+保存状态一定能够提升效率吗?  1.当任务是IO密集型的情况下...
  • Python学习笔记十一

    2017-12-01 01:04:00
    1. 协程  并发的解决方案:  多进程  多线程  什么叫并发:看起来同时进行  如何实现并发:切换+保存状态 ... 进程线程都是由操作系统调度的 ...在多个任务直接切换+保存状态  2.检测应用程序...
  • 保存状态以进行连续爬网 用Cheerio解析类似jQuery的服务器端DOM 平行要求 代理列表和用户代理列表支持 HTTP标头和cookie设置 自动字符集检测和转换 控制台进度指示器 节点从0.10到6.0的支持 安装 npm install ...
  • 协程,网络IO

    2018-07-17 20:53:30
     在应用程序里控制多个任务的切换+保存状态  优点: 应用程序级别的速度要远远高于操作系统的切换  缺点: 只有 一个线程,多个任务一旦有一个阻塞没有切,整个线程都阻塞在原地  一旦引入协程,就需要...

空空如也

空空如也

1 2 3 4 5 ... 18
收藏数 342
精华内容 136
关键字:

任务单保存检测