Ejemplo n.º 1
0
def remove(params):
    pgid = getattr(params, "pgid", None)
    dsid = getattr(params, "model_id", None)
    modelid = getattr(params, "modelid", None)

    if dsid is None and modelid is None:
        reportUsageError(
            "No dataset id or model id provided during 'remove' operation")

    if dsid is not None:
        rsp = server.datasets.update(dsid, project_group_id="")
        id = dsid
        resource = "dataset"
    else:
        rsp = server.trained_models.update(modelid, project_group_id="")
        id = modelid
        resource = "model"

    if rsp is None:
        reportApiError(
            server,
            f"Failure attempting to remove {resource} id {id} from project group id '{pgid}'"
        )
    else:
        reportSuccess(
            server,
            f"Successfully removed {resource} id {id} from project group id '{pgid}'"
        )
Ejemplo n.º 2
0
def delete(params):
    """Deletes one object labels matching the input criteria."""

    dsid = params.get("--dsid", "missing_id")
    labelid = params.get("--label_id", None)

    expected_args = {
        "--ids": "ids",
        "--tagids": "tag_ids",
        "--fileids": "file_ids",
        "--modelid": "model_id",
        "--generate_type": "generate_type",
        "--min_conf": "min_conf",
        "--max_conf": "max_conf",
        "--limit": "limit",
        "--skip": "skip"
    }
    kwargs = translate_flags(expected_args, params)

    rsp = server.object_labels.delete(dsid, labelid, None, kwargs)
    if rsp is None:
        reportApiError(
            server,
            f"Failure attempting to delete label(s) from dataset '{dsid}'")
    else:
        reportSuccess(server,
                      f"Deleted object label id(s) from dataset '{dsid}'")
Ejemplo n.º 3
0
def savelabels(params):
    """Handles the 'savelabels' operation"""

    dsid = params.get("--dsid", "missing_id")
    fileid = params.get("--fileid", "missing_id")

    file_name = params.get("--label_file")
    if file_name is not None:
        try:
            with open(file_name) as json_file:
                data = json.load(json_file)
        except Exception as e:
            print(
                f"ERROR: failed to read json data from file '{file_name}'; {e}",
                file=sys.stderr)
            return -1
    else:
        try:
            data = json.loads(params.get("<json_string>", ""))
        except Exception as e:
            print(f"ERROR: Failed to convert label input to json; {e}",
                  file=sys.stderr)
            return -1

    rsp = server.object_labels.create(dsid, fileid, data)
    if rsp is None:
        reportApiError(
            server,
            f"Failed to save labels for file {fileid} in dataset {dsid}")
    else:
        reportSuccess(
            server,
            f"Successfully created labels for file {fileid} in dataset {dsid}."
        )
Ejemplo n.º 4
0
def upload(params):
    """Handles the 'upload' operation for loading files into a dataset.

    The "<file_paths>" from 'params' is passed to the library.
    """

    dsid = params.get("--dsid", "missing_id")

    rsp = server.files.upload(dsid, params["<file_paths>"])
    if rsp is None:
        try:
            results = server.json()["resultList"]
            total = len(results)
            success = sum([1 for x in results if x["result"] == "success"])
            fail = sum([1 for x in results if x["result"] == "fail"])
        except:
            total = "?"
            success = "?"
            fail = "?"

        reportApiError(
            server,
            f"Failure uploading files to dataset {dsid}; total={total}, successes={success}, fails={fail}"
        )
    else:
        try:
            results = server.json()["resultList"]
            total = len(results)
        except:
            total = "?"
        reportSuccess(
            server, f"Successfully uploaded {total} files to dataset {dsid}")
Ejemplo n.º 5
0
def report(params):
    """Handles the 'list' operation.
    'params' flags are translated into query-parameter variable names."""

    summaryFields = None
    if params["--summary"]:
        summaryFields = ["_id", "original_file_name", "file_type"]

    dsid = params.get("--dsid", "missing_id")

    expectedArgs = {
        '--catid': 'category_id',
        '--parentid': 'parent_id',
        '--sort': 'sortby',
        '--limit': 'limit',
        '--skip': 'skip'
    }
    kwargs = translate_flags(expectedArgs, params)

    rsp = server.files.report(dsid, **kwargs)

    if rsp is None:
        reportApiError(server, "Failure attempting to list files")
    else:
        reportSuccess(server, None, summaryFields=summaryFields)
