def load_image(self, image_index=None, image_filename=None):
     """ Load an image at the image_index.
     """
     if image_index is not None:
         return read_image_bgr(self.image_path(image_index))
     elif image_filename is not None:
         return read_image_bgr(image_filename)
     else:
         raise_from(ValueError('provide an image_index or an image_filename. image_index takes precedence'), None)
예제 #2
0
def detect_image_fusion(image_path1, image_path2, label_path, labels_to_names,
                        model):
    image1 = read_image_bgr(image_path1)
    path_split1 = image_path1.split("/")
    name_image1 = path_split1[-1]
    name_split1 = name_image1.split(".")
    number1 = name_split1[0]

    image2 = read_image_bgr(image_path2)
    path_split2 = image_path2.split("/")
    name_image2 = path_split2[-1]
    name_split2 = name_image2.split(".")
    number2 = name_split2[0]

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

    # preprocess image for network
    image1 = preprocess_image(image1)
    image1, scale1 = resize_image(image1)
    image2 = preprocess_image(image2)
    image2, scale2 = resize_image(image2)

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

    # correct for image scale
    boxes /= scale1

    # 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()
예제 #3
0
def listen():
    while True:
        path = sys.stdin.readline()
        path = path.split('\n')[0]
        if path:
            if path == "stap":
                break
            #make a guess
            path = str(path)
            image = read_image_bgr(imgloc + "/" + path)
            image = preprocess_image(image)
            image, scale = resize_image(image)
            boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
            boxes /= scale
            msg = []
            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)
                msg.append({"topleft":{"x":b[0],"y":b[1]},"bottomright":{"x":b[2],"y":b[3]},"label":labels_to_names[label],"confidence":score})
            print("#" + path + "#" + str(msg) + "#")
            sys.stdout.flush()
예제 #4
0
    def predictImage(self, imagePath):
        model = models.load_model(self.modelWeights, backbone_name="resnet50")
        xmlPath = imagePath[0:imagePath.rfind(".")] + ".xml"
        # load the input image (in BGR order), clone it, and preprocess it
        image = read_image_bgr(imagePath)
        wI, hI, d = image.shape
        output = image.copy()
        image = preprocess_image(image)
        (image, scale) = resize_image(image)
        image = np.expand_dims(image, axis=0)

        # detect objects in the input image and correct for the image scale
        (boxes, scores, labels) = model.predict_on_batch(image)
        boxes /= scale
        boxes1 = []
        for (box, score, label) in zip(boxes[0], scores[0], labels[0]):
            if score < self.CONFIDENCE:
                continue
            boxes1.append(([label, box], score))

        # parse the filename from the input image path, construct the
        # path to the output image, and write the image to disk
        filename = imagePath.split(os.path.sep)[-1]
        # outputPath = os.path.sep.join([args["output"], filename])
        file = open(xmlPath, "w")
        file.write(self.generateXML(filename, imagePath[0:imagePath.rfind("/")], wI, hI, d, boxes1))
        file.close()
        self.combineImageAndPrediction(imagePath, xmlPath)
예제 #5
0
def operate_image(im):
		image = read_image_bgr(im)
		draw = image.copy()
		draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
		image = preprocess_image(image)
		image, scale = resize_image(image)
		return image, draw, scale 
예제 #6
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
예제 #7
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()
예제 #8
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)
예제 #9
0
def inf_img(filename, dataset="", preprocess=False, cartucho=False, output=''):
    img_path = backend.filenameToFullPath(filename, dataset)

    my_model = load_model()

    # img_path = ""
    img = read_image_bgr(img_path)

    if preprocess:
        img = preprocess_image(img)
        img, scale = resize_image(img)

    deep_cp_img = img.copy()
    deep_cp_img = cv2.cvtColor(deep_cp_img, cv2.COLOR_RGB2RGBA)

    boxes, scores, labels = my_model.predict_on_batch(
        np.expand_dims(img, axis=0))

    if preprocess:
        boxes = boxes / scale

    deep_cp_img = draw.draw_boxes(deep_cp_img, boxes, scores, labels)

    if cartucho:
        mAP.gen_detection_results_file(
            img_path.split('/')[-1], boxes, scores, labels)

    if output == '':
        cv2.imwrite("./main_output/inferenced_img.jpg", deep_cp_img)
    else:
        cv2.imwrite(output, deep_cp_img)
    return
