forked from mergeos-bounties/PlantGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_demo_photos.py
More file actions
202 lines (186 loc) · 6.74 KB
/
Copy pathgenerate_demo_photos.py
File metadata and controls
202 lines (186 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""Generate license-safe synthetic plant leaf photos for offline photo-ID demo.
Creates data/samples/photos/*.jpg + manifest.json
"""
from __future__ import annotations
import json
import math
import random
from pathlib import Path
from PIL import Image, ImageDraw, ImageFilter
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "data" / "samples" / "photos"
# (filename, species_id, tags, palette, style)
SPECS = [
(
"monstera_demo.jpg",
"monstera_deliciosa",
["tropical", "fenestrated leaves", "climbing", "indoor", "large leaves", "aroid"],
[(34, 90, 48), (20, 60, 32), (12, 40, 22)],
"fenestrated",
),
(
"snake_demo.jpg",
"snake_plant",
["succulent", "upright", "thick leaves", "drought", "indoor", "low light"],
[(30, 70, 45), (18, 40, 28), (200, 210, 180)],
"upright",
),
(
"pothos_demo.jpg",
"pothos_golden",
# tags must overlap species catalog (Jaccard match)
["trailing", "variegated", "indoor", "vining", "heart shaped leaves", "easy"],
[(50, 120, 40), (180, 190, 80), (30, 80, 30)],
"trailing",
),
(
"aloe_demo.jpg",
"aloe_vera",
["succulent", "thick leaves", "spiky", "drought", "medicinal gel"],
[(90, 140, 70), (60, 100, 50), (200, 220, 160)],
"rosette",
),
(
"peace_demo.jpg",
"peace_lily",
["indoor", "white flowers", "dark green leaves", "shade tolerant", "humidity loving"],
[(20, 50, 30), (15, 35, 22), (240, 240, 245)],
"peace",
),
(
"pilea_demo.jpg",
"pilea_peperomioides",
["pilea", "round leaves", "coin leaves", "indoor", "bright indirect", "pet-safe"],
[(58, 138, 78), (32, 92, 52), (120, 190, 110)],
"pilea",
),
]
def _leaf(draw: ImageDraw.ImageDraw, cx: int, cy: int, scale: float, color: tuple, rot: float = 0.0) -> None:
pts = []
for i in range(14):
t = i / 13 * math.pi
rx = scale * (0.55 + 0.15 * math.sin(3 * t))
ry = scale * 1.1
x = rx * math.sin(t)
y = -ry * math.cos(t)
ca, sa = math.cos(rot), math.sin(rot)
xr, yr = x * ca - y * sa, x * sa + y * ca
pts.append((cx + xr, cy + yr))
draw.polygon(pts, fill=color)
def render(style: str, palette: list[tuple[int, int, int]], seed: int = 0) -> Image.Image:
rng = random.Random(seed)
w, h = 640, 480
img = Image.new("RGB", (w, h), (18, 22, 28))
draw = ImageDraw.Draw(img)
# soft bg
for i in range(8):
c = 22 + i * 4
draw.ellipse((i * 20, i * 15, w - i * 20, h - i * 15), outline=(c, c + 4, c + 8))
base = palette[0]
dark = palette[1]
accent = palette[2] if len(palette) > 2 else palette[0]
if style == "fenestrated":
for i, (dx, dy, sc, rot) in enumerate(
[(-40, 20, 110, -0.4), (60, 10, 120, 0.35), (10, 60, 90, 0.1), (-80, 80, 70, -0.8)]
):
_leaf(draw, w // 2 + dx, h // 2 + dy, sc, base if i % 2 == 0 else dark, rot)
# holes
for _ in range(12):
x = w // 2 + rng.randint(-90, 90)
y = h // 2 + rng.randint(-70, 70)
r = rng.randint(8, 18)
draw.ellipse((x - r, y - r, x + r, y + r), fill=(18, 22, 28))
elif style == "upright":
for i in range(7):
x = w // 2 - 90 + i * 28
draw.rounded_rectangle(
(x, 80 + (i % 3) * 10, x + 22, 400),
radius=10,
fill=base if i % 2 == 0 else dark,
)
# yellow edge stripe
draw.line((x + 4, 90, x + 4, 390), fill=accent, width=3)
elif style == "trailing":
for i in range(10):
x = 80 + i * 50
y = 120 + int(40 * math.sin(i / 2))
_leaf(draw, x, y, 45, base if i % 2 == 0 else accent, rot=i * 0.3)
if i < 9:
draw.line((x, y, x + 50, 120 + int(40 * math.sin((i + 1) / 2))), fill=dark, width=3)
elif style == "rosette":
for i in range(12):
ang = i / 12 * math.tau
_leaf(
draw,
w // 2 + int(30 * math.cos(ang)),
h // 2 + int(20 * math.sin(ang)),
70,
base if i % 2 == 0 else dark,
rot=ang,
)
elif style == "peace":
_leaf(draw, w // 2 - 40, h // 2 + 30, 100, dark, -0.3)
_leaf(draw, w // 2 + 50, h // 2 + 40, 90, base, 0.4)
# white spathe
draw.ellipse((w // 2 + 20, 90, w // 2 + 100, 200), fill=accent)
draw.rectangle((w // 2 + 52, 150, w // 2 + 68, 280), fill=(240, 235, 200))
elif style == "pilea":
center = (w // 2, h // 2 + 45)
stems = [
(-95, -90, 46),
(-40, -135, 58),
(45, -120, 54),
(110, -70, 42),
(-115, 10, 34),
(80, 25, 36),
(-20, -35, 50),
]
for dx, dy, radius in stems:
leaf_center = (center[0] + dx, center[1] + dy)
draw.line((center, leaf_center), fill=dark, width=5)
draw.ellipse(
(
leaf_center[0] - radius,
leaf_center[1] - radius,
leaf_center[0] + radius,
leaf_center[1] + radius,
),
fill=base,
outline=accent,
width=3,
)
draw.line(
(leaf_center[0], leaf_center[1] + radius, leaf_center[0], leaf_center[1] - radius),
fill=dark,
width=2,
)
# small pups at soil line
for x, r in ((center[0] - 58, 20), (center[0] + 42, 18)):
draw.line((x, center[1] + 70, x, center[1] + 30), fill=dark, width=3)
draw.ellipse((x - r, center[1] + 10, x + r, center[1] + 10 + 2 * r), fill=accent)
else:
_leaf(draw, w // 2, h // 2, 100, base, 0)
img = img.filter(ImageFilter.SMOOTH_MORE)
return img
def main() -> None:
OUT.mkdir(parents=True, exist_ok=True)
photos = []
for i, (fname, species, tags, palette, style) in enumerate(SPECS):
img = render(style, palette, seed=10 + i)
path = OUT / fname
img.save(path, quality=90)
photos.append(
{
"file": fname,
"expected_species": species,
"tags": tags,
"label": species.replace("_", " ").title(),
"license": "synthetic-generated-offline",
}
)
print("wrote", path, path.stat().st_size)
man = {"version": 1, "photos": photos}
(OUT / "manifest.json").write_text(json.dumps(man, indent=2) + "\n", encoding="utf-8")
print("manifest", len(photos), "photos")
if __name__ == "__main__":
main()