예제 #1
0
def export_manifest(manifest_path):
    manifest_db = manifest.Manifest()
    manifest_txt = open(manifest_path, 'w')
    manifest_txt.write("id\tfilename\tmd5\tsize\tstate\n")
    for wsi in manifest_db.get_projects():
        manifest_txt.write(wsi[1] + "\t" + wsi[2] +
                           "\tunknown\tunknown\tunknown\n")
예제 #2
0
    def freehand_annotation_get_info():

        slide_id = request.args.get('slide_id', default=1, type=int)
        annotator_id = request.args.get('annotator_id', default=1, type=int)
        annotation_project = request.args.get('project',
                                              default="None",
                                              type=str)
        slide_uuid = request.args.get('slide_uuid', default="", type=str)

        mani = manifest.Manifest()
        wsi = mani.get_project_by_id(slide_id)
        svs_file_path = original_data_root + wsi[1] + '/' + wsi[2]
        dimensions = openslide.OpenSlide(svs_file_path).dimensions
        MPP = openslide.OpenSlide(svs_file_path).properties.get("aperio.MPP")
        properties = dict(openslide.OpenSlide(svs_file_path).properties)

        w = dimensions[0]
        h = dimensions[1]
        return jsonify(
            img_width=w,
            img_height=h,
            um_per_px=MPP if MPP else 0.25,
            max_image_zoom=0,  # max_image_zoom,
            toggle_status=0,  # toggle_status
            properties=properties)
    def nuclei_annotation_v2_update_image():
        slide_id = request.args.get('slide_id', default=1, type=int)
        annotator_id = request.args.get('annotator_id', default=1, type=int)
        annotation_project = request.args.get('project',
                                              default="None",
                                              type=str)
        slide_uuid = request.args.get('slide_uuid', default="", type=str)

        v1 = request.args.get('var1', 0, type=int)  # Top-left x coordinate
        v2 = request.args.get('var2', 0, type=int)  # Top-left y coordinate
        v3 = request.args.get('var3', 0, type=int)  # Bottom-right x
        v4 = request.args.get('var4', 0, type=int)  # Bottom-right y
        v5 = request.args.get('var5', 0, type=int)  # Viewer width (pixel)
        v6 = request.args.get('var6', 0, type=int)  # Viewer height (pixel)
        v7 = request.args.get('var7', 0, type=int)  # Region ID

        region_id = v7

        region_inform = {}
        region_inform["annotator_id"] = annotator_id
        region_inform["annotation_project"] = annotation_project
        region_inform["slide_uuid"] = slide_uuid
        region_inform["region_id"] = region_id

        mani = manifest.Manifest()
        wsi = mani.get_project_by_id(slide_id)
        svs_file_path = original_data_root + wsi[1] + '/' + wsi[2]
        annotation_root_folder = nuclei_annotation_data_root + annotation_project + '/' + wsi[
            1] + '/'

        if not os.path.exists(annotation_root_folder):
            os.mkdir(annotation_root_folder)

        loc_tl_coor = (v1, v2)
        loc_viewing_size = (v3 - v1, v4 - v2)

        mask_file_name = annotation_root_folder + 'a' + str(annotator_id) + '_r' + \
                         str(region_id) + '_mask' + '.txt'
        original_pic_url = annotation_root_folder + 'r' + str(v7) + '.png'
        if not os.path.exists(mask_file_name):
            if not os.path.exists(original_pic_url):
                oslide = openslide.OpenSlide(svs_file_path)
                patch = oslide.read_region((v1, v2), 0,
                                           (v3 - v1 + 1, v4 - v2 + 1))
                patch.save(annotation_root_folder + 'r' + str(v7) + '.png')
                oslide.close()
            mask_file_name = nuclei_annotation_v2_controller.boundary_2_mask(
                region_inform)

        return jsonify(
            background_url=original_pic_url + '?a=' + str(uuid.uuid1()),
            mask_url=mask_file_name + '?a=' + str(uuid.uuid1()),
            top_left_x=loc_tl_coor[0],
            top_left_y=loc_tl_coor[1],
            viewing_size_x=loc_viewing_size[0],
            viewing_size_y=loc_viewing_size[1],
        )