예제 #10
0
def create_score_list(model, filenames, image_dir, save, labels_to_names):
    # filewise_uncertainity=[]
    # scores_list=[]
    csv_columns = ['filename', 'score', 'label_name', 'x', 'y', 'w', 'h']
    with open(save, 'w') as file:
        writer = csv.DictWriter(file, fieldnames=csv_columns)
        writer.writeheader()

    for file in tqdm(filenames):
        image = read_image_bgr(image_dir + file)
        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]):
            if score < 0:
                break
            data = {
                'filename': file,
                'score': score,
                'label_name': labels_to_names[label],
                'x': box[0],
                'y': box[1],
                'w': box[2] - box[0],
                'h': box[3] - box[1]
            }
            with open(save, 'a') as f:
                writer = csv.DictWriter(f, fieldnames=csv_columns)
                writer.writerow(data)

            f.close()
예제 #11
0
def find_los():
    print("FINDING THE LINE OF SCRIMMAGE")
    global maxBox, los_boxes, los_scores, los_labels

    keras.backend.tensorflow_backend.set_session(get_session())

    model_path = 'saved_good_files/los_detection_inference_model.h5'

    # load retinanet model
    model = models.load_model(model_path, backbone_name='resnet50')

    image = read_image_bgr('snap.jpg')

    # 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)

    los_boxes, los_scores, los_labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))

    # correct for image scale
    los_boxes /= scale

    maxScore = 0
    maxBox = None
    # Find the box with the greatest confidence
    for box, score, label in zip(los_boxes[0], los_scores[0], los_labels[0]):
        # scores are sorted so we can break
        if score > maxScore:
            maxScore = score
            maxBox = box
예제 #12
0
    def load_image(self, image_index):
        """ Load an image at the image_index.
        """
        image = self.selected[image_index].iloc[0]
        self._download_image(image)

        return read_image_bgr(path)
예제 #13
0
    def get_predictions(self, img, threshold=0.5):
        image = read_image_bgr(img)
        image = preprocess_image(image)
        image, scale = resize_image(image)

        with self.graph.as_default():
            boxes, scores, labels = self.model.predict_on_batch(
                np.expand_dims(image, axis=0))

        boxes /= scale

        result = []

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

            b = box.astype(int)

            result.append({
                "bbox": b.tolist(),
                "label": self.classes[label],
                "score": float(score)
            })

        return result
예제 #14
0
 def forward_pass(session2: tf.Session, args):
     images_raw = glob(cocopath + "/images/train2017/*.jpg")
     for idx in tqdm(range(10)):
         image = read_image_bgr(images_raw[idx])
         image = preprocess_image(image)
         image, scale = resize_image(image)
         session2.run(out_tensor, feed_dict={in_tensor: [image]})
예제 #15
0
def get_detections(filename):
    image = read_image_bgr(filename)
    image = preprocess_image(image)
    image, scale = resize_image(image)

    with graph.as_default():
        boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
    boxes /= scale

    detections = {}
    i = 0
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        if score < 0.5:
            # Sorted by score so once here break to skip low scores
            break

        detection = {}
        detection['class'] = labels_to_names[label]
        detection['score'] = score.item()
        detection['bbox_x'] = box[0].item()
        detection['bbox_y'] = box[1].item()
        detection['bbox_w'] = box[2].item() - box[0].item()
        detection['bbox_h'] = box[3].item() - box[1].item()

        detections[i] = detection
        i += 1

    return (detections)
