def process_frame(frame):
    height, width, channels = frame.shape
    frame_area = width * height

    draw = frame.copy()

    frame = preprocess_image(frame)
    frame, scale = resize_image(frame)

    boxes, scores, labels, = MODEL.predict_on_batch(np.expand_dims(frame, axis=0))
    boxes /= scale

    box_json_array = []

    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        b = box.astype(int)
        box_area = np.abs(b[2] - b[0]) * np.abs(b[3] - b[1])
        if score < CONFIDENCE_THRESHOLD or box_area > (frame_area * BOX_AREA_RATIO_TO_IGNORE) or label > MAX_CLASS_ID:
            break
        color = label_color(label)
        draw_box(draw, b, color=color)
        caption = '{} {:.3f}'.format(LABELS_TO_NAMES[label], score)
        draw_caption(draw, b, caption)

        box_json_array.append({
            'label': caption,
            'topLeft': [int(b[0]), int(b[1])],
            'bottomRight': [int(b[2]), int(b[3])]
        })

    return draw, box_json_array
Exemple #2
0
def retinanet_test():
    test_images = [
        'fe1b92a1-faaaa1eb.jpg', 'fe1cc363-a3f36598.jpg',
        'fe1d74f0-5cdc4057.jpg', 'fe1d74f0-6969bdb5.jpg',
        'fe1d9184-cd999efe.jpg', 'fe1d9184-d144106a.jpg',
        'fe1d9184-dec09b65.jpg', 'fe1f2409-5b415eb7.jpg',
        'fe1f2409-c16ea1ed.jpg', 'fe1f55fa-19ba3600.jpg'
    ]

    model = kr_models.load_model(retinanet_h5_path +
                                 'densenet121_bdd10k_18.h5',
                                 backbone_name='densenet121')
    kr_models.check_training_model(model)
    inference_model = kr_models.convert_model(model)
    labels_to_names = bu.get_label_names(bdd100k_labels_path +
                                         'class_mapping.csv')
    # load image
    image = read_image_bgr(bdd100k_val_path + test_images[2])

    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)

    # process image
    start = time.time()
    boxes, scores, labels = inference_model.predict_on_batch(
        np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)

    # correct for image scale
    boxes /= scale

    # visualize detections
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        # scores are sorted so we can break
        if score < 0.5:
            break

        color = label_color(label)

        b = box.astype(int)
        draw_box(draw, b, color=color)

        caption = "{} {:.3f}".format(labels_to_names[label], score)
        draw_caption(draw, b, caption)

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
Exemple #3
0
def predict_image(model,
                  image_path,
                  score_threshold=0.1,
                  max_detections=200,
                  return_plot=True):
    """
        Predict an image
        return_plot: Logical. If true, return a image object, else return bounding boxes
        """
    #predict
    raw_image = cv2.imread(image_path)
    image = image_utils.preprocess_image(raw_image)
    image, scale = keras_retinanet_image.resize_image(image)

    if keras.backend.image_data_format() == 'channels_first':
        image = image.transpose((2, 0, 1))

    # run network
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))[:3]

    # correct boxes for image scale
    boxes /= scale

    # select indices which have a score above the threshold
    indices = np.where(scores[0, :] > score_threshold)[0]

    # select those scores
    scores = scores[0][indices]

    # find the order with which to sort the scores
    scores_sort = np.argsort(-scores)[:max_detections]

    # select detections
    image_boxes = boxes[0, indices[scores_sort], :]
    image_scores = scores[scores_sort]
    image_labels = labels[0, indices[scores_sort]]
    image_detections = np.concatenate([
        image_boxes,
        np.expand_dims(image_scores, axis=1),
        np.expand_dims(image_labels, axis=1)
    ],
                                      axis=1)

    if return_plot:
        draw_detections(raw_image,
                        image_boxes,
                        image_scores,
                        image_labels,
                        label_to_name=label_to_name,
                        score_threshold=score_threshold)
        return raw_image
    else:
        return image_boxes
