From 206c2a6877fd795212e03f809e9a78f94ee8dc14 Mon Sep 17 00:00:00 2001 From: "lamonov.pavel" Date: Mon, 21 Jul 2025 19:03:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D0=BA=D1=80=D0=B8=D0=BF=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/rec_validation.py | 27 +++++++++++++++++++++++++++ scripts/score_validation.py | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 scripts/rec_validation.py create mode 100644 scripts/score_validation.py 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))