def handle_single_images(input_handler: InputHandler, number_of_cameras: int):
    Database.initialize()

    input_handler_copy = copy.deepcopy(input_handler)
    while True:
        try:
            image_index, image = input_handler_copy.get_frame(0)

            identifier = SubjectIdentifier(image_index)
            identifier.identify(image)
        except IndexError:
            break

    for i in range(1, number_of_cameras):
        input_handler_copy = copy.deepcopy(input_handler)
        successes = 0
        fails = 0
        while True:
            try:
                image_index, image = input_handler_copy.get_frame(i)

                identifier = SubjectIdentifier()
                subject = identifier.identify(image)

                if subject.name == image_index:
                    successes += 1
                else:
                    fails += 1
                    print(image_index, "identified as", subject.name)
            except IndexError:
                print(successes * 100 / (successes + fails))
                break
def handle_single_images(input_handler: InputHandler, number_of_cameras: int):
    Database.initialize()

    input_handler_copy = copy.deepcopy(input_handler)
    while True:
        try:
            image_index, image = input_handler_copy.get_frame(0)

            identifier = SubjectIdentifier(image_index)
            identifier.identify(image)
        except IndexError:
            break

    for i in range(1, number_of_cameras):
        input_handler_copy = copy.deepcopy(input_handler)
        successes = 0
        fails = 0
        while True:
            try:
                image_index, image = input_handler_copy.get_frame(i)

                identifier = SubjectIdentifier()
                subject = identifier.identify(image)

                if subject.name == image_index:
                    successes += 1
                else:
                    fails += 1
                    print(image_index, 'identified as', subject.name)
            except IndexError:
                print(successes * 100 / (successes + fails))
                break
def run(input_handler: InputHandler, camera_name: str, add_to_db: Optional[str]):
    Database.initialize()
    im0 = input_handler.get_frame(camera_name).image
    im_gray0 = cv2.cvtColor(im0, cv2.COLOR_BGR2GRAY)
    im_draw = numpy.copy(im0)
    tl, br = util.get_rect(im_draw)

    cmt = CMT(im_gray0, tl, br, estimate_rotation=False)
    identifier = SubjectIdentifier(add_to_db)

    structuring_element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))

    while True:
        try:
            # Read image
            im = input_handler.get_frame(camera_name).image

            im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
            im_draw = numpy.copy(im)

            cmt.process_frame(im_gray)

            # Display results
            # Draw updated estimate
            if cmt.has_result:
                cropped_image = im[cmt.tl[1] : cmt.bl[1], cmt.tl[0] : cmt.tr[0]]

                difference = cv2.absdiff(im_gray0, im_gray)

                blurred = cv2.medianBlur(difference, 3)
                display = cv2.compare(blurred, 6, cv2.CMP_GT)

                eroded = cv2.erode(display, structuring_element)
                dilated = cv2.dilate(eroded, structuring_element)

                cropped_mask = dilated[cmt.tl[1] : cmt.bl[1], cmt.tl[0] : cmt.tr[0]]
                cropped_mask[cropped_mask == 255] = 1

                horizontal_center = cropped_mask.shape[1] // 2
                vertical_center = cropped_mask.shape[0] // 2

                cv2.ellipse(
                    cropped_mask,
                    (horizontal_center, vertical_center),
                    (horizontal_center, vertical_center),
                    0,
                    0,
                    360,
                    3,
                    -1,
                )

                cv2.rectangle(im_draw, cmt.tl, cmt.br, (255, 0, 0), 4)
                subject = identifier.identify(cropped_image, cropped_mask)
                cv2.putText(im_draw, subject.name, cmt.tl, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)

            util.draw_keypoints(cmt.tracked_keypoints, im_draw, (255, 255, 255))
            util.draw_keypoints(cmt.votes[:, :2], im_draw)
            util.draw_keypoints(cmt.outliers[:, :2], im_draw, (0, 0, 255))
            cv2.imshow("main", im_draw)
            cv2.waitKey(1)

            im_gray0 = im_gray
        except IndexError:
            Database.save_db()
            exit(0)
def run(input_handler: InputHandler, camera_name: str,
        add_to_db: Optional[str]):
    Database.initialize()
    im0 = input_handler.get_frame(camera_name).image
    im_gray0 = cv2.cvtColor(im0, cv2.COLOR_BGR2GRAY)
    im_draw = numpy.copy(im0)
    tl, br = util.get_rect(im_draw)

    cmt = CMT(im_gray0, tl, br, estimate_rotation=False)
    identifier = SubjectIdentifier(add_to_db)

    structuring_element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))

    while True:
        try:
            # Read image
            im = input_handler.get_frame(camera_name).image

            im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
            im_draw = numpy.copy(im)

            cmt.process_frame(im_gray)

            # Display results
            # Draw updated estimate
            if cmt.has_result:
                cropped_image = im[cmt.tl[1]:cmt.bl[1], cmt.tl[0]:cmt.tr[0]]

                difference = cv2.absdiff(im_gray0, im_gray)

                blurred = cv2.medianBlur(difference, 3)
                display = cv2.compare(blurred, 6, cv2.CMP_GT)

                eroded = cv2.erode(display, structuring_element)
                dilated = cv2.dilate(eroded, structuring_element)

                cropped_mask = dilated[cmt.tl[1]:cmt.bl[1],
                                       cmt.tl[0]:cmt.tr[0]]
                cropped_mask[cropped_mask == 255] = 1

                horizontal_center = cropped_mask.shape[1] // 2
                vertical_center = cropped_mask.shape[0] // 2

                cv2.ellipse(cropped_mask, (horizontal_center, vertical_center),
                            (horizontal_center, vertical_center), 0, 0, 360, 3,
                            -1)

                cv2.rectangle(im_draw, cmt.tl, cmt.br, (255, 0, 0), 4)
                subject = identifier.identify(cropped_image, cropped_mask)
                cv2.putText(im_draw, subject.name, cmt.tl,
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
                            cv2.LINE_AA)

            util.draw_keypoints(cmt.tracked_keypoints, im_draw,
                                (255, 255, 255))
            util.draw_keypoints(cmt.votes[:, :2], im_draw)
            util.draw_keypoints(cmt.outliers[:, :2], im_draw, (0, 0, 255))
            cv2.imshow('main', im_draw)
            cv2.waitKey(1)

            im_gray0 = im_gray
        except IndexError:
            Database.save_db()
            exit(0)