Exemplo n.º 1
0
def cvDrawBoxes(detections, img):
    roi_array = []
    for detection in detections:
        x, y, w, h = detection[2][0],\
            detection[2][1],\
            detection[2][2],\
            detection[2][3]
        xmin, ymin, xmax, ymax = lightnet.convertBack(float(x), float(y),
                                                      float(w), float(h))
        pt1 = (xmin, ymin)
        pt2 = (xmax, ymax)
        roi_array.append((xmin, ymin, xmax, ymax))

        if args.debug:
            cv.rectangle(img, pt1, pt2, (0, 255, 0), 1)
            cv.putText(
                img,
                # detection[0] +
                " [" + str(round(detection[1] * 100, 2)) + "]",
                (pt1[0], pt1[1] - 5),
                cv.FONT_HERSHEY_SIMPLEX,
                0.5,
                [0, 255, 0],
                2)
    return roi_array
Exemplo n.º 2
0
def process(category):
    image_filenames = glob.glob('%s/*.png' % (category), recursive=True)
    image_filenames.extend(glob.glob('%s/*.jpg' % (category), recursive=True))
    # print("Start category: %s" % (category))
    for filename in image_filenames:
        # load the input image
        filename = pathlib.Path(filename).as_posix()
        image = cv.imread(filename)
        h, w, c = image.shape

        # roi
        if False:
            x_spacing = 20
            image = image[0:h, x_spacing:w - x_spacing]
        h, w, c = image.shape

        if args.debug:
            print(w, h)

        x1, y1, x2, y2 = (0, 0, w, h)
        if args.yolo_cfg:
            full_im, _ = darknet.array_to_image(image)
            with inference_lock:
                results = lightnet.detect_from_memory(
                    yolo_net, yolo_meta, full_im, thresh=0.75, debug=False)
            if results:
                detection = results[0]
                x, y, w, h = detection[2][0],\
                    detection[2][1],\
                    detection[2][2],\
                    detection[2][3]
                if w > W and h > H:
                    x1, y1, x2, y2 = lightnet.convertBack(
                        float(x), float(y), float(w), float(h))
        else:
            (success, saliencyMap) = saliency.computeSaliency(image)
            M = cv.moments(saliencyMap.astype("uint8"))

            # calculate x,y coordinate of center
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])
            cv.circle(saliencyMap, (cX, cY), 5, (255, 255, 255), -1)

            cY = h / 2
            if cX < W / 2:
                cX = W / 2
            if cX > w - W / 2:
                cX = w - W / 2
            x1 = int(cX - W / 2)
            y1 = int(cY - H / 2)
            x2 = int(cX + W / 2)
            y2 = int(cY + H / 2)

        filename = filename.replace(
            args.images, 'img_roi').replace('.png', '.jpg')
        # print(filename)
        os.makedirs(os.path.dirname(filename), exist_ok=True)
        cv.imwrite(filename, image[y1:y2, x1:x2])

        if args.debug:
            # show the images
            cv.rectangle(saliencyMap, (x1, y1), (x2, y2), (255, 255, 255), 1)
            cv.imshow("Image", image)
            cv.imshow("Output", saliencyMap)

            key = cv.waitKey(0)
            if key == 27:
                break

    print("Finish category: %s" % (category))