def predict(image):
  image = preprocess_image(image.copy())
  image, scale = resize_image(image)

  boxes, scores, labels = model.predict_on_batch(
    np.expand_dims(image, axis=0)
  )

  boxes /= scale

  return boxes, scores, labels
Exemple #5
0
def run(image_path, model, host='localhost', port=8500, signature_name='serving_default'):
    
    options=[
                      ('grpc.max_send_message_length', 50 * 1024 * 1024),
                                ('grpc.max_receive_message_length', 50 * 1024 * 1024)
                                      ]
    
    channel = grpc.insecure_channel('{host}:{port}'.format(host=host, port=port), options = options)
    
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    
    image = read_image_bgr(image_path)
    
    image = preprocess_image(image.copy())
    image, scale = resize_image(image)
    
    data = np.array(image).astype(tf.keras.backend.floatx())
    
    start = time.time()
    
    
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model
    request.model_spec.signature_name = signature_name
    print('Shape of data going in to inputs',data.shape)
    request.inputs['input_image'].CopyFrom(make_tensor_proto(data, shape=[1, data.shape[0], data.shape[1], 3]))

    result = stub.Predict(request, 10.0)
    
    end = time.time()
    time_diff = end - start
    
    print('time elapased: {}'.format(time_diff))
    print(type(result))
#     print('classes',result.outputs['classes'])
    
    bboxes_proto = result.outputs["filtered_detections/map/TensorArrayStack/TensorArrayGatherV3:0"]
#     print('bboxes proto',bboxes_proto)
    bboxes_proto_shape = tf.TensorShape(bboxes_proto.tensor_shape)
#     print('bboxes_proto_shape',bboxes_proto.tensor_shape)
    bboxes = tf.constant(bboxes_proto.float_val, shape=bboxes_proto_shape)/scale
    
    print(bboxes.numpy().shape)
    
    confidences_proto = result.outputs["filtered_detections/map/TensorArrayStack_1/TensorArrayGatherV3:0"]
    confidences_proto_shape = tf.TensorShape(confidences_proto.tensor_shape)
    confidences = tf.constant(confidences_proto.float_val, shape=confidences_proto_shape)

    labels_proto = result.outputs["filtered_detections/map/TensorArrayStack_2/TensorArrayGatherV3:0"]
    labels_shape = tf.TensorShape(labels_proto.tensor_shape)
    labels = tf.constant(labels_proto.int_val, shape=labels_shape)

    return bboxes.numpy(), confidences.numpy(), labels.numpy()
    def get_localization(self, image, visual=False):
        """Determines the locations of the traffic light in the image

        Args:
            image: camera image

        Returns:
            list of bounding boxes: coordinates [y_up, x_left, y_down, x_right]

        """
        bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        image = preprocess_image(bgr)
        image, scale = resize_image(image)
        # preprocess image for network
        boxes, scores, classes = self.model.predict_on_batch(
            np.expand_dims(image, axis=0))

        boxes /= scale

        boxes = np.squeeze(boxes)
        classes = np.squeeze(classes)
        scores = np.squeeze(scores)

        cls = classes.tolist()

        # The ID for car is 1
        idx_vec = [
            i for i, v in enumerate(cls) if ((v == 1) and (scores[i] > 0.3))
        ]

        if len(idx_vec) == 0:
            print('no detection!')
        else:
            tmp_car_boxes = []
            for idx in idx_vec:
                #dim = image.shape[0:2]
                #box = self.box_normal_to_pixel(boxes[idx], dim)
                box = boxes[idx].astype(int)
                box_w = box[2] - box[0]
                box_h = box[3] - box[1]
                ratio = box_h / (box_w + 0.01)

                if ((ratio < 0.8) and (box_h > 20) and (box_w > 20)
                        and scores[idx] > 0.5):
                    tmp_car_boxes.append(box)
                    print(box, ', confidence: ', scores[idx], 'ratio:', ratio)
                else:
                    print('wrong ratio or wrong size or low confidence, ', box,
                          ', confidence: ', scores[idx], 'ratio:', ratio)

            self.car_boxes = tmp_car_boxes

        return self.car_boxes
