Skip to content

Commit aab2906

Browse files
author
Benteng Ma
committed
fixed a few issue that makes the service die.
1 parent 0fd439d commit aab2906

File tree

2 files changed

+117
-172
lines changed

2 files changed

+117
-172
lines changed

common/vision/lasr_vision_feature_extraction/src/lasr_vision_feature_extraction/__init__.py

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
from os import path
3-
import matplotlib.pyplot as plt
43
import cv2
54
import numpy as np
65
import rospkg
@@ -20,121 +19,6 @@
2019
)
2120

2221

23-
reference_colours = {
24-
"blue_very_light": np.array([240, 248, 255]), # Alice blue
25-
"blue_light": np.array([173, 216, 230]), # Light blue
26-
"blue_sky": np.array([135, 206, 235]), # Sky blue
27-
"blue_powder": np.array([176, 224, 230]), # Powder blue
28-
"blue_celeste": np.array([178, 255, 255]), # Celeste, very pale blue shade
29-
"blue_periwinkle": np.array(
30-
[204, 204, 255]
31-
), # Periwinkle, a mix of light blue and lavender
32-
"blue_cadet": np.array([95, 158, 160]), # Cadet blue, a muted blue-green
33-
"blue": np.array([0, 0, 255]), # Standard blue
34-
"blue_royal": np.array([65, 105, 225]), # Royal blue
35-
"blue_deep": np.array([0, 0, 139]), # Deep blue
36-
"blue_dark": np.array([0, 0, 128]), # Dark blue
37-
# "blue_navy": np.array([0, 0, 80]), # Navy blue
38-
"yellow_very_light": np.array([255, 255, 204]), # Very light yellow
39-
"yellow_light": np.array([255, 255, 224]), # Light yellow
40-
"yellow": np.array([255, 255, 0]), # Standard yellow
41-
"yellow_gold": np.array([255, 215, 0]), # Gold yellow
42-
"yellow_dark": np.array([204, 204, 0]), # Dark yellow
43-
"yellow_mustard": np.array([255, 219, 88]), # Mustard yellow
44-
"red_very_light": np.array([255, 204, 204]), # Very light red
45-
"red_light": np.array([255, 102, 102]), # Light red
46-
"red": np.array([255, 0, 0]), # Standard red
47-
"red_dark": np.array([139, 0, 0]), # Dark red
48-
"red_maroon": np.array([128, 0, 0]), # Maroon
49-
"orange_very_light": np.array([255, 229, 180]), # Very light orange
50-
"orange_light": np.array([255, 179, 71]), # Light orange
51-
"orange": np.array([255, 165, 0]), # Standard orange
52-
"orange_dark": np.array([255, 140, 0]), # Dark orange
53-
"orange_burnt": np.array([204, 85, 0]), # Burnt orange
54-
"black": np.array([30, 30, 30]), # Black
55-
"white": np.array([255, 255, 255]), # White
56-
"gray": np.array([160, 160, 160]), # Gray
57-
}
58-
59-
colour_group_map = {
60-
"blue_very_light": "blue",
61-
"blue_light": "blue",
62-
"blue_sky": "blue",
63-
"blue_powder": "blue",
64-
"blue_celeste": "blue",
65-
"blue_periwinkle": "blue",
66-
"blue_cadet": "blue",
67-
"blue": "blue",
68-
"blue_royal": "blue",
69-
"blue_deep": "blue",
70-
"blue_dark": "blue",
71-
"yellow_very_light": "yellow",
72-
"yellow_light": "yellow",
73-
"yellow": "yellow",
74-
"yellow_gold": "yellow",
75-
"yellow_dark": "yellow",
76-
"yellow_mustard": "yellow",
77-
"red_very_light": "red",
78-
"red_light": "red",
79-
"red": "red",
80-
"red_dark": "red",
81-
"red_maroon": "red",
82-
"orange_very_light": "orange",
83-
"orange_light": "orange",
84-
"orange": "orange",
85-
"orange_dark": "orange",
86-
"orange_burnt": "orange",
87-
"black": "black",
88-
"white": "white",
89-
"gray": "gray",
90-
}
91-
92-
possible_colours = ["blue", "yellow", "red", "orange", "black", "white", "gray"]
93-
94-
95-
def estimate_colour(rgb_array):
96-
# Calculate distances to each reference colour
97-
# distances = {colour: cie76_distance(rgb_array, ref_rgb) for colour, ref_rgb in reference_colours.items()}
98-
distances = {
99-
colour: np.linalg.norm(rgb_array - ref_rgb)
100-
for colour, ref_rgb in reference_colours.items()
101-
}
102-
103-
# Find the colour with the smallest distance
104-
estimated_colour = min(distances, key=distances.get)
105-
106-
return estimated_colour
107-
108-
109-
def split_and_sample_colours(image, mask, square_size):
110-
height, width, _ = image.shape
111-
squares_colours = {}
112-
valid_squares = set()
113-
114-
square_index = 0
115-
116-
for y in range(0, height, square_size):
117-
for x in range(0, width, square_size):
118-
square = image[y : y + square_size, x : x + square_size]
119-
mask_square = mask[y : y + square_size, x : x + square_size]
120-
121-
# Calculate the average colour
122-
average_colour = square.mean(axis=(0, 1))
123-
124-
# Save the average colour in the dictionary
125-
squares_colours[square_index] = estimate_colour(average_colour)
126-
# squares_colours[square_index] = average_colour # estimate_colour(average_colour)
127-
128-
# Check the mask condition
129-
a = np.sum(mask_square)
130-
if np.sum(mask_square) > 0.5 * square_size * square_size:
131-
valid_squares.add(square_index)
132-
133-
square_index += 1
134-
135-
return squares_colours, valid_squares
136-
137-
13822
def gaussian_blur(image, kernel_size, rep=3):
13923
"""
14024
Apply Gaussian blur to an RGB image.
@@ -154,51 +38,6 @@ def gaussian_blur(image, kernel_size, rep=3):
15438
return image
15539

15640

157-
def visualize_grids(image, squares_colours, square_size):
158-
"""
159-
Display the image with coloured grids based on average colours.
160-
161-
Parameters:
162-
image (numpy.ndarray): The original RGB image.
163-
squares_colours (dict): Dictionary with the average colours of each square.
164-
square_size (int): The size of each square.
165-
"""
166-
height, width, _ = image.shape
167-
grid_image = np.zeros((height, width, 3), dtype=np.uint8)
168-
169-
square_index = 0
170-
for y in range(0, height, square_size):
171-
for x in range(0, width, square_size):
172-
# Get the colour from the dictionary
173-
colour = np.array(reference_colours[squares_colours[square_index]])
174-
# Fill the square using numpy slicing
175-
grid_image[y : y + square_size, x : x + square_size] = colour
176-
177-
# Optionally draw a white border around the square
178-
grid_image[y : y + square_size, x : x + 1] = [255, 255, 255] # Left border
179-
grid_image[y : y + square_size, x + square_size - 1 : x + square_size] = [
180-
255,
181-
255,
182-
255,
183-
] # Right border
184-
grid_image[y : y + 1, x : x + square_size] = [255, 255, 255] # Top border
185-
grid_image[y + square_size - 1 : y + square_size, x : x + square_size] = [
186-
255,
187-
255,
188-
255,
189-
] # Bottom border
190-
191-
square_index += 1
192-
193-
plt.figure(figsize=(10, 10))
194-
plt.imshow(grid_image)
195-
plt.title("Image with Average colour Grids")
196-
plt.axis("off")
197-
plt.figure()
198-
plt.imshow(image)
199-
plt.show()
200-
201-
20241
def X2conv(in_channels, out_channels, inner_channels=None):
20342
inner_channels = out_channels // 2 if inner_channels is None else inner_channels
20443
down_conv = nn.Sequential(

common/vision/lasr_vision_feature_extraction/src/lasr_vision_feature_extraction/image_with_masks_and_attributes.py

Lines changed: 117 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,122 @@
11
import numpy as np
2-
from lasr_vision_feature_extraction import (
3-
split_and_sample_colours,
4-
colour_group_map,
5-
estimate_colour,
6-
possible_colours,
7-
)
82
from lasr_vision_feature_extraction.categories_and_attributes import (
93
CategoriesAndAttributes,
104
)
115

6+
reference_colours = {
7+
"blue_very_light": np.array([240, 248, 255]), # Alice blue
8+
"blue_light": np.array([173, 216, 230]), # Light blue
9+
"blue_sky": np.array([135, 206, 235]), # Sky blue
10+
"blue_powder": np.array([176, 224, 230]), # Powder blue
11+
"blue_celeste": np.array([178, 255, 255]), # Celeste, very pale blue shade
12+
"blue_periwinkle": np.array(
13+
[204, 204, 255]
14+
), # Periwinkle, a mix of light blue and lavender
15+
"blue_cadet": np.array([95, 158, 160]), # Cadet blue, a muted blue-green
16+
"blue": np.array([0, 0, 255]), # Standard blue
17+
"blue_royal": np.array([65, 105, 225]), # Royal blue
18+
"blue_deep": np.array([0, 0, 139]), # Deep blue
19+
"blue_dark": np.array([0, 0, 128]), # Dark blue
20+
# "blue_navy": np.array([0, 0, 80]), # Navy blue
21+
"yellow_very_light": np.array([255, 255, 204]), # Very light yellow
22+
"yellow_light": np.array([255, 255, 224]), # Light yellow
23+
"yellow": np.array([255, 255, 0]), # Standard yellow
24+
"yellow_gold": np.array([255, 215, 0]), # Gold yellow
25+
"yellow_dark": np.array([204, 204, 0]), # Dark yellow
26+
"yellow_mustard": np.array([255, 219, 88]), # Mustard yellow
27+
"red_very_light": np.array([255, 204, 204]), # Very light red
28+
"red_light": np.array([255, 102, 102]), # Light red
29+
"red": np.array([255, 0, 0]), # Standard red
30+
"red_dark": np.array([139, 0, 0]), # Dark red
31+
"red_maroon": np.array([128, 0, 0]), # Maroon
32+
"orange_very_light": np.array([255, 229, 180]), # Very light orange
33+
"orange_light": np.array([255, 179, 71]), # Light orange
34+
"orange": np.array([255, 165, 0]), # Standard orange
35+
"orange_dark": np.array([255, 140, 0]), # Dark orange
36+
"orange_burnt": np.array([204, 85, 0]), # Burnt orange
37+
"black": np.array([30, 30, 30]), # Black
38+
"white": np.array([255, 255, 255]), # White
39+
"gray": np.array([160, 160, 160]), # Gray
40+
}
41+
42+
colour_group_map = {
43+
"blue_very_light": "blue",
44+
"blue_light": "blue",
45+
"blue_sky": "blue",
46+
"blue_powder": "blue",
47+
"blue_celeste": "blue",
48+
"blue_periwinkle": "blue",
49+
"blue_cadet": "blue",
50+
"blue": "blue",
51+
"blue_royal": "blue",
52+
"blue_deep": "blue",
53+
"blue_dark": "blue",
54+
"yellow_very_light": "yellow",
55+
"yellow_light": "yellow",
56+
"yellow": "yellow",
57+
"yellow_gold": "yellow",
58+
"yellow_dark": "yellow",
59+
"yellow_mustard": "yellow",
60+
"red_very_light": "red",
61+
"red_light": "red",
62+
"red": "red",
63+
"red_dark": "red",
64+
"red_maroon": "red",
65+
"orange_very_light": "orange",
66+
"orange_light": "orange",
67+
"orange": "orange",
68+
"orange_dark": "orange",
69+
"orange_burnt": "orange",
70+
"black": "black",
71+
"white": "white",
72+
"gray": "gray",
73+
}
74+
75+
possible_colours = ["blue", "yellow", "red", "orange", "black", "white", "gray"]
76+
77+
78+
def estimate_colour(rgb_array):
79+
# Calculate distances to each reference colour
80+
# distances = {colour: cie76_distance(rgb_array, ref_rgb) for colour, ref_rgb in reference_colours.items()}
81+
distances = {
82+
colour: np.linalg.norm(rgb_array - ref_rgb)
83+
for colour, ref_rgb in reference_colours.items()
84+
}
85+
86+
# Find the colour with the smallest distance
87+
estimated_colour = min(distances, key=distances.get)
88+
89+
return estimated_colour
90+
91+
92+
def split_and_sample_colours(image, mask, square_size):
93+
height, width, _ = image.shape
94+
squares_colours = {}
95+
valid_squares = set()
96+
97+
square_index = 0
98+
99+
for y in range(0, height, square_size):
100+
for x in range(0, width, square_size):
101+
square = image[y : y + square_size, x : x + square_size]
102+
mask_square = mask[y : y + square_size, x : x + square_size]
103+
104+
# Calculate the average colour
105+
average_colour = square.mean(axis=(0, 1))
106+
107+
# Save the average colour in the dictionary
108+
squares_colours[square_index] = estimate_colour(average_colour)
109+
# squares_colours[square_index] = average_colour # estimate_colour(average_colour)
110+
111+
# Check the mask condition
112+
a = np.sum(mask_square)
113+
if np.sum(mask_square) > 0.5 * square_size * square_size:
114+
valid_squares.add(square_index)
115+
116+
square_index += 1
117+
118+
return squares_colours, valid_squares
119+
12120

13121
def _softmax(x: list[float]) -> list[float]:
14122
"""Compute softmax values for each set of scores in x without using NumPy."""
@@ -191,20 +299,18 @@ def describe(self) -> dict:
191299
max_attribute = "sleeveless dress"
192300
result["max_dress"] = max_attribute
193301

194-
# QUICK FIX that won't break the code
302+
# QUICK FIX that won't break the code but not returning dress anymore.
195303
result["dress"] = 0.0
196304

197305
# ========= colour estimation below: =========
306+
# blurred_imagge kept here so that we can quickly make it work if we need.
198307
# blurred_image = gaussian_blur(self.image, kernel_size=13, rep=3)
199308
blurred_image = self.image
200-
for cloth in ["top", "down", "outwear", "dress"]:
309+
for cloth in ["top", "outwear", "dress"]: # "down",
201310
mask = self.masks[cloth]
202-
# plt.imshow(mask)
203-
# plt.show()
204311
squares_colours, valid_squares = split_and_sample_colours(
205312
blurred_image, mask, 20
206313
)
207-
# visualize_grids(blurred_image, squares_colours, square_size=20)
208314
_squares_colours = {}
209315
for k in squares_colours.keys():
210316
if k in valid_squares:

0 commit comments

Comments
 (0)