Пример #1
0
    def __init__(self, module_name, settings, pipe):
        """Initialize video capture, logger, and processing plugins.

        Args:
        module_name -- TODO
        settings -- dict of settings for this object.
        pipe -- TODO

        """
        self.module_name = module_name
        self.master_settings = settings['sensor/vision/control']
        self.settings = settings[module_name]
        self.processor = FrameProcessor()
        self._com = Communicator(module_name)
        # Nagging messages from the parent come from here. The process must
        # respond quickly enough or be killed by the parent.
        self._pipe = pipe
        self._fresh_frame_available = Event()

        self._got_im, self._im = None, None
        self._init_stream()

        # Individual stream images will be processed by modules stored here.
        self._plugins = []

        # Load, instantiate, and store the modules defined in the settings file.
        for plugin_name in self.settings['plugins']:
            # Name of module and the class should be the same.
            module_obj = getattr(
                import_module('..' + plugin_name, package='plugins.subpkg'),
                plugin_name)(self.processor, self.settings)
            self._plugins += [module_obj]

        # Configure the VideoCapture object.
        self._vision_process()
Пример #2
0
 def __init__(self, envName, is_atari:bool, no_op_steps=None, agent_history_length=4):
     self.env = gym.make(envName)
     self.process_frame = FrameProcessor()
     self.state = None
     self.last_lives = 0
     self.is_atari = is_atari
     self.no_op_steps = no_op_steps or (10 if is_atari else 0)
     self.agent_history_length = agent_history_length
Пример #3
0
def main(args: typing.List[str]) -> int:
    """Analyse video file or camera for motion. If no file is entered, the first camera is used."""
    video_file = None
    config = read_config()
    frame_processor = FrameProcessor(config)
    if len(args) > 1:
        video_file = args[1]
    frame_processor.capture(video_file)
    return 0
Пример #4
0
def main(args: typing.List[str]) -> int:
    """Analyse video file or camera for motion. If no file is entered, the first camera is used."""
    video_file = None
    config = read_config()
    logger = setup_custom_logger("COLLECTOR")
    logger.info("Application started")

    parser = argparse.ArgumentParser()
    parser.add_argument("-f",
                        "--file",
                        help="Process video file",
                        action='store_true')
    parser.add_argument(
        "-l",
        "--list",
        help="Process .txt file that contains a list of video files",
        action='store_true')
    parser.add_argument("filename", type=str, nargs='?', default="")

    args = parser.parse_args()
    if not args.list:
        frame_processor = FrameProcessor(config, logger)
        if args.file:
            video_file = args.filename
        frame_processor.capture(video_file)
        return 0
    else:
        file = open(args.filename, "r")
        while True:
            next_video_file = file.readline()
            if len(next_video_file) > 0:
                next_video_file = next_video_file.rstrip('\n')
                next_video_file = next_video_file.strip()
                if len(next_video_file) > 0 and next_video_file[0] != '#':
                    frame_processor = FrameProcessor(config, logger)
                    frame_processor.capture(next_video_file)
            else:
                break

        file.close
        return 0
Пример #5
0
    def __init__(self, image_size, frame_delay, in_size, out_size, border_blur,
                 back_blur, model):

        self.processor = FrameProcessor(image_size=image_size,
                                        frame_delay=frame_delay,
                                        in_size=in_size,
                                        out_size=out_size,
                                        border_blur=border_blur,
                                        back_blur=back_blur,
                                        model=model)
        self.capture = cv2.VideoCapture(0)
        self.model = model.split('/')[-1]
Пример #6
0
def start_camera_analysis():
    print("Starting Camera Analysis")

    calibration = np.load('./calibration_1080.npz')
    frame_processor = FrameProcessor(calibration, camera_index=cam_index, debug_mode=debug)
    frame_processor.init_camera()

    while server_running:
        frame_processor.capture()
        transforms = frame_processor.proccess()
        if transforms:
            try:
                cloned_clients = clients[:]
            except:
                continue

            send_transforms(cloned_clients, transforms)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            continue

    print("Stopping Camera Analysis")
Пример #7
0
def play_game(agent, env, game_play, mode, render):
    action_list = ['stay', 'go up', 'go down']
    reward_avg = 0
    for i in range(game_play):
        total_reward = 0
        obs = env.reset()
        if render:
            env.render()
        frame_processor = FrameProcessor(obs)

        obs, reward, done, _ = env.step(
            agent.random_action() + 1)  # do random action at the first frame
        total_reward += reward
        # play one game
        while not done:
            if render:
                env.render()
                time.sleep(1 / 30)
            input_frame = frame_processor.process(obs)
            prob = agent.get_action_prob(input_frame)
            if mode == 0:
                action = np.random.choice(3, p=prob)

            elif mode == 1:
                action = np.argmax(prob)
            if render:
                print('the agent has {:.2f}% stay, {:.2f}% goes up, {:.2f}% goes down ==> it choose to {}' \
                      .format(prob[0] * 100, prob[1] * 100, prob[2] * 100, action_list[action]))
            obs, reward, done, _ = env.step(action + 1)
            total_reward += reward

        reward_avg += (total_reward - reward_avg) / (
            i + 1)  # use moving average to get reward average
        print(
            'in {}th game: the total_reward is: {}, average total_reward is: {}'
            .format(i, total_reward, reward_avg))
