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 #2
0
    def detect(self, image_obj):
        """
        通用检测对外接口
        :param image_obj: 待检测图像, Pillow Image Object 类型
        :return: 检测的 bounding boxes 结果, 维度: [n_boxes, 6], 第二维: [ymin, xmin, ymax, xmax, class, score]
        """
        image_arr = cv2.cvtColor(np.array(image_obj), cv2.COLOR_RGB2BGR)
        image_arr = preprocess_image(image_arr)
        image_arr, scale = resize_image(image_arr)

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

        # correct for image scale
        boxes /= scale

        num = [
            scores[0][i] for i in range(len(scores[0]))
            if scores[0][i] >= self.score_thresh
        ]
        num = len(num)

        results = []

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

            if self.is_useless(box, boxes[0][:num]):
                continue

            box = list(map(int, box))
            score = round(score, 2)
            results.append([
                box[1], box[0], box[3], box[2], self.class_list[label], score
            ])
#             results.append([box[1], box[0], box[3], box[2], self.class_list[0], score])

        return results
def img_inference(img_path, model):
    pred = []
    image = read_image_bgr(img_path)
    name = img_path.split('/')[-1].split('.')[0]
    # 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.4:
            break

        color = label_color(label)

        b = box.astype(int)
        pred.append(str(label))
        for i in b:
            pred.append(str(i))

        draw_box(draw, b, color=color)

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

    op_name = OUTPUT + name + '_OUTPUT.png'
    cv2.imwrite(op_name, draw)
    # pci = calculate_pci(boxes[0])
    return (op_name, pred)
Beispiel #4
0
    def preprocess_group(self, image_group, annotations_group):
        for index, (image, annotations) in enumerate(zip(image_group, annotations_group)):
            # preprocess the image (subtract imagenet mean)
            image = preprocess_image(image)

            # randomly transform both image and annotations
            image, annotations = random_transform(image, annotations, self.image_data_generator)

            # resize image
            image, image_scale = self.resize_image(image)

            # apply resizing to annotations too
            annotations[:, :4] *= image_scale

            # copy processed data back to group
            image_group[index] = image
            annotations_group[index] = annotations

        return image_group, annotations_group
Beispiel #5
0
def pred_img(img_path):

    # 加载数据
    image = read_image_bgr(img_path)

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

    # 输入网络准备
    image = preprocess_image(image)
    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, "RetinaNet GPU:NVIDIA Tesla V100 32GB by XJ",
                    (50, 50), font, 1.2, (0, 255, 0), 3)
        cv2.imwrite("./images/test_result.png", draw)

    # cv2.imshow('result.jpg',draw)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    def __next__(self):
        if self.current + 1 > self.batches:
            raise StopIteration('there is no more batches!')
        curr_data_batch = self.data[self.current *
                                    self.batch_size:(self.current + 1) *
                                    self.batch_size]
        processed_images = []
        scales = []
        for image in curr_data_batch:
            processed_image = image * 255.
            processed_image = preprocess_image(processed_image)
            processed_image, scale = resize_image(processed_image,
                                                  min_side=512)
            processed_images.append(np.expand_dims(processed_image, 0))
            scales.append(scale)
        processed_images = np.concatenate(processed_images, axis=0)

        self.current += 1
        return processed_images, scales
Beispiel #7
0
def predict_crop(model, crop_bgr, args):
    img_ar = preprocess_image(crop_bgr)
    img_ar, scale = resize_image(img_ar,
                                 min_side=args.image_min_side,
                                 max_side=args.image_max_side)
    img_ar = np.expand_dims(img_ar, axis=0)

    boxes, scores, labels = model.predict_on_batch(img_ar)
    boxes /= scale

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

        x1, y1, x2, y2 = box.astype(int)
        results.append([x1, y1, x2, y2, label, score])

    return results
def inference_img(path, show=False):
    # load image
    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)

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

    # correct for image scale
    boxes /= scale
    if show:
        # 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()
        #print(scores)
    idx = np.argmax(scores[0])
    #print(idx)
    out_label = labels[0][idx]
    if out_label == -1:
        out_label = 0
    #print(out_label)
    return out_label
Beispiel #9
0
def detect_one_image(image, model):
    draw = image.copy()

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

    # 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

        (x1, y1, x2, y2) = box
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        if blur:
            #Extracting the area to blur
            region = draw[y1:y2, x1:x2]

            #Create a blurred image of the area
            #   OBS: Region size has to be odd numbers!!!
            blurred_region = cv2.blur(region, (21, 21))

            #Set the area of interest to the blurred image of that area.
            draw[y1:y1 + blurred_region.shape[0],
                 x1:x1 + blurred_region.shape[1]] = blurred_region

        else:
            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)
    return draw
