File size: 3,995 Bytes
4eecd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Floor detection checks.

1. Synthetic rooms (real_data test split, camera 'synthetic'): the generator draws a floor below the wall, so ground
   truth = non-wall pixels below the lowest wall / skirting pixel of each column. Scores the raw general model, the
   cleaned mask, and the wall/floor line. Synthetic floors are flat planks: this is a floor of 'is it wired up right',
   not a real-world accuracy figure.
2. Real photos (surface_eval / surface_eval2 interiors): no floor labels exist, so this writes a contact sheet to look at,
   and reports how often the floor mask overlaps what the wall model calls wall (it should be ~0).

    python eval_floor.py --sheet out.jpg
"""
from __future__ import annotations

import argparse
import csv
import glob
import json
from pathlib import Path

import cv2
import numpy as np

import floor_detect as FD

ROOT = Path(__file__).parent


def synthetic_gt(mask):
    """Floor = the pixels below the lowest wall / skirting / object pixel of their column, that are none of those."""
    h, w = mask.shape
    solid = np.isin(mask, (1, 2, 3, 4, 5, 6, 7, 8, 9))
    has = solid.any(axis=0)
    lowest = np.where(has, h - 1 - np.argmax(solid[::-1], axis=0), h)
    return (np.arange(h)[:, None] > lowest[None, :]) & ~solid


def iou(a, b):
    u = (a | b).sum()
    return float((a & b).sum() / u) if u else 1.0


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--limit", type=int, default=0)
    ap.add_argument("--sheet")
    ap.add_argument("--real", type=int, default=24)
    a = ap.parse_args()
    rows = [r for r in csv.DictReader(open(ROOT / "real_data" / "metadata.csv")) if r["split"] == "test" and r["camera_id"] == "synthetic"]
    rows.sort(key=lambda r: r["image"])
    if a.limit:
        rows = rows[:a.limit]
    res = {"raw": [], "cleaned": [], "cleaned_no_walls": []}
    line_err = []
    for r in rows:
        stem = r["image"].rsplit(".", 1)[0]
        rgb = cv2.cvtColor(cv2.imread(str(ROOT / "real_data/test/images" / r["image"])), cv2.COLOR_BGR2RGB)
        cls = np.asarray(cv2.imread(str(ROOT / "real_data/test/masks" / f"{stem}.png"), cv2.IMREAD_UNCHANGED))
        gt = synthetic_gt(cls)
        prob = FD.floor_probability(rgb)
        res["raw"].append(iou(prob >= 0.5, gt))
        res["cleaned_no_walls"].append(iou(FD.clean_floor(prob, None), gt))
        m = FD.clean_floor(prob, cls)
        res["cleaned"].append(iou(m, gt))
        fit = FD.floor_line(m)["fit"]
        if fit and gt.any():
            ys = np.nonzero(gt.any(axis=1))[0]
            line_err.append(abs((fit["y_at_left"] + fit["y_at_right"]) / 2 - ys[0]) / rgb.shape[0])
    out = {"synthetic_images": len(rows), **{k: round(float(np.mean(v)), 4) for k, v in res.items()},
           "line_mean_abs_error_frac_of_height": round(float(np.mean(line_err)), 4) if line_err else None,
           "line_found_share": round(len(line_err) / max(len(rows), 1), 3)}
    print(json.dumps(out, indent=1))
    if a.sheet:
        imgs = sorted(glob.glob(str(ROOT / "surface_eval2/images/*.jpg")) + glob.glob(str(ROOT / "surface_eval/images/*.jpg")))
        rng = np.random.default_rng(4)
        pick = [imgs[i] for i in rng.permutation(len(imgs))[:a.real]]
        tiles, overlap = [], []
        for f in pick:
            rgb = cv2.cvtColor(cv2.imread(f), cv2.COLOR_BGR2RGB)
            m, info = FD.detect_floor(rgb)
            vis = rgb.copy()
            vis[m] = (0.45 * vis[m] + 0.55 * np.array([176, 125, 79])).astype(np.uint8)
            for x, y in info["line"]["points"]:
                cv2.circle(vis, (x, y), max(2, rgb.shape[1] // 250), (255, 255, 255), -1)
            tiles.append(cv2.resize(vis, (320, 240)))
        while len(tiles) % 6:
            tiles.append(np.zeros((240, 320, 3), np.uint8))
        grid = np.vstack([np.hstack(tiles[i:i + 6]) for i in range(0, len(tiles), 6)])
        cv2.imwrite(a.sheet, cv2.cvtColor(grid, cv2.COLOR_RGB2BGR))


if __name__ == "__main__":
    main()