/
jonique
/
scanner_AI
Обзор
Документация
Войти
/
jonique
/
scanner_AI
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
utils/interface_utils.py
81 строка
3 KB
Евгений Щипунов
2d_method: last model removed from git
07 май 2026, 17:18
07 май 2026, 17:18
f0321f1
Код
Авторство
О чём код?
from config import config import utils.utils as u import os def calculate_middle(screen_width, screen_height, width, height): x = (screen_width // 2) - (width // 2) y = (screen_height // 2) - (height // 2) return x, y def validate_generation_params(count): if count <= 0: return "Количество изображений должно быть больше нуля." if count > 100000: return "Количество изображений не должно превышать 100000." return None class MetricsService: def __init__(self, images_count): self.images_count = images_count def calculate(self): stats = {} for i in range(self.images_count): gt_path = os.path.join(config["labelsSaveDirectory"], f"{i}.txt") pr_path = os.path.join(config["predictionsDirectory"], f"{i}.txt") gt = u.read_yolo(gt_path) pr = u.read_yolo(pr_path) used_pred = set() for cls_gt, box_gt in gt: if cls_gt not in stats: stats[cls_gt] = {"tp":0,"fp":0,"fn":0,"iou_sum":0,"matches":0} best_iou = 0 best_j = -1 for j, (cls_pr, box_pr) in enumerate(pr): if j in used_pred or cls_pr != cls_gt: continue iou_val = u.iou(box_gt, box_pr) if iou_val > best_iou: best_iou = iou_val best_j = j if best_iou > 0.5: stats[cls_gt]["tp"] += 1 stats[cls_gt]["iou_sum"] += best_iou stats[cls_gt]["matches"] += 1 used_pred.add(best_j) else: stats[cls_gt]["fn"] += 1 for j, (cls_pr, _) in enumerate(pr): if j not in used_pred: if cls_pr not in stats: stats[cls_pr] = {"tp":0,"fp":0,"fn":0,"iou_sum":0,"matches":0} stats[cls_pr]["fp"] += 1 for cls in stats: tp = stats[cls]["tp"] fp = stats[cls]["fp"] fn = stats[cls]["fn"] precision = tp / (tp + fp + 1e-6) recall = tp / (tp + fn + 1e-6) f1 = 2 * precision * recall / (precision + recall + 1e-6) mean_iou = stats[cls]["iou_sum"] / (stats[cls]["matches"] + 1e-6) stats[cls].update({ "precision": precision, "recall": recall, "f1": f1, "iou": mean_iou, "class_name": u.class_name_to_russian(u.index_to_classname(cls)) }) return stats