import json, numpy as np, collections from PIL import Image, ImageDraw from shapely.geometry import Polygon R = json.load(open("rhino.json")); T = json.load(open("transfo.json")) s, x0, y1, dx, dy = T["s"], T["x0"], T["y1"], T["dx"], T["dy"] f = lambda x, y: ((x - x0) * s + dx, (y1 - y) * s + dy) W, H = 1654, 1170 cand = [] for c in R["courbes"]: if not c["f"] or len(c["p"]) < 4: continue try: P = Polygon(c["p"]) except Exception: continue if not P.is_valid: continue a = P.area if not (40 <= a <= 1500): continue mrr = P.minimum_rotated_rectangle if mrr.area <= 0 or a / mrr.area < 0.88: continue cx, cy = f(P.centroid.x, P.centroid.y) if not (0 <= cx <= W and 0 <= cy <= H): continue cand.append({"l": c["l"], "a": round(a, 1), "poly": P, "c": (cx, cy)}) print(collections.Counter(c["l"] for c in cand).most_common()) ov = Image.open("base100-1.png").convert("RGB"); d = ImageDraw.Draw(ov) pal = {} cols = [(230, 40, 40), (40, 90, 230), (20, 160, 60), (200, 120, 0), (160, 0, 200), (0, 170, 170), (120, 120, 120), (230, 0, 130)] for c in cand: col = pal.setdefault(c["l"], cols[len(pal) % len(cols)]) xs, ys = c["poly"].exterior.xy d.line([f(x, y) for x, y in zip(xs, ys)], fill=col, width=3) y = 10 for l, col in pal.items(): d.rectangle([10, y, 30, y + 14], fill=col); d.text((36, y), f"{l}", fill=(0, 0, 0)); y += 18 ov.save("controle-maisons.png") json.dump([{"l": c["l"], "a": c["a"], "p": [list(p) for p in c["poly"].exterior.coords]} for c in cand], open("cand_maisons.json", "w"))