Esempio n. 1
0
def extract_summary_images(action_parameters):
    team_uuid = action_parameters['team_uuid']
    model_uuid = action_parameters['model_uuid']

    previous_training_updated = None
    previous_eval_updated = None

    while True:
        model_entity = retrieve_model_entity(team_uuid, model_uuid)

        training_folder, training_event_file_path, training_updated = blob_storage.get_training_event_file_path(
            team_uuid, model_uuid)
        if training_event_file_path is not None and training_updated != previous_training_updated:
            __extract_summary_images_for_event_file(team_uuid, model_uuid,
                                                    training_folder,
                                                    training_event_file_path,
                                                    action_parameters)
        previous_training_updated = training_updated

        eval_folder, eval_event_file_path, eval_updated = blob_storage.get_eval_event_file_path(
            team_uuid, model_uuid)
        if eval_event_file_path is not None and eval_updated != previous_eval_updated:
            __extract_summary_images_for_event_file(team_uuid, model_uuid,
                                                    eval_folder,
                                                    eval_event_file_path,
                                                    action_parameters)
        previous_eval_updated = eval_updated

        if is_done(model_entity):
            return

        if action.remaining_timedelta(action_parameters) > timedelta(
                minutes=2):
            time.sleep(60)
        action.retrigger_if_necessary(action_parameters)
Esempio n. 2
0
def __should_stop(team_uuid, video_uuid, tracker_uuid, tracker_client_entity,
                  action_parameters):
    if (tracker_client_entity['tracking_stop_requested']
            or datetime.now(timezone.utc) -
            tracker_client_entity['update_time'] > timedelta(minutes=2)):
        storage.tracker_stopping(team_uuid, video_uuid, tracker_uuid)
        return True
    action.retrigger_if_necessary(action_parameters)
    return False
Esempio n. 3
0
def __extract_summary_images_for_event_file(team_uuid, model_uuid, folder,
                                            event_file_path,
                                            action_parameters):
    for event in summary_iterator(event_file_path):
        action.retrigger_if_necessary(action_parameters)
        for value in event.summary.value:
            if value.HasField('image'):
                blob_storage.store_event_summary_image(
                    team_uuid, model_uuid, folder, event.step, value.tag,
                    value.image.encoded_image_string)
Esempio n. 4
0
def delete_model_blobs(team_uuid, model_uuid, action_parameters):
    client = util.storage_client()
    prefix = '%s/' % __get_model_folder(team_uuid, model_uuid)
    for blob in client.list_blobs(BUCKET_BLOBS, prefix=prefix):
        __delete_blob(blob.name)
        action.retrigger_if_necessary(action_parameters)
Esempio n. 5
0
def extract_frames(action_parameters):
    team_uuid = action_parameters['team_uuid']
    video_uuid = action_parameters['video_uuid']

    # Read the video_entity from storage and store the fact that frame extraction is now/still
    # active.
    video_entity = storage.frame_extraction_active(team_uuid, video_uuid)

    # Write the video out to a temporary file.
    video_blob_name = video_entity['video_blob_name']
    video_filename = '/tmp/%s' % str(uuid.uuid4().hex)
    os.makedirs(os.path.dirname(video_filename), exist_ok=True)

    while True:
        if blob_storage.write_video_to_file(video_blob_name, video_filename):
            break
        # The video blob hasn't been uploaded yet. Wait a second and try again.
        action.retrigger_if_necessary(action_parameters)
        time.sleep(1)

    storage.frame_extraction_active(team_uuid, video_uuid)

    try:
        # Open the video file with cv2.
        vid = cv2.VideoCapture(video_filename)
        if not vid.isOpened():
            message = "Error: Unable to open video for video_uuid=%s." % video_uuid
            logging.critical(message)
            raise exceptions.HttpErrorInternalServerError(message)
        try:
            previously_extracted_frame_count = video_entity[
                'extracted_frame_count']

            # If we haven't extracted any frames yet, we need to create the video frame entities
            # and update the video entity with the width, height, fps, and frame_count.
            if previously_extracted_frame_count == 0:
                width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
                height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
                fps = vid.get(cv2.CAP_PROP_FPS)
                # Count the frames. Getting the CAP_PROP_FRAME_COUNT property is not reliable.
                # Instead, we iterate through the video using vid.grab(), which is faster than
                # vid.read().
                frame_count = 0
                while True:
                    action.retrigger_if_necessary(action_parameters)
                    success = vid.grab()
                    if not success:
                        # We've reached the end of the video. All finished counting!
                        break
                    frame_count += 1
                video_entity = storage.frame_extraction_starting(
                    team_uuid, video_uuid, width, height, fps, frame_count)

                # Back up to the beginning of the video. Setting the CAP_PROP_POS_FRAMES property
                # is not reliable. Instead, we release vid and open it again.
                vid.release()
                vid = cv2.VideoCapture(video_filename)
            else:
                # We are continuing the extraction. Skip to the next frame we need to extract.
                # Setting the CAP_PROP_POS_FRAMES property is not reliable. Instead, we skip
                # through frames using vid.grab().
                for i in range(previously_extracted_frame_count):
                    ret = vid.grab()

            frame_number = previously_extracted_frame_count

            action.retrigger_if_necessary(action_parameters)

            while True:
                success, frame = vid.read()
                if not success:
                    # We've reached the end of the video. All finished extracting frames!
                    video_entity = storage.frame_extraction_done(
                        team_uuid, video_uuid, frame_number)
                    break
                # Store the frame as a jpg image, which are smaller/faster than png.
                success, buffer = cv2.imencode('.jpg', frame)
                if success:
                    video_entity = storage.store_frame_image(
                        team_uuid, video_uuid, frame_number, 'image/jpg',
                        buffer.tostring())
                    frame_number += 1
                else:
                    logging.error('cv2.imencode() returned %s' % success)
                action.retrigger_if_necessary(action_parameters)

        finally:
            # Release the cv2 video.
            vid.release()
    finally:
        # Delete the temporary file.
        os.remove(video_filename)