diff --git a/scripts/rec_validation.py b/scripts/rec_validation.py new file mode 100644 index 0000000..ea1b4fb --- /dev/null +++ b/scripts/rec_validation.py @@ -0,0 +1,27 @@ +import os +from paddleocr import TextRecognition + +ocr = TextRecognition( + model_name="PP-OCRv5_server_rec" + # model_dir="output/PP-OCRv5_server_rec_vin" +) + +with open("train_data/val.txt", "r") as label_file: + lines = label_file.readlines() + total = len(lines) + matches, mismatches = 0, 0 + for idx, line in enumerate(lines, start=1): + file_name, label = line.split("\t") + label = label.strip() + path = os.path.join("train_data", os.path.join("images", file_name)) + result = ocr.predict(path) + print(f"{idx}/{total} ", end="") + if result[0]["rec_text"] and result[0]["rec_text"] == label: + print(f"match {label}") + matches += 1 + else: + print(f"mismatch {label}") + mismatches += 1 + +print(f"{matches} matches of {total}") +print(f"{mismatches} mismatches of {total}") diff --git a/scripts/score_validation.py b/scripts/score_validation.py new file mode 100644 index 0000000..6c17724 --- /dev/null +++ b/scripts/score_validation.py @@ -0,0 +1,27 @@ +import os +from paddleocr import PaddleOCR +from typing import Optional + +ocr = PaddleOCR(rec_model_dir="output/vin_rec_inference") + +def get_best_accuracy(ocr: PaddleOCR, file_path: str) -> Optional[tuple[str, float]]: + result = ocr.predict(file_path) + texts_with_scores = [item for item in filter( + lambda x: len(x[0]) == 17, + zip(result[0]["rec_texts"], result[0]["rec_scores"]) + )] + if texts_with_scores: + return max( + texts_with_scores, + key=lambda y: y[1] + ) + else: + return None + + +with open("train_data/val.txt", "r") as label_file: + for line in label_file.readlines(): + file_name, label = line.split("\t") + path = os.path.join("train_data", os.path.join("images", file_name)) + print(file_name) + print(get_best_accuracy(ocr, path))