示例#1
0
def post_segmentation(request, model_id):
    '''
    Endpoint for API POST Segmentation Job requests.
    Enforces protobuf msg input.

    Parameters:
        model_id (str) - model_id with which the segmentation job should use.

    Returns:
        1. SegmentationTask protobuf message if "accept" header is "application/x-protobuf"
        2. JSON response otherwise
    '''

    # Tries to create a segmentation task entry in the database.
    seg_job = SegmentationJob()
    try:
        # Set model ID
        seg_job.model_id = model_id
        # Generate Segmentation ID and 'time_created' using current datetime with TZ support.
        seg_job.time_field = timezone.now()
        # Generate file on disk and point database to location
        request_data = bytes(request.body)
        # Check valid model input
        seg_pb = Model_pb2.ModelInput()
        seg_pb.ParseFromString(request_data)
        # Save model input
        file_io = io.BytesIO(request_data)
        fname = "{}.pb".format("Segmentation_{}".format(
            seg_job.segmentation_id))
        seg_job.model_input.save(fname, File(file_io))
        seg_job.save()

    except:
        seg_job.delete()
        msg = "Invalid request."
        details = "The posted model input is not valid. "+\
         "It might have empty fields that are required."
        return bad_request_helper(request, msg, details, 400)

    # Start asynchronous segmentation with state machine

    m_v = ModelVersion.objects.filter(model_version_id=model_id)[0]
    if m_v.model_type == ModelVersion.ModelVersionType.Phantom:
        start_phantom_segmentation.delay(model_id, seg_job.segmentation_id)
    elif m_v.model_type == ModelVersion.ModelVersionType.Pytorch:
        start_pytorch_segmentation_single_structure.delay(
            model_id, seg_job.segmentation_id)
    elif m_v.model_type == ModelVersion.ModelVersionType.Tensorflow:
        start_phantom_segmentation.delay(model_id, seg_job.segmentation_id)
    else:
        start_phantom_segmentation.delay(model_id, seg_job.segmentation_id)

    # Construct SegmentationTask response.
    response = seg_job.get_task_response()
    return format_and_send_response(request, response)
示例#2
0
def acquire_model_input(seg_job):
    '''
    Acquires the ModelInput protobuf message object from the corresponding
    segmentation job.

    Parameters:
        seg_job - django.db.SegmentationJob - Django model for a segmentation
            job.
    Returns:
        model_in - ModelInput.pb - Protobuf message object for the model input
    '''
    file_path = open(seg_job.model_input.path, 'rb')
    file_input = file_path.read()
    file_path.close()
    model_in = Model_pb2.ModelInput()
    model_in.ParseFromString(file_input)
    return model_in