Beispiel #1
0
async def detect_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)
    # 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 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)
    marked_image = cv2.cvtColor(draw, cv2.COLOR_RGB2BGR)
    marked_image_path = os.path.join(
        os.path.split(image_path)[0], 'marked-' + os.path.split(image_path)[1])
    marked_image_name = os.path.split(marked_image_path)[1]
    cv2.imwrite(marked_image_path, marked_image)
    return marked_image_name
Beispiel #2
0
def label(model, image_path, save_path, csv_path, label_name=True):
    # copy to draw on
    image = read_image_bgr(image_path)
    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))

    # correct for image scale
    boxes /= scale

    # visualize detections
    with open(csv_path, 'w') as csv_file:
        csv_file.write('x1,y1,x2,y2')
        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)
            csv_file.write('{},{},{},{}'.format(*b))
            draw_box(draw, b, color=color)

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

    cv.imwrite(save_path, cv.cvtColor(draw, cv.COLOR_RGB2BGR))
Beispiel #3
0
def visualize_detections(draw, boxes, scores, labels):
    # load label to names mapping for visualization purposes
    labels_to_names = {
        0: 'arrabida',
        1: 'camara',
        2: 'clerigos',
        3: 'musica',
        4: 'serralves'
    }

    # visualize detections
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        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()
Beispiel #4
0
def run(generator, args):
    # display images, one at a time
    for i in range(generator.size()):
        # load the data
        image = generator.load_image(i)
        annotations, masks = generator.load_annotations(i)

        # apply random transformations
        if args.random_transform:
            image, annotations, masks = generator.random_transform_group_entry(
                image, annotations, masks)

        # resize the image and annotations
        if args.resize:
            image, image_scale = generator.resize_image(image)
            annotations[:, :4] *= image_scale
            for m in range(len(masks)):
                masks[m], _ = generator.resize_image(masks[m])

        # draw anchors on the image
        if args.anchors:
            anchors = generator.generate_anchors(image.shape)
            labels, _, _ = generator.compute_anchor_targets(
                anchors, [image], [annotations], generator.num_classes())
            draw_boxes(image,
                       anchors[np.max(labels[0], axis=1) == 1], (255, 255, 0),
                       thickness=1)

        # draw annotations on the image
        if args.annotations:
            # draw annotations in red
            draw_annotations(image,
                             annotations,
                             color=(0, 0, 255),
                             label_to_name=generator.label_to_name)

            # draw regressed anchors in green to override most red annotations
            # result is that annotations without anchors are red, with anchors are green
            anchors = generator.generate_anchors(image.shape)
            labels, _, boxes = generator.compute_anchor_targets(
                anchors, [image], [annotations], generator.num_classes())
            draw_boxes(image, boxes[0, np.max(labels[0], axis=1) == 1],
                       (0, 255, 0))

        # Draw masks over the image with random colours
        if args.masks:
            for m in range(len(masks)):
                # crop the mask with the related bbox size, and then draw them
                box = annotations[m, :4].astype(int)
                mask = masks[m][box[1]:box[3], box[0]:box[2]]
                draw_mask(image, box, mask, annotations[m, 4].astype(int))
                # add the label caption
                caption = '{}'.format(
                    generator.label_to_name(annotations[m, 4]))
                draw_caption(image, box, caption)

        cv2.imshow('Image', image)
        if cv2.waitKey() == ord('q'):
            return False
    return True
Beispiel #5
0
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
Beispiel #6
0
def show_image_with_boxes(df):
  # pick a random image
  filepath = df.sample()['image_name'].values[0]

  # get all rows for this image
  df2 = df[df['image_name'] == filepath]
  im = np.array(Image.open(filepath))

  # if there's a PNG it will have alpha channel
  im = im[:,:,:3]

  for idx, row in df2.iterrows():
    d_class = row["class_name"]
    box = [
      row['x_min'],
      row['y_min'],
      row['x_max'],
      row['y_max'],
    ]
    print(box)
    
    draw_box(im, box, color=(255, 0, 0))
    caption = "{}".format(d_class)
    draw_caption(im, box, caption)

  plt.axis('off')
  plt.imshow(im)
  plt.show()
