CoolFace
Apppublic

sandl/private_aggregates_segmentation

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
image_utils.py57 linesDownload Raw Back to root
1import cv22from zipfile import ZipFile3import pandas as pd4import matplotlib.pyplot as plt5from PIL import Image6 7STATISTICS_COLUMNS = ['Average bond coat thickness', 'Min bond coat thickness', 'Max bond coat thickness',8                      'Average distance between cracks', 'Standard deviation of distance between cracks', 'Cracks density']9 10 11def save_to_zip(detailed_stats, aggregated_stats, plot_fig, output_images_list, file_names, cracks_detailed_list):12    aggregated_stats.to_csv("aggregated_statistics.csv", sep=';', index=False)13    detailed_stats.to_csv("detailed_statistics.csv", sep=';', index=True)14    plt.savefig("plot.png")15    for i, output_image in enumerate(output_images_list):16        print("*****************")17        if output_image is not None: 18            print(file_names[i].split('.')[0])19            image = Image.fromarray(output_image)20            filename = file_names[i].split('.')[0]21            image.save(f"{filename}_predictions.png")22            cracks_dataframe = cracks_detailed_list[i]23            cracks_dataframe.to_csv(f"cracks_details_{filename}.csv", sep=';', index=True)24    with ZipFile("outputs.zip", "w") as zipObj:25        zipObj.write("aggregated_statistics.csv")26        zipObj.write("detailed_statistics.csv")27        zipObj.write("plot.png")28        for i, output_image in enumerate(output_images_list):29            if output_image is not None:30                filename = file_names[i].split('.')[0]31                zipObj.write(f"{filename}_predictions.png")32                zipObj.write(f"cracks_details_{filename}.csv")33    print("zips output saved")34    return "outputs.zip"35 36 37def sort_contours(cnts, method="left-to-right"):38    # initialize the reverse flag and sort index39    reverse = False40    i = 041 42    # handle if we need to sort in reverse43    if method == "right-to-left" or method == "bottom-to-top":44        reverse = True45 46    # handle if we are sorting against the y-coordinate rather than47    # the x-coordinate of the bounding box48    if method == "top-to-bottom" or method == "bottom-to-top":49        i = 150 51    # construct the list of bounding boxes and sort them from top to52    # bottom53    boundingBoxes = [cv2.boundingRect(c) for c in cnts]54    (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes), key=lambda b: b[1][i], reverse=reverse))55 56    # return the list of sorted contours and bounding boxes57    return (cnts, boundingBoxes)