Esempio n. 1
0
def test():
    reconstructed_images = []
    record_iterator = tf.python_io.tf_record_iterator(
        path=
        'datasets/battery_word_seg/tfrecord/trainval-00000-of-00002.tfrecord')
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for string_iterator in record_iterator:
            plt.figure(figsize=(12, 12))
            example = tf.train.Example()
            example.ParseFromString(string_iterator)
            height = example.features.feature['image/height'].int64_list.value[
                0]
            width = example.features.feature['image/width'].int64_list.value[0]
            png_string = example.features.feature[
                'image/encoded'].bytes_list.value[0]
            #             label = example.features.feature['image/object/class/label'].int64_list.value[0]
            #             xmin = example.features.feature['image/object/bbox/xmin'].float_list.value[0]
            #             xmax = example.features.feature['image/object/bbox/xmax'].float_list.value[0]
            #             ymin = example.features.feature['image/object/bbox/ymin'].float_list.value[0]
            #             ymax = example.features.feature['image/object/bbox/ymax'].float_list.value[0]

            encoded_mask_string = example.features.feature[
                'image/segmentation/class/encoded'].bytes_list.value[0]

            plt.subplot(131)
            mask_decode_png = tf.image.decode_png(encoded_mask_string,
                                                  channels=1)
            fix_mask = tf.cast(tf.greater(mask_decode_png, 0), tf.uint8)

            redecode_mask_img = sess.run(mask_decode_png)
            #             write_file("mask.csv",redecode_mask_img)
            print(redecode_mask_img.shape)
            redecode_mask = redecode_mask_img * 255
            mask_img = np.squeeze(redecode_mask, axis=2)
            plt.imshow(mask_img)
            im = Image.fromarray(mask_img)
            im.save("pets.png")

            plt.subplot(132)
            decoded_img = tf.image.decode_jpeg(png_string, channels=3)
            reconstructed_img = sess.run(decoded_img)
            print(reconstructed_img.shape)
            plt.imshow(reconstructed_img)

            plt.subplot(133)
            vis_util.draw_mask_on_image_array(image=reconstructed_img,
                                              mask=np.squeeze(
                                                  sess.run(fix_mask), axis=2),
                                              alpha=0.8)
            plt.imshow(reconstructed_img)

            plt.show()

        coord.request_stop()
        coord.join(threads)
 def test_draw_mask_on_image_array(self):
   test_image = np.asarray([[[0, 0, 0], [0, 0, 0]],
                            [[0, 0, 0], [0, 0, 0]]], dtype=np.uint8)
   mask = np.asarray([[0, 1],
                      [1, 1]], dtype=np.uint8)
   expected_result = np.asarray([[[0, 0, 0], [0, 0, 127]],
                                 [[0, 0, 127], [0, 0, 127]]], dtype=np.uint8)
   visualization_utils.draw_mask_on_image_array(test_image, mask,
                                                color='Blue', alpha=.5)
   self.assertAllEqual(test_image, expected_result)
Esempio n. 3
0
 def test_draw_mask_on_image_array(self):
   test_image = np.asarray([[[0, 0, 0], [0, 0, 0]],
                            [[0, 0, 0], [0, 0, 0]]], dtype=np.uint8)
   mask = np.asarray([[0, 1],
                      [1, 1]], dtype=np.uint8)
   expected_result = np.asarray([[[0, 0, 0], [0, 0, 127]],
                                 [[0, 0, 127], [0, 0, 127]]], dtype=np.uint8)
   visualization_utils.draw_mask_on_image_array(test_image, mask,
                                                color='Blue', alpha=.5)
   self.assertAllEqual(test_image, expected_result)
Esempio n. 4
0
def draw_tracked_people(img_bgr, tracked_people):
    """
    Draw bounding box and mask of detected and tracked people, each with a different color based on their ids.
    :param img_bgr: Original image where people are detected and tracked.
    :param tracked_people: a list of TrackedPerson objects.
    :return A numpy
    """
    if len(tracked_people) == 0:
        return img_bgr

    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    # img_pil = Image.fromarray(img_rgb)

    for person in tracked_people:
        color = STANDARD_COLORS[person.id % len(STANDARD_COLORS)]
        if person.body_box is not None:
            xmin, ymin, xmax, ymax = person.body_box
            draw_bounding_box_on_image_array(img_rgb,
                                             ymin,
                                             xmin,
                                             ymax,
                                             xmax,
                                             color=color,
                                             display_str_list=[
                                                 'ID:{}  Score:{:.2f}'.format(
                                                     person.id,
                                                     person.body_score)
                                             ],
                                             use_normalized_coordinates=False)
        if person.face_box is not None:
            xmin, ymin, xmax, ymax = person.face_box
            draw_bounding_box_on_image_array(
                img_rgb,
                ymin,
                xmin,
                ymax,
                xmax,
                color=color,
                display_str_list=['Score:{:.2f}'.format(person.face_score)],
                use_normalized_coordinates=False)
        if person.body_mask is not None:
            draw_mask_on_image_array(img_rgb, person.body_mask, color=color)

    return cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