Exemple #7
0
def main(args=None):
    args = parse_args(args)

    model_bin = args.bin
    img_fn = args.img
    predict_count = args.count
    backbone = args.backbone

    print("loading model...")
    if args.gpu:
        setup_gpu(0)

    model = models.load_model(model_bin, backbone_name=backbone)

    print(f'model input shape: {model.inputs[0].shape}')

    start_time = time.time()

    image = cv2.imread(img_fn)
    image, scale = resize_image(image)
    image = preprocess_image(image)
    print("prepoocess image at {} s".format(time.time() - start_time))

    labels_to_names = {0: 'Pedestrian'}

    print(f'make {predict_count} predictions:')
    for _ in range(0, predict_count):
        start_time = time.time()
        boxes, scores, labels = model.predict_on_batch(
            np.expand_dims(image, axis=0))
        print("\t{} s".format(time.time() - start_time))

    print("*" * 20)
    print('bboxes:', boxes.shape)
    print('scores:', scores.shape)
    print('labels:', labels.shape)

    boxes /= scale

    objects_count = 0

    print("*" * 20)
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        # scores are sorted so we can break
        if score < 0.5:
            break
        b = np.array(box.astype(int)).astype(int)
        # x1 y1 x2 y2
        print(f'{labels_to_names[label]}:')
        print(f'\tscore: {score}')
        print(f'\tbox: {b[0]} {b[1]} {b[2]} {b[3]}')
        objects_count = objects_count + 1
    print(f'found objects: {objects_count}')
Exemple #8
0
def run_detection_video(video_path):
    count = 0
    success = True
    start = time.time()
    while success:
        if count % 100 == 0:
            print("frame: ", count)
        count += 1  # see what frames you are at
        # Read next image
        success, image = vcapture.read()
        
        if success:
            
            # so we can keep orig image scale
            draw = image.copy()
            draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
            
#             image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                
             # preprocess image for network
            image = preprocess_image(image)
            image, scale = resize_image(image)
            
            # Do compute
            boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
            
            # correct for image scale
            boxes /= scale
            
            
             # visualize detections
            for box, score, label in zip(boxes[0], scores[0], labels[0]):
                # scores are sorted so we can break
                if score < 0.4:
                    break

                color = label_color(label)

                b = box.astype(int)
                draw_box(draw, b, color=color)

                caption = "{} {:.3f}".format(labels_to_names[label], score)
                draw_caption(draw, b, caption)
            
            vwriter.write(draw) # overwrites video slice


    vcapture.release()
    vwriter.release() # 
    end = time.time()
    
    print("Total Time: ", end - start)
def object_detection(model, inputData_list, dataset_path, output_path):

    for img_name in inputData_list:
        print("handling " + img_name)
        imagePath = dataset_path + img_name
        image = read_image_bgr(imagePath)

        print('image shape:', image.shape)

        # copy to draw on
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        # 모델에 입력전에 이미지 사전 처리. keras-retinanet은 image
        image = preprocess_image(image)
        image, scale = resize_image(image)
        print('resized image size:', image.shape, 'scale:', scale)

        # 이미지에 대해 Object Detection 수행.
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            np.expand_dims(image, axis=0))
        print(boxes.shape, scores.shape, labels.shape)
        print("processing time: ", time.time() - start)

        # correct for image scale
        boxes /= scale

        # visualize detections
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            # scores are sorted so we can break
            if score < 0.5:
                break

            labels_to_num[label] += 1

            b = box.astype(int)
            #print(b)
            object_img = draw[b[1]:b[3], b[0]:b[2]]
            #print(object_img)
            object_img = Image.fromarray(object_img)

            imagePath_str = imagePath.replace('/', '-')

            # 객체 dump
            # os.chdir(output_path)
            object_img.save(output_path + "{}_path: ({}).jpg".format(
                labels_to_names_seq[label] +
                str(labels_to_num[label]), imagePath_str))
        # os.chdir('../')

    print("detection 완료!")
