Spaces:
Sleeping
Sleeping
Upload src/dot2dot/generator.py with huggingface_hub
Browse files- src/dot2dot/generator.py +15 -13
src/dot2dot/generator.py
CHANGED
|
@@ -8,10 +8,13 @@ from .config import AgePreset
|
|
| 8 |
from .image_utils import (
|
| 9 |
contour_length,
|
| 10 |
dedupe_points,
|
|
|
|
| 11 |
find_relevant_contours,
|
|
|
|
| 12 |
pil_to_cv_gray,
|
| 13 |
preprocess_binary,
|
| 14 |
sample_contour,
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
|
|
@@ -19,34 +22,33 @@ from .image_utils import (
|
|
| 19 |
class DotLayout:
|
| 20 |
points: list[tuple[int, int]]
|
| 21 |
source_size: tuple[int, int]
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
class DotLayoutGenerator:
|
| 25 |
def generate(self, image: Image.Image, preset: AgePreset) -> DotLayout:
|
| 26 |
gray = pil_to_cv_gray(image)
|
| 27 |
binary = preprocess_binary(gray)
|
| 28 |
-
contours =
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if not contours:
|
| 31 |
raise ValueError("Nao foi possivel identificar contornos validos na imagem.")
|
| 32 |
|
| 33 |
-
|
| 34 |
total_target = max(
|
| 35 |
preset.min_dots,
|
| 36 |
-
min(preset.max_dots, int(
|
| 37 |
)
|
| 38 |
|
| 39 |
-
points
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
points.extend(sample_contour(contour, sample_count))
|
| 44 |
-
|
| 45 |
-
points = dedupe_points(points, preset.min_distance_px)
|
| 46 |
if len(points) < preset.min_dots:
|
| 47 |
-
largest = contours[0]
|
| 48 |
points = dedupe_points(
|
| 49 |
-
sample_contour(
|
| 50 |
max(10, preset.min_distance_px - 8),
|
| 51 |
)
|
| 52 |
|
|
@@ -54,4 +56,4 @@ class DotLayoutGenerator:
|
|
| 54 |
step = len(points) / preset.max_dots
|
| 55 |
points = [points[int(i * step)] for i in range(preset.max_dots)]
|
| 56 |
|
| 57 |
-
return DotLayout(points=points, source_size=image.size)
|
|
|
|
| 8 |
from .image_utils import (
|
| 9 |
contour_length,
|
| 10 |
dedupe_points,
|
| 11 |
+
find_external_contours,
|
| 12 |
find_relevant_contours,
|
| 13 |
+
normalize_contour_start,
|
| 14 |
pil_to_cv_gray,
|
| 15 |
preprocess_binary,
|
| 16 |
sample_contour,
|
| 17 |
+
select_primary_contour,
|
| 18 |
)
|
| 19 |
|
| 20 |
|
|
|
|
| 22 |
class DotLayout:
|
| 23 |
points: list[tuple[int, int]]
|
| 24 |
source_size: tuple[int, int]
|
| 25 |
+
closed: bool = True
|
| 26 |
|
| 27 |
|
| 28 |
class DotLayoutGenerator:
|
| 29 |
def generate(self, image: Image.Image, preset: AgePreset) -> DotLayout:
|
| 30 |
gray = pil_to_cv_gray(image)
|
| 31 |
binary = preprocess_binary(gray)
|
| 32 |
+
contours = find_external_contours(binary)
|
| 33 |
+
if not contours:
|
| 34 |
+
contours = find_relevant_contours(binary)
|
| 35 |
|
| 36 |
if not contours:
|
| 37 |
raise ValueError("Nao foi possivel identificar contornos validos na imagem.")
|
| 38 |
|
| 39 |
+
primary_contour = normalize_contour_start(select_primary_contour(contours))
|
| 40 |
total_target = max(
|
| 41 |
preset.min_dots,
|
| 42 |
+
min(preset.max_dots, int(contour_length(primary_contour) / 70)),
|
| 43 |
)
|
| 44 |
|
| 45 |
+
points = dedupe_points(
|
| 46 |
+
sample_contour(primary_contour, total_target, closed=True),
|
| 47 |
+
preset.min_distance_px,
|
| 48 |
+
)
|
|
|
|
|
|
|
|
|
|
| 49 |
if len(points) < preset.min_dots:
|
|
|
|
| 50 |
points = dedupe_points(
|
| 51 |
+
sample_contour(primary_contour, preset.min_dots + 6, closed=True),
|
| 52 |
max(10, preset.min_distance_px - 8),
|
| 53 |
)
|
| 54 |
|
|
|
|
| 56 |
step = len(points) / preset.max_dots
|
| 57 |
points = [points[int(i * step)] for i in range(preset.max_dots)]
|
| 58 |
|
| 59 |
+
return DotLayout(points=points, source_size=image.size, closed=True)
|