Beispiel #10
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
        # 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

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

    print("Total Time: ", end - start)
def image_pred(model,img_path):


    #files = os.walk(img_dir).next()[2]

    #for file in files:
    #print os.path.join(img_dir, file)
    # load image
    image = read_image_bgr(os.path.join(img_path))

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

    # process image
    _, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))

    # 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
    bb_score= np.empty((1,6))
    # store detections with confidence > 0.5
    flag = 0;
    for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
        if score < 0.5:
             continue
        bb = detections[0, idx, :4].astype(int)
        bb_temp =  np.append(bb,[label,score])
        if(flag == 0):
            flag =1
            bb_score[0,:]=np.expand_dims(bb_temp,axis=0)
        #print(np.expand_dims(bb_temp,axis=0).shape)
        else:
            bb_score = np.append(np.expand_dims(bb_temp,axis = 0),bb_score,axis=0)

    print(bb_score.shape)
    if(flag  == 0):
        return []
    else:
        return bb_score  #format :[x1,y1,x2,y2,label,score]
Beispiel #12
0
def detect_plate(image_url, is_faulty=False):
    if not os.path.exists('outputs'):
        os.makedirs('outputs')

    img = urllib.request.urlopen(image_url)
    img = Image.open(img)
    img = img.convert('RGB')
    img.save(CONFIG["default_image_download_path"], "JPEG")

    image = read_image_bgr(CONFIG["default_image_download_path"])
    image = preprocess_image(image)
    (image, scale) = resize_image(image)
    image = np.expand_dims(image, axis=0)

    boxes, scores, labels = faulty_model.predict_on_batch(
        image) if is_faulty == True else model.predict_on_batch(image)
    boxes /= scale

    curr_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    save_file_path = f"outputs/pred_{curr_time}.png"
    save_prediction(CONFIG["default_image_download_path"], save_file_path,
                    boxes, scores, labels)

    object_detected = []
    # Loop over the detections
    for (box, score, label) in zip(boxes[0], scores[0], labels[0]):
        # Filter out weak detections
        if score < CONFIG["confidence"]:
            continue
        box = box.astype("int")
        object_detected.append({
            "label": LABELS[label],
            "score": str(score),
            "coord": {
                "y_min": str(box[1]),
                "x_min": str(box[0]),
                "y_max": str(box[3]),
                "x_max": str(box[2])
            }
        })

    return object_detected, save_file_path
Beispiel #13
0
    def process_detection(self, color_img):

        H, W = color_img.shape[:2]

        pre_image = preprocess_image(color_img)
        res_image, scale = resize_image(pre_image)

        batch_image = np.expand_dims(res_image, axis=0)
        print batch_image.shape
        print batch_image.dtype
        boxes, scores, labels = self.detector.predict_on_batch(batch_image)

        valid_dets = np.where(scores[0] >= self.det_threshold)

        boxes /= scale

        scores = scores[0][valid_dets]
        boxes = boxes[0][valid_dets]
        labels = labels[0][valid_dets]

        filtered_boxes = []
        filtered_scores = []
        filtered_labels = []

        for box, score, label in zip(boxes, scores, labels):

            box[0] = np.minimum(np.maximum(box[0], 0), W)
            box[1] = np.minimum(np.maximum(box[1], 0), H)
            box[2] = np.minimum(np.maximum(box[2], 0), W)
            box[3] = np.minimum(np.maximum(box[3], 0), H)

            bb_xywh = np.array(
                [box[0], box[1], box[2] - box[0], box[3] - box[1]])
            if bb_xywh[2] < 0 or bb_xywh[3] < 0:
                continue

            if label in filtered_labels:  # only single instance for each class
                continue
            filtered_boxes.append(bb_xywh)
            filtered_scores.append(score)
            filtered_labels.append(label)
        return (filtered_boxes, filtered_scores, filtered_labels)
Beispiel #14
0
def main():
    classes = ["hat", "person"]
    labels_to_names = {0: "hat", 1: "person"}
    #加载模型
    model_path = './model/resnet50_csv_03.h5'
    model = models.load_model(model_path, backbone_name='resnet50')
    img_dir = './images'
    for img_path in get_image_list(img_dir):
        image = read_image_bgr(img_path)
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        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("processing time: ", time.time() - start)
        # 矫正比例
        boxes /= scale
        # 目标检测可视化展示
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            # 设置预测得分最低阈值
            if score < 0.50:
                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)
        new_img_path = img_path.replace('images', 'images/result')
        plt.savefig(new_img_path,
                    format='png',
                    transparent=True,
                    pad_inches=0,
                    dpi=300,
                    bbox_inches='tight')