Beispiel #7
0
def pred_video(video_path):
    cap = cv2.VideoCapture(video_path)
    width = cap.get(3)
    height = cap.get(4)
    fps = cap.get(5)
    print((width, height))

    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
    writer = cv2.VideoWriter()
    writer.open('./images/output.mp4', fourcc, int(fps),
                (int(width), int(height)))

    while (cap.isOpened()):
        # Capture frame-by-frame
        ret, frame = cap.read()
        if ret == True:

            # 图像copy
            draw = frame.copy()
            # draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

            # 输入网络准备
            image = preprocess_image(frame)
            image, scale = resize_image(image)

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

            # 映射到原图
            boxes /= scale

            # 可视化检测结果
            for box, score, label in zip(boxes[0], scores[0], labels[0]):
                # 是排序额score
                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, color=color)

            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(draw,
                        "Model:RetinaNet GPU:NVIDIA Tesla V100 32GB by XJ",
                        (40, 50), font, 0.6, (0, 0, 255), 2)
            cv2.imshow('result.jpg', draw)
            writer.write(draw)

        # Press Q on keyboard to  exit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    writer.release()
    cv2.destroyAllWindows()
Beispiel #8
0
def read_images(images_dir,model,lbls,output):
	images = os.listdir(images_dir)
	for im in images:
		base = im
		im = os.path.join(images_dir,im)
		image, draw, scale =operate_image(im)

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

		# correct for image scale
		boxes /= scale
		print("processing time: ", time.time() - start)
		# 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
			label = label -1       
			color = label_color(label)
    
			b = box.astype(int)
			draw_box(draw, b, color=color)
    
			caption = "{} {:.3f}".format(lbls[label], score)
			draw_caption(draw, b, caption)
			out = os.path.join(output,base)
			write_image(out,draw)
Beispiel #9
0
def main(args=None):
    args = parse_args(args)
    load_model(args)
    cap = cv2.VideoCapture(args.capture)
    while (cap.isOpened()):
        ret, frame = cap.read()

        draw = frame.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        boxes, scores, labels = run_detection_image(model, frame)

        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)

        # Display the resulting frame
        resized_image = cv2.resize(frame, (1280, 820))
        cv2.imshow('Video', resized_image)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Beispiel #10
0
def run():
    args = parse_args()
    print(args)
    if not test_args(args):
        return
    keras.backend.tensorflow_backend.set_session(get_session())
    label_map = get_labels(args.label)
    model = models.load_model(args.model, backbone_name=args.backbone)

    for img_file in args.image:
        img = read_image_bgr(img_file)
        canvas = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2RGB)
        img, scale = resize_image(preprocess_image(img))
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            np.expand_dims(img, axis=0))
        print("Processing time: ", time.time() - start)

        boxes /= scale
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            if score < args.threshold:  # Labels are sorted
                break
            color = label_color(label)
            b = box.astype(int)
            draw_box(canvas, b, color=color)
            caption = "{} {:.3f}".format(label_map[label], score)
            draw_caption(canvas, b, caption)
        canvas = cv2.cvtColor(canvas, cv2.COLOR_RGB2BGR)
        out_file = args.out_prefix + os.path.split(img_file)[1]
        out_full = os.path.join(args.out_dir, out_file)
        cv2.imwrite(out_full, canvas)
        print("Done with writing to file {:s}.".format(out_full))
Beispiel #11
0
def detect(image):
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    image = preprocess_image(image)
    image, scale = resize_image(image)
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    boxes /= scale
    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 < 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)

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
Beispiel #12
0
def predict(model, class_names, image_file, predict_file):
    image = read_image_bgr(image_file)
    draw = image.copy()

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

    start = time.time()
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    print("File {} Processing Time {:.3f}".format(image_file,
                                                  time.time() - start))

    boxes /= scale

    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        if score < 0.5:
            break

        color = label_color(label)

        b = box.astype(int)
        draw_box(draw, b, color=color)
        caption = "{} {:.3f}".format(class_names[label], score)
        draw_caption(draw, b, caption)

    cv2.imwrite(predict_file, draw)
