Spaces:
Sleeping
Sleeping
Delete visualization.py
Browse files- visualization.py +0 -95
visualization.py
DELETED
|
@@ -1,95 +0,0 @@
|
|
| 1 |
-
import matplotlib.pyplot as plt
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import json
|
| 6 |
-
import os
|
| 7 |
-
from io import BytesIO
|
| 8 |
-
|
| 9 |
-
def generate_annotated_image(image, masks, threshold=0.5):
|
| 10 |
-
"""
|
| 11 |
-
Generate an annotated image with masks overlaid.
|
| 12 |
-
|
| 13 |
-
Parameters:
|
| 14 |
-
- image (PIL.Image.Image): The original image.
|
| 15 |
-
- masks (list of dict): List of masks with their respective scores.
|
| 16 |
-
- threshold (float): Minimum score to display a mask.
|
| 17 |
-
|
| 18 |
-
Returns:
|
| 19 |
-
- PIL.Image.Image: Annotated image.
|
| 20 |
-
"""
|
| 21 |
-
fig, ax = plt.subplots()
|
| 22 |
-
ax.imshow(np.array(image))
|
| 23 |
-
|
| 24 |
-
for mask in masks:
|
| 25 |
-
if mask['score'] > threshold:
|
| 26 |
-
mask_arr = mask['mask'].squeeze().astype(np.uint8)
|
| 27 |
-
ax.imshow(mask_arr, cmap='jet', alpha=0.5) # Overlay mask on image
|
| 28 |
-
|
| 29 |
-
plt.axis('off')
|
| 30 |
-
buf = BytesIO()
|
| 31 |
-
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
|
| 32 |
-
buf.seek(0)
|
| 33 |
-
annotated_image = Image.open(buf)
|
| 34 |
-
buf.close()
|
| 35 |
-
|
| 36 |
-
return annotated_image
|
| 37 |
-
|
| 38 |
-
def save_annotated_image(image, output_path):
|
| 39 |
-
"""
|
| 40 |
-
Save the annotated image to a file.
|
| 41 |
-
|
| 42 |
-
Parameters:
|
| 43 |
-
- image (PIL.Image.Image): The annotated image.
|
| 44 |
-
- output_path (str): Path where the image will be saved.
|
| 45 |
-
"""
|
| 46 |
-
image.save(output_path)
|
| 47 |
-
|
| 48 |
-
def create_summary_table(objects_data):
|
| 49 |
-
"""
|
| 50 |
-
Create a summary table from the objects data.
|
| 51 |
-
|
| 52 |
-
Parameters:
|
| 53 |
-
- objects_data (list of dict): List containing data for each object.
|
| 54 |
-
|
| 55 |
-
Returns:
|
| 56 |
-
- pandas.DataFrame: Summary table.
|
| 57 |
-
"""
|
| 58 |
-
df = pd.DataFrame(objects_data)
|
| 59 |
-
return df
|
| 60 |
-
|
| 61 |
-
def save_summary_table(df, output_path):
|
| 62 |
-
"""
|
| 63 |
-
Save the summary table to a CSV file.
|
| 64 |
-
|
| 65 |
-
Parameters:
|
| 66 |
-
- df (pandas.DataFrame): Summary table.
|
| 67 |
-
- output_path (str): Path where the table will be saved.
|
| 68 |
-
"""
|
| 69 |
-
df.to_csv(output_path, index=False)
|
| 70 |
-
|
| 71 |
-
def generate_output(image, masks, objects_data, master_id, output_dir="."):
|
| 72 |
-
"""
|
| 73 |
-
Generate and save the final output including annotated image and summary table.
|
| 74 |
-
|
| 75 |
-
Parameters:
|
| 76 |
-
- image (PIL.Image.Image): The original image.
|
| 77 |
-
- masks (list of dict): List of masks with their respective scores.
|
| 78 |
-
- objects_data (list of dict): List of data for each object.
|
| 79 |
-
- master_id (str): Unique identifier for the master image.
|
| 80 |
-
- output_dir (str): Directory to save the output files.
|
| 81 |
-
"""
|
| 82 |
-
if not os.path.exists(output_dir):
|
| 83 |
-
os.makedirs(output_dir)
|
| 84 |
-
|
| 85 |
-
# Generate annotated image
|
| 86 |
-
annotated_image = generate_annotated_image(image, masks)
|
| 87 |
-
annotated_image_path = os.path.join(output_dir, f"{master_id}_annotated.png")
|
| 88 |
-
save_annotated_image(annotated_image, annotated_image_path)
|
| 89 |
-
|
| 90 |
-
# Create and save summary table
|
| 91 |
-
summary_table = create_summary_table(objects_data)
|
| 92 |
-
summary_table_path = os.path.join(output_dir, f"{master_id}_summary.csv")
|
| 93 |
-
save_summary_table(summary_table, summary_table_path)
|
| 94 |
-
|
| 95 |
-
return annotated_image_path, summary_table_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|