Beispiel #15
0
def run_detection_on_image(image_path, min_score=0.5):
    image = read_image_bgr(image_path)
    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)
    # process image
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    # correct for image scale
    boxes /= scale
    return_boxes = []
    return_scores = []
    return_image_name = []
    # captions = list()

    lala = re.sub("[A-Z a-z /.]+", "", image_path).split('_')[-2:]

    # visualize detections
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        # scores are sorted so we can break
        if score < min_score:
            break
        b = box.astype(int)
        b = list(b)
        absolute_boxes = [b[0], b[1], b[2], b[3], label]
        return_boxes.append(absolute_boxes)
        return_scores.append(score)
        return_image_name.append(image_path.split('/')[-1])
        # draw_box(draw, b, (0, 255, 0))
        # gt = [row['xmin'], row['ymin'], row['xmax'], row['ymax']]
        # draw_box(draw, gt, color=(255, 0, 0))
        # caption = "{} {:.3f}".format(labels_to_names[label], score)
        # captions.append(caption)
        # draw_caption(draw, b, caption)

    # file, ext = os.path.splitext(image_path)
    # image_name = file.split('/')[-1] + ext
    # output_path = os.path.join('results/', image_name)
    # draw_conv = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    # cv2.imwrite(output_path, draw_conv)

    return return_boxes, return_scores, return_image_name
Beispiel #16
0
def main(args):

    # ==========================================
    # Load pretrained model
    # ==========================================
    model = keras.models.load_model(os.path.expanduser(args.model),
                                    custom_objects=custom_objects)
    classes = load_classes(os.path.expanduser(args.classes))

    # ==========================================
    # Detect bounding boxes
    # ==========================================
    input_img = cv2.imread(os.path.expanduser(args.input_img))
    output_img = input_img.copy()

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

    # detect bounding boxes
    _, _, boxes, nms_classification = model.predict_on_batch(input_img)
    num_detected_boxes = len(boxes[0, :, :])

    # visualize
    for i in range(num_detected_boxes):
        class_id = np.argmax(nms_classification[0, i, :])
        score = nms_classification[0, i, class_id]
        if score < args.score_threshold:
            continue

        # draw bounding box on a copy of the original input image
        color = label_color(class_id)
        coord = boxes[0, i, :] / scale
        draw_box(output_img, coord, color=color)

        # draw caption for the above box
        caption = '%s %.3f' % (classes[class_id], score)
        draw_caption(output_img, coord, caption=caption)

    # save output image
    cv2.imwrite(os.path.expanduser(args.output_img), output_img)
Beispiel #17
0
    def detect(self, img_path, min_prob=0.6):
        image = read_image_bgr(img_path)
        image = preprocess_image(image)
        image, scale = resize_image(image)
        boxes, scores, labels = self.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 = self.classes[label]
            processed_boxes.append({
                "box": box,
                "score": score,
                "label": label
            })

        return processed_boxes
def detect_objects(image):
    detected_objects = []
    image = preprocess_image(image)
    global model
    if model is None:
        model_path = "/tmp/debris_model_v3_10_6.h5"
        model = models.load_model(model_path, backbone_name='resnet50')
    boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
    detected_label =set()
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        #only shows the highest confidence box
        if label in detected_label:
            continue
        detected_label.add(label)
        if score < 0.15:
            break
        color = label_color(label)
        b = box.astype(int)
        detected_objects.append({'x1':b[0], 'y1':b[1], 'x2': b[2], 'y2':b[3],'label':label, 'label_name': label_lookup[label], 'score':float(score)})
    return detected_objects
Beispiel #19
0
def run_detection_image(model, labels_to_names, data):
    print("start predict {}")
    with graph.as_default():
        imgdata = base64.b64decode(data)
        npImage = np.asarray(Image.open(BytesIO(imgdata)).convert('RGB'))
        image = npImage[:, :, ::-1].copy()

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

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

        # correct for image scale
        boxes /= scale

        objects = []
        reaponse = {'objects': objects}

        # 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
            obj = {
                'name': labels_to_names[label],
                'score': str(score),
                'xmin': str(b[0]),
                'ymin': str(b[1]),
                'xmax': str(b[2]),
                'ynax': str(b[3])
            }
            objects.append(obj)
            #caption += "output= {} {} {} {} {} {:.3f}\n".format(b[0], b[1], b[2], b[3], labels_to_names[label], score)
        reaponse_json = json.dumps(reaponse)
        print("done {}", reaponse_json)
        return reaponse_json