Beispiel #13
0
def TestSinglePic(model, image, savepath, imgname):
    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    image = preprocess_image(image)
    image, scale = resize_image(image)
    boxes, scores, labels = model.predict(np.expand_dims(image, axis=0))
    boxes /= scale

    # visualize detections
    flag = False
    boxes, scores, labels = boxes_filter(boxes[0], scores[0], labels[0])
    # boxes, scores, labels = boxes[0], scores[0], labels[0]
    for box, score, label in zip(boxes, scores, labels):
        # scores are sorted so we can break
        if flag and score < 0.3:
            continue
        flag = True
        label = label.astype(int)
        color = label_color(label)

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

        caption = "{} {:.3f}".format(label + 1, score)
        draw_caption(draw, b, caption)

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.savefig(os.path.join(savepath, imgname), bbox_inches='tight')
    plt.clf()
def show_image_debug(draw, boxes, scores, labels, masks, classes):
    from keras_retinanet.utils.visualization import draw_box, draw_caption
    from keras_maskrcnn.utils.visualization import draw_mask
    from keras_retinanet.utils.colors import label_color

    # visualize detections
    limit_conf = 0.2
    for box, score, label, mask in zip(boxes, scores, labels, masks):
        # scores are sorted so we can break
        if score < limit_conf:
            break

        color = label_color(label)
        color_mask = (255, 0, 0)

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

        mask = mask[:, :]
        draw_mask(draw, b, mask, color=color_mask)

        caption = "{} {:.3f}".format(classes[label], score)
        print(caption)
        draw_caption(draw, b, caption)
    draw = cv2.cvtColor(draw, cv2.COLOR_RGB2BGR)
    show_image(draw)
Beispiel #15
0
def detect(graph, model, image_paths):
    label_to_name = {0: 'open', 1: 'closed'}
    saved = []
    for path in image_paths:
        img = read_image_bgr(path)
        draw = img.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
        img = preprocess_image(img)
        img, scale = resize_image(img)
        with graph.as_default():
            bs, ss, ls = model.predict_on_batch(np.expand_dims(img, axis=0))
            bs /= scale

            for b, s, l in zip(bs[0], ss[0], ls[0]):
                if s < 0.5:
                    break
                color = label_color(l + 5)
                b = b.astype(int)
                # pred = gen_prediction(l, _b, s)
                # result.append(pred)
                caption = '{} {:.3f}'.format(label_to_name[l], s)
                draw_box(draw, b, color)
                draw_caption(draw, b, caption)
            dest = get_dest_with_fname(path)
            # thr = Thread(target=save_det, args=(draw, dest))
            # thr.start()
            cv2.imwrite(dest, cv2.cvtColor(draw, cv2.COLOR_BGR2RGB))
            saved.append(os.path.basename(dest))
    return saved
Beispiel #16
0
    def process(self, image):
        draw = image.copy()
        h, w, _ = draw.shape
        # draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
        image = preprocess_image(image)
        image, scale = resize_image(image)

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

        boxes /= scale
        # visualize detections
        centers = []
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            # scores are sorted so we can break
            if score < 0.5 or self.labels_to_names[label] not in ['person']:
                break
            x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])

            w, h = (x2 - x1), (y2 - y1)
            centers.append([x1, y1, w, h])
            color = label_color(label)
            b = box.astype(int)
            draw_box(draw, b, color=color)

            caption = "{} {:.3f}".format(self.labels_to_names[label], score)
            draw_caption(draw, b, caption)
        return draw, centers
def plot_pred(boxes, scores, labels, draw):
    labels_to_names = {
        0: 'car',
        1: 'articulated_truck',
        2: 'bus',
        3: 'bicycle',
        4: 'motorcycle',
        5: 'motorized_vehicle',
        6: 'pedestrian',
        7: 'single_unit_truck',
        8: 'work_van',
        9: 'pickup_truck',
        10: 'non-motorized_vehicle'
    }

    # 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.suptitle("Retina")
    plt.imshow(draw)
