-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
168 lines (135 loc) · 5.81 KB
/
Copy pathevaluate.py
File metadata and controls
168 lines (135 loc) · 5.81 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
"""
two modes:
python evaluate.py --detection-only fast, geometry only, no OCR
python evaluate.py full pipeline including OCR
the results are printed as a table and written to evaluation_results.json.
"""
import argparse
import glob
import json
import os
import time
from collections import defaultdict
import cv2
import scanner
import preprocessing
def condition_of(path):
# MIDV encodes the capture condition in the filename: TS38_01.tif => 'TS'.
return os.path.basename(path)[:2]
def evaluate_detection(files):
# Compare the baseline detector against the improved one, per condition
stats = defaultdict(lambda: {"total": 0, "baseline": 0, "improved": 0,
"contour": 0, "minarea": 0})
failures = []
t_base = t_impr = 0.0
for path in files:
image = cv2.imread(path)
if image is None:
continue
cond = condition_of(path)
stats[cond]["total"] += 1
t0 = time.time()
quad_base = scanner.detect_document(image)
t_base += time.time() - t0
t0 = time.time()
quad_impr, method = preprocessing.detect_document_improved(image)
t_impr += time.time() - t0
if quad_base is not None:
stats[cond]["baseline"] += 1
if quad_impr is not None:
stats[cond]["improved"] += 1
stats[cond][method] += 1
else:
failures.append(os.path.basename(path))
n = max(len(files), 1)
return stats, failures, t_base / n, t_impr / n
def evaluate_full(files):
# run the whole pipeline (OCR included) and count extracted fields
import extract # imported lazily: loading EasyOCR is slow
per_image = []
field_hits = defaultdict(int)
total_time = 0.0
for path in files:
t0 = time.time()
try:
result = extract.process_document(path)
except Exception as exc: # keep going on a bad image
per_image.append({"file": os.path.basename(path), "error": str(exc)})
continue
elapsed = time.time() - t0
total_time += elapsed
fields = result["fields"]
extracted = [k for k, v in fields.items() if v is not None]
for k in extracted:
field_hits[k] += 1
per_image.append({
"file": os.path.basename(path),
"condition": condition_of(path),
"extracted": len(extracted),
"missing": len(fields) - len(extracted),
"warnings": len(result["validation"]["warnings"]),
"ocr_confidence": result["validation"]["ocr_confidence"],
"seconds": round(elapsed, 2),
})
return per_image, field_hits, total_time / max(len(files), 1)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data", default="data", help="dataset folder")
parser.add_argument("--detection-only", action="store_true",
help="skip OCR, measure geometry only (fast)")
args = parser.parse_args()
files = sorted(glob.glob(os.path.join(args.data, "**", "*.tif"), recursive=True))
if not files:
print(f"no .tif images found under {args.data}/ — run download_dataset.py first")
return
print(f"evaluating {len(files)} images\n")
report = {"images": len(files)}
# detection metrics
stats, failures, t_base, t_impr = evaluate_detection(files)
print(f"{'cond':6s} {'n':>3s} {'baseline':>10s} {'improved':>10s} method")
tot_b = tot_i = tot_n = 0
for cond in sorted(stats):
s = stats[cond]
tot_n += s["total"]; tot_b += s["baseline"]; tot_i += s["improved"]
method = f"contour {s['contour']}, minarea {s['minarea']}"
print(f"{cond:6s} {s['total']:3d} {s['baseline']:>10d} {s['improved']:>10d} {method}")
print(f"{'ALL':6s} {tot_n:3d} {tot_b:>10d} {tot_i:>10d}")
print(f"\ndetection rate: baseline {tot_b}/{tot_n} ({100*tot_b/tot_n:.0f}%), "
f"improved {tot_i}/{tot_n} ({100*tot_i/tot_n:.0f}%)")
print(f"average detection time: baseline {t_base*1000:.0f} ms, improved {t_impr*1000:.0f} ms")
if failures:
print(f"\nfailure cases ({len(failures)}): {', '.join(failures)}")
report["detection"] = {
"per_condition": {c: dict(v) for c, v in stats.items()},
"baseline_rate": round(tot_b / tot_n, 3),
"improved_rate": round(tot_i / tot_n, 3),
"baseline_ms": round(t_base * 1000),
"improved_ms": round(t_impr * 1000),
"failures": failures,
}
# field extraction metrics
if not args.detection_only:
print("\nrunning full pipeline with OCR (this is slow on CPU)...\n")
per_image, field_hits, avg_t = evaluate_full(files)
print(f"{'file':16s} {'cond':5s} {'got':>4s} {'miss':>5s} {'warn':>5s} {'conf':>6s} {'sec':>6s}")
for r in per_image:
if "error" in r:
print(f"{r['file']:16s} ERROR: {r['error']}")
continue
conf = f"{r['ocr_confidence']:.2f}" if r["ocr_confidence"] is not None else "-"
print(f"{r['file']:16s} {r['condition']:5s} {r['extracted']:>4d} "
f"{r['missing']:>5d} {r['warnings']:>5d} {conf:>6s} {r['seconds']:>6.1f}")
print("\nfields extracted, across all images:")
for field, count in sorted(field_hits.items(), key=lambda kv: -kv[1]):
print(f" {field:18s} {count}/{len(files)}")
print(f"\naverage runtime per image: {avg_t:.1f} s")
report["extraction"] = {
"per_image": per_image,
"field_hits": dict(field_hits),
"avg_seconds": round(avg_t, 2),
}
with open("evaluation_results.json", "w", encoding="utf-8") as f:
json.dump(report, f, indent=2, ensure_ascii=False)
print("\nsaved to evaluation_results.json")
if __name__ == "__main__":
main()