Пример #8
0
    else:
        detector = MovementDetector(args.min_contour_area,
                                    args.rectangle_separation,
                                    args.gray_threshold,
                                    args.gray_sma_interval, min_rect_size)

    if args.onvif_credentials is not None:
        onvif_connector = ONVIFConnector(
            urlparse(args.url).hostname, args.onvif_port,
            args.onvif_credentials[0], args.onvif_credentials[1], logger)
    else:
        onvif_connector = None

    processor = FrameProcessor(detector, recognizer, onvif_connector, logger,
                               class_colors, class_names, background_names,
                               background_boxes, args.min_class_conf,
                               args.background_overlap, args.screenshot_dir,
                               args.screenshot_quality,
                               args.raw_screenshot_dir, args.max_screen_size)

    # insert credentials into URL if required
    url = args.url.format(user=args.credentials[0], passwd=args.credentials[1]
                          ) if args.credentials is not None else args.url

    if args.mjpg:
        watch_mjpg(url, args.credentials, args.retry_interval)
    else:
        watch(url, args.retry_interval)

    logger.info('Stop watching')
Пример #9
0
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()
Пример #10
0
    agent = PG(run_name=arg.run_name,
               input_shape=[160,160],
               n_action=3,
               learning_rate=arg.learning_rate,
               save_path=arg.save_path,
               record_io=False,
               record=True,
               gpu_fraction=0.9)

    for i in range(arg.games_num):
        replay_buffer = ReplayBuffer(input_shape=[160, 160], start_size=32, max_size=10000000)
        memory = []
        total_reward = 0

        obs = env.reset()
        frame_processor = FrameProcessor(obs)
        obs, reward, done, _ = env.step(agent.random_action() + 1)  # do random action at the first frame
        total_reward += reward
        # play one game
        while not done:
            input_frame = frame_processor.process(obs)
            prob = agent.get_action_prob(input_frame)
            action = np.random.choice(3, p=prob)
            obs, reward, done, _ = env.step(action + 1)

            if reward == 0:
                replay_buffer.store_transition(input_frame, action)

            else:
                total_reward += reward
                if reward == 1:
Пример #11
0
def main():
    args = parse_args()

    FOLDER = args.output_folder
    FRAME_LIMIT = args.frame_limit
    VIDEO_FILE = args.video_file
    DRAW_BOXES = args.draw_boxes
    SCORES_FILE = args.scores_file
    DETECTION = args.detection
    GAZE_ESTIMATION = not args.no_gaze_estimation
    OUTPUT_TYPE = args.output_type
    NO_SCORES = args.no_scores

    if GAZE_ESTIMATION:
        processor = FrameProcessor()
    else:
        processor = None

    if DETECTION == "cv2":
        detector = CVFaceDetector()
    elif DETECTION == "cnn":
        detector = MTCNNFaceDetector()
    else:
        raise Exception(f"detection method {DETECTION} is invalid, expected one of [cv2, cnn]")

    df_faces = []
    if NO_SCORES:
        print(f">> Reading frames from {VIDEO_FILE}")
        frames = [
            cv2.imread(os.path.join(VIDEO_FILE, frame_file))
            for frame_file in os.listdir(VIDEO_FILE)
        ]
        h, w, _ = frames[0].shape
        df = pd.DataFrame([{
            'track_id': i // 128,
            'frame_id': i,
            'abs_frame_id': i,
            'box_id': 0,
            'x': 0,
            'y': 0,
            'x2': w,
            'y2': h,
        } for i in range(len(frames))])
    else:
        print(f">> Reading video from {VIDEO_FILE}")
        df = pd.read_csv(SCORES_FILE, header=None, index_col=None, names=[
            "track_id", "frame_id", "box_id", "x", "y", "x2", "y2"] + list(range(80)))
        df["abs_frame_id"] = df.frame_id + df.track_id - 128
        frames = collect_frames(VIDEO_FILE, FRAME_LIMIT)


    if os.path.isdir(FOLDER):
        shutil.rmtree(FOLDER)
    print(">> Matching frames")
    for i, frame in tqdm(enumerate(frames), total=len(frames)):
        new_frame = match_faces_bodies(
                frame,
                df[df.abs_frame_id == i],
                processor=processor,
                detector=detector,
                draw_boxes=DRAW_BOXES
        )
        frames[i], new_df_faces = new_frame
        df_faces += new_df_faces

    if not os.path.isdir(FOLDER):
        print(f">> Creating folder {FOLDER}")
        os.makedirs(FOLDER)
    if OUTPUT_TYPE == "frames":
        os.makedirs(os.path.join(FOLDER, "frames"))
        save_video_to_files(frames, os.path.join(FOLDER, "frames"))
        df_faces = pd.DataFrame(df_faces)

        print(">> Saving box files")
        box_ids = df_faces.box_id.unique()
        for box_id in tqdm(box_ids, total=len(box_ids)):
            with open(os.path.join(FOLDER, f"person{box_id}.txt"), "w") as f:
                for _, frame in df_faces[df_faces.box_id == box_id].iterrows():
                    bbox = str([frame.x, frame.y, frame.x2, frame.y2]).replace(" ", "").strip("[]")
                    f.write(f"frame_{frame.frame_id}.png,{bbox}\n")
    else:
        output_video_file(os.path.join(FOLDER, "video.mp4"), frames)