Beispiel #18
0
def make_prediction(image):
    
    #set id for labeling
    ID = {0:'gate', 1:'flare'}
    print('init fn')
    draw = image.copy()

    #preprocess image for model
    image = preprocess_image(image)
    (image, scale) = resize_image(image)
    image = np.expand_dims(image, axis=0)

    #make prediction
    (boxes, scores, labels) = model.predict_on_batch(image)

    #filtering and drawing boxes
    box_out = []
    label_out = []
    for (box, score, label) in zip(boxes[0], scores[0], labels[0]):
        
        #threshold score can be optimize for optimum range of results
        if score < 0.3:
            continue

        box /= scale
        box = box.astype("int")
        
        box_out.append(box)
        label_out.append(ID[label])
        
        draw_box(draw, box, color=(0,0,255))
        caption = "{} {:.3f}".format(ID[label], score) #output label
        draw_caption(draw, box, caption)
    
    return label_out, box_out, draw 
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
Beispiel #20
0
def plot(file_path):
    image = read_image_bgr(file_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, 960, 1280)

    label, score, box = res(file_path)
    label, score, box = after_processing(label, score, box)
    for box, score, label in zip(box, score, label):
        color = label_color(label)

        b = box
        draw_box(draw, b, color=color)

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

    plt.figure(figsize=(20, 20))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
Beispiel #21
0
def predict(image):
    # 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)

    # 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

        print("Item  ", labels_to_names[label], score)
        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)

    cv2.imshow('Prediction', draw)
Beispiel #22
0
def draw_detections(image, boxes, scores, labels):

    for index, value in enumerate(scores):
        color = label_color(labels[index])
        b = boxes[index].astype(int)
        draw_box(image, b, color=color)
        caption = "{} {:.3f}".format('License Plate', value)
        draw_caption(image, b, caption)
