Spaces:
Sleeping
Sleeping
change output format
Browse files
app.py
CHANGED
|
@@ -13,19 +13,21 @@ COLOR_NAME = ['black', 'brown', 'blue', 'gray', 'green', 'orange', 'pink', 'purp
|
|
| 13 |
|
| 14 |
def get_top_names(img):
|
| 15 |
# resize images to smaller size
|
| 16 |
-
anchor =
|
| 17 |
width = img.shape[1]
|
| 18 |
height = img.shape[0]
|
| 19 |
-
if width >
|
| 20 |
if width >= height:
|
| 21 |
dim = (np.floor(width/height*anchor).astype(int), anchor)
|
| 22 |
else:
|
| 23 |
dim = (anchor, np.floor(height/width*anchor).astype(int))
|
| 24 |
img = cv2.resize(img, dim, interpolation=cv2.INTER_LINEAR)
|
| 25 |
|
|
|
|
| 26 |
w2c = np.load('w2c11_j.npy').astype(np.float16)
|
| 27 |
_, _, name_idx_img, _ = im2c(img, w2c)
|
| 28 |
|
|
|
|
| 29 |
filtered_counts = Counter(name_idx_img[name_idx_img <= 10])
|
| 30 |
sorted_counts = sorted(filtered_counts.items(), key=lambda x: x[1], reverse=True)
|
| 31 |
top_3_values = [num for num, count in sorted_counts[:3]]
|
|
@@ -42,7 +44,7 @@ def classify_and_log(images):
|
|
| 42 |
for folder in category_folders.values():
|
| 43 |
os.makedirs(folder, exist_ok=True)
|
| 44 |
|
| 45 |
-
log_file = os.path.join(output_folder, "
|
| 46 |
|
| 47 |
results = {i: [] for i in range(11)}
|
| 48 |
|
|
@@ -60,7 +62,7 @@ def classify_and_log(images):
|
|
| 60 |
|
| 61 |
print(f"Image:{filename} -> Top 3 colors:{category}\n")
|
| 62 |
|
| 63 |
-
log.write(f"{filename} -> {category}\n")
|
| 64 |
|
| 65 |
results[cat_id[0]].append(target_path)
|
| 66 |
|
|
@@ -87,7 +89,7 @@ with gr.Blocks() as demo:
|
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column():
|
| 89 |
image_input = gr.File(
|
| 90 |
-
label="Drag
|
| 91 |
file_types=["image"],
|
| 92 |
file_count="multiple"
|
| 93 |
)
|
|
@@ -106,7 +108,7 @@ with gr.Blocks() as demo:
|
|
| 106 |
# with gr.Row():
|
| 107 |
# image_output = {str(i): gr.Gallery(label=f"{i}") for i in range(11)}
|
| 108 |
|
| 109 |
-
log_output = gr.File(label="download
|
| 110 |
|
| 111 |
classify_btn.click(
|
| 112 |
classify_and_log,
|
|
|
|
| 13 |
|
| 14 |
def get_top_names(img):
|
| 15 |
# resize images to smaller size
|
| 16 |
+
anchor = 256
|
| 17 |
width = img.shape[1]
|
| 18 |
height = img.shape[0]
|
| 19 |
+
if width > 512 or height > 512:
|
| 20 |
if width >= height:
|
| 21 |
dim = (np.floor(width/height*anchor).astype(int), anchor)
|
| 22 |
else:
|
| 23 |
dim = (anchor, np.floor(height/width*anchor).astype(int))
|
| 24 |
img = cv2.resize(img, dim, interpolation=cv2.INTER_LINEAR)
|
| 25 |
|
| 26 |
+
# obtain color names of all the pixels
|
| 27 |
w2c = np.load('w2c11_j.npy').astype(np.float16)
|
| 28 |
_, _, name_idx_img, _ = im2c(img, w2c)
|
| 29 |
|
| 30 |
+
# compute the order of each name based on the numbers of each name
|
| 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]]
|
|
|
|
| 44 |
for folder in category_folders.values():
|
| 45 |
os.makedirs(folder, exist_ok=True)
|
| 46 |
|
| 47 |
+
log_file = os.path.join(output_folder, "top3colors.txt")
|
| 48 |
|
| 49 |
results = {i: [] for i in range(11)}
|
| 50 |
|
|
|
|
| 62 |
|
| 63 |
print(f"Image:{filename} -> Top 3 colors:{category}\n")
|
| 64 |
|
| 65 |
+
log.write(f"{filename} -> 1 {category[0]}, 2 {category[1]}, 3 {category[2]}\n")
|
| 66 |
|
| 67 |
results[cat_id[0]].append(target_path)
|
| 68 |
|
|
|
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column():
|
| 91 |
image_input = gr.File(
|
| 92 |
+
label="Drag/Select more than one images",
|
| 93 |
file_types=["image"],
|
| 94 |
file_count="multiple"
|
| 95 |
)
|
|
|
|
| 108 |
# with gr.Row():
|
| 109 |
# image_output = {str(i): gr.Gallery(label=f"{i}") for i in range(11)}
|
| 110 |
|
| 111 |
+
log_output = gr.File(label="download results")
|
| 112 |
|
| 113 |
classify_btn.click(
|
| 114 |
classify_and_log,
|
naming.py
CHANGED
|
@@ -72,8 +72,3 @@ if __name__ == "__main__":
|
|
| 72 |
|
| 73 |
cv2.imwrite('colormap_j.jpg', cv2.cvtColor(color_img, cv2.COLOR_BGR2RGB))
|
| 74 |
|
| 75 |
-
# print(prob_map.sum(axis=2))
|
| 76 |
-
# print(name_idx_img)
|
| 77 |
-
# print(max_prob_img.shape)
|
| 78 |
-
# print(color_img)
|
| 79 |
-
|
|
|
|
| 72 |
|
| 73 |
cv2.imwrite('colormap_j.jpg', cv2.cvtColor(color_img, cv2.COLOR_BGR2RGB))
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|