예제 #16
0
def returnProcessedImage(path2image): 
    # preprocessing the image to be fed into the model
    image = read_image_bgr(path2image)
    image = preprocess_image(image)
    image, scale = resize_image(image)
    image = np.expand_dims(image, axis=0)
    return image
def detectAlphabets(imageToRecognize):
    args = parse_args()
    args.val_path = imageToRecognize
    # os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    keras.backend.tensorflow_backend.set_session(get_session())
    model = keras.models.load_model(os.path.join(dir, '../snapshots/resnet50_csv_wtext.h5'), custom_objects=custom_objects)
    test_image_data_generator = keras.preprocessing.image.ImageDataGenerator()
    #
    # # create a generator for testing data
    test_generator = CSVGenerator(
        csv_data_file=args.annotations,
        csv_class_file=args.classes,
        image_data_generator=test_image_data_generator,
        batch_size=args.batch_size
    )
    # index = 0
    # load image
    image = read_image_bgr(args.val_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)
    print('detections:', detections)
    # 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]
    print("label=", predicted_labels)
    # correct for image scale
    scaled_detection = detections[0, :, :4] / scale

    # visualize detections
    recognized = {}

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
        if score < 0.35:
            continue
        b = scaled_detection[idx, :4].astype(int)
        cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 1)

        caption = test_generator.label_to_name(label)
        if caption == "equal":
            caption = "="
        cv2.putText(draw, caption, (b[0], b[1] - 1), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
        recognized[caption] = b
        print(caption + ", score=" + str(score))

    plt.imshow(draw)
    plt.show()
    return recognized, draw
예제 #18
0
def cartucho_evaluate(dataset, preprocess=False, verbose=False):
    imgs_path = config.IMG_PATHS[config.DATASETS_NAMES.index(dataset)]

    img_lst = os.listdir(imgs_path)

    my_model = load_model()

    img_done = 0

    for img in img_lst:
        img_path = imgs_path + "/" + img
        # inf_img(img, dataset, True)

        img = read_image_bgr(img_path)

        if preprocess:
            img = preprocess_image(img)
            img, scale = resize_image(img)

        boxes, scores, labels = my_model.predict_on_batch(
            np.expand_dims(img, axis=0))

        if preprocess:
            boxes = boxes / scale

        mAP.gen_detection_results_file(
            img_path.split('/')[-1], boxes, scores, labels)
        img_done += 1

        if verbose:
            if img_done % 10 == 0:
                print("Image inférée: " + str(img_done) + '/' +
                      str(len(img_lst)))

    return
예제 #19
0
    def get_frame(self, i, mask=False):
        image_path = join(self.frames_path, r'{i}.bmp'.format(i=i))

        image = read_image_bgr(image_path)
        if mask:
            return image * self.field_mask[:, :, np.newaxis]
        return image
예제 #20
0
def evaluateSet(setpath,weightnumber=13):
    gpu = 0
    setup_gpu(gpu)
    model_path = os.path.join('..', 'keras_retinanet', 'resnet50_csv_' + str(weightnumber) + '_final.h5')
    # load retinanet model
    model = models.load_model(model_path, backbone_name='resnet50')
    iset = readSet(setpath) #read set
    imagelist = iset[:,0] # load imagelist
    resultlist = [["image","bounds","score", "label"]]
    for i in imagelist:
        image = read_image_bgr(str("../" + i)) #originally bgr(i)
        if draw:
            image_copy = image.copy()
        image = preprocess_image(image)
        image, scale = resize_image(image)
        boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
        boxes /= scale
        coords = []
        cats = []
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
          if score < 0.4:
            break
          else:
            resultlist.append([i,box,label,score])
    return resultlist
예제 #21
0
def img_inference(model, session, img_path, thresh=0.8):
    image = read_image_bgr(img_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    draw = image.copy()

    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)
    print(f'Image resized to :{image.shape}\n')
    # process image
    start = time.time()
    with session.as_default():
        with session.graph.as_default():
            boxes, scores, labels = model.predict_on_batch(image[None, ...])
    print("processing time: ", time.time() - start)

    # correct for image scale
    boxes /= scale
    try:
        output = boxes[0][0]
        xmin = np.int(output[0])
        ymin = np.int(output[1])
        xmax = np.int(output[2])
        ymax = np.int(output[3])
        imgout = draw[ymin:ymax, xmin:xmax, :]
        imgout = cv2.cvtColor(imgout, cv2.COLOR_RGB2GRAY)
        # imgout = cv2.medianBlur(imgout,5)
        # imgout = cv2.adaptiveThreshold(imgout,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,9,3)
    except:
        output = None
        imgout = draw
        imgout = cv2.cvtColor(imgout, cv2.COLOR_RGB2GRAY)
    imgout = auto_rotate(imgout)
    return output, imgout
예제 #22
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))
예제 #23
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))
예제 #24
0
    def detect_objects(self, img_file):
        """
Read & preprocess the image
        """
        image = read_image_bgr(img_file)
        image = preprocess_image(image)
        image, scale = resize_image(image)
        """
Obtain the bounding boxes for the objects detected in the image & correct their scale
        """
        boxes, scores, labels = self.model.predict_on_batch(
            np.expand_dims(image, axis=0))
        boxes /= scale
        """
Store bounding box details for valid detections (confidence > threshold)
        """
        bounding_boxes = {}
        count = 0
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            """            
Scores are sorted in descending order so that we can `break` on reaching below threshold
            """
            if score < self.threshold:
                break

            bounding_boxes[count] = {
                "bounding_box": box.astype(int),
                "label": self.labels[label]
            }
            count += 1

        return bounding_boxes