Esempio n. 5
0
def run_inference_for_images(workPath,
                             model_name,
                             path_to_labels,
                             inputPath,
                             outputMASKpath=None,
                             resizeRatio=0.125,
                             scoreThreshold=0.5):
    print("正在检测,请勿关闭此窗口!否则,将退出检测!")
    #起始时间记录
    start = datetime.datetime.now()
    startTime = start.strftime('%Y-%m-%d %H:%M:%S.%f')
    print("楼面预分割任务起始时间:" + startTime)

    #准备需要的路径
    # What model to use.
    MODEL_NAME = model_name
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = os.path.join(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(path_to_labels, 'label_map.pbtxt')

    # the Number of classes
    NUM_CLASSES = 10

    #载入图graph
    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='')

    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)

    #创建临时文件夹
    TempPath = os.path.join(workPath, "Temp")
    if os.path.exists(TempPath) == True:
        shutil.rmtree(TempPath)  #清空
        os.mkdir(TempPath)
    else:
        os.mkdir(TempPath)

    if outputMASKpath == None:
        MaskPath = os.path.join(TempPath, "Mask")
    else:
        MaskPath = outputMASKpath
    if os.path.exists(MaskPath) != True:
        #shutil.rmtree(MaskPath)   #清空
        os.mkdir(MaskPath)

    #计算待检测文件夹中有多少待检测文件
    num_files = 0
    for file in os.listdir(inputPath):
        fname, ftype = os.path.splitext(file)
        if ftype == ".JPG" or ftype == ".jpg":
            num_files = num_files + 1
    print("待检测文件夹中图像数量:" + str(num_files))

    #detection_graph.as_default()
    with tf.Session(graph=detection_graph) as sess:
        file_index = 0
        for file in os.listdir(inputPath):
            fname, ftype = os.path.splitext(file)

            if ftype != ".JPG" and ftype != ".jpg":
                continue
            else:
                file_index = file_index + 1

            if os.path.exists(os.path.join(MaskPath, file)) == True:
                print("已检测过第%d图片:%s" % (file_index, file))
                continue

            print("检测第%d图片:%s" % (file_index, file))
            sPDstart = datetime.datetime.now()  #singlePicDetection
            sPDstartTime = sPDstart.strftime('%Y-%m-%d %H:%M:%S.%f')
            print("开始时间:" + sPDstartTime)

            filepath = os.path.join(inputPath, file)
            '''
            sCDstart=datetime.datetime.now()
            sCDstartTime=sCDstart.strftime('%Y-%m-%d %H:%M:%S.%f')
            print(sCDstartTime)          
            '''
            #CJY  at  2019.7.11  为了防止图片损坏的情况,使用try except
            try:
                image = Image.open(filepath)
            except (OSError, NameError):
                print('OSError, Path:', filepath)
                continue

            img_w = image.size[0]
            img_h = image.size[1]
            w = int(img_w * resizeRatio)
            h = int(img_h * resizeRatio)
            image = image.resize((w, h))

            # 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)  #最耗费时间 0.10s左右

            # 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, sess, 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)
            plt.figure(figsize=IMAGE_SIZE)
            plt.imshow(image_np)
            #print(output_dict['detection_boxes'])       '''

            #mask图像绘制与保存
            objectNum = 0
            mask_np = np.ones([h, w, 3], dtype=np.uint8,
                              order='C') * 255  #创建空白掩膜画布
            for index, boxScore in enumerate(output_dict['detection_scores']):
                if boxScore > scoreThreshold:
                    masks = output_dict.get(
                        'detection_masks'
                    )  #output_dict["detection_masks"][index]
                    vis_util.draw_mask_on_image_array(mask_np, masks[index],
                                                      "black", 1.0)
                    objectNum = objectNum + 1

            mask_np = cv2.cvtColor(mask_np, cv2.COLOR_BGR2GRAY)

            #识别天空
            resizeRatio2 = 0.05
            image_gray = cv2.cvtColor(
                cv2.resize(
                    image_np,
                    (int(img_w * resizeRatio2), int(img_h * resizeRatio2))),
                cv2.COLOR_BGR2GRAY)  #resize中 写  宽、高

            seeds = [
                Grow.Point(0, 0),
                Grow.Point(image_gray.shape[0] - 1, 0),
                Grow.Point(0, image_gray.shape[1] - 1),
                Grow.Point(image_gray.shape[0] - 1, image_gray.shape[1] - 1)
            ]

            SkyMask = Grow.regionGrow(image_gray, seeds, 3)
            kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
            SkyMask = cv2.morphologyEx(SkyMask, cv2.MORPH_CLOSE, kernel)
            contours, hierarchy = cv2.findContours(SkyMask, cv2.RETR_TREE,
                                                   cv2.CHAIN_APPROX_NONE)

            m = np.zeros(SkyMask.shape, dtype=np.uint8, order='C')  #创建黑色掩膜画布
            areaTH = int(SkyMask.shape[0] * SkyMask.shape[1] * 0.1)
            for i in range(0, len(contours)):
                if cv2.contourArea(contours[i]) >= areaTH:
                    cv2.drawContours(m, contours, i, 255, -1)

            SkyMask = cv2.resize(m, (w, h))
            mask_np = (~SkyMask) & mask_np

            #if (objectNum > 0):
            im = Image.fromarray(mask_np)
            im = im.resize((img_w, img_h))
            im.save(os.path.join(MaskPath, file))
            #'''
            '''
            sCDend=datetime.datetime.now()
            sCDendTime=sCDend.strftime('%Y-%m-%d %H:%M:%S.%f')
            print(sCDendTime)
            print('单张切片完成时间: %s Seconds'%(cend-cstart)) 
            '''
            '''
            #写入文件
            if os.path.exists(workPath + "Temp/temp.txt")==True:
                os.remove(workPath + "Temp/temp.txt")
            '''

            #4.生成指定成果

            #单张图像检测结束时间
            sPDend = datetime.datetime.now()
            sPDendTime = sPDend.strftime('%Y-%m-%d %H:%M:%S.%f')
            print("结束时间:" + sPDendTime)
            print('单张图像分割时间: %s Seconds' % (sPDend - sPDstart))

            #将信息记录在dTemp.txt中
            detailsLogfile = open(os.path.join(outputMASKpath, 'dTemp.txt'),
                                  'a')  #TempPath
            detailsLogfile.write(fname + "\n")
            detailsLogfile.write("start:" + sPDstartTime + "\n")
            detailsLogfile.write("end:" + sPDendTime + "\n")
            detailsLogfile.write("segmentation useTime:%s" %
                                 (sPDend - sPDstart) + "\n")
            detailsLogfile.close()

            #break

    #删除临时文件夹
    if os.path.exists(TempPath) == True:
        shutil.rmtree(TempPath)

    #写入标志位,是否完成对所有文件的检测
    #finishfile = open(os.path.join(workPath,'FinishFlag.txt'),'w')
    #finishfile.write("1")
    #finishfile.close()

    end = datetime.datetime.now()
    endTime = end.strftime('%Y-%m-%d %H:%M:%S.%f')
    print("楼面预分割任务结束时间:" + endTime)
    print('楼面预分割任务运行时间: %s Seconds' % (end - start))
