|
|
@ -108,10 +108,11 @@ def recognize_digit(prototypes, image):
|
|
|
|
"""Takes a normalized digit image and returns (detected number, score, all_scores)
|
|
|
|
"""Takes a normalized digit image and returns (detected number, score, all_scores)
|
|
|
|
where score is between 0 and 1. Higher numbers are more certain the number is correct.
|
|
|
|
where score is between 0 and 1. Higher numbers are more certain the number is correct.
|
|
|
|
all_scores is for debugging.
|
|
|
|
all_scores is for debugging.
|
|
|
|
|
|
|
|
If the most likely detection is NOT a number, None is returned instead.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
scores = sorted([
|
|
|
|
scores = sorted([
|
|
|
|
(compare_images(prototypes[n], image), n)
|
|
|
|
(compare_images(prototype, image), int(n) if n.isdigit() else None)
|
|
|
|
for n in range(10)
|
|
|
|
for n, prototype in prototypes["odo-digits"].items()
|
|
|
|
], reverse=True)
|
|
|
|
], reverse=True)
|
|
|
|
best_score, number = scores[0]
|
|
|
|
best_score, number = scores[0]
|
|
|
|
runner_up_score, _ = scores[1]
|
|
|
|
runner_up_score, _ = scores[1]
|
|
|
@ -130,14 +131,20 @@ def compare_images(prototype, image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_prototypes(prototypes_path):
|
|
|
|
def load_prototypes(prototypes_path):
|
|
|
|
return [
|
|
|
|
prototypes = {}
|
|
|
|
Image.open(os.path.join(prototypes_path, "{}.png".format(n)))
|
|
|
|
for kind in os.listdir(prototypes_path):
|
|
|
|
for n in range(10)
|
|
|
|
prototypes[kind] = {}
|
|
|
|
]
|
|
|
|
path = os.path.join(prototypes_path, kind)
|
|
|
|
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
|
|
|
|
if not filename.endswith(".png"):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
name = filename[:-4]
|
|
|
|
|
|
|
|
prototypes[kind][name] = Image.open(os.path.join(path, filename))
|
|
|
|
|
|
|
|
return prototypes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@cli
|
|
|
|
@cli
|
|
|
|
def read_digit(digit, prototypes_path="./odo-digit-prototypes", verbose=False):
|
|
|
|
def read_digit(digit, prototypes_path="./prototypes", verbose=False):
|
|
|
|
"""For debugging. Compares an extracted digit image to each prototype and prints scores."""
|
|
|
|
"""For debugging. Compares an extracted digit image to each prototype and prints scores."""
|
|
|
|
prototypes = load_prototypes(prototypes_path)
|
|
|
|
prototypes = load_prototypes(prototypes_path)
|
|
|
|
digit = Image.open(digit)
|
|
|
|
digit = Image.open(digit)
|
|
|
@ -165,7 +172,7 @@ def recognize_odometer(prototypes, frame):
|
|
|
|
|
|
|
|
|
|
|
|
@cli
|
|
|
|
@cli
|
|
|
|
@argh.arg("frames", nargs="+")
|
|
|
|
@argh.arg("frames", nargs="+")
|
|
|
|
def read_frame(frames, prototypes_path="./odo-digit-prototypes", verbose=False, include_last=False):
|
|
|
|
def read_frame(frames, prototypes_path="./prototypes", verbose=False, include_last=False):
|
|
|
|
"""For testing. Takes any number of frame images (or segments) and prints the odometer reading."""
|
|
|
|
"""For testing. Takes any number of frame images (or segments) and prints the odometer reading."""
|
|
|
|
prototypes = load_prototypes(prototypes_path)
|
|
|
|
prototypes = load_prototypes(prototypes_path)
|
|
|
|
for filename in frames:
|
|
|
|
for filename in frames:
|
|
|
|