Exemple #10
0
def model_predict(img_path, model):
	im = np.array(Image.open(img_path))
	imp = preprocess_image(im)
	imp, scale = resize_image(im)
	boxes, scores, labels = model.predict(np.expand_dims(imp, axis=0))
	predictions = []
	for box, score, label in zip(boxes[0], scores[0], labels[0]):
		if score < 0.5:
			break
		class_name = label_map[str(label)]
		predictions.append(class_name)	    

	return predictions
Exemple #11
0
def load_image(image_path):
    # Load image
    image = read_image_bgr(image_path)

    # Copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # Preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)

    return image, draw, scale
def test_gen(image_ids, bs = 2, size=672,test = True):
    imgs = []
    scale = None
    idx = 0
    if test:
        path = 'C:\\Users\\Pawan\\Downloads\\dataset_test_rgb\\rgb\\test\\'
    else:
        path = 'C:\\Users\\Pawan\\Downloads\\dataset_test_rgb\\rgb\\test\\'
    
    while idx < len(image_ids):
        if len(imgs) < bs:
            imgs.append(resize_image(preprocess_image(read_image_bgr(path + image_ids[idx] + '.png')),min_side=size,max_side=size)[0])            
            if scale is None:
                scale = resize_image(preprocess_image(read_image_bgr(path + image_ids[idx] + '.png')),min_side=size,max_side=size)[1]
            idx += 1
        else:
            yield np.array(imgs),scale
            imgs = []
            
            
    if len(imgs) > 0:
        yield np.array(imgs),scale
def face(img_path):
    coordinate = []
    label_name_vector = []

    # load image
    image = read_image_bgr(img_path)
    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    
    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)
    
    # process image
    start = time.time()
    _, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)
    
    # compute predicted labels and scores
    predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
    scores = detections[0, np.arange(detections.shape[1]), 4 + predicted_labels]
    
    # correct for image scale
    detections[0, :, :4] /= scale
    
    # visualize detections
    for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
        if score < 0.5:
            continue
        b = detections[0, idx, :4].astype(int)
        cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
        caption = "{:.3f}".format(score)
        cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
        cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)
     
        coordinate.append(b[0])
        coordinate.append(b[1])
        coordinate.append(b[2])
        coordinate.append(b[3])
        label_name_vector.append(labels_to_names[label])

    write_output(label_name_vector, coordinate)
    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    img_output_path = os.path.join('output', 'retina_net_face.jpg')
    plt.imsave(img_output_path, draw)
    plt.show()
    
    return(img_output_path)
def predict(image_list):
    ret_detection = []
    draws = []
    x = 0

    for img in image_list:
        # load image
        if (x % 50 == 0):
            print("Image No.: %d out of %d images(RetinaNet)" %
                  (x, len(image_list)))
        image = read_image_bgr(img)

        # copy to draw on
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        # preprocess image for network
        image = preprocess_image(image)
        image, scale = resize_image(image)

        # process image
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            np.expand_dims(image, axis=0))

        # print("processing time: ", time.time() - start)

        labels, scores, boxes = slice_frcnn_list(labels, scores, boxes, 0.35)

        # correct for image scale

        boxes[0] /= scale

        new = []
        for item in boxes:
            i = item.astype(int)
            new.append(i)
        labels = np.asarray(labels)
        scores = np.asarray(scores)
        boxes = np.asarray(boxes)

        # plot_pred(boxes, scores, labels, draw)

        ret_detection.append(
            np.atleast_2d(
                np.squeeze(format_retina_output(labels, scores, boxes))))
        draws.append(draw)
        x += 1
    ret_detection = convert_list_array_to_list_list(ret_detection)

    return ret_detection, draws
Exemple #15
0
def find_objects_single(model, image, min_side=800, max_side=1333):
    """Short method to detect objects. Only supports batch size = 1."""
    if isinstance(image, str):
        image = read_image_bgr(image)
    else:
        image = image.copy()
    image = preprocess_image(image)
    image, scale = resize_image(image, min_side=min_side, max_side=max_side)

    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    boxes[0, :, :] /= scale  # Taking in account the resizing factor

    return boxes, scores, labels