예제 #4
0
def import_manifest(manifest_path):
    manifest_db = manifest.Manifest()
    manifest_txt = open(manifest_path).readlines()
    for wsi in manifest_txt:
        info = wsi.split('\t')
        if info[0] == "id":
            continue
        try:
            manifest_db.insert(slide_uuid=info[0], svs_file=info[1])
        except Exception as e:
            print(e)
    thread_controller.BackgroundThread(manifest_controller.get_table,
                                       0).start()
def get_available_slide_id():
    mani = manifest.Manifest()
    data = []
    for wsi in mani.get_projects():
        temp = {"id": wsi[0], "text": wsi[0]}
        icon_file_path = icon_root + wsi[1] + '/' + 'icon.png'
        if not os.path.exists(icon_file_path):
            # try:  # image_size
            #     svs_file_path = original_data_root + wsi[1] + '/' + wsi[2]
            #     openslide.OpenSlide(svs_file_path)
            # except:
            continue
        data.append(temp)
    return data
예제 #6
0
def export_database(database_file_folder):
    manifest_db = manifest.Manifest()
    manifest_csv = open(database_file_folder + '/manifest.txt', 'w')
    manifest_csv.write("id\tfilename\tmd5\tsize\tstate\n")
    for wsi in manifest_db.get_projects():
        for temp in wsi:
            manifest_csv.write(str(temp) + '\t')
        manifest_csv.write("\n")
    mission_db = mission.Mission()
    mission_csv = open(database_file_folder + '/predict_mask.txt', 'w')
    mission_csv.write("id\tfilename\tmd5\tsize\tstate\n")
    for wsi in mission_db.get_predict_masks():
        for temp in wsi:
            mission_csv.write(str(temp) + '\t')
        mission_csv.write("\n")
def get_dzi_path(start_no=0, end_no=0):
    mani = manifest.Manifest()
    data = []
    wsis = mani.get_projects()
    if end_no <= 0:
        end_no = end_no + len(wsis)
    for wsi in wsis[start_no:end_no]:
        temp = []
        temp.append(wsi[0])  # ID
        temp.append(wsi[1])  # UUID
        temp.append(wsi[2])  # svs_file

        svs_file_path = original_data_root + wsi[1] + '/' + wsi[2]
        temp.append("https://pk.zhanghaichuan.com/dzi_online/" +
                    svs_file_path + ".dzi")

        data.append(temp)
    return data
    def nuclei_annotation_v2_auto_predict():
        slide_id = request.args.get('slide_id', default=1, type=int)
        annotator_id = request.args.get('annotator_id', default=1, type=int)
        annotation_project = request.args.get('project',
                                              default="None",
                                              type=str)
        slide_uuid = request.args.get('slide_uuid', default="", type=str)

        v7 = request.args.get('var7', 0, type=int)  # Region ID

        region_id = v7

        region_inform = {}
        region_inform["annotator_id"] = annotator_id
        region_inform["annotation_project"] = annotation_project
        region_inform["slide_uuid"] = slide_uuid
        region_inform["region_id"] = region_id

        mani = manifest.Manifest()
        wsi = mani.get_project_by_id(slide_id)
        annotation_root_folder = nuclei_annotation_data_root + annotation_project + '/' + wsi[
            1] + '/'

        if not os.path.exists(annotation_root_folder):
            os.mkdir(annotation_root_folder)

        original_pic_url = annotation_root_folder + 'r' + str(v7) + '.png'
        print(original_pic_url)

        boundary_file_name = annotation_root_folder + 'a' + str(annotator_id) + '_r' + \
                             str(region_id) + '_boundary' + '.txt'
        annotation_file_name = annotation_root_folder + 'a' + str(annotator_id) + '_r' + \
                               str(region_id) + '_annotation' + '.txt'
        command = "/home1/zhc/.conda/envs/hovernet/bin/python3.6 " \
                  "/home1/zhc/singleInfer/demo.py " \
                  + original_pic_url + ' ' + boundary_file_name + ' ' + annotation_file_name
        print(command)
        os.system(command)
        nuclei_annotation_v2_controller.boundary_2_point(region_inform)
        nuclei_annotation_v2_controller.boundary_2_mask(region_inform)

        return jsonify({})