예제 #25
0
def show(image_path):
    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)

    boxes, scores, labels = model.predict(np.expand_dims(image, axis=0))
    boxes /= scale

    st.write("Things found:")

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

        visualize_image(draw, box, score, label)
        st.write(labels_to_names[label], 'with score:', score)

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    st.pyplot()
예제 #26
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
예제 #27
0
    def detect(self, image_path: PathLike) -> DetectionOutput:
        image = read_image_bgr(image_path)
        if self.debug:
            print("debug")
            cv2.imwrite("debug/input_rgb.png", image)
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        if self.debug:
            cv2.imwrite("debug/input_bgr.png", image)

        image = preprocess_image(image)
        image, scale = resize_image(image)
        boxes, scores, classes = self.model.predict_on_batch(
            np.expand_dims(image, axis=0))

        width = image.shape[1]
        height = image.shape[0]
        bboxes = [
            BBox(x0=box[0] / width,
                 x1=box[2] / width,
                 y0=box[1] / height,
                 y1=box[3] / height) for box in boxes[0]
        ]
        detections = [
            Detection(bbox=bbox, label=config.labels[klass], confidence=score)
            for bbox, score, klass in zip(bboxes, scores[0], classes[0])
            if score > 0.25
        ]
        output = DetectionOutput(image_name=str(image_path),
                                 detections=detections)

        if self.debug:
            visualize(output, "debug/output.png")

        return output