Exemple #16
0
def predict(image):
    image = preprocess_image(image)
    image, scale = resize_image(image)

    # process image
    start = time.time()
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    # print("processing time: ", str(1000 * (time.time() - start)) + " ms")

    # correct for image scale
    boxes /= scale

    return boxes, scores, labels
def Draw_out(path):
    # load image
    image = read_image_bgr(path)
    # copy to draw ondraw = image.copy()
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)
    #print ('scale',scale)

    # process image
    start = time.time()
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    #boxes是检测到的可能目标的框框
    #print ('boxes',boxes)
    #是对应的分值
    #print ('scores',scores)
    #对应的第几个标签
    #print ('labels',labels)
    #print("processing time: ", time.time() - start)

    # correct for image scale
    boxes /= scale

    # visualize detections
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        # scores are sorted so we can break
        if score < 0.4:
            break

        color = label_color(label)
        #print ('label',label)
        b = box.astype(int)
        #b是box取整
        #print ('b ',b)
        draw_box(draw, b, color=color)

        caption = "{} {:.3f}".format(labels_to_names[label], score)
        #single 0.431 是标签和分值
        #print ('caption ',caption)
        draw_caption(draw, b, caption)
    plt.figure(figsize=(10, 10))
    plt.axis('on')
    plt.imshow(draw)
    #plt.show()
    plt.savefig("static/js/powerbank_out.png")
    return caption
def test_gen(image_ids, test_path , bs = 1, min_size = 1000, max_size = 1400, test = True):

    imgs = []
    scale = None
    idx = 0
    if test:
        path  = test_path
    else:
        path = 'training_data/images/'
    
    while idx < len(image_ids):
        if len(imgs) < bs:
            image_id_iter = image_ids[idx]
            imgs.append(resize_image(preprocess_image(load_image_file(path + "/" + image_ids[idx] + '.jpg')),min_side=min_size,max_side=max_size)[0])       
            scale = resize_image(preprocess_image(load_image_file(path + "/" +  image_ids[idx] + '.jpg')),min_side=min_size,max_side=max_size)[1]
            idx += 1

        else:
            yield image_id_iter, np.array(imgs), scale
            imgs = []

    if len(imgs) > 0:
        yield image_id_iter, np.array(imgs), scale      
Exemple #19
0
def run_detection_image(model, image):
    with graph.as_default():
        # preprocess image for network
        image = preprocess_image(image)
        image, scale = resize_image(image)

        # process image
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            np.expand_dims(image, axis=0))

        # correct for image scale
        boxes /= scale
        return boxes, scores, labels
def get_boxes(img, model):
    img = img.copy()
    preprocess_image(img)
    img, scale = resize_image(img, min_side=1024, max_side=1024)

    # process image
    start = time.time()
    boxes, scores, labels = model.predict_on_batch(np.expand_dims(img, axis=0))
    print("processing time: ", time.time() - start)

    # correct for image scale
    boxes /= scale

    return boxes, scores, labels
def get_retinanet_predictions(model, image):
    from keras_retinanet.utils.image import preprocess_image, resize_image

    show_debug_images = False
    show_mirror_predictions = False

    if show_debug_images:
        # copy to draw on
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image, min_side=600, max_side=800)

    # Add mirror
    if 1:
        image = np.stack((image, image[:, ::-1, :]), axis=0)
    else:
        image = np.array([image])

    # process image
    start = time.time()
    print('Image shape: {} Scale: {}'.format(image.shape, scale))
    boxes, scores, labels = model.predict_on_batch(image)
    print('Detections shape: {} {} {}'.format(boxes.shape, scores.shape,
                                              labels.shape))
    print("Processing time: {:.2f} sec".format(time.time() - start))

    if show_debug_images:
        if show_mirror_predictions:
            draw = draw[:, ::-1, :]
        boxes_init = boxes.copy()
        boxes_init /= scale

    boxes[:, :, 0] /= image.shape[2]
    boxes[:, :, 2] /= image.shape[2]
    boxes[:, :, 1] /= image.shape[1]
    boxes[:, :, 3] /= image.shape[1]

    if show_debug_images:
        if show_mirror_predictions:
            show_image_debug(draw.astype(np.uint8), boxes_init[1:], scores[1:],
                             labels[1:])
        else:
            show_image_debug(draw.astype(np.uint8), boxes_init[:1], scores[:1],
                             labels[:1])

    return boxes, scores, labels