def make_bg(slide_id):
    manifest_db = manifest.Manifest()
    info = manifest_db.get_project_by_id(slide_id)
    original_data_folder = original_data_root + info[1] + '/'
    svs_file = original_data_folder + info[2]
    analysis_data_folder = analysis_data_root + info[1] + '/'
    if not os.path.exists(analysis_data_folder):
        os.mkdir(analysis_data_folder)
    if info[4] is None or not os.path.exists(analysis_data_folder + info[4]):
        if info[3] is None or not os.path.exists(analysis_data_folder +
                                                 info[3]):
            generate_smaller_image_from_svs_file(
                svs_file, analysis_data_folder + 'smaller_image.png')
            manifest_db.update_smaller_image_by_id(info[0],
                                                   'smaller_image.png')
            info = manifest_db.get_project_by_id(slide_id)
        smaller_image = analysis_data_folder + info[3]
        generate_background_mask_from_smaller_image(
            smaller_image, analysis_data_folder + 'background_mask.png')
        manifest_db.update_background_mask_by_id(info[0],
                                                 'background_mask.png')
    def nuclei_annotation_get_info():

        slide_id = request.args.get('slide_id', default=1, type=int)
        annotator_id = request.args.get('annotator_id', default=1, type=int)
        annotation_project = request.args.get('project',
                                              default="None",
                                              type=str)
        slide_uuid = request.args.get('slide_uuid', default="", type=str)

        mani = manifest.Manifest()
        wsi = mani.get_project_by_id(slide_id)
        svs_file_path = original_data_root + wsi[1] + '/' + wsi[2]
        dimensions = openslide.OpenSlide(svs_file_path).dimensions

        w = dimensions[0]
        h = dimensions[1]
        return jsonify(
            img_width=w,
            img_height=h,
            um_per_px=0.25,
            max_image_zoom=0,  # max_image_zoom,
            toggle_status=0  # toggle_status
        )
예제 #11
0
def clear_database():
    manifest_db = manifest.Manifest()
    manifest_db.delete_all_projects()
    mission_db = mission.Mission()
    mission_db.delete_all_predict()
def continue_slide_id():
    mani = manifest.Manifest()
    mani.continue_id()
    return
def remove_wsi_by_id(slide_id):
    mani = manifest.Manifest()
    mani.delete_project_by_id(slide_id)
def get_total_number():
    mani = manifest.Manifest()
    return len(mani.get_projects())
def get_info_by_uuid(slide_uuid):
    mani = manifest.Manifest()
    return mani.get_project_by_uuid(slide_uuid)
def add_wsi(uuid, svs_file_name):
    mani = manifest.Manifest()
    mani.insert(slide_uuid=uuid, svs_file=svs_file_name)
def get_table(start_no=0, end_no=0, make_icon_mask=1):
    mani = manifest.Manifest()
    data = []
    wsis = mani.get_projects()
    if end_no <= 0:
        end_no = end_no + len(wsis)
    print(start_no, end_no)
    for wsi in wsis[start_no:end_no]:
        print(wsi)
        temp = []
        temp.append(wsi[0])  # ID
        temp.append(
            '<button type="button" onclick="btn_click(\'remove_wsi\',' +
            str(wsi[0]) + ')">remove</button>')
        temp.append(wsi[1])  # UUID

        svs_file_path = original_data_root + wsi[1] + '/' + wsi[2]
        try:  # image_size
            icon_file_path = icon_root + wsi[1] + '/' + 'icon.png'
            if os.path.exists(icon_file_path):
                dimensions = [-1, -1]
            else:
                # raise Exception("error")
                dimensions = openslide.OpenSlide(svs_file_path).dimensions
        except Exception as e:
            print(e)
            if os.path.exists(svs_file_path):
                temp.append('<a "target="_blank">' + wsi[2] + '</a>')
                temp.append('Not SVS file')
                temp.append('Unsupported file')
                data.append(temp)
            else:
                temp.append('<a "target="_blank">' + wsi[2] + '</a>')
                temp.append('No SVS file')
                data.append(temp)
            continue

        temp.append('<a href="/slide?slide_id=' + str(wsi[0]) +
                    '"target="_blank">' + wsi[2] + '</a>')

        icon_file_path = icon_root + wsi[1] + '/' + 'icon.png'
        if not os.path.exists(icon_file_path) and make_icon_mask != 0:
            try:
                if not os.path.exists(icon_root + wsi[1] + '/'):
                    os.mkdir(icon_root + wsi[1] + '/')
                image_processing.generate_icon_image_from_svs_file(
                    svs_file_path, icon_file_path)
            except Exception as e:
                print(e)
                pass
        temp.append('<img src="' + icon_file_path + '"/>')

        temp.append(
            str(dimensions[0]).rjust(6, '_') + ' * ' +
            str(dimensions[1]).rjust(6, '_'))

        if wsi[4] is None or not os.path.exists(analysis_data_root + wsi[1] +
                                                '/' + wsi[4]):
            temp.append(
                '<button type="button" onclick="btn_click(\'make_bg_mask\',' +
                str(wsi[0]) + ')"> Make BG Mask</button>')
        else:
            temp.append(
                '<a href="/slide?slide_id=' + str(wsi[0]) + '&mask_url=' +
                wsi[4] + '"target="_blank">' + 'Background Mask ' + '</a>'
                # + '<button type="button" onclick="btn_click(\'make_bg_mask\','
                # + str(wsi[0]) + ')"> Remake</button>'
            )
        temp.append("model_selection")
        temp.append('<a href="/mission_table?slide_uuid=' + str(wsi[1]) +
                    '"target="_blank">' + 'Mission' + '</a>')

        data.append(temp)
    return data
