Update app.py
Browse files
app.py
CHANGED
|
@@ -7,10 +7,9 @@ import cv2
|
|
| 7 |
import numpy as np
|
| 8 |
import re
|
| 9 |
from collections import Counter
|
| 10 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration,
|
| 11 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 12 |
from sklearn.cluster import KMeans
|
| 13 |
-
import webcolors
|
| 14 |
from difflib import get_close_matches
|
| 15 |
|
| 16 |
# === Device setup ===
|
|
@@ -132,20 +131,70 @@ DETAIL_LABELS = [
|
|
| 132 |
]
|
| 133 |
|
| 134 |
# === Enhanced color detection ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
def get_color_name(rgb_tuple):
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
return closest_name
|
| 149 |
|
| 150 |
def extract_dominant_colors(image: Image.Image, n_colors=3):
|
| 151 |
"""Extract dominant colors using K-means clustering."""
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
import re
|
| 9 |
from collections import Counter
|
| 10 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration, BlipForQuestionAnswering
|
| 11 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 12 |
from sklearn.cluster import KMeans
|
|
|
|
| 13 |
from difflib import get_close_matches
|
| 14 |
|
| 15 |
# === Device setup ===
|
|
|
|
| 131 |
]
|
| 132 |
|
| 133 |
# === Enhanced color detection ===
|
| 134 |
+
# Comprehensive color mapping (CSS3 colors)
|
| 135 |
+
CSS3_COLORS = {
|
| 136 |
+
(240, 248, 255): 'aliceblue', (250, 235, 215): 'antiquewhite', (0, 255, 255): 'aqua',
|
| 137 |
+
(127, 255, 212): 'aquamarine', (240, 255, 255): 'azure', (245, 245, 220): 'beige',
|
| 138 |
+
(255, 228, 196): 'bisque', (0, 0, 0): 'black', (255, 235, 205): 'blanchedalmond',
|
| 139 |
+
(0, 0, 255): 'blue', (138, 43, 226): 'blueviolet', (165, 42, 42): 'brown',
|
| 140 |
+
(222, 184, 135): 'burlywood', (95, 158, 160): 'cadetblue', (127, 255, 0): 'chartreuse',
|
| 141 |
+
(210, 105, 30): 'chocolate', (255, 127, 80): 'coral', (100, 149, 237): 'cornflowerblue',
|
| 142 |
+
(255, 248, 220): 'cornsilk', (220, 20, 60): 'crimson', (0, 255, 255): 'cyan',
|
| 143 |
+
(0, 0, 139): 'darkblue', (0, 139, 139): 'darkcyan', (184, 134, 11): 'darkgoldenrod',
|
| 144 |
+
(169, 169, 169): 'darkgray', (0, 100, 0): 'darkgreen', (189, 183, 107): 'darkkhaki',
|
| 145 |
+
(139, 0, 139): 'darkmagenta', (85, 107, 47): 'darkolivegreen', (255, 140, 0): 'darkorange',
|
| 146 |
+
(153, 50, 204): 'darkorchid', (139, 0, 0): 'darkred', (233, 150, 122): 'darksalmon',
|
| 147 |
+
(143, 188, 143): 'darkseagreen', (72, 61, 139): 'darkslateblue', (47, 79, 79): 'darkslategray',
|
| 148 |
+
(0, 206, 209): 'darkturquoise', (148, 0, 211): 'darkviolet', (255, 20, 147): 'deeppink',
|
| 149 |
+
(0, 191, 255): 'deepskyblue', (105, 105, 105): 'dimgray', (30, 144, 255): 'dodgerblue',
|
| 150 |
+
(178, 34, 34): 'firebrick', (255, 250, 240): 'floralwhite', (34, 139, 34): 'forestgreen',
|
| 151 |
+
(255, 0, 255): 'fuchsia', (220, 220, 220): 'gainsboro', (248, 248, 255): 'ghostwhite',
|
| 152 |
+
(255, 215, 0): 'gold', (218, 165, 32): 'goldenrod', (128, 128, 128): 'gray',
|
| 153 |
+
(0, 128, 0): 'green', (173, 255, 47): 'greenyellow', (240, 255, 240): 'honeydew',
|
| 154 |
+
(255, 105, 180): 'hotpink', (205, 92, 92): 'indianred', (75, 0, 130): 'indigo',
|
| 155 |
+
(255, 255, 240): 'ivory', (240, 230, 140): 'khaki', (230, 230, 250): 'lavender',
|
| 156 |
+
(255, 240, 245): 'lavenderblush', (124, 252, 0): 'lawngreen', (255, 250, 205): 'lemonchiffon',
|
| 157 |
+
(173, 216, 230): 'lightblue', (240, 128, 128): 'lightcoral', (224, 255, 255): 'lightcyan',
|
| 158 |
+
(250, 250, 210): 'lightgoldenrodyellow', (211, 211, 211): 'lightgray', (144, 238, 144): 'lightgreen',
|
| 159 |
+
(255, 182, 193): 'lightpink', (255, 160, 122): 'lightsalmon', (32, 178, 170): 'lightseagreen',
|
| 160 |
+
(135, 206, 250): 'lightskyblue', (119, 136, 153): 'lightslategray', (176, 196, 222): 'lightsteelblue',
|
| 161 |
+
(255, 255, 224): 'lightyellow', (0, 255, 0): 'lime', (50, 205, 50): 'limegreen',
|
| 162 |
+
(250, 240, 230): 'linen', (255, 0, 255): 'magenta', (128, 0, 0): 'maroon',
|
| 163 |
+
(102, 205, 170): 'mediumaquamarine', (0, 0, 205): 'mediumblue', (186, 85, 211): 'mediumorchid',
|
| 164 |
+
(147, 112, 219): 'mediumpurple', (60, 179, 113): 'mediumseagreen', (123, 104, 238): 'mediumslateblue',
|
| 165 |
+
(0, 250, 154): 'mediumspringgreen', (72, 209, 204): 'mediumturquoise', (199, 21, 133): 'mediumvioletred',
|
| 166 |
+
(25, 25, 112): 'midnightblue', (245, 255, 250): 'mintcream', (255, 228, 225): 'mistyrose',
|
| 167 |
+
(255, 228, 181): 'moccasin', (255, 222, 173): 'navajowhite', (0, 0, 128): 'navy',
|
| 168 |
+
(253, 245, 230): 'oldlace', (128, 128, 0): 'olive', (107, 142, 35): 'olivedrab',
|
| 169 |
+
(255, 165, 0): 'orange', (255, 69, 0): 'orangered', (218, 112, 214): 'orchid',
|
| 170 |
+
(238, 232, 170): 'palegoldenrod', (152, 251, 152): 'palegreen', (175, 238, 238): 'paleturquoise',
|
| 171 |
+
(219, 112, 147): 'palevioletred', (255, 239, 213): 'papayawhip', (255, 218, 185): 'peachpuff',
|
| 172 |
+
(205, 133, 63): 'peru', (255, 192, 203): 'pink', (221, 160, 221): 'plum',
|
| 173 |
+
(176, 224, 230): 'powderblue', (128, 0, 128): 'purple', (255, 0, 0): 'red',
|
| 174 |
+
(188, 143, 143): 'rosybrown', (65, 105, 225): 'royalblue', (139, 69, 19): 'saddlebrown',
|
| 175 |
+
(250, 128, 114): 'salmon', (244, 164, 96): 'sandybrown', (46, 139, 87): 'seagreen',
|
| 176 |
+
(255, 245, 238): 'seashell', (160, 82, 45): 'sienna', (192, 192, 192): 'silver',
|
| 177 |
+
(135, 206, 235): 'skyblue', (106, 90, 205): 'slateblue', (112, 128, 144): 'slategray',
|
| 178 |
+
(255, 250, 250): 'snow', (0, 255, 127): 'springgreen', (70, 130, 180): 'steelblue',
|
| 179 |
+
(210, 180, 140): 'tan', (0, 128, 128): 'teal', (216, 191, 216): 'thistle',
|
| 180 |
+
(255, 99, 71): 'tomato', (64, 224, 208): 'turquoise', (238, 130, 238): 'violet',
|
| 181 |
+
(245, 222, 179): 'wheat', (255, 255, 255): 'white', (245, 245, 245): 'whitesmoke',
|
| 182 |
+
(255, 255, 0): 'yellow', (154, 205, 50): 'yellowgreen'
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
def get_color_name(rgb_tuple):
|
| 186 |
+
"""Find the closest named color to the given RGB tuple."""
|
| 187 |
+
min_distance = float('inf')
|
| 188 |
+
closest_color = 'unknown'
|
| 189 |
+
|
| 190 |
+
for color_rgb, color_name in CSS3_COLORS.items():
|
| 191 |
+
# Calculate Euclidean distance
|
| 192 |
+
distance = sum((a - b) ** 2 for a, b in zip(rgb_tuple, color_rgb)) ** 0.5
|
| 193 |
+
if distance < min_distance:
|
| 194 |
+
min_distance = distance
|
| 195 |
+
closest_color = color_name
|
| 196 |
+
|
| 197 |
+
return closest_color
|
|
|
|
| 198 |
|
| 199 |
def extract_dominant_colors(image: Image.Image, n_colors=3):
|
| 200 |
"""Extract dominant colors using K-means clustering."""
|