예제 #28
0
def predict(imagePath):
	# load the input image (in BGR order), clone it, and preprocess it
	image = read_image_bgr(imagePath)
	output = image.copy()
	image = preprocess_image(image)
	(image, scale) = resize_image(image)
	image = np.expand_dims(image, axis=0)

	# detect objects in the input image and correct for the image scale
	(boxes, scores, labels) = model.predict_on_batch(image)
	boxes /= scale

	# loop over the detections
	for (box, score, label) in zip(boxes[0], scores[0], labels[0]):
		# filter out weak detections
		if score < 0.5:
			continue
	
		# convert the bounding box coordinates from floats to integers
		box = box.astype("int")
	
		# build the label and draw the label + bounding box on the output
		# image
		label = "{}: {:.2f}".format(LABELS[label], score)
		cv2.rectangle(output, (box[0], box[1]), (box[2], box[3]),
			(0, 255, 0), 2)
		cv2.putText(output, label, (box[0], box[1] - 10),
			cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

	# show the output image
	cv2.imwrite("prediction.jpg", output)
	return boxes
예제 #29
0
    def process_image(self, window, file, output_type):
        """
        Process input image.
        :param window: PyQt window
        :param file: the name of the input file
        :param output_type: the type of the output
        :return: the name of the output file
        """
        image = read_image_bgr(file.full_path)
        objects_detected = self.detect_objects(image)
        output_name = file.get_output_name(output_type)

        if output_type == OutputType.BORDERS:
            output_image = self.draw_bounding_boxes(image, objects_detected)

        elif output_type == OutputType.PANORAMA:
            objects_cropped = self.crop_objects(image, objects_detected)
            matcher = Matcher(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
            matcher.process_objects(0, objects_cropped)
            matcher.save_objects(output_name.split('.')[0] + '-dir/', 0, objects_cropped)
            output_image = matcher.get_panorama(InputType.IMAGE)
        else:
            raise ValueError

        # Save output image to tmp/
        image = Image.fromarray(output_image)
        image.save(output_name)

        # Visualize output image
        window.actualize_output_label(npimg_to_pixmap(output_image))

        return output_name
예제 #30
0
def run_detection_image(model, labels_to_names, filepath):
    image = read_image_bgr(filepath)

    # 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))

    # correct for image scale
    boxes /= scale
    caption = ""

    # 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
        b = np.array(box.astype(int)).astype(int)
        # x1 y1 x2 y2
        caption += "output= {} {} {} {} {} {:.3f}\n".format(
            b[0], b[1], b[2], b[3], labels_to_names[label], score)
    return caption
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)
    
    image_paths = list()

    with open(args.csv_path, 'w', newline='') as csvfile:
        csv_writer = csv.writer(csvfile, delimiter=',')



        tree = ET.parse(args.gt_path)
        # get root element
        root = tree.getroot()
        annotation_items = root.findall('./annotation')
        for annotation_item in annotation_items:
            filename_item = annotation_item.find('./filename')
            text = filename_item.text

            if 'FigureSeparationTraining2016' in args.gt_path:
                image_path = os.path.join('/datasets/ImageCLEF/2016/training/FigureSeparationTraining2016/', text+'.jpg')
            elif 'FigureSeparationTest2016GT' in args.gt_path:
                image_path = os.path.join('/datasets/ImageCLEF/2016/test/FigureSeparationTest2016/', text+'.jpg')
            else:
                raise Exception('Error {0}'.format(args.gt_path))

            image_paths.append(image_path+"\n")

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

            csv_path_individual = os.path.join('csv/', text + '.csv')
            jpg_path_individual = os.path.join('preview/', text + '.jpg')
            with open(csv_path_individual, 'w', newline='') as csvfile_individual:
                csv_writer_individual = csv.writer(csvfile_individual, delimiter=',')

                object_items = annotation_item.findall('./object')
                for idx, object_item in enumerate(object_items):
                    point_items = object_item.findall('./point')
                    x1 = point_items[0].get('x')
                    y1 = point_items[0].get('y')
                    x2 = point_items[3].get('x')
                    y2 = point_items[3].get('y')
                    if int(x1) >= int(x2) or int(y1) >= int(y2):
                        continue
                    csv_writer.writerow([image_path, x1, y1, x2, y2, 'panel'])
                    csv_writer_individual.writerow([image_path, x1, y1, x2, y2, 'panel'])

                    color = label_color(idx)
                    box = [int(x1), int(y1), int(x2), int(y2)]
                    draw_box(draw, box, color)

                cv2.imwrite(jpg_path_individual, draw)

    with open(args.list_path, "w") as text_file:
            text_file.writelines(image_paths)