Spaces:
Runtime error
Runtime error
Sagar Bharadwaj commited on
Commit ·
d4675c1
1
Parent(s): ba60f57
Split island creation and adding numbers
Browse files- colorbynumber/main.py +16 -6
- colorbynumber/numbered_islands.py +80 -29
colorbynumber/main.py
CHANGED
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
| 4 |
from .config import default_config
|
| 5 |
from .simplify_image import simplify_image
|
| 6 |
from .gen_islands import GenerateIslands
|
| 7 |
-
from .numbered_islands import
|
| 8 |
|
| 9 |
class ColorByNumber:
|
| 10 |
def __init__(self, image_path,
|
|
@@ -47,13 +47,23 @@ class ColorByNumber:
|
|
| 47 |
self.island_borders_list = island_borders_list
|
| 48 |
self.centroid_coords_list = centroid_coords_list
|
| 49 |
|
| 50 |
-
|
|
|
|
| 51 |
islands = self.island_borders_list,
|
| 52 |
-
image_shape = self.image.shape,
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
-
self.numbered_islands = numbered_islands
|
| 57 |
|
| 58 |
return self.numbered_islands
|
| 59 |
|
|
|
|
| 4 |
from .config import default_config
|
| 5 |
from .simplify_image import simplify_image
|
| 6 |
from .gen_islands import GenerateIslands
|
| 7 |
+
from .numbered_islands import create_islands, add_numbers_to_image
|
| 8 |
|
| 9 |
class ColorByNumber:
|
| 10 |
def __init__(self, image_path,
|
|
|
|
| 47 |
self.island_borders_list = island_borders_list
|
| 48 |
self.centroid_coords_list = centroid_coords_list
|
| 49 |
|
| 50 |
+
# Create the islands image
|
| 51 |
+
self.islands_image = create_islands(
|
| 52 |
islands = self.island_borders_list,
|
| 53 |
+
image_shape = self.image.shape,
|
| 54 |
+
padding = self.config["border_padding"],
|
| 55 |
+
border_color = self.config["border_color"]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Add numbers to the islands image
|
| 59 |
+
self.numbered_islands = add_numbers_to_image(
|
| 60 |
+
image=self.islands_image,
|
| 61 |
+
centroid_coords_list=self.centroid_coords_list,
|
| 62 |
+
color_id_list=[color_id for color_id, _ in self.island_borders_list],
|
| 63 |
+
font_size=self.config["font_size"],
|
| 64 |
+
font_color=self.config["font_color"],
|
| 65 |
+
font_thickness=self.config["font_thickness"]
|
| 66 |
)
|
|
|
|
| 67 |
|
| 68 |
return self.numbered_islands
|
| 69 |
|
colorbynumber/numbered_islands.py
CHANGED
|
@@ -38,6 +38,63 @@ def _add_text_to_image(image, text, position, font_size, font_color, font_thickn
|
|
| 38 |
)
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def create_numbered_islands(islands, image_shape,
|
| 42 |
centroid_coords_list = None,
|
| 43 |
config = default_config,
|
|
@@ -60,33 +117,27 @@ def create_numbered_islands(islands, image_shape,
|
|
| 60 |
font_color = config["font_color"]
|
| 61 |
font_thickness = config["font_thickness"]
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
if binary:
|
| 87 |
-
# Convert numbered_islands to binary using openCV
|
| 88 |
-
numbered_islands = cv2.cvtColor(numbered_islands, cv2.COLOR_BGR2GRAY)
|
| 89 |
-
_, numbered_islands = cv2.threshold(numbered_islands, 127, 255, cv2.THRESH_BINARY)
|
| 90 |
-
return numbered_islands
|
| 91 |
-
|
| 92 |
return numbered_islands
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
|
| 41 |
+
def add_numbers_to_image(image,
|
| 42 |
+
centroid_coords_list, color_id_list,
|
| 43 |
+
font_size, font_color, font_thickness):
|
| 44 |
+
"""Add numbers to the image.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
image (np.array): Numpy image.
|
| 48 |
+
centroid_coords_list (list): A list of centroid coordinates for the islands.
|
| 49 |
+
color_id_list (list): A list of color ids.
|
| 50 |
+
Returns:
|
| 51 |
+
np.array: A new image with the numbers added.
|
| 52 |
+
"""
|
| 53 |
+
numbered_islands = image.copy()
|
| 54 |
+
for idx in range(len(centroid_coords_list)):
|
| 55 |
+
centroid = centroid_coords_list[idx]
|
| 56 |
+
color_id = color_id_list[idx]
|
| 57 |
+
if not np.isnan(centroid).any():
|
| 58 |
+
numbered_islands = _add_text_to_image(
|
| 59 |
+
image=numbered_islands,
|
| 60 |
+
text=str(color_id),
|
| 61 |
+
position=centroid,
|
| 62 |
+
font_size=font_size,
|
| 63 |
+
font_color=font_color,
|
| 64 |
+
font_thickness=font_thickness
|
| 65 |
+
)
|
| 66 |
+
return numbered_islands
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def create_islands(islands, image_shape,
|
| 70 |
+
padding, border_color, binary = False):
|
| 71 |
+
"""Create a new image with the islands numbered.
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
islands (list): A list of tuples.
|
| 75 |
+
Each tuple contains the color id and the coordinates of the island border.
|
| 76 |
+
image_shape (tuple): The shape of the original image.
|
| 77 |
+
padding (int): The padding to add to the image.
|
| 78 |
+
border_color (tuple): The color of the border.
|
| 79 |
+
binary (bool): If True, the output will be a binary image.
|
| 80 |
+
"""
|
| 81 |
+
# Create an all white image
|
| 82 |
+
width, height, channels = image_shape
|
| 83 |
+
numbered_islands = np.ones((width + padding*2, height + padding*2, channels),
|
| 84 |
+
dtype=np.uint8) * 255
|
| 85 |
+
|
| 86 |
+
for color_id, island_coordinates in islands:
|
| 87 |
+
numbered_islands[island_coordinates] = border_color
|
| 88 |
+
|
| 89 |
+
if binary:
|
| 90 |
+
# Convert numbered_islands to binary using openCV
|
| 91 |
+
numbered_islands = cv2.cvtColor(numbered_islands, cv2.COLOR_BGR2GRAY)
|
| 92 |
+
_, numbered_islands = cv2.threshold(numbered_islands, 127, 255, cv2.THRESH_BINARY)
|
| 93 |
+
return numbered_islands
|
| 94 |
+
|
| 95 |
+
return numbered_islands
|
| 96 |
+
|
| 97 |
+
|
| 98 |
def create_numbered_islands(islands, image_shape,
|
| 99 |
centroid_coords_list = None,
|
| 100 |
config = default_config,
|
|
|
|
| 117 |
font_color = config["font_color"]
|
| 118 |
font_thickness = config["font_thickness"]
|
| 119 |
|
| 120 |
+
islands_image = create_islands(
|
| 121 |
+
islands,
|
| 122 |
+
image_shape,
|
| 123 |
+
padding,
|
| 124 |
+
border_color,
|
| 125 |
+
binary
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if not show_numbers:
|
| 129 |
+
return islands_image
|
| 130 |
+
|
| 131 |
+
if not centroid_coords_list:
|
| 132 |
+
centroid_coords_list = [
|
| 133 |
+
_get_centroid(island_coordinates) for _, island_coordinates in islands
|
| 134 |
+
]
|
| 135 |
+
numbered_islands = add_numbers_to_image(
|
| 136 |
+
islands_image,
|
| 137 |
+
centroid_coords_list,
|
| 138 |
+
[color_id for color_id, _ in islands],
|
| 139 |
+
font_size,
|
| 140 |
+
font_color,
|
| 141 |
+
font_thickness
|
| 142 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
return numbered_islands
|