def mainDataset(dataset, output, name, weights, fichClass):
    # load the class label mappings
    LABELS = open(fichClass).read().strip().split("\n")
    LABELS = {int(L.split(",")[1]): L.split(",")[0] for L in LABELS}

    # load the model from disk and grab all input image paths
    model = models.load_model(weights, backbone_name=name)
    imagePaths = list(paths.list_images(dataset))
    # loop over the input image paths
    for (i, imagePath) in enumerate(imagePaths):
        # load the input image (in BGR order), clone it, and preprocess it
        print("[INFO] predicting on image {} of {}".format(
            i + 1, len(imagePaths)))

        # 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 < confidence:
                continue
            boxes1.append(([LABELS[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(imagePath[0:imagePath.rfind(".")] + ".xml", "w")
        file.write(
            generateXML(imagePath[0:imagePath.rfind(".")], imagePath, hI, wI,
                        d, boxes1))
        file.close()
Beispiel #21
0
def predict_chunked_image(img_path: str,
                          model: keras.models.Model,
                          chunk_sizes: Tuple[int, int] = (500, 500)):

    print("Reading Image")
    image = read_image_bgr(img_path)

    print("Processing Image")
    image = preprocess_image(image)

    img_gen = chunk_image_generator(image,
                                    chunk_size=chunk_sizes,
                                    displacement=chunk_sizes)
    results = []
    i = 0

    print("Predicting")
    for x_start, y_start, chunk in img_gen:
        chunk, scale = resize_image(chunk)

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

        boxes /= scale
        out_dict = {
            'x_start': x_start,
            'y_start': y_start,
            'scale': scale,
            'boxes': boxes,
            'scores': scores,
            'labels': labels
        }

        i += 1

        print("Done with image {i} at x:{x}, y:{y}".format(i=i,
                                                           x=x_start,
                                                           y=y_start))
        results.append(out_dict)

    return (results)
Beispiel #22
0
    def automate(self):
        self.processingLabel.config(text="Processing     ")
        self.processingLabel.update_idletasks()
        open_cv_image = np.array(self.img)
        # Convert RGB to BGR
        opencvImage= open_cv_image[:, :, ::-1].copy()
        # opencvImage = cv2.cvtColor(np.array(self.img), cv2.COLOR_RGB2BGR)
        image = preprocess_image(opencvImage)
        boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
        for idx, (box, label, score) in enumerate(zip(boxes[0], labels[0], scores[0])):
            curr_label_list = self.labelListBox.get(0, END)
            curr_label_list = list(curr_label_list)
            if score < 0.5:
                continue

            if config.labels_to_names[label] not in curr_label_list:
                continue

            b = box.astype(int)

            self.bboxId = self.canvas.create_rectangle(b[0], b[1],
                                                       b[2], b[3],
                                                       width=2,
                                                       outline=config.COLORS[len(self.bboxList) % len(config.COLORS)])
            self.bboxList.append((b[0], b[1], b[2], b[3]))
            o1 = self.canvas.create_oval(b[0] - 3, b[1] - 3, b[0] + 3, b[1] + 3, fill="red")
            o2 = self.canvas.create_oval(b[2] - 3, b[1] - 3, b[2] + 3, b[1] + 3, fill="red")
            o3 = self.canvas.create_oval(b[2] - 3, b[3] - 3, b[2] + 3, b[3] + 3, fill="red")
            o4 = self.canvas.create_oval(b[0] - 3, b[3] - 3, b[0] + 3, b[3] + 3, fill="red")
            self.bboxPointList.append(o1)
            self.bboxPointList.append(o2)
            self.bboxPointList.append(o3)
            self.bboxPointList.append(o4)
            self.bboxIdList.append(self.bboxId)
            self.bboxId = None
            self.objectLabelList.append(str(config.labels_to_names[label]))
            self.objectListBox.insert(END, '(%d, %d) -> (%d, %d)' % (b[0], b[1], b[2], b[3]) + ': ' +
                                      str(config.labels_to_names[label]))
            self.objectListBox.itemconfig(len(self.bboxIdList) - 1,
                                          fg=config.COLORS[(len(self.bboxIdList) - 1) % len(config.COLORS)])
        self.processingLabel.config(text="Done              ")
Beispiel #23
0
def run_detection_image(filepath):
    image = read_image_bgr(filepath)
    #     print(image.shape)
    #     print(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

        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)

    file, ext = os.path.splitext(filepath)
    image_name = file.split('/')[-1] + ext
    output_path = '/home/jetsontx2/mahmoud/aerial_pedestrian_detection/output_UC3M/'+ image_name
    print(output_path)
    draw_conv = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    cv2.imwrite(output_path, draw_conv)
Beispiel #24
0
def predict(image_path):
    #image = read_image_bgr('/
    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))
    #boxes, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)

    #print(labels)

    # 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 = "{} {:.2f}".format(labels_to_names[label], score)
        draw_caption(draw, b, caption)
    print(label)
    print(score)
    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
Beispiel #25
0
def object_detection():
    print("PERFORMING OBJECT DETECTION")
    global p0, boxes, scores, labels
    p0 = np.array([[[0, 0]]], dtype=np.float32)

    keras.backend.tensorflow_backend.set_session(get_session())
    model_path = 'saved_good_files/player_detection_inference_model.h5'
    model = models.load_model(model_path, backbone_name='resnet50')

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

    # process image
    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.5:
            break

        color = label_color(label)

        b = box.astype(int)
        center = [(b[0] + b[2]) / 2, (b[1] + b[3]) / 2]
        p1 = np.array([[[center[0], center[1]]]], dtype=np.float32)
        p0 = np.concatenate((p0, p1))
        draw_box(draw, b, color=color)
    """
Beispiel #26
0
def show_image_with_predictionss(filepath, threshold=0.5):
  
  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 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:.2f}"
    draw_caption(im, box, caption)

  plt.axis('off')
  plt.imshow(im)
  plt.show()
Beispiel #27
0
def run_detection(model, filepath, labels_file="data/labels.csv"):
  
    labels = [i.split(",") for i in open(labels_file).read().split("\n")]
    labels_to_names = dict([(int(str_id), name) for name, str_id in labels])
    
    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))
    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
        print(box, score, label)
        if (label == 0 and score < .3) or score < .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.savefig("ouptut_run_detection.png")
Beispiel #28
0
def load_model(load=False):
    if load:
        try:
            print('loading the model...............')
            model = keras_retinanet.models.load_model(MODEL_PATH,
                                                      backbone_name='resnet50',
                                                      convert=True)
            image_path = '/home/syh/commdity_recognition/development/server/static/download/train_20180307_1725.jpg'
            image = read_image_bgr(image_path)
            image = preprocess_image(image)
            image, scale = resize_image(image)
            model.predict_on_batch(np.expand_dims(image, axis=0))
            print('finished load the model...............')

            # print(model.summary())
        except ImportError as ex:
            print("Can't load the model: %s" % ex)
    else:
        return None

    return model
    def detect(self, cv2_image):
        """Run predict on the image after preprocessing."""
        # Convert image for network
        # img_inf = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2RGB)
        img_inf = preprocess_image(cv2_image)
        img_inf, scale = resize_image(img_inf)

        # Run inference
        boxes, scores, labels = self.model.predict_on_batch(
            np.expand_dims(img_inf, axis=0))

        # Convert bbox into image position
        boxes /= scale

        # Clean prediction output
        bbox, scores, classes_pred = self.filter_prediction(
            boxes[0], scores[0], labels[0])

        # Convert to Signate frame output here:
        signate_detection = self.convert_to_signate(bbox, scores, classes_pred)
        return ([bbox, scores, classes_pred, signate_detection])
Beispiel #30
0
def predict_on_image(model, labels_to_names, image, thresh):
    # copy to draw on
    draw = image.copy()
    # draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB, dst=draw)

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

    # 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 < thresh:
            break

        # color = label_color(label)
        if score < 0.99:
            color = (0, 0, 255)
        elif label == 1:
            color = (0, 255, 0)
        else:
            color = (0, 255, 255)
        b = np.round(box, 0).astype(int)
        draw_box(draw, b, color=color, thickness=1)

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

    # return cv2.cvtColor(draw, cv2.COLOR_RGB2BGR, dst=draw)
    return draw
Beispiel #31
0
def save_img(filename, dest, com):
    # load image
    image = read_image_bgr(filename)

    # 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 < tresh:
            break

        color = label_color(label)

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

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

    plt.figure(figsize=(30, 30))
    plt.axis('off')
    plt.imshow(draw)
    title, ext = os.path.splitext(os.path.basename(filename))
    plt.savefig(dest + title + com + ext)