Esempio n. 1
0
    def __init__(self, project):
        self.__project_variant = project.get_variant()
        self.__project_dimensions = project.get_dimensions()
        self.__images_directory = project.get_input_image_directory()
        self.__waypoints_directory = project.get_artifacts_directory(
            "waypoints")

        self.jobs = cli.jobs.get_job_types()
        #{
        #    "trace": cli.jobs.trace.Trace(project),
        #    "imdata": cli.jobs.imdata.ImageData(project),
        #    "shapes": cli.jobs.shapes.Shapes(project),
        #    "classedge": cli.jobs.classify.ClassifyEdges(project),
        #    "jclass": cli.jobs.jclass.JunctionClassify(project),
        #    "jclasssum": cli.jobs.jclasssum.JunctionClassSummarize(project),
        #    "fastclass": cli.jobs.edgeseg.FastSegClassify(project),
        #}

        # Use this for an ordered list of the jobs in the chain
        job_keys = []
        priortized_jobs = sorted(self.jobs.values(), key=lambda x: x.priority)
        for pj in priortized_jobs:
            job_keys.append(pj.job_name)

        self.job_types = job_keys
Esempio n. 2
0
def get_waypoint_tasks(project):
    import cells.waypoints.seeding
    import cells.images

    input_image_directory = project.get_input_image_directory()
    variant = project.get_variant()
    dimensions = project.get_dimensions()

    image_data = cells.images.get_image_collection(input_image_directory,
                                                   variant, dimensions)

    counter = 0
    for image_data_row in image_data:
        if image_data_row.get("stain") == "TxRed":
            # print image_data_row
            counter += 1
            if counter > 5:
                break

    input_waypoints_directory = project.get_artifacts_directory("waypoints")

    waypoint_files = glob.glob(input_waypoints_directory + os.sep + "*")
    waypoint_tasks = cells.waypoints.seeding.get_waypoints_tasks(
        waypoint_files, image_data)

    # Calculate task status data
    waypoint_tasks_data = {}

    tasks_total = 0
    tasks_done = 0
    tasks_wip = 0
    tasks_open = 0
    tasks_progress = 0.0

    tasks_total = len(waypoint_tasks)

    for waypoint_task in waypoint_tasks:
        if waypoint_task.get("status") == "open":
            tasks_open = tasks_open + 1
        elif waypoint_task.get("status") == "in-progress":
            tasks_wip = tasks_wip + 1
        elif waypoint_task.get("status") == "complete":
            tasks_done = tasks_done + 1

    if tasks_total > 0:
        tasks_progress = round(100.0 * float(tasks_done) / tasks_total, 2)

    waypoint_tasks_data["total"] = tasks_total
    waypoint_tasks_data["done"] = tasks_done
    waypoint_tasks_data["wip"] = tasks_wip
    waypoint_tasks_data["open"] = tasks_open
    waypoint_tasks_data["progress"] = tasks_progress

    return waypoint_tasks, waypoint_tasks_data
Esempio n. 3
0
def getCachedWaypointImage(project, image_name):
    import cv2
    import cells.waypoints.seeding

    cache_root = os.path.join(app.static_folder, "tmp")
    image_cache = os.path.join(cache_root, "images")

    if not os.path.isdir(cache_root):
        os.makedirs(cache_root)
    if not os.path.isdir(image_cache):
        os.makedirs(image_cache)

    input_waypoints_directory = project.get_artifacts_directory("waypoints")

    working_file_name = image_name + ".tsv-working"
    working_file = os.path.join(input_waypoints_directory, working_file_name)

    if not os.path.isfile(working_file):
        return getCachedImage(project, image_name)

    image_file = os.path.join(project.get_input_image_directory(), image_name)
    task_image_name = "task-wp-" + image_name + ".png"

    working_temp_image_name = "temp-" + image_name
    working_temp_file = os.path.join(image_cache, working_temp_image_name)

    image = cv2.imread(image_file)

    image_waypoints = cells.waypoints.seeding.load_waypoints(working_file)

    for waypoints_row in image_waypoints:
        waypoints = waypoints_row.get("waypoints")

        for idx in range(0, len(waypoints)):
            r, c = waypoints[idx]

            r_next, c_next = waypoints[(idx + 1) % len(waypoints)]
            x, y = c, r
            x_next, y_next = c_next, r_next

            cv2.line(image, (x, y), (x_next, y_next), (244, 238, 66), 1)

    cv2.imwrite(working_temp_file, image)

    image = Image.open(working_temp_file)
    output_image = image.convert("RGB")
    output_image.save(image_cache + os.sep + task_image_name, "PNG")

    image_relative_url = "/static/tmp/images/" + task_image_name
    # Cache bust
    image_relative_url = image_relative_url + "?cachev=" + str(time.time())
    return image_relative_url
