Files
aimsight-tools/scripts/thumbnails.py
T

171 lines
10 KiB
Python
Raw Normal View History

"""Thumbnails of the camera images around sensors, at full resolution, for the diagnostic agent.
Usage: .venv/bin/python tools/scripts/thumbnails.py data/PS_DES_COUDRES/
--sensors REF_L,REF_R --times 2026-10-13T22:00Z,2026-10-14T02:00Z [--radius 100] [--contrast [MIN,MAX]] [--out FOLDER]
One row per sensor, one column per time. When there is no image at the requested time, the nearest one is
taken: the gap is printed and the actual time of the image is in the thumbnail's title. Each thumbnail is centred on the raw position (x, y) of the sensor
in the "advanced" export, read at the measurement nearest to the image; the pixels are not retouched, so the brightness
can be compared from one thumbnail to another. Prints the positions used, the background (median of the pixels of the
thumbnail), the 99th percentile and the peak (maximum) of the grey level of each thumbnail (0 to 255), then the path of the PNG.
--contrast: all thumbnails in grey levels, with the same stretch for the whole sheet, so that a faint
spot shows and can be compared from one thumbnail to another. Without a value, the stretch goes from the 1st percentile to the maximum
of the sheet (a saturated pixel somewhere crushes it: then give --contrast MIN,MAX again, with the help of the background and the
peak printed for each thumbnail). A smaller --radius (30 to 50) magnifies. The bounds are printed.
The frame centre (px) is written under each thumbnail: when the x, y of the export jump, the frame moves
by as much. --fixed keeps, on each row, the frame centred on the position at the first requested time, and marks
the x, y of the export at each time with a red cross: a spot that enters or leaves the frame is not confused with
a spot that appears. A frame cut by the edge of the image is flagged (image size): on some
exports, x, y are not pixels (values close to 0), and the frame falls in the corner of the image.
--zone X,Y,W,H: the same zone of the image (pixels of the original image: top left corner, width, height, as
gif.py) at each time, side by side (up to 4 per row, fewer when the zone is wide), same stretch with --contrast; --sensors becomes optional:
those given are marked with a red cross and their name at their x, y at each time. For the scene around
the targets (foreground, object, lighting), which a large --radius would make too small.
Each thumbnail is rendered at its size in pixels (one image pixel for one PNG pixel) from 260 to 1000 px a side;
Read shrinks a PNG that is too wide: with a large --radius or a large --zone, few times per sheet.
"""
import argparse
import os
import sys
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt # noqa: E402
from data import find_files, image_time, iso, load, parse_time # noqa: E402
def percentile(grey, p):
"""Percentile p (0 to 100) of the pixels of a thumbnail, without interpolation."""
v = sorted(grey.ravel().tolist())
return v[min(int(p / 100 * len(v)), len(v) - 1)]
def main():
a = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
a.add_argument("folder")
a.add_argument("--sensors", default="", help="comma-separated list (optional with --zone)")
a.add_argument("--times", required=True, help="times of the images, comma-separated (UTC)")
a.add_argument("--radius", type=int, default=100, help="half side of the thumbnail in pixels (default 100)")
a.add_argument("--contrast", nargs="?", const="auto", metavar="MIN,MAX", help="grey levels, same stretch for the whole sheet: MIN,MAX (0 to 255) or, without a value, from the 1st percentile to the maximum of the sheet")
a.add_argument("--fixed", action="store_true", help="one fixed frame per sensor (position at the first time), x, y of each time marked with a cross")
a.add_argument("--zone", metavar="X,Y,W,H", help="the same zone of the image at each time (pixels of the original image: top left corner, width, height)")
a.add_argument("--out", default=os.path.join("out", "diagnostic"), help="folder of the PNG")
o = a.parse_args()
by = load(o.folder)
images = {image_time(p): p for p in find_files(o.folder)["images"]}
names, times = [s for s in o.sensors.split(",") if s], [parse_time(h) for h in o.times.split(",")]
zone = tuple(map(int, o.zone.split(","))) if o.zone else None
if not names and not zone:
sys.exit("Give --sensors, or --zone X,Y,W,H.")
missing = [s for s in names if s not in by]
if missing:
sys.exit(f"Unknown sensors: {', '.join(missing)}. Known: {', '.join(sorted(by))}.")
known = sorted(h for h in images if h)
if not known:
sys.exit(f"No image in {o.folder}.")
nearest = {h: min(known, key=lambda i: abs(i - h)) for h in times} # image at the requested time, otherwise the nearest
for h in times:
if nearest[h] != h:
print(f"No image at {iso(h)}: the nearest is the one of {iso(nearest[h])} (gap {abs(nearest[h] - h)}).")
def levels(c):
grey = c[..., :3].mean(axis=-1) if c.ndim == 3 else c
return grey * 255 if grey.max() <= 1 and c.dtype.kind == "f" else grey
def measurement(s, t):
return min((m for m in by[s] if m["x"] is not None), key=lambda m: abs(m["t"] - t))
r = o.radius
crops, centres, crosses = {}, {}, {}
for j, h in enumerate(times):
img = plt.imread(images[nearest[h]])
height, width = img.shape[:2]
if zone:
X, Y, W, H = zone
c = img[max(Y, 0):Y + H, max(X, 0):X + W]
if c.size == 0:
sys.exit(f"--zone {o.zone} outside the image ({width} × {height} px).")
grey = levels(c)
crops[0, j] = (c, grey)
crosses[0, j] = [(s, m["x"] - max(X, 0), m["y"] - max(Y, 0)) for s in names for m in [measurement(s, nearest[h])]]
print(f"zone {o.zone} {iso(h)}: {os.path.basename(images[nearest[h]])}, background {percentile(grey, 50):.0f}, "
f"p99 {percentile(grey, 99):.0f}, peak {grey.max():.0f}"
+ ("" if c.shape[:2] == (H, W) else f", cut by the image edge ({width} × {height} px)")
+ "".join(f"; {s} x={x + max(X, 0):.1f} y={y + max(Y, 0):.1f}" for s, x, y in crosses[0, j]))
continue
for i, s in enumerate(names):
m = measurement(s, nearest[h])
x, y = centres[i] if o.fixed and j else (round(m["x"]), round(m["y"]))
if j == 0:
centres[i] = (x, y)
c = img[max(y - r, 0):y + r, max(x - r, 0):x + r]
if c.size == 0:
sys.exit(f"{s} {iso(h)}: x={m['x']:.1f} y={m['y']:.1f} outside the image ({width} × {height} px); "
"--zone X,Y,W,H looks at a fixed zone.")
crosses[i, j] = (m["x"] - max(x - r, 0), m["y"] - max(y - r, 0), x, y)
grey = levels(c)
crops[i, j] = (c, grey)
cut = x - r < 0 or y - r < 0 or x + r > width or y + r > height
print(f"{s} {iso(h)}: x={m['x']:.1f} y={m['y']:.1f} (measurement of {iso(m['t'])}), {os.path.basename(images[nearest[h]])}, "
f"background {percentile(grey, 50):.0f}, p99 {percentile(grey, 99):.0f}, peak {grey.max():.0f}"
+ (f", frame cut by the image edge ({width} × {height} px)" if cut else ""))
bounds = {}
if o.contrast:
if o.contrast == "auto":
bounds = dict(vmin=min(percentile(g, 1) for _, g in crops.values()), vmax=max(g.max() for _, g in crops.values()))
else:
bounds = dict(zip(("vmin", "vmax"), map(float, o.contrast.split(","))))
print(f"Common stretch: {bounds['vmin']:.0f} to {bounds['vmax']:.0f} (grey levels, 0 to 255)")
# A thumbnail at its size in pixels (at 100 dpi) once it exceeds 2.6 inches: a large --radius is no longer shrunk.
side = max(c.shape[1] for c, _ in crops.values()) / 100, max(c.shape[0] for c, _ in crops.values()) / 100
side = tuple(v * min(1, 10 / max(side)) for v in side) # at most 1000 px a side per thumbnail
if zone:
nc = max(1, min(4, len(times), int(20 // max(2.6, side[0])))) # sheet at most ~2000 px wide: Read shrinks it beyond
nl = -(-len(times) // nc)
fig, axes = plt.subplots(nl, nc, figsize=(max(2.6, side[0]) * nc, (max(2.6, side[1]) + 0.4) * nl), squeeze=False)
for ax in axes.ravel()[len(times):]:
ax.axis("off")
else:
fig, axes = plt.subplots(len(names), len(times), figsize=(max(2.6, side[0]) * len(times), (max(2.6, side[1]) + 0.2) * len(names)), squeeze=False)
for (i, j), (c, grey) in crops.items():
ax = axes[j // nc, j % nc] if zone else axes[i, j]
if o.contrast:
ax.imshow(grey, cmap="gray", interpolation="nearest", **bounds)
else:
ax.imshow(c, interpolation="nearest")
ax.set_title(f"{'zone' if zone else names[i]} {nearest[times[j]].strftime('%d/%m %H:%MZ')}" + ("" if nearest[times[j]] == times[j] else " (requested " + times[j].strftime('%H:%MZ') + ")"), fontsize=9)
if zone:
for s, cx, cy in crosses[i, j]:
ax.plot(cx, cy, "+", color="r", ms=10, mew=1)
ax.annotate(s, (cx, cy), xytext=(4, 4), textcoords="offset points", color="r", fontsize=7)
ax.set_xlim(-0.5, c.shape[1] - 0.5)
ax.set_ylim(c.shape[0] - 0.5, -0.5)
ax.set_xlabel(f"zone {o.zone}", fontsize=7)
ax.set_xticks([])
ax.set_yticks([])
continue
cx, cy, x, y = crosses[i, j]
if o.fixed:
ax.plot(cx, cy, "+", color="r", ms=10, mew=1)
ax.set_xlabel(f"frame centre {x}, {y}" + (f"\nx, y {cx + max(x - r, 0):.1f}, {cy + max(y - r, 0):.1f}" if o.fixed else ""), fontsize=7)
ax.set_xticks([])
ax.set_yticks([])
station = os.path.basename(os.path.normpath(o.folder))
fig.suptitle(f"{station}: zone {o.zone} (x, y, width, height in px; UTC times)" if zone else
f"{station}: {2 * r} px thumbnails around each sensor (UTC times)", fontsize=10)
os.makedirs(o.out, exist_ok=True)
what = f"zone{o.zone.replace(',', '-')}" + ("_" + "-".join(names) if names else "") if zone else "-".join(names)
path = os.path.join(o.out, f"{station}_thumbnails_{what}_"
f"{'-'.join(h.strftime('%d%H%M') for h in times)}{'_contrast' if o.contrast else ''}{'_fixed' if o.fixed else ''}.png")
fig.savefig(path, dpi=100, bbox_inches="tight")
print(path)
if __name__ == "__main__":
main()