Ejemplo n.º 6
0
def predict(params):
    pgid = getattr(params, "pgid", None)
    modelid = getattr(params, "modelid", "latest")
    method = getattr(params, "method", "post")
    try:
        expectedArgs = [
            "confthre", "containHeatMap", "containRle", "containPolygon",
            "attrthre", "waitForResults", "genCaption"
        ]
        infParams = dict(
            filter(
                lambda elem:
                (elem[0] in expectedArgs) and (elem[1] is not None),
                vars(params).items()))
        # print(infParams)

        files = {'files': (params.files, open(params.files, 'rb'))}

        rsp = server.projects.predict(pgid, modelid, infParams)
        if rsp is None:
            reportApiError(
                server,
                f"Failed trying to predict with model '{modelid}' for project group {pgid}"
            )
        else:
            reportSuccess(server)
    except IOError as ioe:
        reportUsageError(
            f"IO Error accessing file {params.files}; errno={ioe.errno}; msg={str(ioe)}\n"
        )
Ejemplo n.º 7
0
def report(params):
    """Handles the 'list' operation."""

    summaryFields = None
    if params["--summary"]:
        summaryFields = ["_id", "name", "file_id"]

    dsid = params.get("--dsid", "missing_id")
    fileid = params.get("--fileid", None)

    expected_args = {"--ids": "ids",
                     "--tagids": "tag_ids",
                     "--modelid": "model_id",
                     "--gen_type": "generate_type",
                     "--min_conf": "min_conf",
                     "--max_conf": "max_conf",
                     "--limit": "limit",
                     "--skip": "skip"
                     }
    kwargs = translate_flags(expected_args, params)

    rsp = server.object_labels.report(dsid, fileid, **kwargs)

    if rsp is None:
        reportApiError(server, "Failure attempting to list tags")
    else:
        reportSuccess(server, None, summaryFields=summaryFields)
Ejemplo n.º 8
0
def create(params):
    """Handles the 'create' operation for adding an object tag to a dataset.
    """

    dsid = params.get("--dsid", "missing_id")
    fileid = params.get("--fileid", "missing_id")
    tagid = params.get("--tagid", None)

    file_name = params.get("--label_file")
    if file_name is not None:
        try:
            with open(file_name) as json_file:
                data = json.load(json_file)
        except Exception as e:
            print(f"ERROR: failed to read json data from file '{file_name}'; {e}")
            return -1
    else:
        try:
            data = json.loads(params.get("<json_string>", ""))
        except Exception as e:
            print(f"ERROR: Failed to convert label input to json; {e}")
            return -1

    logger.debug(f"dsid={dsid}, fileid={fileid}, tagid={tagid}, data={data}")
    rsp = server.object_labels.create(dsid, fileid, tagid, data)
    if rsp is None:
        reportApiError(server, f"Failed to create object tag in dataset {dsid}")
    else:
        # noinspection PyBroadException
        try:
            labelid = server.json()["id"]
        except:
            labelid = "???"
        reportSuccess(server, f"Successfully created object tag with id {labelid} in dataset {dsid}")
Ejemplo n.º 9
0
def delete(params):
    """Undeploys the model identified by the --modelid parameter."""

    modelid = params.get("--modelid", "missing_id")
    rsp = server.deployed_models.delete(modelid)
    if rsp is None:
        reportApiError(server, f"Failure attempting to undeploy model id '{modelid}'")
    else:
        reportSuccess(server, f"Undeployed model id '{modelid}'")
Ejemplo n.º 10
0
def show(params):
    """Handles the 'show' operation to show details of a single model"""

    model = params.get("--modelid", "missing_id")
    rsp = server.deployed_models.show(model)
    if rsp is None:
        reportApiError(server, f"Failure attempting to get model id '{model}'")
    else:
        reportSuccess(server)
Ejemplo n.º 11
0
def delete(params):
    pgid = getattr(params, "pgid", None)
    rsp = server.projects.delete(pgid)
    if rsp is None:
        reportApiError(
            server,
            "Failure attempting to delete project group id '{}'".format(pgid))
    else:
        reportSuccess(server, "Deleted project group id '{}'".format(pgid))
