Spaces:
Running
Running
GitHub Actions commited on
Commit ·
e22760d
1
Parent(s): 0241c3f
Sync from GitHub Actions
Browse files
autocatalog/data/preprocessing.py
CHANGED
|
@@ -7,54 +7,72 @@ COLOR_FEATURE_DIM = 37
|
|
| 7 |
def extract_color_features(image, image_size=COLOR_IMAGE_SIZE):
|
| 8 |
image = image.convert("RGB").resize((image_size, image_size))
|
| 9 |
margin = int(image_size * 0.10)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
rgb = np.asarray(image, dtype=np.float32) / 255.0
|
| 16 |
hsv = np.asarray(image.convert("HSV"), dtype=np.float32) / 255.0
|
| 17 |
-
|
| 18 |
-
rgb_flat = rgb.
|
| 19 |
-
hsv_flat = hsv.
|
| 20 |
-
|
| 21 |
saturation = hsv_flat[:, 1]
|
| 22 |
value = hsv_flat[:, 2]
|
| 23 |
foreground_mask = (saturation > 0.08) | (value < 0.92)
|
| 24 |
-
|
| 25 |
if foreground_mask.sum() < 256:
|
| 26 |
foreground_mask = np.ones(len(hsv_flat), dtype=bool)
|
| 27 |
-
|
| 28 |
selected_rgb = rgb_flat[foreground_mask]
|
| 29 |
selected_hsv = hsv_flat[foreground_mask]
|
| 30 |
-
|
| 31 |
-
hue_hist, _ = np.histogram(
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
hue_hist = hue_hist.astype(np.float32)
|
| 38 |
saturation_hist = saturation_hist.astype(np.float32)
|
| 39 |
value_hist = value_hist.astype(np.float32)
|
| 40 |
-
|
| 41 |
hue_hist /= max(hue_hist.sum(), 1.0)
|
| 42 |
saturation_hist /= max(saturation_hist.sum(), 1.0)
|
| 43 |
value_hist /= max(value_hist.sum(), 1.0)
|
| 44 |
-
|
| 45 |
rgb_mean = selected_rgb.mean(axis=0).astype(np.float32)
|
| 46 |
rgb_std = selected_rgb.std(axis=0).astype(np.float32)
|
| 47 |
rgb_median = np.median(selected_rgb, axis=0).astype(np.float32)
|
| 48 |
-
|
| 49 |
-
features = np.concatenate(
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
if features.shape[0] != COLOR_FEATURE_DIM:
|
| 59 |
raise ValueError(
|
| 60 |
f"Expected {COLOR_FEATURE_DIM} color features, "
|
|
|
|
| 7 |
def extract_color_features(image, image_size=COLOR_IMAGE_SIZE):
|
| 8 |
image = image.convert("RGB").resize((image_size, image_size))
|
| 9 |
margin = int(image_size * 0.10)
|
| 10 |
+
image = image.crop(
|
| 11 |
+
(
|
| 12 |
+
margin,
|
| 13 |
+
margin,
|
| 14 |
+
image_size - margin,
|
| 15 |
+
image_size - margin,
|
| 16 |
+
)
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
rgb = np.asarray(image, dtype=np.float32) / 255.0
|
| 20 |
hsv = np.asarray(image.convert("HSV"), dtype=np.float32) / 255.0
|
| 21 |
+
|
| 22 |
+
rgb_flat = rgb.reshape(-1, 3)
|
| 23 |
+
hsv_flat = hsv.reshape(-1, 3)
|
| 24 |
+
|
| 25 |
saturation = hsv_flat[:, 1]
|
| 26 |
value = hsv_flat[:, 2]
|
| 27 |
foreground_mask = (saturation > 0.08) | (value < 0.92)
|
| 28 |
+
|
| 29 |
if foreground_mask.sum() < 256:
|
| 30 |
foreground_mask = np.ones(len(hsv_flat), dtype=bool)
|
| 31 |
+
|
| 32 |
selected_rgb = rgb_flat[foreground_mask]
|
| 33 |
selected_hsv = hsv_flat[foreground_mask]
|
| 34 |
+
|
| 35 |
+
hue_hist, _ = np.histogram(
|
| 36 |
+
selected_hsv[:, 0],
|
| 37 |
+
bins=12,
|
| 38 |
+
range=(0.0, 1.0),
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
saturation_hist, _ = np.histogram(
|
| 42 |
+
selected_hsv[:, 1],
|
| 43 |
+
bins=8,
|
| 44 |
+
range=(0.0, 1.0),
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
value_hist, _ = np.histogram(
|
| 48 |
+
selected_hsv[:, 2],
|
| 49 |
+
bins=8,
|
| 50 |
+
range=(0.0, 1.0),
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
hue_hist = hue_hist.astype(np.float32)
|
| 54 |
saturation_hist = saturation_hist.astype(np.float32)
|
| 55 |
value_hist = value_hist.astype(np.float32)
|
| 56 |
+
|
| 57 |
hue_hist /= max(hue_hist.sum(), 1.0)
|
| 58 |
saturation_hist /= max(saturation_hist.sum(), 1.0)
|
| 59 |
value_hist /= max(value_hist.sum(), 1.0)
|
| 60 |
+
|
| 61 |
rgb_mean = selected_rgb.mean(axis=0).astype(np.float32)
|
| 62 |
rgb_std = selected_rgb.std(axis=0).astype(np.float32)
|
| 63 |
rgb_median = np.median(selected_rgb, axis=0).astype(np.float32)
|
| 64 |
+
|
| 65 |
+
features = np.concatenate(
|
| 66 |
+
[
|
| 67 |
+
hue_hist,
|
| 68 |
+
saturation_hist,
|
| 69 |
+
value_hist,
|
| 70 |
+
rgb_mean,
|
| 71 |
+
rgb_std,
|
| 72 |
+
rgb_median,
|
| 73 |
+
]
|
| 74 |
+
).astype(np.float32)
|
| 75 |
+
|
| 76 |
if features.shape[0] != COLOR_FEATURE_DIM:
|
| 77 |
raise ValueError(
|
| 78 |
f"Expected {COLOR_FEATURE_DIM} color features, "
|