Esempio n. 6
0
def visualize_boxes_with_opc_info(image,
                                  boxes,
                                  classes,
                                  scores,
                                  category_index,
                                  instance_masks=None,
                                  keypoints=None,
                                  use_normalized_coordinates=False,
                                  max_boxes_to_draw=20,
                                  min_score_thresh=.5,
                                  agnostic_mode=False,
                                  line_thickness=4):
    """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], 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.

  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_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 keypoints is not None:
                box_to_keypoints_map[box].extend(keypoints[i])
            if scores is None:
                box_to_color_map[box] = 'black'
            else:
                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 = '{}: {}%'.format(class_name,
                                                   int(100 * scores[i]))
                else:
                    display_str = 'score: {}%'.format(int(100 * scores[i]))

                if class_name in opc_client.opc_id.keys():
                    display_str = display_str + " " + opc_info.get_value(
                        class_name)
                    print display_str
                box_to_display_str_map[box].append(display_str)
                if agnostic_mode:
                    box_to_color_map[box] = 'DarkOrange'
                else:
                    box_to_color_map[box] = vis_util.STANDARD_COLORS[
                        classes[i] % len(vis_util.STANDARD_COLORS)]

    # Draw all boxes onto image.
    for box, color in box_to_color_map.items():
        ymin, xmin, ymax, xmax = box
        if instance_masks is not None:
            vis_util.draw_mask_on_image_array(image,
                                              box_to_instance_masks_map[box],
                                              color=color)
        vis_util.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)
        if keypoints is not None:
            vis_util.draw_keypoints_on_image_array(
                image,
                box_to_keypoints_map[box],
                color=color,
                radius=line_thickness / 2,
                use_normalized_coordinates=use_normalized_coordinates)

    return image