def predict_save(model, test_img_fold, test_img_list):
    # load image
    img_name_list = []
    bboxes_list = []
    class_list = []
    score_list = []
    for i in range(len(test_img_list)):
        # for i in range(1):
        img_name = test_img_list[i]
        img_path = os.path.join(test_img_fold, img_name)
        image = read_image_bgr(img_path)
        # copy to draw on
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
        # preprocess image for network
        image = preprocess_image(image)
        image, scale = resize_image(image)
        # process image
        start = time.time()
        # print(image.shape)
        # print(scale)
        boxes, scores, labels = model.predict_on_batch(
            np.expand_dims(image, axis=0))
        print("processing time: ", time.time() - start)
        # correct for image scale
        boxes /= scale
        i = 0
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            # scores are sorted so we can break
            if score < 0.5:
                break
            color = label_color(label)
            b = box.astype(int)
            img_name_list.append(img_name)
            bboxes_list.append(b)
            class_list.append(labels[0][i])
            score_list.append(score)
            i += 1
            draw_box(draw, b, color=color)
            caption = "{} {:.3f}".format(labels_to_names[label], score)
            draw_caption(draw, b, caption)
        imsave('result/' + img_name, draw)
    submit = pd.DataFrame()
    submit['img_name'] = img_name_list
    submit['bbox'] = bboxes_list
    submit['class'] = class_list
    submit['score'] = score_list
    # submit.to_csv('submit.csv', index=None)
    submit.to_pickle('submit.pkl')
Exemple #23
0
	def callback(self, imageMsg):
		image = self.bridge.imgmsg_to_cv2(imageMsg, "bgr8")
		image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
		image = image[:, :, ::-1].copy()

		# copy to draw on
		draw = image.copy()
		draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

		# Image formatting specific to Retinanet
		image = preprocess_image(image)
		image, scale = resize_image(image)

		# Run the inferencer
		try:
			with self.session.as_default():
				with self.session.graph.as_default():
					boxes, scores, labels = self.model.predict_on_batch(np.expand_dims(image, axis=0))
		except Exception as e:
			rospy.logerr(e)
			rospy.logwarn("WARNING: Has your model been converted to an inference model yet? "
				"see https://github.com/fizyr/keras-retinanet#converting-a-training-model-to-inference-model")
			return

		# correct for image scale
		boxes /= scale

		# visualize detections
		for box, score, label in zip(boxes[0], scores[0], labels[0]):
			# scores are sorted so we can break
			if score < self.confidence_cutoff:
				break

			# Add boxes and captions
			b = np.array(box).astype(int)
			cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), self.color, self.thickness, cv2.LINE_AA)

			if (label > len(self.labels_to_names)):
				print("WARNING: Got unknown label, using 'detection' instead")
				caption = "Detection {:.3f}".format(score)
			else:
				caption = "{} {:.3f}".format(self.labels_to_names[label], score)

			cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 2)
			cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)

		# Write out image
		image_message_out = self.bridge.cv2_to_imgmsg(draw, encoding="rgb8")
		self.img_pub.publish(image_message_out)
Exemple #24
0
def detect_single(img_path, min_prob=0.4):
    image = read_image_bgr(img_path)
    image = preprocess_image(image)
    image, scale = resize_image(image)
    boxes, scores, labels = detection_model.predict_on_batch(np.expand_dims(image, axis=0))
    boxes /= scale
    processed_boxes = []
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        if score < min_prob:
            continue
        box = box.astype(int).tolist()
        label = classes[label]
        processed_boxes.append({'box': box, 'score': float(score), 'label': label})
        
    return processed_boxes