def predict_mask_with_job_id(slide_id, model_name="0"):
    # if str(model_name) == '0':
    #     model_path = "Model/resnet_34_crd_model_59.pth"
    # elif str(model_name) == '1':
    #     model_path = "Model/resnet_34_transfer_predicted_crd_model_best.pth"
    # else:
    model_path = "models/" + model_name

    make_bg(slide_id)

    manifest_db = manifest.Manifest()
    info = manifest_db.get_project_by_id(slide_id)
    mission_db = mission.Mission()
    job_id = mission_db.insert(slide_uuid=info[1],
                               slide_id=info[0],
                               job_type=model_name,
                               total=-1)

    original_data_folder = original_data_root + info[1] + '/'
    analysis_data_folder = analysis_data_root + info[1] + '/'

    svs_file = original_data_folder + info[2]
    # if not os.path.exists(analysis_data_folder + 'patch/'):
    #     os.mkdir(analysis_data_folder + 'patch/')

    mask = cv2.imread(analysis_data_folder + info[4], cv2.IMREAD_GRAYSCALE)
    oslide = openslide.OpenSlide(svs_file)
    w, h = oslide.dimensions
    pre_result = numpy.zeros((mask.shape[0], mask.shape[1], 4),
                             dtype=numpy.uint8)
    times = mask.shape[1] / w * 2000
    mission_db.update_total_by_id(job_id=job_id, total=w // 2000 * h // 2000)

    if "subtype" not in str(model_name):
        myModule = predict_module.ResNetClassification(model_path=model_path,
                                                       num_classes=2,
                                                       batch_size=64,
                                                       num_workers=0)

        for x in range(w // 2000):
            for y in range(h // 2000):
                if available_region(mask[int(y * times):int(y * times + times),
                                         int(x * times):int(x * times +
                                                            times)]):
                    # if os.path.exists(analysis_data_folder + 'patch/' + str(x) + '_' + str(y) + '.png'):
                    #     patch = Image.open(analysis_data_folder + 'patch/' + str(x) + '_' + str(y) + '.png')
                    # else:
                    #     patch = oslide.read_region((x * 2000, y * 2000), 0, (2000, 2000))
                    #     patch.save(analysis_data_folder + 'patch/' + str(x) + '_' + str(y) + '.png')
                    patch = oslide.read_region((x * 2000, y * 2000), 0,
                                               (2000, 2000))
                    patch = numpy.array(patch.convert('RGB'))
                    result = myModule.predict(
                        numpy.resize(patch, tuple([1, 2000, 2000, 3])))
                    # result = myModule.predict(total_image)
                    # probablity_list.append((x, y, result[0]))
                    pre_result[int(y * times):int(y * times + times),
                               int(x * times):int(x * times + times),
                               numpy.argmax(result[0])] = result[
                                   0, numpy.argmax(result[0])] * 255
                mission_db.update_finished_by_id(job_id=job_id,
                                                 finished=x * h // 2000 + y +
                                                 1)

        pre_result = post_processing(pre_result)
        mask_file_name = 'mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, pre_result)
        mission_db.update_predict_mask_by_id(job_id=job_id,
                                             predict_mask=mask_file_name)

        mask_file = cv2.imread(analysis_data_folder + mask_file_name)

        original_file_name = "smaller_image.png"
        original_file = cv2.imread(analysis_data_folder + original_file_name)

        alpha = numpy.zeros(mask_file.shape)
        alpha[:, :, 0] = mask_file[:, :, 0]
        alpha[:, :, 1] = mask_file[:, :, 0]
        alpha[:, :, 2] = mask_file[:, :, 0]
        alpha = alpha.astype(float) / 128

        mask_file = mask_file.astype(float)
        original_file = original_file.astype(float)
        mask_file = cv2.multiply((1 - alpha) * 0.7, mask_file)
        original_file = cv2.multiply(0.3 + alpha * 0.7, original_file)

        out_image = original_file + mask_file
        result_file_name = 'mission' + str(job_id) + '_result.png'
        cv2.imwrite(analysis_data_folder + result_file_name, out_image)

    elif "subtype" in str(model_name) and "hybrid" in str(model_name):
        myModule = predict_module.ResNetClassification(model_path=model_path,
                                                       num_classes=4,
                                                       batch_size=64,
                                                       num_workers=0)
        for x in range(w // 2000):
            for y in range(h // 2000):
                if available_region(mask[int(y * times):int(y * times + times),
                                         int(x * times):int(x * times +
                                                            times)]):
                    patch = oslide.read_region((x * 2000, y * 2000), 0,
                                               (2000, 2000))
                    patch = numpy.array(patch.convert('RGB'))
                    result = myModule.predict(
                        numpy.resize(patch, tuple([1, 2000, 2000, 3])))
                    pre_result[int(y * times):int(y * times + times),
                               int(x * times):int(x * times + times),
                               numpy.argmax(result[0])] = result[
                                   0, numpy.argmax(result[0])] * 255
                mission_db.update_finished_by_id(job_id=job_id,
                                                 finished=x * h // 2000 + y +
                                                 1)

        sub = ["health", "ccRCC", "pRCC", "chRCC"]
        result_sub = [0, 0, 0, 0]
        for i in range(4):
            result_sub[i] = numpy.sum(pre_result[:, :, i] != 0)
        result_sub_sum = numpy.sum(result_sub)
        summary = ""
        for i in range(4):
            result_sub[i] = round(result_sub[i] / result_sub_sum * 100, 2)
            summary = summary + sub[i] + ": " + str(result_sub[i]) + "%, "

        result = numpy.zeros((pre_result.shape[0], pre_result.shape[1], 3))
        for i in range(3):
            result[:, :, i] = pre_result[:, :, i + 1]
        mask_file_name = 'result_mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, result)

        result = numpy.zeros((pre_result.shape[0], pre_result.shape[1], 3))

        i = 1
        result[:, :, 0] = pre_result[:, :, i + 1] * [15] / 255
        result[:, :, 1] = pre_result[:, :, i + 1] * [147] / 255
        result[:, :, 2] = pre_result[:, :, i + 1] * [254] / 255
        i = 0
        result[:, :, i] += pre_result[:, :, i + 1]
        i = 2
        result[:, :, i] += pre_result[:, :, i + 1]

        mask_file_name = 'mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, result)

        mission_db.update_predict_mask_by_id(job_id=job_id,
                                             predict_mask=summary)

    elif "_subtype" in str(model_name) and "hybrid" not in str(model_name):
        myModule = predict_module.ResNetClassification(model_path=model_path,
                                                       num_classes=3,
                                                       batch_size=64,
                                                       num_workers=0)

        for x in range(w // 2000):
            for y in range(h // 2000):
                if available_region(mask[int(y * times):int(y * times + times),
                                         int(x * times):int(x * times +
                                                            times)]):
                    patch = oslide.read_region((x * 2000, y * 2000), 0,
                                               (2000, 2000))
                    patch = numpy.array(patch.convert('RGB'))
                    result = myModule.predict(
                        numpy.resize(patch, tuple([1, 2000, 2000, 3])))
                    pre_result[int(y * times):int(y * times + times),
                               int(x * times):int(x * times + times),
                               numpy.argmax(result[0])] = result[
                                   0, numpy.argmax(result[0])] * 255
                mission_db.update_finished_by_id(job_id=job_id,
                                                 finished=x * h // 2000 + y +
                                                 1)

        sub = ["ccRCC", "pRCC", "chRCC"]
        result_sub = [0, 0, 0]
        for i in range(3):
            result_sub[i] = numpy.sum(pre_result[:, :, i] != 0)
        result_sub_sum = numpy.sum(result_sub)
        summary = ""
        for i in range(3):
            result_sub[i] = round(result_sub[i] / result_sub_sum * 100, 2)
            summary = summary + sub[i] + ": " + str(result_sub[i]) + "%, "

        result = numpy.zeros((pre_result.shape[0], pre_result.shape[1], 3))
        for i in range(3):
            result[:, :, i] = pre_result[:, :, i]
        mask_file_name = 'mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, result)

        mission_db.update_predict_mask_by_id(job_id=job_id,
                                             predict_mask=summary)

    elif "MixMatch-subtype" in str(model_name):
        myModule = predict_module2.ResNetClassification(model_path=model_path,
                                                        batch_size=64,
                                                        num_workers=0)

        for x in range(w // 2000):
            for y in range(h // 2000):
                if available_region(mask[int(y * times):int(y * times + times),
                                         int(x * times):int(x * times +
                                                            times)]):
                    patch = oslide.read_region((x * 2000, y * 2000), 0,
                                               (2000, 2000))
                    patch = numpy.array(patch.convert('RGB'))
                    result1, result2 = myModule.predict(
                        numpy.resize(patch, tuple([1, 2000, 2000, 3])))
                    if result1[0, 1] > 0.5:
                        pre_result[int(y * times):int(y * times + times),
                                   int(x * times):int(x * times + times),
                                   0] = result1[0, 1] * 255
                    elif result1[0, 0] > 0.5:
                        pre_result[int(y * times):int(y * times + times),
                                   int(x * times):int(x * times + times),
                                   numpy.argmax(result2[0]) +
                                   1] = result2[0,
                                                numpy.argmax(result2[0])] * 255
                mission_db.update_finished_by_id(job_id=job_id,
                                                 finished=x * h // 2000 + y +
                                                 1)

        sub = ["health", "ccRCC", "pRCC", "chRCC"]
        result_sub = [0, 0, 0, 0]
        for i in range(4):
            result_sub[i] = numpy.sum(pre_result[:, :, i] != 0)
        result_sub_sum = numpy.sum(result_sub)
        summary = ""
        for i in range(4):
            result_sub[i] = round(result_sub[i] / result_sub_sum * 100, 2)
            summary = summary + sub[i] + ": " + str(result_sub[i]) + "%, "

        result = numpy.zeros((pre_result.shape[0], pre_result.shape[1], 3))
        for i in range(3):
            result[:, :, i] = pre_result[:, :, i + 1]
        mask_file_name = 'mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, result)

        mission_db.update_predict_mask_by_id(job_id=job_id,
                                             predict_mask=summary)
def get_project_by_similar_svs_file(svs_file):
    mani = manifest.Manifest()
    result = mani.get_project_by_similar_svs_file(svs_file)
    return result
    def nuclei_annotation_update_image():
        slide_id = request.args.get('slide_id', default=1, type=int)
        annotator_id = request.args.get('annotator_id', default=1, type=int)
        annotation_project = request.args.get('project',
                                              default="None",
                                              type=str)
        slide_uuid = request.args.get('slide_uuid', default="", type=str)

        mani = manifest.Manifest()
        wsi = mani.get_project_by_id(slide_id)
        svs_file_path = original_data_root + wsi[1] + '/' + wsi[2]
        annotation_root_folder = nuclei_annotation_data_root + annotation_project + '/' + wsi[
            1] + '/'

        if not os.path.exists(annotation_root_folder):
            os.mkdir(annotation_root_folder)

        error_message = 'N/A'
        request_status = 0

        v1 = request.args.get('var1', 0, type=int)  # Top-left x coordinate
        v2 = request.args.get('var2', 0, type=int)  # Top-left y coordinate
        v3 = request.args.get('var3', 0, type=int)  # Bottom-right x
        v4 = request.args.get('var4', 0, type=int)  # Bottom-right y
        v5 = request.args.get('var5', 0, type=int)  # Viewer width (pixel)
        v6 = request.args.get('var6', 0, type=int)  # Viewer height (pixel)
        v7 = request.args.get('var7', 0, type=int)  # Region ID

        loc_tl_coor = (v1, v2)
        loc_viewing_size = (v3 - v1, v4 - v2)
        loc_viewer_size = (v5, v6)

        result_pic_url = annotation_root_folder + 'a' + str(
            annotator_id) + '_r' + str(v7) + '.png'
        if not os.path.exists(result_pic_url):
            original_pic_url = annotation_root_folder + 'r' + str(v7) + '.png'
            if not os.path.exists(original_pic_url):
                oslide = openslide.OpenSlide(svs_file_path)
                patch = oslide.read_region((v1, v2), 0,
                                           (v3 - v1 + 1, v4 - v2 + 1))
                patch.save(annotation_root_folder + 'r' + str(v7) + '.png')
                oslide.close()
            original_pic = cv2.imread(original_pic_url)

            region_image_url = annotation_root_folder + 'r' + str(v7) + '.txt'
            if not os.path.exists(region_image_url):
                # with graph.as_default():
                #     mask = model.predict(original_pic_url)
                # print(mask)
                # region_image = model.water_image(mask)
                region_image = np.zeros([512, 512], dtype=np.uint8)
                np.savetxt(region_image_url,
                           region_image,
                           fmt="%d",
                           delimiter=",")
            else:
                region_image = np.loadtxt(region_image_url,
                                          delimiter=",",
                                          dtype=int)

            annotator_data_url = annotation_root_folder + 'a' + str(
                annotator_id) + '_r' + str(v7) + '.txt'
            if not os.path.exists(annotator_data_url):
                annotator_data = np.zeros(np.max(region_image) + 1)
                print(annotator_data)
                np.savetxt(annotator_data_url,
                           annotator_data,
                           fmt="%d",
                           delimiter=",")
            else:
                annotator_data = np.loadtxt(annotator_data_url,
                                            delimiter=",",
                                            dtype=int)

            colour = [
                tuple([124, 252, 0]),
                tuple([0, 255, 255]),
                tuple([137, 43, 224]),
                tuple([255 * 0.82, 255 * 0.41, 255 * 0.12]),
                tuple([255, 0, 0]),
                tuple([0, 128, 255])
            ]
            color_scheme = [[0.49, 0.99, 0], [0, 1, 1], [0.54, 0.17, 0.88],
                            [0.82, 0.41, 0.12], [1, 0, 0], [0, 0.5, 1]]
            mask = np.zeros(original_pic.shape)
            mask[region_image == -1] = tuple([0, 0, 0])
            for i, val in enumerate(annotator_data):
                if i != 1 and val != 0:
                    # mask[region_image == i] = colour[val - 1]
                    mask[region_image ==
                         i] = (original_pic[region_image == i] * 2.7 +
                               colour[val - 1]) / 3.3
                # mask[region_image == i][0] = original_pic[region_image == i][0] *color_scheme[val - 1][0]
                # mask[region_image == i][1] = original_pic[region_image == i][1] *color_scheme[val - 1][1]
                # mask[region_image == i][2] = original_pic[region_image == i][2] *color_scheme[val - 1][2]
                else:
                    mask[region_image == i] = original_pic[
                        region_image == i]  # web.rm_file(last_url)
            mask[region_image == -1] = tuple([255, 0, 0])
            bound_size = 4
            mask[:, 0:bound_size] = tuple([255, 0, 0])
            mask[:, 511 - bound_size:511] = tuple([255, 0, 0])
            mask[0:bound_size, :] = tuple([255, 0, 0])
            mask[511 - bound_size:511, :] = tuple([255, 0, 0])

            cv2.imwrite(result_pic_url, mask)

        # Update URL configuration
        slide_url = 'static/data/nuclei_annotation_data/' + annotation_project + '/' \
                    + wsi[1] + '/a' + str(annotator_id) + '_r' + str(v7) + '.png'
        result_pic = cv2.imread(result_pic_url)
        cv2.imwrite(slide_url, result_pic)

        return jsonify(slide_url=slide_url + '?a=' + str(uuid.uuid1()),
                       top_left_x=loc_tl_coor[0],
                       top_left_y=loc_tl_coor[1],
                       viewing_size_x=loc_viewing_size[0],
                       viewing_size_y=loc_viewing_size[1],
                       exit_code=request_status,
                       error_message=error_message)
def refresh_npy():
    global annotation_project_table
    result = []
    available_slide_id = []
    for item in manifest_controller.get_available_slide_id():
        available_slide_id.append(item['id'])
    file_list = os.listdir(manifest_root)
    file_list.sort()
    mani = manifest.Manifest()
    for file in file_list:
        print("start import manifest:", file)
        temp = []
        if file[-4:] != ".txt":
            continue
        slide_table_file = open('export/' + file[:-4] + '_slide_table.txt',
                                'w')
        slide_id = []
        missing_slide_uuid = []

        project_name = file[:-4]
        temp.append(project_name)

        manifest_txt = open(manifest_root + file).readlines()
        for wsi in manifest_txt:
            info = wsi.split('\t')
            if info[0] == "id":
                continue
            try:
                info[0] = '[*] ' + info[0]
                wsi = mani.get_project_by_uuid(info[0][4:])
                if int(wsi[0]) not in available_slide_id:
                    info[0] = '[' + str(wsi[0]) + '] ' + wsi[1]
                    raise Exception
                if int(wsi[0]) in slide_id:
                    continue
                slide_table_file.write(
                    str(wsi[0]) + '\t' + str(wsi[1]) + '\t' + str(wsi[2]) +
                    '\n')
                slide_id.append(int(wsi[0]))
            except:
                missing_slide_uuid.append(info[0])
        slide_table_file.close()
        # print(missing_slide_uuid)
        # print(slide_id)

        missing_slide_id_str = ""
        slide_id_str = ""
        if not slide_id:
            # temp.append('Empty Manifest!')
            # result.append(temp)
            print(project_name, "Empty Manifest!")
            continue

        min_slide_id = min(slide_id)
        for i in range(max(slide_id) + 1):
            if i not in slide_id:
                if i >= min_slide_id:
                    missing_slide_id_str = missing_slide_id_str + str(i) + ', '
            elif i - 1 not in slide_id and i in slide_id:
                slide_id_str = slide_id_str + str(i)
                if i + 1 in slide_id:
                    slide_id_str = slide_id_str + '-'
                else:
                    slide_id_str = slide_id_str + ', '
            elif i - 1 in slide_id and i in slide_id and i + 1 not in slide_id:
                slide_id_str = slide_id_str + str(i) + ', '
        temp.append(slide_id_str)
        temp.append(missing_slide_id_str)
        temp.append('<button type="button" onclick="alert(' +
                    str(missing_slide_uuid) +
                    '.join(\'\\n\'))">see more</button>')

        annotation_project_root = nuclei_annotation_root + project_name + '/'
        if not os.path.exists(annotation_project_root):
            os.mkdir(annotation_project_root)

        temp.append("Unavailable")

        annotation_project_root = freehand_annotation_root + project_name + '/'
        if not os.path.exists(annotation_project_root):
            os.mkdir(annotation_project_root)

        temp.append("Unavailable")

        temp.append('<a href="/nuclei_annotation_v2?' + 'project=' +
                    str(project_name)  # '&slide_id=' + str(slide_id[0])
                    + '" target="_Blank">nuclei annotate </a>' + '(' +
                    '<a href="/nuclei_annotation?' + 'project=' +
                    str(project_name)  # '&slide_id=' + str(slide_id[0])
                    + '" target="_Blank">v1 </a>' + ')')
        temp.append('<a href="/freehand_annotation?' + 'project=' +
                    str(project_name)  # '&slide_id=' + str(slide_id[0])
                    + '" target="_Blank">freehand annotate </a>' + '(' +
                    '<a href="/freehand_stomache_annotation?' + 'project=' +
                    str(project_name)  # '&slide_id=' + str(slide_id[0])
                    + '" target="_Blank">stomach</a>' + ')')

        temp.append("Unavailable")
        temp.append("Unavailable")

        temp.append('<a href="' + '/static/export/' + file[:-4] + '_slide_table.txt" ' + \
                    'download="' + file[:-4] + '_slide_table.txt' + '">Available Slides </a>' +
                    '<a href="' + '/available_slide_file/' + file[:-4] + '" ' + \
                    'download="' + file[:-4] + '.txt' + '">Slide List </a>'
                    )

        result.append(temp)

    filename = manifest_root + 'table.npy'
    result = numpy.array(result)
    annotation_project_table = result.copy()
    numpy.save(filename, result)  # 保存为.npy格式

    refresh_freehand_annotation_progress()
    refresh_nuclei_annotation_progress()

    return