-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_display.py
More file actions
144 lines (115 loc) · 4.54 KB
/
read_display.py
File metadata and controls
144 lines (115 loc) · 4.54 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
#!/usr/bin/env python3
"""
Read the main display number from ultrasonic gauge images.
Uses TFLite for lightweight inference.
Usage:
python read_display.py captures/img_0079.jpeg
python read_display.py captures/
python read_display.py captures/ --verify labels_new.json
"""
import cv2
import numpy as np
import os
import sys
import argparse
from pathlib import Path
from display_utils import (
find_main_digits, find_decimal_position,
is_dash_display, crop_digit, set_rotation
)
class DigitClassifier:
"""Lightweight digit classifier using TFLite."""
def __init__(self, model_path='models/digit_cnn.tflite'):
try:
from tflite_runtime.interpreter import Interpreter
self.interpreter = Interpreter(model_path=model_path)
except ImportError:
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
self.interpreter = tf.lite.Interpreter(model_path=model_path)
self.interpreter.allocate_tensors()
self.input_details = self.interpreter.get_input_details()
self.output_details = self.interpreter.get_output_details()
def predict(self, digit_crops):
results = []
for crop in digit_crops:
inp = crop.astype(np.float32).reshape(1, 28, 28, 1) / 255.0
self.interpreter.set_tensor(self.input_details[0]['index'], inp)
self.interpreter.invoke()
output = self.interpreter.get_tensor(self.output_details[0]['index'])
results.append(str(np.argmax(output)))
return results
def read_display(image_path, classifier):
"""Read the display number from an image."""
img = cv2.imread(str(image_path))
if img is None:
return None
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if is_dash_display(gray):
return '----'
digits, binary = find_main_digits(gray)
if not digits:
return None
crops = []
for d in digits:
crop = crop_digit(binary, d['x'], d['y'], d['w'], d['h'])
if crop is None:
return None
crops.append(crop)
predicted = classifier.predict(crops)
decimal_after = find_decimal_position(gray, digits)
if decimal_after is not None and decimal_after < len(predicted) - 1:
return ''.join(predicted[:decimal_after + 1]) + '.' + ''.join(predicted[decimal_after + 1:])
else:
return ''.join(predicted)
def main():
parser = argparse.ArgumentParser(description='Read gauge display numbers')
parser.add_argument('target', help='Image file or directory')
parser.add_argument('--verify', help='Labels JSON for accuracy check')
parser.add_argument('--rotate', type=int, default=180,
help='Rotation in degrees (default: 180)')
args = parser.parse_args()
set_rotation(args.rotate)
target = Path(args.target)
print("Loading model...")
classifier = DigitClassifier()
if target.is_dir():
images = sorted(target.glob('*.jpeg')) + sorted(target.glob('*.jpg')) + sorted(target.glob('*.png'))
else:
images = [target]
verify_labels = {}
if args.verify:
import json
with open(args.verify) as f:
verify_labels = json.load(f)
correct = total_verified = 0
results = {}
for img_path in images:
result = read_display(img_path, classifier)
results[img_path.name] = result
display = result if result else "NO READING"
if verify_labels:
expected = verify_labels.get(img_path.name)
if expected:
total_verified += 1
match = (result == expected)
if match:
correct += 1
status = "✓" if match else f"✗ (expected {expected})"
print(f" {img_path.name}: {display} {status}")
else:
print(f" {img_path.name}: {display} (no label)")
else:
print(f" {img_path.name}: {display}")
if verify_labels and total_verified > 0:
print(f"\n{'='*50}")
print(f"Accuracy: {correct}/{total_verified} ({correct/total_verified*100:.1f}%)")
mismatches = [(n, results.get(n), verify_labels.get(n))
for n in [p.name for p in images]
if verify_labels.get(n) and results.get(n) != verify_labels.get(n)]
if mismatches:
print(f"\nMismatches ({len(mismatches)}):")
for name, got, expected in mismatches:
print(f" {name}: got '{got}', expected '{expected}'")
if __name__ == '__main__':
main()