-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·86 lines (68 loc) · 2.67 KB
/
run_tests.py
File metadata and controls
executable file
·86 lines (68 loc) · 2.67 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
#!/usr/bin/env python3
# Pressure Advance Camera calibration for Klipper
#
# Copyright (C) 2025 Marius Wachtler <undingen@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import os
import re
import sys
import cv2
from line_analyzer import LineAnalyzer
from retrieve_rect import RetrieveRect
def get_best_line(image_path, debug=False):
seg_img = cv2.imread(image_path.replace(".jpg", "_out.png"), cv2.IMREAD_UNCHANGED)
# Disable segmenting the images via the API in the tests (because would cost something).
# Instead just use the prepared images
if seg_img is None and 0:
from segment_image import SegmentImage
segmenter = SegmentImage()
seg_img = segmenter.segment(image_path)
if seg_img is None:
raise ValueError("Image not found")
retrieve_rect = RetrieveRect(debug=debug)
rect_img = retrieve_rect.process_image(seg_img)
analyzer = LineAnalyzer(rect_img, debug=debug)
smoothest = analyzer.get_smoothest_lines()
return smoothest[0][0]
def main():
failing_tests = []
num_passed_tests = 0
pattern = re.compile(r"_(\d+(?:,\d+)*)\.jpg$", re.IGNORECASE)
for root, _, files in os.walk("test_data"):
for file in files:
if file.lower().endswith(".jpg"):
path = os.path.join(root, file)
m = pattern.search(file)
if not m:
print(f"Skipping {path}: filename pattern not matched")
continue
expected_values = [int(val) for val in m.group(1).split(",")]
try:
result = get_best_line(path, debug=False)
except Exception as e:
print(f"FAIL: {path} Exception: {e}")
failing_tests.append(f"{path} (Exception: {e})")
continue
if result in expected_values:
# print(f"PASS: {path} Expected one of {expected_values}, Got: {result}")
num_passed_tests += 1
else:
print(
f"FAIL: {path} Expected one of {expected_values}, Got: {result}"
)
failing_tests.append(
f"{path} (Expected one of {expected_values}, Got: {result})"
)
if failing_tests:
print()
print("Summary:")
print(f"Num tests passed: {num_passed_tests}")
print(f"Num tests failed: {len(failing_tests)}")
if failing_tests:
print("\nFailed tests:")
for fail in failing_tests:
print(fail)
sys.exit(1 if failing_tests else 0)
if __name__ == "__main__":
main()