def main():
    """
    Main function.
    """
    classifier = None
    label = None
    settings = opt.map_settings()
    key = opt.default_settings()
    window_name = "Camera %d" % (CAMERA_DEFAULT)

    # Parse command-line arguments
    try:
        short_opts = [""]
        long_opts = ["help", "classifier=", "label=", "settings="]
        opts, args = getopt.getopt(sys.argv[1:], short_opts, long_opts)
    except getopt.GetoptError as error:
        print("Invalid argument: '" + str(error) + "'\n")
        print_usage()

    if len(opts) == 0:
        print_usage()

    for o, a in opts:
        if o == "--help":
            print_usage()
        elif o == "--classifier":
            classifier = opt.validate_file(a)
        elif o == "--label":
            label = a
        elif o == "--settings":
            key = a

    if not label:
        print("\n  Label not specified!\n")
        print_usage()
    elif key not in settings.keys():
        print("\n  Settings not specified!\n")
        print_usage()

    # Setup training set, objects, and window
    configuration = config.Config(settings[key])
    recognizer = configuration.recognizer()
    width = int(recognizer["width"])
    height = int(recognizer["height"])
    training_path = sys.path[1] + "/data/faces/" + label + "/training/"
    os.makedirs(training_path, exist_ok=True)

    dwidth, dheight = misc.get_display_resolution()
    print("Display resolution: %dx%d" % (dwidth, dheight))

    detector_obj = detector.Detector(classifier, configuration)
    stream = camera.Camera(CAMERA_DEFAULT, configuration)
    print("Capture Resolution: %dx%d" % (stream.get_width(), stream.get_height()))

    cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
    cv2.moveWindow(window_name, (dwidth - stream.get_width()) // 2, 0)

    p = 0

    # Begin using the camera
    if not stream.open():
        print("Failed to open Camera", CAMERA_DEFAULT)
        exit(1)

    while True:
        retval, frame = stream.read()
        faces = detector_obj.detect(frame)

        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 2)

        cv2.putText(frame, "Photos taken: {}".format(p), (0, 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))
        cv2.putText(frame, "Press 'w' to take photo", (0, 22), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))

        cv2.imshow(window_name, frame)
        key = cv2.waitKey(1)

        if key == 27:
            cv2.destroyWindow(window_name)
            cv2.waitKey(1)
            cv2.waitKey(1)
            cv2.waitKey(1)
            cv2.waitKey(1)
            stream.release()
            break
        elif key == ord("w") and len(faces) >= 1:
            retval, frame = stream.read()  # Get frame without drawings
            (x, y, w, h) = faces[0]

            image = imgproc.preprocess(frame, width, height, x, y, w, h)

            if p < 10:
                cv2.imwrite(training_path + label + ".0" + str(p) + ".png", image)
            else:
                cv2.imwrite(training_path + label + "." + str(p) + ".png", image)

            p = p + 1

    stream.release()
Example #2
0
def main():
    """
    Main function.
    """
    classifier = None
    flags = 0
    img = None
    label = None
    settings = opt.map_settings()
    key = opt.default_settings()
    window_name = 'Camera %d' % (CAMERA_DEFAULT)

    # Parse command-line arguments
    try:
        short_opts = ['']
        long_opts = ['help', 'classifier=', 'image=', 'label=', 'settings=']
        opts, args = getopt.getopt(sys.argv[1:], short_opts, long_opts)
    except getopt.GetoptError as error:
        print('Invalid argument: \'' + str(error) + '\'\n')
        print_usage()

    if len(opts) == 0:
        print_usage()

    for o, a, in opts:
        if o == '--help':
            print_usage()
        elif o == '--classifier':
            classifier = opt.validate_file(a)
        elif o == '--image':
            img = opt.validate_file(a)
        elif o == '--label':
            label = opt.validate_recognizer(sys.path[0], a)
        elif o == '--settings':
            key = a

    if not label:
        print('\n  Label not specified!\n')
        print_usage()
    elif key not in settings.keys():
        print('\n  Settings not specified!\n')
        print_usage()

    # Setup objects and window
    configuration = config.Config(settings[key])
    dwidth, dheight = misc.get_display_resolution()
    recognizer_obj = recognizer.Recognizer(classifier, label, configuration)
    stream = camera.Camera(CAMERA_DEFAULT, configuration)
    print('Capture resolution: %dx%d' % (stream.get_width(), stream.get_height()))

    # Recognize in a still image
    if img:
        image, objects, labels, confidences = recognizer_obj.recognize_from_file(img)
        draw_face_info(image, objects, labels, confidences)
        cv2.imshow(img, image)
        cv2.waitKey(0)
        return

    cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
    cv2.moveWindow(window_name, (dwidth - stream.get_width()) // 2, 0)

    # Begin using the camera
    if not stream.open():
        print('Failed to open Camera', CAMERA_DEFAULT)
        exit(1)

    while True:
        start = time.time()
        retval, frame = stream.read()

        # Check flags
        if flags & 1:
            objects, labels, confidences = recognizer_obj.recognize(frame)
            draw_face_info(frame, objects, labels, confidences)

        end = time.time()
        fps = 1 // (end - start)

        cv2.putText(frame, 'FPS: [%d]' % (fps), (0, 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))
        cv2.imshow(window_name, frame)

        key = cv2.waitKey(1)

        if key >= (1024 * 1024):
            key = key - (1024 * 1024)

        # Determine action
        if key == 27:
            cv2.destroyWindow(window_name)
            cv2.waitKey(1)
            cv2.waitKey(1)
            cv2.waitKey(1)
            cv2.waitKey(1)
            stream.release()
            break
        elif key == ord('f'):
            flags = flags ^ 1