Exemple #25
0
 def predict(self,img_path):
     image = read_image_bgr(img_path)
     image = preprocess_image(image)
     image, scale = resize_image(image)
     print(scale)
     boxes, scores, labels = self.model.predict_on_batch(np.expand_dims(image, axis=0))
     result = []
     result_score = []
     for box, score, label in zip(boxes[0], scores[0], labels[0]):
         if score < self.PREDICTION_CONFIDENCE:
             break
         else:
             result.append(label)
             result_score.append(score)
     return self.process_prediction(result)
Exemple #26
0
def evaluate(file_path, model, return_image=False, debug=False):
    """
    Keras-RetinaNet evaluation adapted from github.com/fizyr/keras-retinanet
    :param model: loaded pretrained model. We ideally load this only once
    :param file_path: path to temp file
    :param return_image: Whether to return annotated image
    :param debug: Whether to return classification confidence scores
    :return:
    """
    image = read_image_bgr(file_path)

    if return_image:
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    image = preprocess_image(image)
    image, scale = resize_image(image)

    _, _, boxes, nms_class = model.predict_on_batch(
        np.expand_dims(image, axis=0))

    predicted_labels = np.argmax(nms_class[0, :, :], axis=1)

    scores = nms_class[0, np.arange(nms_class.shape[1]), predicted_labels]

    boxes /= scale

    classes_found = []
    for i in np.where(scores > THRESHOLD)[0]:
        label = labels_to_classes[predicted_labels[i]]
        score = scores[i]

        classes_found.append(label)

        # TODO: write own version with better presentation
        if return_image:
            colour = label_colour(predicted_labels[i])
            b = boxes[0, i, :].astype(int)

            if debug:
                caption = "{} {:.3f}".format(label, score)
            else:
                caption = "{}".format(label)

            draw_box(draw, b, color=colour)
            draw_caption(draw, b, caption)

    return {'classes': classes_found, 'image': draw if return_image else None}
Exemple #27
0
def object_detector(file, detector_model, score_threshold):
    """runs object detector on images to detect trees

    Args:
        file : path to image
        detector_model : tree detector model
        score_threshold : score threshold for non-max suppression
    Returns:
        detector_output : tuple contanining detector output

    """

    image = read_image_bgr(file)
    image = preprocess_image(image)
    image, scale = resize_image(image)

    unscaled_image = np.asarray(Image.open(file))

    # convert grayscale image to a 3-channel image
    if np.ndim(unscaled_image) == 2 or unscaled_image.shape[2] == 1:
        unscaled_image = np.repeat(unscaled_image[:, :, np.newaxis], 3, axis=2)

    # drop alpha channel
    unscaled_image = unscaled_image[:, :, :3]

    # bgr to rgb
    unscaled_image[:, :, ::-1].copy()

    # run detector on image
    start = time.time()
    boxes, scores, labels = detector_model.predict_on_batch(
        np.expand_dims(image, axis=0))
    print("detection time: ", time.time() - start)

    # extract tree patches from image
    boxes /= scale
    tree_patches = extract_patches(unscaled_image, boxes[0], scores[0],
                                   score_threshold)
    valid_boxes = [
        box for i, box in enumerate(boxes[0]) if scores[0][i] > score_threshold
    ]
    valid_scores = [
        score for i, score in enumerate(scores[0])
        if scores[0][i] > score_threshold
    ]
    detector_output = (tree_patches, unscaled_image, valid_boxes, valid_scores,
                       score_threshold)
    return detector_output
