コード例 #1
0
ファイル: face.py プロジェクト: ippei8jp/ov_facedetect
def main():
    # get options
    args = build_argparser().parse_args()

    # log setting
    log.basicConfig(format="[ %(levelname)s ] %(asctime)-15s %(message)s",
                    level=log.INFO if not args.verbose else log.DEBUG,
                    stream=sys.stdout)
    log.debug(str(args))

    # Pre-process
    visualizer = Visualizer(args)
    frame_processor = FrameProcessor(args)

    # display flag
    display = not args.no_show

    # frame number
    frame_number = 0

    # open input file
    input_stream = visualizer.open_input_stream(args.input)
    if input_stream is None or not input_stream.isOpened():
        # error
        log.error(f"Cannot open input stream '{args.input}'")
        raise FileNotFoundError(f"Cannot open input stream '{args.input}'")
    # output stream
    output_stream = visualizer.open_output_stream(args.output)

    # break or normal end
    break_flag = False

    # for initialize frame timer
    visualizer.update_fps()

    # main loop
    while input_stream.isOpened():
        # frame_start_time = time.time()
        # input frame
        has_frame, frame = input_stream.read()
        if not has_frame:
            # end of frame
            break

        # cropping
        frame = visualizer.crop_frame(frame)

        # Recognition process
        rois, landmarks, headposes = frame_processor.process(frame)
        """
        for idx, landmark in enumerate(landmarks) :
            print(f'INDEX {idx} ======================================')
            print(f'left_eye         : {landmark.left_eye}')
            print(f'right_eye        : {landmark.right_eye}')
            print(f'nose_tip         : {landmark.nose_tip}')
            print(f'left_lip_corner  : {landmark.left_lip_corner}')
            print(f'right_lip_corner : {landmark.right_lip_corner}')
            print('####################################################')
        """
        """
        for idx, headpose in enumerate(headposes) :
            print(f'INDEX {idx} ======================================')
            print(f'pitch         : {headpose.pitch}')
            print(f'yaw           : {headpose.yaw}')
            print(f'roll          : {headpose.roll}')
            print('####################################################')
        """

        # Result output
        visualizer.draw_detections(frame, rois, landmarks, headposes)
        visualizer.update_fps()
        visualizer.draw_status(frame, rois, frame_number)
        if args.perf_stats:
            log.info('Performance stats:')
            # log.info(pprint.pformat(frame_processor.get_performance_stats()))
            pprint.pprint(frame_processor.get_performance_stats())

        if output_stream:
            # output to file
            output_stream.write(frame)

        if display:
            visualizer.display_interactive_window(frame)
            break_flag = visualizer.should_stop_display()
            if break_flag:
                break

        frame_number += 1
        # frame_time = time.time()- frame_start_time
        # print(f'frame_time : {frame_time}')

    # Hold the window waiting for keystrokes at the last frame
    if display:  # display mode
        if not break_flag:  # not break loop
            if frame_number > 0:  # no input error
                print("Press any key to exit")
                visualizer.should_stop_display(True)

    # Release resources
    if output_stream:
        output_stream.release()
    if input_stream:
        input_stream.release()
    visualizer.terminete()