Ejemplo n.º 12
0
def show(params):
    """Handles the 'show' operation to show details of a single dataset"""

    dsid = params.get("--dsid", "missing_id")
    rsp = server.datasets.show(dsid)
    if rsp is None:
        reportApiError(server, f"Failure attempting to get dataset id '{dsid}'")
    else:
        reportSuccess(server)
Ejemplo n.º 13
0
def status(params):
    """Handles the 'status' operation to show training status messages of a dltask"""

    dltask = params.get("--taskid", "missing_id")
    rsp = server.dl_tasks.status(dltask)
    if rsp is None:
        reportApiError(server,
                       f"Failure attempting to get dltask id '{dltask}'")
    else:
        reportSuccess(server)
Ejemplo n.º 14
0
def show(params):
    """Handles the 'show' operation to show details of a single dltask"""

    dltask = params.get("--taskid", "missing_id")
    rsp = server.dl_tasks.show(dltask)
    if rsp is None:
        reportApiError(server,
                       f"Failure attempting to get dltask id '{dltask}'")
    else:
        reportSuccess(server)
Ejemplo n.º 15
0
def token(params):
    """ Handles getting an authentication token for a specific user"""

    user = params.get("--user", None)
    pw = params.get("--password", None)
    rsp = server.users.get_token(user, pw)
    if rsp is None or rsp.get("result", "fail") == "fail":
        reportApiError(server, f"Failed to get token for user '{user}'")
    else:
        reportSuccess(server, rsp["token"])
Ejemplo n.º 16
0
def delete(params):
    """Deletes one dataset identified by the --dsid parameter.

    Future work should allow a list of datasets."""

    dsid = params.get("--dsid", "missing_id")
    rsp = server.datasets.delete(dsid)
    if rsp is None:
        reportApiError(server, f"Failure attempting to delete dataset id {dsid}")
    else:
        reportSuccess(server, f"Deleted dataset id '{dsid}'")
Ejemplo n.º 17
0
def delete(params):
    """Deletes one model identified by the --modelid parameter.

    Future work should allow a list of trained-models."""

    modelid = params.get("--modelid", "missing_id")
    rsp = server.trained_models.delete(modelid)
    if rsp is None:
        reportApiError(server, f"Failure attempting to delete model id '{modelid}'")
    else:
        reportSuccess(server, f"Deleted model id '{modelid}'")
Ejemplo n.º 18
0
def export(params):
    """Handles the 'export' operation for the indicated model"""

    model = params.get("--modelid", "missing_id")
    filename = params.get("--filename", None)

    filepath = server.trained_models.export(model, filename, status_callback=None)
    if filepath is None:
        reportApiError(server, f"Failure attempting to export model id '{model}'")
    else:
        reportSuccess(server, filepath)
Ejemplo n.º 19
0
def create(params):
    expectedArgs = ["name", "description", "enforce_pwf", "auto_deploy"]
    kwargs = dict(
        filter(
            lambda elem: (elem[0] in expectedArgs) and (elem[1] is not None),
            vars(params).items()))

    rsp = server.projects.create(**kwargs)
    if rsp is None:
        reportApiError(server, "Failure while creating a project group")
    else:
        reportSuccess(server, "Successfully created project group")
Ejemplo n.º 20
0
def export(params):
    """Handles the 'export' operation."""

    dsid = params.get("--dsid", "missing_id")
    filename = params.get("--filename", None)
    raw_mode = params.get("--raw", "false")

    filepath = server.datasets.export(dsid, filename, status_callback=None, raw=raw_mode)
    if filepath is None:
        reportApiError(server, f"Failure attempting to export dataset id '{dsid}'")
    else:
        reportSuccess(server, filepath)
Ejemplo n.º 21
0
def delete(params):
    """Deletes one dltask identified by the --taskid parameter.

    Future work should allow a list of dltasks."""

    taskid = params.get("--taskid", "missing_id")
    rsp = server.dl_tasks.delete(taskid)
    if rsp is None:
        reportApiError(server,
                       f"Failure attempting to delete dltask id '{taskid}'")
    else:
        reportSuccess(server, f"Deleted dltask id '{taskid}'")