Exemple #28
0
    def auto_label_coco(self):
        # use this to change which GPU to use
        gpu = 0
        # set the modified tf session as backend in keras
        setup_gpu(gpu)
        model_path = self.retina_weight
        model = models.load_model(model_path, backbone_name='resnet50')

        pic_list = os.listdir(self.pic_dir)
        no_label_img_list = []
        for pic_name in pic_list:
            pic_start = time.time()
            mpos = []
            mclass = []
            mscore = []

            pic_fullpath = Path(self.pic_dir).joinpath(pic_name)
            print("[INFO]picfullpath:", pic_fullpath, type(pic_fullpath))

            image = read_image_bgr(pic_fullpath)
            image = preprocess_image(image)
            image, scale = resize_image(image)
            boxes, scores, labels = model.predict_on_batch(
                np.expand_dims(image, axis=0))
            boxes /= scale
            class_dict = {}
            if self.coco_classes != "all":
                class_dict = self.coco_classes
            for box, score, label in zip(boxes[0], scores[0], labels[0]):
                # scores are sorted so we can break
                # if score < 0.5:
                if score < float(self.retina_threshold):
                    break
                involed_class = labels_to_names[label]
                if self.coco_classes != "all" and involed_class not in class_dict:
                    continue
                mpos.append(box)
                mclass.append(involed_class)
                mscore.append(score)
            no_label_img = annotation_single_img(self.pic_dir, pic_name,
                                                 self.xml_dir, mclass, mpos)
            pic_end = time.time()
            print("[INFO]single pic time:", str(pic_end - pic_start))
            if no_label_img != None:
                no_label_img_list.append(no_label_img)
        if no_label_img_list != []:
            print("[WARNING] There are some picture which have no label, Please remove them:", \
                  str(no_label_img_list))
Exemple #29
0
def get_labels_from_model(images, read_image_path, save_image_path, model):
    Labels = []

    # load label to names mapping for visualization purposes
    labels_to_names = {0: 'round_single', 1: 'round_double', 2: 'unclear_single', 3: 'unclear_double', 4: 'hexagonal_single', 5: 'square_single', 6: 'trigonal_single', 7: 'void_single', 8: 'bubbles_single'}

    for image in images:
        # change image.split("/") to "\\" if used on windows
        path_separated = image.split("/")
        image_name = path_separated[len(path_separated) - 1]
        print(image_name)
        image = read_image_bgr(read_image_path + image_name)
    
    # copy to draw on
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)    

    # preprocess image for network
        image = preprocess_image(image)
        print('image preprocessed')
        image, scale = resize_image(image)

    # process image
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
        print('processing time: ', time.time() - start)

    # correct for image scale
        boxes /= scale

    # visualize boxes and store annotations to array Labels
        for idx, (box, score, label) in enumerate(zip(boxes[0], scores[0], labels[0])):
            if score < 0.5:
                continue
            b = boxes[0, idx, :4].astype(int)

            Labels.append([image_name, b, labels_to_names[label], score])

            color = label_color(label)
            draw_box(draw, b, color=color)
            
            caption = "{} {:.3f}".format(labels_to_names[label], score)
            draw_caption(draw, b, caption)

        save_image_name = image_name.split(".")[0] + '_pred'
        cv2.imwrite(os.path.join(save_image_path, '{}.jpg'.format(save_image_name)), draw)
        
    return Labels
Exemple #30
0
def run_inference(path):
    image = read_image_bgr(path)
    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = preprocess_image(image)

    image, scale = resize_image(image, min_side=val_gen.image_min_side, max_side=val_gen.image_max_side)
    # process image

    start = time.time()
    boxes, scores, labels = prediction_model.predict_on_batch(np.expand_dims(image, axis=0))
    boxes /=scale
    return boxes[0], scores[0], labels[0]
Exemple #31
0
def decode_image_retinanet(img_path,max_height=None):
  ## Going to read the image via retinanet helper- the original way
  start = timer()
  image = read_image_bgr(img_path)
  # preprocess image for network
  image = preprocess_image(image)
  image, scale = resize_image(image)
  end = timer()
  print("decode time=",end - start)
  #('decode time=', 0.028119802474975586)
  # these are the best scores
  #('Label', 'person', ' at ', array([409, 167, 728, 603]), ' Score ', 0.9681119)  
  #('Label', 'person', ' at ', array([  0, 426, 512, 785]), ' Score ', 0.8355836)
  #('Label', 'person', ' at ', array([ 723,  475, 1067,  791]), ' Score ', 0.72344124)
  #('Label', 'tie', ' at ', array([527, 335, 569, 505]), ' Score ', 0.525432)
  return image,image