Spaces:
Sleeping
Sleeping
add zip downloading
Browse files- __pycache__/naming.cpython-39.pyc +0 -0
- app.py +45 -16
__pycache__/naming.cpython-39.pyc
ADDED
|
Binary file (2.25 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
import cv2
|
|
|
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
from naming import im2c
|
| 8 |
from collections import Counter
|
|
@@ -31,45 +35,64 @@ def get_top_names(img):
|
|
| 31 |
filtered_counts = Counter(name_idx_img[name_idx_img <= 10])
|
| 32 |
sorted_counts = sorted(filtered_counts.items(), key=lambda x: x[1], reverse=True)
|
| 33 |
top_3_values = [num for num, count in sorted_counts[:3]]
|
|
|
|
| 34 |
top_3_colors = [COLOR_NAME[i] for i in top_3_values]
|
| 35 |
-
# print("Top 3 colors:",
|
| 36 |
-
return top_3_values, top_3_colors
|
| 37 |
|
| 38 |
|
| 39 |
def classify_and_log(images):
|
| 40 |
-
output_folder = "classified_results"
|
| 41 |
-
os.makedirs(output_folder, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
for folder in category_folders.values():
|
| 45 |
os.makedirs(folder, exist_ok=True)
|
| 46 |
|
| 47 |
-
log_file = os.path.join(
|
| 48 |
|
| 49 |
results = {i: [] for i in range(11)}
|
| 50 |
|
|
|
|
| 51 |
with open(log_file, "w") as log:
|
| 52 |
for id_img, img in enumerate(images):
|
| 53 |
filename = os.path.basename(img.name)
|
| 54 |
img_array = cv2.imread(img).astype(np.float32)
|
| 55 |
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
|
| 56 |
|
| 57 |
-
cat_id, category = get_top_names(img_array)
|
| 58 |
-
# print(category_folders[cat_id[0]], filename)
|
| 59 |
-
target_path = os.path.join(category_folders[cat_id[0]], filename)
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
print(f"Image:{filename} -> Top 3 colors:{category}\n")
|
| 64 |
|
| 65 |
-
log.write(f"{filename} -> 1 {category[0]}
|
| 66 |
|
| 67 |
results[cat_id[0]].append(target_path)
|
| 68 |
|
| 69 |
-
# output_images = [results[i] for i in range(11)]
|
| 70 |
-
# print(output_images)
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
def swap_to_gallery(images):
|
|
@@ -85,6 +108,12 @@ def remove_back_to_files():
|
|
| 85 |
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("## Image color categorization")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column():
|
|
|
|
| 1 |
+
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
import cv2
|
| 5 |
+
import zipfile
|
| 6 |
+
import uuid
|
| 7 |
+
|
| 8 |
import numpy as np
|
| 9 |
+
import gradio as gr
|
| 10 |
|
| 11 |
from naming import im2c
|
| 12 |
from collections import Counter
|
|
|
|
| 35 |
filtered_counts = Counter(name_idx_img[name_idx_img <= 10])
|
| 36 |
sorted_counts = sorted(filtered_counts.items(), key=lambda x: x[1], reverse=True)
|
| 37 |
top_3_values = [num for num, count in sorted_counts[:3]]
|
| 38 |
+
top_3_counts = [count/(dim[0]*dim[1]) for num, count in sorted_counts[:3]]
|
| 39 |
top_3_colors = [COLOR_NAME[i] for i in top_3_values]
|
| 40 |
+
# print("Top 3 colors:", top_3_counts)
|
| 41 |
+
return top_3_values, top_3_counts, top_3_colors
|
| 42 |
|
| 43 |
|
| 44 |
def classify_and_log(images):
|
| 45 |
+
# output_folder = "classified_results"
|
| 46 |
+
# os.makedirs(output_folder, exist_ok=True)
|
| 47 |
+
|
| 48 |
+
# create a temporary directory
|
| 49 |
+
session_id = str(uuid.uuid4())
|
| 50 |
+
output_dir = f"temp_{session_id}"
|
| 51 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 52 |
|
| 53 |
+
|
| 54 |
+
category_folders = {i: os.path.join(output_dir, COLOR_NAME[i]) for i in range(11)}
|
| 55 |
for folder in category_folders.values():
|
| 56 |
os.makedirs(folder, exist_ok=True)
|
| 57 |
|
| 58 |
+
log_file = os.path.join(output_dir, "top3colors.txt")
|
| 59 |
|
| 60 |
results = {i: [] for i in range(11)}
|
| 61 |
|
| 62 |
+
|
| 63 |
with open(log_file, "w") as log:
|
| 64 |
for id_img, img in enumerate(images):
|
| 65 |
filename = os.path.basename(img.name)
|
| 66 |
img_array = cv2.imread(img).astype(np.float32)
|
| 67 |
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
|
| 68 |
|
| 69 |
+
cat_id, cat_counts, category = get_top_names(img_array)
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
for i in range(3):
|
| 72 |
+
if cat_counts[i] > 0.15:
|
| 73 |
+
target_path = os.path.join(category_folders[cat_id[i]], filename)
|
| 74 |
+
cv2.imwrite(target_path, cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB))
|
| 75 |
|
| 76 |
+
# print(f"Image:{filename} -> Top 3 colors:{category}\n")
|
| 77 |
|
| 78 |
+
log.write(f"{filename} -> 1 {category[0]} {100*cat_counts[0]:.2f}%, 2 {category[1]} {100*cat_counts[1]:.2f}%, 3 {category[2]} {100*cat_counts[2]:.2f}%\n")
|
| 79 |
|
| 80 |
results[cat_id[0]].append(target_path)
|
| 81 |
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# compile all images into a zip file
|
| 84 |
+
zip_path = f"{output_dir}.zip"
|
| 85 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 86 |
+
for root, _, files in os.walk(output_dir):
|
| 87 |
+
for file in files:
|
| 88 |
+
file_path = os.path.join(root, file)
|
| 89 |
+
arcname = os.path.relpath(file_path, start=output_dir)
|
| 90 |
+
zipf.write(file_path, arcname)
|
| 91 |
+
|
| 92 |
+
# optional: clean up the output directory
|
| 93 |
+
shutil.rmtree(output_dir)
|
| 94 |
+
|
| 95 |
+
return zip_path
|
| 96 |
|
| 97 |
|
| 98 |
def swap_to_gallery(images):
|
|
|
|
| 108 |
|
| 109 |
with gr.Blocks() as demo:
|
| 110 |
gr.Markdown("## Image color categorization")
|
| 111 |
+
gr.Markdown("We categorize images into different classes based on the frequency of different colors appearing in each image.")
|
| 112 |
+
gr.Markdown("The 11 color catergories include: black, brown, blue, gray, green, orange, pink, purple, red, white and yellow.")
|
| 113 |
+
gr.Markdown("The classification is based on the color naming model from paper _Van De Weijer, Joost, et al. Learning color names for real-world applications. IEEE Transactions on Image Processing 18.7 (2009): 1512-1523._")
|
| 114 |
+
gr.Markdown("The output results are in a zip file with all the images in the correspoding folders.")
|
| 115 |
+
gr.Markdown("Note that one image can be classified into multiple categories (top 3 categories and frequency > 15%), and the top 3 categories are listed in the log file.")
|
| 116 |
+
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column():
|