Beispiel #23
0
    def annotate_image(self,
                       path_to_image,
                       num_gpus=1,
                       min_image_dimension=800,
                       max_image_dimension=1024,
                       steps_per_epoch=100,
                       validation_steps=70):

        labels_to_names = self._load_labels()

        model = models.load_model(self._path_to_model,
                                  backbone_name='resnet50')

        # load image
        image = read_image_bgr(path_to_image)

        # 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()
        outputs = model.predict_on_batch(np.expand_dims(image, axis=0))
        print("processing time: ", time.time() - start)

        boxes = outputs[-4][0]
        scores = outputs[-3][0]
        labels = outputs[-2][0]
        masks = outputs[-1][0]

        # correct for image scale
        boxes /= scale

        # visualize detections
        for box, score, label, mask in zip(boxes, scores, labels, masks):
            if score < 0.5:
                break

            color = label_color(label)

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

            mask = mask[:, :, label]
            draw_mask(draw, b, mask, color=label_color(label))

            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()
        """config = ImageMonkeyConfig(len(labels), num_gpus, min_image_dimension, max_image_dimension, steps_per_epoch, validation_steps) 
def draw_detections(image, boxes, scores, labels):
  for box, score, label in zip(boxes[0], scores[0], labels[0]):
    if score < THRES_SCORE:
        break
    color = label_color(label)
    b = box.astype(int)
    draw_box(image, b, color=color)
    caption = "{} {:.3f}".format(labels_to_names[label], score)
    draw_caption(image, b, caption)
Beispiel #25
0
def show_image_with_predictions(df, threshold=0.6):
  # choose a random image
  row = df.sample()
  filepath = row['image_name'].values[0]
  print("filepath:", filepath)

  # get all rows for this image
  df2 = df[df['image_name'] == filepath]
  im = np.array(Image.open(filepath))
  print("im.shape:", im.shape)

  # if there's a PNG it will have alpha channel
  im = im[:,:,:3]

  # plot true boxes
  for idx, row in df2.iterrows():
    box = [
      row['x_min'],
      row['y_min'],
      row['x_max'],
      row['y_max'],
    ]
    print(box)
    draw_box(im, box, color=(255, 0, 0))
  
  ### plot predictions ###

  # get predictions
  imp = preprocess_image(im)
  imp, scale = resize_image(im)

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

  # standardize box coordinates
  boxes /= scale

  # loop through each prediction for the input image
  for box, score, label in zip(boxes[0], scores[0], labels[0]):
    # scores are sorted so we can quit as soon
    # as we see a score below threshold
    if score < threshold:
      break

    box = box.astype(np.int32)
    color = label_color(label)
    draw_box(im, box, color=color)

    class_name = label_map[label]
    caption = f"{class_name} {score:.3f}"
    draw_caption(im, box, caption)

  plt.axis('off')
  plt.imshow(im)
  plt.show()
Beispiel #26
0
def predict_one_image(model, img_path):
    """predict the image with the model

    Args:
        model: A keras.models.Model object.
        img_path: the path of image

    Returns:
        draw: numpy.ndarray.including the box info
        boxes: List, the value is a list,length is 6. 6 represent x, y, w, h, label, score
    """
    starttime = time()
    image = read_image_bgr(img_path)

    labels_to_names, cls_index = get_labels(
        metadata_dir="/home/sk49/workspace/dataset/open_images_dataset_v4/challenge2018", version="challenge2018")
    print(labels_to_names)
    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    cv2.imwrite("a.jpg", draw)
    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)

    # process image
    start = time()

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

    # correct for image scale
    boxes /= scale
    # visualize detections
    boxes = []
    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)
        print(type(b), b)
        cv2.imwrite("jj.jpg", draw)
        draw_box(draw, b, color=color)
        print("2", type(draw))
        caption = "{} {:.3f}".format(labels_to_names[label], score)
        draw_caption(draw, b, caption)
        print("3", type(draw))
        draw = cv2.cvtColor(draw, cv2.COLOR_RGB2BGR)
        cv2.imwrite("jj1.jpg", draw)
        print("total", (time() - starttime), "s")

    return draw
Beispiel #27
0
    def retinamask(self):
        backbone = models.backbone('resnet50')
        batch_size = 1
        train_generator, validation_generator = Retinamask.create_generators(
            batch_size, self.annotations, self.classes)
        freeze_backbone = 'store_true'
        weights = self.Input_weights_path
        print('Creating model, this may take a second...')
        model, training_model, prediction_model = Retinamask.create_models(
            backbone_retinanet=backbone.maskrcnn,
            num_classes=train_generator.num_classes(),
            weights=weights,
            freeze_backbone=freeze_backbone)
        #print(model.summary())
        training_model.fit_generator(generator=train_generator,
                                     steps_per_epoch=1000,
                                     epochs=self.epoch,
                                     verbose=1,
                                     max_queue_size=1)
        training_model.save(self.trained_weights_path + 'retinamask.h5')

        #Testing
        model_path = self.trained_weights_path + 'retinamask.h5'
        model = models.load_model(model_path, backbone_name='resnet50')
        labels_to_names = {0: 'ship'}
        # load image
        image = read_image_bgr(test_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)
        # process image
        start = time.time()
        outputs = model.predict_on_batch(np.expand_dims(image, axis=0))
        print("processing time: ", time.time() - start)
        boxes = outputs[-4][0]
        scores = outputs[-3][0]
        labels = outputs[-2][0]
        masks = outputs[-1][0]
        # correct for image scale
        boxes /= scale
        # visualize detections
        for box, score, label, mask in zip(boxes, scores, labels, masks):
            if score < 0.5:
                break
            color = label_color(label)
            b = box.astype(int)
            draw_box(draw, b, color=color)
            mask = mask[:, :, label]
            draw_mask(draw, b, mask, color=label_color(label))
            caption = "{} {:.3f}".format(labels_to_names[label], score)
            draw_caption(draw, b, caption)
            plt.imsave(self.output_image_path + 'output.jpg', draw)
Beispiel #28
0
def img_inference(img_path, pos_window, model, labels_to_names):
    window_size = (240, 250)
    over_lap_x = 100
    over_lap_y = 80
    list_bb = []
    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)
    # print(scale)

    # process image
    start = time.time()
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)
    boxes /= scale
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        box_restore = []
        # scores are sorted so we can break
        if score < 0.8 or labels_to_names[label] != "solar":
            break

        box_restore.append(box[0] + pos_window[1] *
                           (window_size[1] - over_lap_y))
        box_restore.append(box[1] + pos_window[0] *
                           (window_size[0] - over_lap_x))
        box_restore.append(box[2] + pos_window[1] *
                           (window_size[1] - over_lap_y))
        box_restore.append(box[3] + pos_window[0] *
                           (window_size[0] - over_lap_x))
        # print(box)
        # print(score)
        # print(labels_to_names[label])
        list_bb.append(box_restore)
        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=(10, 10))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
    # for box in list_bb:
    #     print(box)
    # print(list_bb[0])
    return list_bb
def run_detection_video(video_path):

    count = 0
    success = True
    start = time.time()
    while success:
        if count % 100 == 0:
            print("frame: ", count)
        count += 1
        # Read next image
        success, image = vcapture.read()

        if success:

            draw = image.copy()
            draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

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

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

            boxes /= scale
            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)
            detected_frame = cv2.cvtColor(draw, cv2.COLOR_RGB2BGR)
            vwriter.write(detected_frame)  # overwrites video slice

            print_log = "ALERT : Identified Object : " + str(ALERT_OBJECT)
            print(print_log)
            logData(print_log)
            cv2.imwrite(ALERT_IMAGE_NAME, image)
            global IS_SEND_ALERT
            print('IS_SEND_ALERT : ' + str(IS_SEND_ALERT))
            if IS_SEND_ALERT == False:
                sendAlertEmail()
                sendAlertSMS()
                IS_SEND_ALERT = True

    vcapture.release()
    vwriter.release()  #
    end = time.time()

    print("Total Time: ", end - start)
Beispiel #30
0
def predict_img(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)):
        img_name = test_img_list[i]
        if ".xml" in img_name:
            continue
        img_path = os.path.join(test_img_fold, img_name)
        # image = read_image_bgr(img_path)
        image_BGR = cv2.imread(img_path)
        # copy to draw on
        draw = image_BGR
        image = cv2.cvtColor(image_BGR, cv2.COLOR_BGR2RGB)  # 不需要
        image = np.asarray(image)[:, :, ::-1].copy()
        # preprocess image for network
        image = preprocess_image(image)
        image, scale = resize_image(image)
        # process image
        start = time.time()
        # print(image.shape)
        # print(scale)
        # boxes: 预测的box,scores:预测的概率,labels预测的labels
        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,概率小于0.5的被舍弃
            if score < 0.2:
                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)
            print(label)
            caption = "{} {:.3f}".format(labels_to_names[label], score)
            draw_caption(draw, b, caption)
        # imsave('result/'+img_name, draw)
        # print(draw)
        cv2.imwrite('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)
Beispiel #31
0
def run_detection_image(path):

    image = cv2.imread(path)
    # image base 64   retimag = np_to_base64(image)
    #    print(base64_to_pil(retimag))
    # # 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)
    #
    # # 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.3:
            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=(30, 30))
    #plt.axis('off')
    # abcd = plt.imshow(draw)
    # #plt.show()
    #
    # #file, ext = os.path.splitext(filepath)
    # #image_name = file.split('/')[-1] + ext
    # #output_path = os.path.join('examples1/results/', image_name)
    #
    # draw_conv = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    # #cv2.imwrite(output_path, draw_conv)
    # return image
    retimag = np_to_base64(draw)
    strimg = base64_to_pil(retimag)
    strimg.save("testimg.png")
    print(strimg)
    return send_file('testimg.png', mimetype="image/png")