Ejemplo n.º 22
0
def download_asset(params):
    """Handles downloading the indicated asset from the indicated model"""

    model = params.get("--modelid", "missing_id")
    asset = params.get("--assettype", "unknown_asset")
    filename = params.get("--filename", None)

    rsp = server.trained_models.download_asset(model, asset, filename)
    if rsp is None:
        reportApiError(server, f"Failure attempting to asset '{asset}' for model id '{model}'")
    else:
        reportSuccess(server)
Ejemplo n.º 23
0
def getlabels(params):
    """Handles the 'getlabels' operation"""

    dsid = params.get("--dsid", "missing_id")
    fileid = params.get("--fileid", "missing_id")

    rsp = server.object_labels.getlabels(dsid, fileid)
    if rsp is None:
        reportApiError(
            server,
            f"Failed to get labels for dataset {dsid} and file {fileid}")
    else:
        reportSuccess(server, None)
Ejemplo n.º 24
0
def show(params):
    """Handles the 'show' operation to show details of a single category"""

    dsid = params.get("--ds_id", "missing_id")
    catid = params.get("--cat_id", "missing_id")

    rsp = server.categories.show(dsid, catid)
    if rsp is None:
        reportApiError(
            server,
            f"Failure attempting to get category id '{catid}' in dataset id '{dsid}'"
        )
    else:
        reportSuccess(server)
Ejemplo n.º 25
0
def change(params):
    """Handles the 'change' operation for the indicated model"""

    model = params.get("--modelid", "missing_id")
    expectedArgs = {'--pgid': 'pg_id', '--prodstatus': 'production_status'}
    kwargs = translate_flags(expectedArgs, params)

    rsp = server.trained_models.update(model, **kwargs)

    if rsp is None:
        reportApiError(server,
                       f"Failure attempting to change model id '{model}'")
    else:
        reportSuccess(server)
Ejemplo n.º 26
0
def delete(params):
    """Deletes one category identified by the --ds_id and --cat_id parameters."""

    dsid = params.get("--ds_id", "missing_id")
    catid = params.get("--cat_id", "missing_id")

    rsp = server.categories.delete(dsid, catid)
    if rsp is None:
        reportApiError(
            server,
            f"Failure attempting to delete category id '{catid}' in dataset '{dsid}'"
        )
    else:
        reportSuccess(server,
                      f"Deleted category id '{catid}' in dataset '{dsid}'")
Ejemplo n.º 27
0
def import_dataset(params):
    """Handles the 'export' operation."""

    filename = params.get("<file-path>", None)

    rsp = server.datasets.import_dataset(filename)
    if rsp is None:
        reportApiError(server, "Failure while importing a dataset.")
    else:
        try:
            dsid = server.json()["dataset_id"]
        except:
            dsid = "???"

        reportSuccess(server, f"Successfully created dataset with id {dsid}.")
Ejemplo n.º 28
0
def import_model(params):
    """Handles the 'import' operation."""

    filename = params.get("<file-path>", None)

    rsp = server.trained_models.import_model(filename)
    if rsp is None:
        reportApiError(server, "Failure while importing a model.")
    else:
        try:
            modid = server.json()["trained_model_id"]
        except:
            modid = "???"

        reportSuccess(server, f"Successfully imported trained-model with id {modid}.")
Ejemplo n.º 29
0
def deploy(params):
    pgid = getattr(params, "pgid", None)
    modelid = getattr(params, "modelid", "latest")

    rsp = server.projects.deploy(pgid, modelid=modelid)
    if rsp is None:
        reportApiError(
            server,
            f"Failed trying to deploy '{modelid}' model for project group {pgid}"
        )
    else:
        reportSuccess(
            server,
            f"Successfully deployed '{modelid}' model for project group {pgid}"
        )
Ejemplo n.º 30
0
def update(params):
    """Handles the 'change' operation for modifying a dataset.

    Expected flags in 'params' are translated to Json Field names to identify modifications to be made
    """

    dsid = params.get("--dsid", "missing_id")

    expectedArgs = {'--pgid': 'project_group_id'}
    kwargs = translate_flags(expectedArgs, params)

    rsp = server.datasets.update(dsid, **kwargs)
    if rsp is None:
        reportApiError(server, f"Failure attempting to change dataset id {dsid}")
    else:
        reportSuccess(server, f"Changed dataset id {dsid}")