Esempio n. 4
0
def getTask(project_hash, task_hash):
    if request.method == 'POST':
        return postTask(project_hash, task_hash)
    else:
        import cells.waypoints.seeding
        import cli.project
        import cv2

        project = get_project(project_hash)
        # waypoint_tasks, waypoint_tasks_data = getWaypointTasks(project)

        project_header = {"hash": project_hash}

        task_hash_decoded = base64.urlsafe_b64decode(task_hash.encode("ascii"))
        task_type = task_hash_decoded.split("::")[0]
        task_key = task_hash_decoded.split("::")[1]

        image_name = task_key

        # Load Image Data
        image_file = os.path.join(project.get_input_image_directory(),
                                  image_name)
        input_image = cv2.imread(image_file)
        height, width, channels = input_image.shape

        input_waypoints_directory = project.get_artifacts_directory(
            "waypoints")
        working_file_name = image_name + ".tsv-working"
        working_file = os.path.join(input_waypoints_directory,
                                    working_file_name)
        completed_file_name = image_name + ".tsv"
        completed_file = os.path.join(input_waypoints_directory,
                                      completed_file_name)

        task_status = "open"

        if os.path.isfile(working_file):
            task_status = "in-progress"
        elif os.path.isfile(completed_file):
            task_status = "complete"

        # Load waypoints
        if task_status == "complete":
            image_waypoints = cells.waypoints.seeding.load_waypoints(
                completed_file)
        elif task_status == "in-progress":
            image_waypoints = cells.waypoints.seeding.load_waypoints(
                working_file)
        else:
            image_waypoints = []

        row = 1
        for waypoints_row in image_waypoints:
            waypoints_row["row"] = row
            row += 1

        # Get png versions of images
        working_image_wp_url = getCachedWaypointImage_v2(
            input_image, image_waypoints, image_name)
        working_image_url = getCachedImage(project, image_name)

        view_type_parameter = request.args.get('vt')

        if view_type_parameter == "review":
            view_type = "review"
        else:
            view_type = "waypoints"

        task_data = {
            "hash": task_hash,
            "status": task_status,
            "image_name": task_key,
            "image_width": height,
            "image_height": width,
            "image_wp_url": working_image_wp_url,
            "image_url": working_image_url
        }

        return render_template('tasks/task.html',
                               project=project_header,
                               task=task_data,
                               image_url=working_image_url,
                               image_waypoints=image_waypoints,
                               view_type=view_type)
Esempio n. 5
0
def postTask(project_hash, task_hash):
    import cells.waypoints.seeding
    import cells.images

    if request.form["task_action"] == "save":
        project = get_project(project_hash)

        input_waypoints_directory = project.get_artifacts_directory(
            "waypoints")

        task_hash_decoded = base64.urlsafe_b64decode(task_hash.encode("ascii"))
        task_type = task_hash_decoded.split("::")[0]
        image_name = task_hash_decoded.split("::")[1]

        working_file_name = image_name + ".tsv-working"
        working_file = os.path.join(input_waypoints_directory,
                                    working_file_name)

        waypoints = []
        waypoints_raw = json.loads(request.form["waypoints"])

        for point in waypoints_raw:
            waypoints.append([point.get("y"), point.get("x")])

        cells.waypoints.seeding.append_waypoints(working_file,
                                                 waypoints,
                                                 created_from=image_name)
    elif request.form["task_action"] == "done":
        project = get_project(project_hash)

        input_waypoints_directory = project.get_artifacts_directory(
            "waypoints")

        task_hash_decoded = base64.urlsafe_b64decode(task_hash.encode("ascii"))
        task_type = task_hash_decoded.split("::")[0]
        image_name = task_hash_decoded.split("::")[1]

        working_file_name = image_name + ".tsv-working"
        working_file = os.path.join(input_waypoints_directory,
                                    working_file_name)

        finished_file = working_file.replace(".tsv-working", ".tsv")

        if not os.path.isfile(working_file):
            raise Exception("Tried to save a working file that didn't exist.")

        if os.path.isfile(finished_file):
            raise Exception("Tried to overwrite a finished waypoints file.")

        shutil.move(working_file, finished_file)
    elif request.form["task_action"] == "reopen":
        project = get_project(project_hash)

        input_waypoints_directory = project.get_artifacts_directory(
            "waypoints")

        task_hash_decoded = base64.urlsafe_b64decode(task_hash.encode("ascii"))
        task_type = task_hash_decoded.split("::")[0]
        image_name = task_hash_decoded.split("::")[1]

        working_file_name = image_name + ".tsv-working"
        working_file = os.path.join(input_waypoints_directory,
                                    working_file_name)

        finished_file = working_file.replace(".tsv-working", ".tsv")

        if not os.path.isfile(finished_file):
            raise Exception("Tried to save a finished file that didn't exist.")

        if os.path.isfile(working_file):
            raise Exception(
                "Tried to overwrite an existing working waypoints file.")

        shutil.move(finished_file, working_file)
    elif request.form["task_action"] == "delete":
        project = get_project(project_hash)

        input_waypoints_directory = project.get_artifacts_directory(
            "waypoints")

        task_hash_decoded = base64.urlsafe_b64decode(task_hash.encode("ascii"))
        task_type = task_hash_decoded.split("::")[0]
        image_name = task_hash_decoded.split("::")[1]

        working_file_name = image_name + ".tsv-working"
        working_file = os.path.join(input_waypoints_directory,
                                    working_file_name)

        wp_row_raw = request.form["wp_row"]

        if wp_row_raw is not None:
            wp_row = int(wp_row_raw)

            if wp_row > 0:
                cells.waypoints.seeding.delete_waypoints(working_file, wp_row)

    return redirect("/projects/" + project_hash + "/tasks/" + task_hash)