number-plate-study/car_new_att.ipynb

5042 lines
4.9 MiB
Plaintext
Raw Normal View History

2024-11-28 18:44:43 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Шаблоны сохранены в папке 'templates'.\n"
]
}
],
"source": [
"import os\n",
"from PIL import Image, ImageDraw, ImageFont\n",
"\n",
"# Убедитесь, что у вас установлен шрифт RoadNumbers2.0.ttf\n",
"FONT_PATH = \"RoadNumbers2.0.ttf\"\n",
"OUTPUT_DIR = \"templates\"\n",
"\n",
"# Буквы и цифры, используемые в российских номерах\n",
"SYMBOLS = \"DABEKMHOPCTYX0123456789\"\n",
"IMG_SIZE = (28, 28)\n",
"FONT_SIZE = 24\n",
"\n",
"# Создаем директорию для шаблонов\n",
"os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
"\n",
"# Загружаем шрифт\n",
"try:\n",
" font = ImageFont.truetype(FONT_PATH, FONT_SIZE)\n",
"except IOError:\n",
" print(f\"Шрифт {FONT_PATH} не найден. Убедитесь, что он находится в текущей директории.\")\n",
" exit()\n",
"\n",
"# Генерация изображений для каждого символа\n",
"for symbol in SYMBOLS:\n",
" # Создаем пустое изображение с белым фоном\n",
" img = Image.new(\"L\", IMG_SIZE, 255)\n",
" draw = ImageDraw.Draw(img)\n",
"\n",
" # Получаем размеры текста с использованием Font.getbbox\n",
" text_width, text_height = draw.textbbox((0, 0), symbol, font=font)[2:] # [2:] берёт ширину и высоту\n",
"\n",
" # Рассчитываем координаты для центрирования текста\n",
" x = (IMG_SIZE[0] - text_width) // 2\n",
" y = (IMG_SIZE[1] - text_height) // 2\n",
"\n",
" # Рисуем текст\n",
" draw.text((x, y), symbol, font=font, fill=0)\n",
"\n",
" # Сохраняем изображение\n",
" img.save(os.path.join(OUTPUT_DIR, f\"{symbol}.png\"))\n",
"\n",
"print(f\"Шаблоны сохранены в папке '{OUTPUT_DIR}'.\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[2], line 153\u001b[0m\n\u001b[0;32m 150\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mКонфигурационный файл создан: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mCONFIG_FILE\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 152\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m--> 153\u001b[0m \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[1;32mIn[2], line 142\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m 139\u001b[0m config_lines \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 141\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m symbol \u001b[38;5;129;01min\u001b[39;00m SYMBOLS:\n\u001b[1;32m--> 142\u001b[0m \u001b[43mcreate_augmentations\u001b[49m\u001b[43m(\u001b[49m\u001b[43msymbol\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mFONT_SIZES\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mMAX_ROTATION\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mAUGMENTATIONS\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig_lines\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 144\u001b[0m \u001b[38;5;66;03m# Записываем все строки конфигурации одним разом\u001b[39;00m\n\u001b[0;32m 145\u001b[0m config_path \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(OUTPUT_DIR, CONFIG_FILE)\n",
"Cell \u001b[1;32mIn[2], line 74\u001b[0m, in \u001b[0;36mcreate_augmentations\u001b[1;34m(symbol, font_sizes, max_rotation, augmentations, config_lines)\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(augmentations):\n\u001b[0;32m 73\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m font_size \u001b[38;5;129;01min\u001b[39;00m font_sizes:\n\u001b[1;32m---> 74\u001b[0m current_font \u001b[38;5;241m=\u001b[39m \u001b[43mImageFont\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtruetype\u001b[49m\u001b[43m(\u001b[49m\u001b[43mFONT_PATH\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfont_size\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 76\u001b[0m \u001b[38;5;66;03m# Создаём пустое изображение с белым фоном\u001b[39;00m\n\u001b[0;32m 77\u001b[0m img \u001b[38;5;241m=\u001b[39m Image\u001b[38;5;241m.\u001b[39mnew(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mL\u001b[39m\u001b[38;5;124m\"\u001b[39m, IMG_SIZE, \u001b[38;5;241m255\u001b[39m)\n",
"File \u001b[1;32mc:\\Users\\leonk\\Documents\\code\\study\\.venv\\lib\\site-packages\\PIL\\ImageFont.py:879\u001b[0m, in \u001b[0;36mtruetype\u001b[1;34m(font, size, index, encoding, layout_engine)\u001b[0m\n\u001b[0;32m 876\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m FreeTypeFont(font, size, index, encoding, layout_engine)\n\u001b[0;32m 878\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 879\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfreetype\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfont\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 880\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m:\n\u001b[0;32m 881\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_path(font):\n",
"File \u001b[1;32mc:\\Users\\leonk\\Documents\\code\\study\\.venv\\lib\\site-packages\\PIL\\ImageFont.py:876\u001b[0m, in \u001b[0;36mtruetype.<locals>.freetype\u001b[1;34m(font)\u001b[0m\n\u001b[0;32m 875\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfreetype\u001b[39m(font: StrOrBytesPath \u001b[38;5;241m|\u001b[39m BinaryIO) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m FreeTypeFont:\n\u001b[1;32m--> 876\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mFreeTypeFont\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfont\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msize\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlayout_engine\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mc:\\Users\\leonk\\Documents\\code\\study\\.venv\\lib\\site-packages\\PIL\\ImageFont.py:273\u001b[0m, in \u001b[0;36mFreeTypeFont.__init__\u001b[1;34m(self, font, size, index, encoding, layout_engine)\u001b[0m\n\u001b[0;32m 268\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfont \u001b[38;5;241m=\u001b[39m core\u001b[38;5;241m.\u001b[39mgetfont(\n\u001b[0;32m 269\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m, size, index, encoding, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfont_bytes, layout_engine\n\u001b[0;32m 270\u001b[0m )\n\u001b[0;32m 272\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_path(font):\n\u001b[1;32m--> 273\u001b[0m font \u001b[38;5;241m=\u001b[39m \u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrealpath\u001b[49m\u001b[43m(\u001b[49m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfspath\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfont\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 274\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m sys\u001b[38;5;241m.\u001b[39mplatform \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwin32\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m 275\u001b[0m font_bytes_path \u001b[38;5;241m=\u001b[39m font \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(font, \u001b[38;5;28mbytes\u001b[39m) \u001b[38;5;28;01melse\u001b[39;00m font\u001b[38;5;241m.\u001b[39mencode()\n",
"File \u001b[1;32m~\\AppData\\Local\\Programs\\Python\\Python39\\lib\\ntpath.py:664\u001b[0m, in \u001b[0;36mrealpath\u001b[1;34m(path)\u001b[0m\n\u001b[0;32m 662\u001b[0m \u001b[38;5;66;03m# Ensure that the non-prefixed path resolves to the same path\u001b[39;00m\n\u001b[0;32m 663\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 664\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43m_getfinalpathname\u001b[49m\u001b[43m(\u001b[49m\u001b[43mspath\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m==\u001b[39m path:\n\u001b[0;32m 665\u001b[0m path \u001b[38;5;241m=\u001b[39m spath\n\u001b[0;32m 666\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m ex:\n\u001b[0;32m 667\u001b[0m \u001b[38;5;66;03m# If the path does not exist and originally did not exist, then\u001b[39;00m\n\u001b[0;32m 668\u001b[0m \u001b[38;5;66;03m# strip the prefix anyway.\u001b[39;00m\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"import os\n",
"import random\n",
"from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageFilter\n",
"import cv2\n",
"import numpy as np\n",
"\n",
"# Убедитесь, что у вас установлен шрифт RoadNumbers2.0.ttf\n",
"FONT_PATH = \"RoadNumbers2.0.ttf\"\n",
"OUTPUT_DIR = \"dataset\"\n",
"CONFIG_FILE = \"dataset_config.txt\"\n",
"\n",
"# Буквы и цифры, используемые в российских номерах\n",
"SYMBOLS = \"ABEKMHOPCTYX0123456789\"\n",
"IMG_SIZE = (28, 28)\n",
"FONT_SIZES = [26, 32, 38, 46, 54, 58]\n",
"MAX_ROTATION = 15\n",
"AUGMENTATIONS = 160\n",
"\n",
"# Создаём директорию для датасета\n",
"os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
"\n",
"# Загружаем шрифт\n",
"try:\n",
" font = ImageFont.truetype(FONT_PATH, FONT_SIZES[0])\n",
"except IOError:\n",
" print(f\"Шрифт {FONT_PATH} не найден. Убедитесь, что он находится в текущей директории.\")\n",
" exit()\n",
"\n",
"# Функция расширения объектов\n",
"def expand_characters(img, kernel_size=(2, 2), iterations=1):\n",
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)\n",
" img_array = np.array(img)\n",
" expanded = cv2.dilate(img_array, kernel, iterations=iterations)\n",
" return Image.fromarray(expanded)\n",
"\n",
"# Функция для добавления случайных разрывов в контуре\n",
"def add_random_gaps(img, num_gaps=5, gap_size=5):\n",
" draw = ImageDraw.Draw(img)\n",
" for _ in range(num_gaps):\n",
" x1 = random.randint(0, IMG_SIZE[0] - gap_size)\n",
" y1 = random.randint(0, IMG_SIZE[1] - gap_size)\n",
" x2 = x1 + gap_size\n",
" y2 = y1 + gap_size\n",
" draw.rectangle([x1, y1, x2, y2], fill=255) # Белый прямоугольник для разрыва\n",
" return img\n",
"\n",
"# Функция для добавления блюра\n",
"def apply_blur(img):\n",
" blur_type = random.choice([\"box\", \"gaussian\", \"motion\"])\n",
" if blur_type == \"box\":\n",
" img = img.filter(ImageFilter.BoxBlur(random.randint(1, 4)))\n",
" elif blur_type == \"gaussian\":\n",
" img = img.filter(ImageFilter.GaussianBlur(random.randint(1, 3)))\n",
" elif blur_type == \"motion\":\n",
" kernel_size = random.randint(4, 8)\n",
" kernel_motion_blur = np.zeros((kernel_size, kernel_size))\n",
" kernel_motion_blur[int((kernel_size - 1) / 2), :] = 1\n",
" kernel_motion_blur /= kernel_size\n",
" img_array = cv2.filter2D(np.array(img), -1, kernel_motion_blur)\n",
" img = Image.fromarray(img_array)\n",
" return img\n",
"\n",
"# Функция для добавления шума\n",
"def add_noise(img, intensity=30):\n",
" img_array = np.array(img)\n",
" noise = np.random.normal(0, intensity, img_array.shape).astype(np.int32)\n",
" noisy_img = np.clip(img_array + noise, 0, 255).astype(np.uint8)\n",
" return Image.fromarray(noisy_img)\n",
"\n",
"# Функция для создания аугментаций\n",
"def create_augmentations(symbol, font_sizes, max_rotation, augmentations, config_lines):\n",
" for i in range(augmentations):\n",
" for font_size in font_sizes:\n",
" current_font = ImageFont.truetype(FONT_PATH, font_size)\n",
"\n",
" # Создаём пустое изображение с белым фоном\n",
" img = Image.new(\"L\", IMG_SIZE, 255)\n",
" draw = ImageDraw.Draw(img)\n",
"\n",
" # Получаем размеры текста с использованием Font.getbbox\n",
" text_bbox = draw.textbbox((0, 0), symbol, font=current_font)\n",
" text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]\n",
"\n",
" # Рассчитываем координаты для центрирования текста\n",
" x = (IMG_SIZE[0] - text_width) // 2 - text_bbox[0]\n",
" y = (IMG_SIZE[1] - text_height) // 2 - text_bbox[1]\n",
"\n",
" # Рисуем текст\n",
" draw.text((x, y), symbol, font=current_font, fill=0)\n",
"\n",
" # Для первого прогона символа с данным размером шрифта сохраняем без аугментаций\n",
" if i == 0:\n",
" symbol_dir = os.path.join(OUTPUT_DIR, symbol)\n",
" os.makedirs(symbol_dir, exist_ok=True)\n",
" file_path = os.path.join(symbol_dir, f\"{symbol}_{font_size}_{i}.png\")\n",
" img.save(file_path)\n",
"\n",
" config_lines.append(f\"{file_path},{symbol}\\n\")\n",
" continue\n",
"\n",
" # Случайный поворот изображения\n",
" rotation = random.uniform(-max_rotation, max_rotation)\n",
" img = img.rotate(rotation, expand=False, fillcolor=255)\n",
"\n",
" # Случайное расширение символов\n",
" if random.random() < 0.5:\n",
" kernel_size = (random.randint(1, 2), random.randint(1, 2))\n",
" iterations = 1\n",
" img = expand_characters(img, kernel_size=kernel_size, iterations=iterations)\n",
"\n",
" # Случайное добавление разрывов\n",
" if random.random() < 0.75:\n",
" num_gaps = random.randint(1, 5)\n",
" gap_size = random.randint(2, 4)\n",
" img = add_random_gaps(img, num_gaps=num_gaps, gap_size=gap_size)\n",
"\n",
" if random.random() < 0.25:\n",
" img = ImageOps.invert(img)\n",
"\n",
" # Случайное добавление блюра\n",
" if random.random() < 0.85:\n",
" img = apply_blur(img)\n",
"\n",
" # Случайное добавление шума\n",
" if random.random() < 0.1:\n",
" noise_intensity = random.randint(10, 20)\n",
" img = add_noise(img, intensity=noise_intensity)\n",
"\n",
" # Сохраняем изображение\n",
" symbol_dir = os.path.join(OUTPUT_DIR, symbol)\n",
" os.makedirs(symbol_dir, exist_ok=True)\n",
" file_path = os.path.join(symbol_dir, f\"{symbol}_{font_size}_{i}.png\")\n",
" img.save(file_path)\n",
"\n",
" config_lines.append(f\"{file_path},{symbol}\\n\")\n",
"\n",
"# Основной блок генерации датасета\n",
"def main():\n",
" config_lines = []\n",
"\n",
" for symbol in SYMBOLS:\n",
" create_augmentations(symbol, FONT_SIZES, MAX_ROTATION, AUGMENTATIONS, config_lines)\n",
"\n",
" # Записываем все строки конфигурации одним разом\n",
" config_path = os.path.join(OUTPUT_DIR, CONFIG_FILE)\n",
" with open(config_path, \"w\") as config:\n",
" config.writelines(config_lines)\n",
"\n",
" print(f\"Датасет сохранен в папке '{OUTPUT_DIR}'.\")\n",
" print(f\"Конфигурационный файл создан: {CONFIG_FILE}\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Датасет сохранен в папке 'dataset'.\n",
"Конфигурационный файл создан: dataset_config.txt\n"
]
}
],
"source": [
"import os\n",
"import random\n",
"from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageFilter\n",
"import cv2\n",
"import numpy as np\n",
"\n",
"# Убедитесь, что у вас установлен шрифт RoadNumbers2.0.ttf\n",
"FONT_PATH = \"RoadNumbers2.0.ttf\"\n",
"OUTPUT_DIR = \"dataset\"\n",
"CONFIG_FILE = \"dataset_config.txt\"\n",
"\n",
"# Буквы и цифры, используемые в российских номерах\n",
"SYMBOLS = \"ABEKMHOPCTYX0123456789\"\n",
"IMG_SIZE = (28, 28)\n",
"FONT_SIZES = [26, 32, 38, 46, 54, 58]\n",
"MAX_ROTATION = 15\n",
"AUGMENTATIONS = 160\n",
"\n",
"# Создаём директорию для датасета\n",
"os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
"\n",
"# Загружаем шрифт\n",
"try:\n",
" font = ImageFont.truetype(FONT_PATH, FONT_SIZES[0])\n",
"except IOError:\n",
" print(f\"Шрифт {FONT_PATH} не найден. Убедитесь, что он находится в текущей директории.\")\n",
" exit()\n",
"\n",
"# Функция расширения объектов\n",
"def expand_characters(img, kernel_size=(2, 2), iterations=1):\n",
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)\n",
" img_array = np.array(img)\n",
" expanded = cv2.dilate(img_array, kernel, iterations=iterations)\n",
" return Image.fromarray(expanded)\n",
"\n",
"# Функция для добавления случайных разрывов в контуре\n",
"def add_random_gaps(img, num_gaps=5, gap_size=5):\n",
" draw = ImageDraw.Draw(img)\n",
" for _ in range(num_gaps):\n",
" x1 = random.randint(0, IMG_SIZE[0] - gap_size)\n",
" y1 = random.randint(0, IMG_SIZE[1] - gap_size)\n",
" x2 = x1 + gap_size\n",
" y2 = y1 + gap_size\n",
" draw.rectangle([x1, y1, x2, y2], fill=255) # Белый прямоугольник для разрыва\n",
" return img\n",
"\n",
"# Функция для добавления блюра\n",
"def apply_blur(img):\n",
" blur_type = random.choice([\"box\", \"gaussian\", \"motion\"])\n",
" if blur_type == \"box\":\n",
" img = img.filter(ImageFilter.BoxBlur(random.randint(1, 4)))\n",
" elif blur_type == \"gaussian\":\n",
" img = img.filter(ImageFilter.GaussianBlur(random.randint(1, 3)))\n",
" elif blur_type == \"motion\":\n",
" kernel_size = random.randint(4, 8)\n",
" kernel_motion_blur = np.zeros((kernel_size, kernel_size))\n",
" kernel_motion_blur[int((kernel_size - 1) / 2), :] = 1\n",
" kernel_motion_blur /= kernel_size\n",
" img_array = cv2.filter2D(np.array(img), -1, kernel_motion_blur)\n",
" img = Image.fromarray(img_array)\n",
" return img\n",
"\n",
"# Функция для добавления шума\n",
"def add_noise(img, intensity=30):\n",
" img_array = np.array(img)\n",
" noise = np.random.normal(0, intensity, img_array.shape).astype(np.int32)\n",
" noisy_img = np.clip(img_array + noise, 0, 255).astype(np.uint8)\n",
" return Image.fromarray(noisy_img)\n",
"\n",
"# Новая функция для смещения изображения\n",
"def shift_image(img, max_shift=4):\n",
" \"\"\"\n",
" Сдвигает изображение на случайное количество пикселей по осям X и Y.\n",
"\n",
" :param img: PIL Image объект.\n",
" :param max_shift: Максимальное смещение в пикселях по каждой оси.\n",
" :return: Сдвинутое изображение.\n",
" \"\"\"\n",
" x_shift = random.randint(-max_shift, max_shift)\n",
" y_shift = random.randint(-max_shift, max_shift)\n",
" return img.transform(\n",
" img.size,\n",
" Image.AFFINE,\n",
" (1, 0, x_shift, 0, 1, y_shift),\n",
" fillcolor=255 # Заполнение пустых областей белым цветом\n",
" )\n",
"\n",
"# Функция для создания аугментаций\n",
"def create_augmentations(symbol, font_sizes, max_rotation, augmentations, config_lines):\n",
" for i in range(augmentations):\n",
" for font_size in font_sizes:\n",
" current_font = ImageFont.truetype(FONT_PATH, font_size)\n",
"\n",
" # Создаём пустое изображение с белым фоном\n",
" img = Image.new(\"L\", IMG_SIZE, 255)\n",
" draw = ImageDraw.Draw(img)\n",
"\n",
" # Получаем размеры текста с использованием Font.getbbox\n",
" text_bbox = draw.textbbox((0, 0), symbol, font=current_font)\n",
" text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]\n",
"\n",
" # Рассчитываем координаты для центрирования текста\n",
" x = (IMG_SIZE[0] - text_width) // 2 - text_bbox[0]\n",
" y = (IMG_SIZE[1] - text_height) // 2 - text_bbox[1]\n",
"\n",
" # Рисуем текст\n",
" draw.text((x, y), symbol, font=current_font, fill=0)\n",
"\n",
" # Для первого прогона символа с данным размером шрифта сохраняем без аугментаций\n",
" if i == 0:\n",
" symbol_dir = os.path.join(OUTPUT_DIR, symbol)\n",
" os.makedirs(symbol_dir, exist_ok=True)\n",
" file_path = os.path.join(symbol_dir, f\"{symbol}_{font_size}_{i}.png\")\n",
" img.save(file_path)\n",
"\n",
" config_lines.append(f\"{file_path},{symbol}\\n\")\n",
" continue\n",
"\n",
" # Случайное смещение символа\n",
" if random.random() < 0.7: # 50% вероятность применения смещения\n",
" img = shift_image(img, max_shift=4)\n",
"\n",
" # Случайный поворот изображения\n",
" rotation = random.uniform(-max_rotation, max_rotation)\n",
" img = img.rotate(rotation, expand=False, fillcolor=255)\n",
"\n",
" # Случайное расширение символов\n",
" if random.random() < 0.5:\n",
" kernel_size = (random.randint(1, 2), random.randint(1, 2))\n",
" iterations = 1\n",
" img = expand_characters(img, kernel_size=kernel_size, iterations=iterations)\n",
"\n",
" # Случайное добавление разрывов\n",
" if random.random() < 0.6:\n",
" num_gaps = random.randint(1, 5)\n",
" gap_size = random.randint(2, 4)\n",
" img = add_random_gaps(img, num_gaps=num_gaps, gap_size=gap_size)\n",
"\n",
" # Инверсия цветов с вероятностью 25%\n",
" if random.random() < 0.15:\n",
" img = ImageOps.invert(img)\n",
"\n",
" # Случайное добавление блюра\n",
" if random.random() < 0.85:\n",
" img = apply_blur(img)\n",
"\n",
" # Случайное добавление шума\n",
" if random.random() < 0.1:\n",
" noise_intensity = random.randint(10, 20)\n",
" img = add_noise(img, intensity=noise_intensity)\n",
"\n",
" # Сохраняем изображение\n",
" symbol_dir = os.path.join(OUTPUT_DIR, symbol)\n",
" os.makedirs(symbol_dir, exist_ok=True)\n",
" file_path = os.path.join(symbol_dir, f\"{symbol}_{font_size}_{i}.png\")\n",
" img.save(file_path)\n",
"\n",
" config_lines.append(f\"{file_path},{symbol}\\n\")\n",
"\n",
"# Основной блок генерации датасета\n",
"def main():\n",
" config_lines = []\n",
"\n",
" for symbol in SYMBOLS:\n",
" create_augmentations(symbol, FONT_SIZES, MAX_ROTATION, AUGMENTATIONS, config_lines)\n",
"\n",
" # Записываем все строки конфигурации одним разом\n",
" config_path = os.path.join(OUTPUT_DIR, CONFIG_FILE)\n",
" with open(config_path, \"w\") as config:\n",
" config.writelines(config_lines)\n",
"\n",
" print(f\"Датасет сохранен в папке '{OUTPUT_DIR}'.\")\n",
" print(f\"Конфигурационный файл создан: {CONFIG_FILE}\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABicAAAGZCAYAAADioeLHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA0dElEQVR4nO3deZRdVZ0+/G/NYyoJAYUAZsBIFBlcjC2TLMAWgtjYCCTAAgUVBHFqFBuVQQYVRQWFRkWCYsQlYiNoJEyKsLR10WpaEBRNdAkSIEMllaHG8/7Bm/pRVALZm2RXhs9nrfyRe+9zz77n3jr7nHrOPVVTVVUVAAAAAAAAhdSO9AAAAAAAAIAti3ICAAAAAAAoSjkBAAAAAAAUpZwAAAAAAACKUk4AAAAAAABFKScAAAAAAICilBMAAAAAAEBRygkAAAAAAKAo5QQAAAAAAFCUcgIAYAObOHFinHrqqSM9DAA2gJkzZ0ZNTU3Mnz9/pIfyoi688MKoqakZ6WEAbHbMA5BPOUExDz/8cJx00kmx/fbbR1NTU4wfPz5OPPHEePjhh0d6aABs5Fbv8D//3yte8Yo45JBDYvbs2cXH87Of/WzYeLbaaqvYb7/94jvf+U7x8QBsqubNmxdnn312vOY1r4nW1tZobW2N173udXHWWWfF3LlzR3p4G72JEycOmYuam5tjypQpce6558aiRYtGengAxTz/eOGBBx4Ydn9VVbHjjjtGTU1NHHXUUVnLuOyyy+K///u/X+ZI1y/zAJu6+pEeAFuGW2+9NaZPnx5bbbVVnHbaaTFp0qSYP39+XH/99XHLLbfEzTffHMccc8xIDxOAjdzFF18ckyZNiqqqYsGCBTFz5sw48sgj4/bbb88+yHg5zjnnnNh7770jImLhwoXxve99L0466aRYsmRJnHXWWcXHA7ApueOOO+L444+P+vr6OPHEE2P33XeP2traePTRR+PWW2+Na6+9NubNmxcTJkwY6aG+qJNPPjlOOOGEaGpqGpHl77HHHvGRj3wkIiJWrVoVDz30UHzpS1+Kn//85/HrX/96RMYEMFKam5tj1qxZccABBwy5/ec//3n84x//eFnb6ssuuyyOPfbY+Ld/+7cht5sHIJ9ygg3uL3/5S5x88skxefLkuP/++2ObbbYZvO8DH/hAHHjggXHyySfH3LlzY/LkySM4UgA2dkcccUTstddeg/8/7bTT4pWvfGV897vfHZFy4sADD4xjjz128P9nnnlmTJ48OWbNmrXByomqqmLVqlXR0tKyQZ4foIS//OUvccIJJ8SECRPinnvuie22227I/Z/97Gfjmmuuidrajf/L/nV1dVFXVzdiy99+++3jpJNOGvz/6aefHu3t7fH5z38+/vznP8eUKVM2yHKXL18ebW1tG+S5AXIdeeSR8f3vfz+uuuqqqK//f7/2nDVrVuy5557x7LPPrvdlmgcg38a/p8cm74orrogVK1bE1772tSHFRETE1ltvHdddd10sX748Pve5z0XE/7sG3qOPPhrHHXdcdHR0xLhx4+IDH/hArFq1aki+pqYmLrzwwmHLq6mpiTe96U2Dt63p8hsv/Lrfmq6919XVFdtuu23U1NTEz372s8Hb3/SmN8XrX//6Ya/185///LDrDN52220xbdq0GD9+fDQ1NcVOO+0Un/70p6O/v39Yfv78+WsdJwDDjRkzJlpaWoYceEQ8t6P8kY98JHbcccdoamqKnXfeOT7/+c9HVVUREbFy5cqYOnVqTJ06NVauXDmYW7RoUWy33Xbxxje+cY3b6ZfS2NgYY8eOHTaeF1rb9V7XdL3aiRMnxlFHHRV33nln7LXXXtHS0hLXXXdd8tgANiaf+9znYvny5XHDDTcMKyYiIurr6+Occ86JHXfccfC2uXPnxqmnnhqTJ0+O5ubm2HbbbeNd73pXLFy4cEj21FNPjYkTJw57zjVte++666444IADYsyYMdHe3h4777xz/Od//ueQx1x99dWxyy67RGtra4wdOzb22muvmDVr1uD9a9p2r+sxwOrjikceeSQOOeSQaG1tje23337w2CjXtttuGxHxovPR6mOPmTNnDrvvhcdZq9fdI488EjNmzIixY8cOOysZYGMwffr0WLhwYdx1112Dt/X09MQtt9wSM2bMWGPmpY4dIp7bLi5fvjxuvPHGwd/TrP6bcmv7mxPXXHNN7LLLLoOXNj/rrLNiyZIlQx5jHmBL55sTbHC33357TJw4MQ488MA13n/QQQfFxIkT48c//vGQ24877riYOHFiXH755fGrX/0qrrrqqli8eHF861vfWuuylixZEpdffvla73/+5TdW23nnndf6+C984QuxYMGCtd6/LmbOnBnt7e3x4Q9/ONrb2+Pee++NT33qU7F06dK44oor1ph5z3veM7i+br311vjhD3/4ssYAsLno7OyMZ599Nqqqiqeffjquvvrq6OrqGnKmUFVVcfTRR8d9990Xp512Wuyxxx5x5513xrnnnhtPPPFEfPGLX4yWlpa48cYbY//994/zzz8/rrzyyoiIOOuss6KzszNmzpy5Tmc/LVu2bPDsq0WLFsWsWbPiD3/4Q1x//fXr9XU/9thjMX369Hjve98b7373u1907gLYFNxxxx3x6le/Ovbdd991ztx1113x17/+Nd75znfGtttuGw8//HB87Wtfi4cffjh+9atfJZ/Q8/DDD8dRRx0Vu+22W1x88cXR1NQUjz/+eDz44IODj/n6178e55xzThx77LGDJ0vNnTs3/ud//metv+SKSDsGWLx4cbzlLW+Jt7/97XHcccfFLbfcEh/72Mdi1113jSOOOOIlX0dvb+/gXLRq1ar47W9/G1deeWUcdNBBMWnSpKR18lLe8Y53xJQpU+Kyyy4b8ks7gI3FxIkT41/+5V/iu9/97uA2dPbs2dHZ2RknnHBCXHXVVUMevy7HDhER3/72t+P000+PffbZJ97znvdERMROO+201nFceOGFcdFFF8Vhhx0WZ555Zjz22GNx7bXXxm9+85t48MEHo6GhYfCx5gG2ZMoJNqjOzs548skn421ve9uLPm633XaLH/3oR7Fs2bLB2yZNmhS33XZbRDz3y6KOjo645ppr4j/+4z9it912W+PzXH755dHQ0BB77rnnGu9/4eU3XswzzzwTX/jCF+KII454WX9sddasWUMuvXHGGWfEGWecEddcc01ccsklQ65J2NfXFxER+++//+Av2h5//HHlBMD/77DDDhvy/6ampvjmN78Zhx9++OBtP/rRj+Lee++NSy65JM4///yIeG4eecc73hFf/vKX4+yzz46ddtop9t133/joRz8an/3sZ+OYY46JBQsWxM033xxf+tKX4jWvec06jedd73rXkP/X1tbGpZdeOuz2l+vxxx+Pn/70p/Gv//qv6/V5AUbC0qVL48knnxx2ze6I5042Wr1PHBHR1tY2uC/9vve9b/Ca2qvtt99+MX369HjggQfWejLU2tx1113R09MTs2fPjq233nqNj/nxj38cu+yyS3z/+99Peu6UY4Ann3wyvvWtb8XJJ58cEc9dsnDChAlx/fXXr9MvpebMmTPsG+r7779/3HrrrUljXhe77777kG+NAGyMZsyYER//+Mdj5cqV0dLSEt/5znfi4IMPjvHjxw977LoeO5x00klxxhlnxOTJk4ecGLUmzzzzTFx++eXx5je/OWbPnj14icKpU6fG2WefHTfddFO8853vHHy8eYAtmcs6sUGtLhtGjRr1oo9bff/SpUsHb3vhtbrf//73R0TET37ykzU+xxNPPBFXX311fPKTn4z29vbsMa/26U9/OkaPHh3nnHPOGu/v7++PZ599dsi/FStWDHvc8w9KVp9he+CBB8aKFSvi0UcfHfLYnp6eiIgR+yNKABu7r371q3HXXXfFXXfdFTfddFMccsghcfrppw/Z8f7JT34SdXV1w7bfH/nIR6KqqiGF84UXXhi77LJLnHLKKfG+970vDj744LVu99fkU5/61OB4vve978X06dPj/PPPjy9/+csv/8U+z6RJkxQTwGZj9T7/mvbZ3/SmN8U
"text/plain": [
"<Figure size 1600x400 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import os\n",
"import random\n",
"import numpy as np\n",
"import cv2\n",
"from PIL import Image, ImageDraw, ImageFont, ImageFilter\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Путь к шрифту\n",
"FONT_PATH = \"RoadNumbers2.0.ttf\" # Убедитесь, что шрифт находится в текущей директории\n",
"FONT_SIZE = 26 # Размер шрифта (можно изменить при необходимости)\n",
"IMG_SIZE = (28, 28) # Размер изображения\n",
"\n",
"# Функция для создания изображения с символом\n",
"def create_symbol_image(symbol, font_path, font_size, img_size):\n",
" try:\n",
" font = ImageFont.truetype(font_path, font_size)\n",
" except IOError:\n",
" print(f\"Шрифт {font_path} не найден. Убедитесь, что он находится в текущей директории.\")\n",
" exit()\n",
" \n",
" # Создаём пустое изображение с белым фоном\n",
" img = Image.new(\"L\", img_size, 255) # \"L\" для градаций серого\n",
" draw = ImageDraw.Draw(img)\n",
" \n",
" # Получаем размеры текста\n",
" text_bbox = draw.textbbox((0, 0), symbol, font=font)\n",
" text_width = text_bbox[2] - text_bbox[0]\n",
" text_height = text_bbox[3] - text_bbox[1]\n",
" \n",
" # Рассчитываем координаты для центрирования текста\n",
" x = (img_size[0] - text_width) // 2 - text_bbox[0]\n",
" y = (img_size[1] - text_height) // 2 - text_bbox[1]\n",
" \n",
" # Рисуем текст\n",
" draw.text((x, y), symbol, font=font, fill=0) # Черный цвет\n",
" \n",
" return img\n",
"\n",
"# Функция для применения блюра\n",
"def apply_blur(img, blur_type):\n",
" if blur_type == \"box\":\n",
" radius = 4\n",
" blurred_img = img.filter(ImageFilter.BoxBlur(radius))\n",
" elif blur_type == \"gaussian\":\n",
" radius = 3\n",
" blurred_img = img.filter(ImageFilter.GaussianBlur(radius))\n",
" elif blur_type == \"motion\":\n",
" kernel_size = 8\n",
" # Создаём ядро для motion blur (размытие по горизонтали)\n",
" kernel_motion_blur = np.zeros((kernel_size, kernel_size))\n",
" kernel_motion_blur[int((kernel_size - 1) / 2), :] = 1\n",
" kernel_motion_blur /= kernel_size\n",
" img_array = np.array(img)\n",
" blurred_array = cv2.filter2D(img_array, -1, kernel_motion_blur)\n",
" blurred_img = Image.fromarray(blurred_array)\n",
" else:\n",
" raise ValueError(\"Неподдерживаемый тип блюра.\")\n",
" \n",
" return blurred_img\n",
"\n",
"# Основная функция\n",
"def main():\n",
" symbol = '7' # Цифра для обработки\n",
" blur_types = [\"box\", \"gaussian\", \"motion\"]\n",
" \n",
" # Создаём оригинальное изображение\n",
" original_img = create_symbol_image(symbol, FONT_PATH, FONT_SIZE, IMG_SIZE)\n",
" \n",
" # Применяем каждый тип блюра\n",
" blurred_images = {}\n",
" for blur_type in blur_types:\n",
" blurred_img = apply_blur(original_img, blur_type)\n",
" blurred_images[blur_type] = blurred_img\n",
" \n",
" # Настройка отображения с помощью matplotlib\n",
" num_blurs = len(blur_types)\n",
" plt.figure(figsize=(4 * (num_blurs + 1), 4)) # Размер фигуры зависит от количества изображений\n",
" \n",
" # Отображаем оригинальное изображение\n",
" plt.subplot(1, num_blurs + 1, 1)\n",
" plt.imshow(original_img, cmap='gray')\n",
" plt.title(\"Оригинал\")\n",
" plt.axis('off')\n",
" \n",
" # Отображаем размытые изображения\n",
" for idx, blur_type in enumerate(blur_types, start=2):\n",
" plt.subplot(1, num_blurs + 1, idx)\n",
" plt.imshow(blurred_images[blur_type], cmap='gray')\n",
" plt.title(f\"{blur_type.capitalize()} Blur\")\n",
" plt.axis('off')\n",
" \n",
" plt.tight_layout()\n",
" plt.show()\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import torch\n",
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"from torch.utils.data import DataLoader, Dataset\n",
"from torchvision import transforms\n",
"from PIL import Image\n",
"\n",
"DATASET_DIR = \"dataset\"\n",
"SAVE_PATH = \"best_model_3.pth\"\n",
"BATCH_SIZE = 32\n",
"EPOCHS = 30\n",
"LEARNING_RATE = 0.001\n",
"DEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"CLASSES = \"ABEKMHOPCTYX0123456789\"\n",
"NUM_CLASSES = len(CLASSES)\n",
"CLASS_TO_IDX = {char: idx for idx, char in enumerate(CLASSES)}\n",
"IDX_TO_CLASS = {idx: char for char, idx in CLASS_TO_IDX.items()}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Размер тренировочного набора: 16896\n",
"Размер тестового набора: 4224\n"
]
}
],
"source": [
"from sklearn.model_selection import train_test_split\n",
"from torch.utils.data import Subset\n",
"\n",
"class LicensePlateDataset(Dataset):\n",
" def __init__(self, dataset_dir, transform=None):\n",
" self.data = []\n",
" self.transform = transform\n",
" with open(os.path.join(dataset_dir, \"dataset_config.txt\"), \"r\") as file:\n",
" for line in file:\n",
" path, label = line.strip().split(\",\")\n",
" self.data.append((path, CLASS_TO_IDX[label]))\n",
"\n",
" def __len__(self):\n",
" return len(self.data)\n",
"\n",
" def __getitem__(self, idx):\n",
" img_path, label = self.data[idx]\n",
" image = Image.open(img_path).convert(\"L\")\n",
" if self.transform:\n",
" image = self.transform(image)\n",
" return image, label\n",
"\n",
"\n",
"transform = transforms.Compose([\n",
" transforms.ColorJitter(brightness=0.4, contrast=0.4),\n",
" transforms.ToTensor(),\n",
" transforms.Normalize(mean=[0.5], std=[0.5]),\n",
"])\n",
"\n",
"full_dataset = LicensePlateDataset(DATASET_DIR, transform=transform)\n",
"\n",
"indices = list(range(len(full_dataset)))\n",
"train_indices, test_indices = train_test_split(indices, test_size=0.2, random_state=42)\n",
"\n",
"train_dataset = Subset(full_dataset, train_indices)\n",
"test_dataset = Subset(full_dataset, test_indices)\n",
"\n",
"train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)\n",
"test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)\n",
"\n",
"print(f\"Размер тренировочного набора: {len(train_dataset)}\")\n",
"print(f\"Размер тестового набора: {len(test_dataset)}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"import torch.nn.functional as F\n",
"\n",
"class SimpleCNN(nn.Module):\n",
" def __init__(self, num_classes):\n",
" super(SimpleCNN, self).__init__()\n",
" self.conv_layers = nn.Sequential(\n",
" nn.Conv2d(1, 32, kernel_size=3, padding=1), # Увеличиваем количество фильтров\n",
" nn.LeakyReLU(negative_slope=0.1), # Используем LeakyReLU вместо ReLU\n",
" nn.BatchNorm2d(32), # Нормализация\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" \n",
" nn.Conv2d(32, 64, kernel_size=3, padding=1),\n",
" nn.ELU(), # Используем ELU вместо ReLU\n",
" nn.BatchNorm2d(64),\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" \n",
" nn.Conv2d(64, 128, kernel_size=3, padding=1),\n",
" nn.SiLU(), # Используем SiLU (Swish) вместо ReLU\n",
" nn.BatchNorm2d(128),\n",
" nn.MaxPool2d(kernel_size=2, stride=2)\n",
" )\n",
" self.fc_layers = nn.Sequential(\n",
" nn.Linear(128 * 3 * 3, 256),\n",
" nn.GELU(), # Используем GELU для полносвязного слоя\n",
" nn.Dropout(0.5),\n",
" nn.Linear(256, num_classes)\n",
" )\n",
"\n",
" def forward(self, x):\n",
" x = self.conv_layers(x)\n",
" x = x.view(x.size(0), -1)\n",
" x = self.fc_layers(x)\n",
" return x\n",
"\n",
"model = SimpleCNN(len(CLASSES)).to(DEVICE)\n",
"criterion = nn.CrossEntropyLoss(label_smoothing=0.1)\n",
"#criterion = nn.CrossEntropyLoss()\n",
"optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9, weight_decay=1e-4)\n",
"#optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE, weight_decay=1e-4)\n",
"\n",
"#optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch [1/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Training: 0%| | 0/528 [00:00<?, ?it/s]"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 2.2562, Train Accuracy: 41.64%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 1.5400, Test Accuracy: 72.47%\n",
"Best model saved with test accuracy: 72.47%\n",
"Epoch [2/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 1.4775, Train Accuracy: 72.54%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 1.2487, Test Accuracy: 82.43%\n",
"Best model saved with test accuracy: 82.43%\n",
"Epoch [3/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 1.2714, Train Accuracy: 80.63%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 1.1178, Test Accuracy: 86.65%\n",
"Best model saved with test accuracy: 86.65%\n",
"Epoch [4/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 1.1700, Train Accuracy: 84.59%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 1.0693, Test Accuracy: 88.02%\n",
"Best model saved with test accuracy: 88.02%\n",
"Epoch [5/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 1.0998, Train Accuracy: 87.51%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 1.0128, Test Accuracy: 89.89%\n",
"Best model saved with test accuracy: 89.89%\n",
"Epoch [6/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 1.0531, Train Accuracy: 89.01%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.9741, Test Accuracy: 90.77%\n",
"Best model saved with test accuracy: 90.77%\n",
"Epoch [7/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 1.0165, Train Accuracy: 90.26%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.9434, Test Accuracy: 92.14%\n",
"Best model saved with test accuracy: 92.14%\n",
"Epoch [8/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.9889, Train Accuracy: 91.57%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.9299, Test Accuracy: 93.42%\n",
"Best model saved with test accuracy: 93.42%\n",
"Epoch [9/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.9668, Train Accuracy: 92.20%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.9245, Test Accuracy: 92.57%\n",
"Epoch [10/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.9427, Train Accuracy: 92.90%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.9001, Test Accuracy: 92.87%\n",
"Epoch [11/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.9336, Train Accuracy: 92.98%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8980, Test Accuracy: 93.11%\n",
"Epoch [12/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.9161, Train Accuracy: 93.77%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8720, Test Accuracy: 94.34%\n",
"Best model saved with test accuracy: 94.34%\n",
"Epoch [13/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.9031, Train Accuracy: 94.06%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8702, Test Accuracy: 93.92%\n",
"Epoch [14/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8949, Train Accuracy: 94.36%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8564, Test Accuracy: 94.79%\n",
"Best model saved with test accuracy: 94.79%\n",
"Epoch [15/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8834, Train Accuracy: 94.86%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8516, Test Accuracy: 94.77%\n",
"Epoch [16/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8761, Train Accuracy: 94.99%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8566, Test Accuracy: 94.11%\n",
"Epoch [17/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8690, Train Accuracy: 95.05%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8463, Test Accuracy: 94.86%\n",
"Best model saved with test accuracy: 94.86%\n",
"Epoch [18/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8607, Train Accuracy: 95.47%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8430, Test Accuracy: 94.77%\n",
"Epoch [19/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8550, Train Accuracy: 95.57%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8356, Test Accuracy: 94.84%\n",
"Epoch [20/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8473, Train Accuracy: 95.70%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8284, Test Accuracy: 94.72%\n",
"Epoch [21/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8450, Train Accuracy: 95.86%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8287, Test Accuracy: 95.08%\n",
"Best model saved with test accuracy: 95.08%\n",
"Epoch [22/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8378, Train Accuracy: 96.04%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8208, Test Accuracy: 95.36%\n",
"Best model saved with test accuracy: 95.36%\n",
"Epoch [23/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8315, Train Accuracy: 96.20%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8261, Test Accuracy: 95.31%\n",
"Epoch [24/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8314, Train Accuracy: 96.29%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8259, Test Accuracy: 95.38%\n",
"Best model saved with test accuracy: 95.38%\n",
"Epoch [25/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8240, Train Accuracy: 96.51%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8189, Test Accuracy: 94.91%\n",
"Epoch [26/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8187, Train Accuracy: 96.73%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8071, Test Accuracy: 95.41%\n",
"Best model saved with test accuracy: 95.41%\n",
"Epoch [27/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8145, Train Accuracy: 96.63%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8207, Test Accuracy: 95.27%\n",
"Epoch [28/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8134, Train Accuracy: 96.76%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8146, Test Accuracy: 95.50%\n",
"Best model saved with test accuracy: 95.50%\n",
"Epoch [29/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8097, Train Accuracy: 96.77%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8127, Test Accuracy: 95.12%\n",
"Epoch [30/30]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.8054, Train Accuracy: 96.92%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.8038, Test Accuracy: 95.55%\n",
"Best model saved with test accuracy: 95.55%\n",
"Обучение завершено.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r"
]
}
],
"source": [
"from tqdm import tqdm\n",
"\n",
"best_accuracy = 0.0\n",
"for epoch in range(EPOCHS):\n",
" model.train()\n",
" running_loss = 0.0\n",
" correct_train = 0\n",
" total_train = 0\n",
"\n",
" print(f\"Epoch [{epoch+1}/{EPOCHS}]\")\n",
" train_bar = tqdm(train_loader, desc=\"Training\", leave=False)\n",
"\n",
" for images, labels in train_bar:\n",
" images, labels = images.to(DEVICE), labels.to(DEVICE)\n",
" optimizer.zero_grad()\n",
" outputs = model(images)\n",
" loss = criterion(outputs, labels)\n",
" loss.backward()\n",
" optimizer.step()\n",
"\n",
" running_loss += loss.item()\n",
" _, predicted = torch.max(outputs, 1)\n",
" total_train += labels.size(0)\n",
" correct_train += (predicted == labels).sum().item()\n",
"\n",
" train_bar.set_postfix(loss=f\"{running_loss/len(train_loader):.4f}\", acc=f\"{100 * correct_train / total_train:.2f}%\")\n",
"\n",
" train_accuracy = 100 * correct_train / total_train\n",
" print(f\"Train Loss: {running_loss/len(train_loader):.4f}, Train Accuracy: {train_accuracy:.2f}%\")\n",
"\n",
" model.eval()\n",
" correct_test = 0\n",
" total_test = 0\n",
" test_loss = 0.0\n",
"\n",
" with torch.no_grad():\n",
" test_bar = tqdm(test_loader, desc=\"Testing\", leave=False)\n",
" for images, labels in test_bar:\n",
" images, labels = images.to(DEVICE), labels.to(DEVICE)\n",
"\n",
" outputs = model(images)\n",
" loss = criterion(outputs, labels)\n",
" test_loss += loss.item()\n",
"\n",
" _, predicted = torch.max(outputs, 1)\n",
" total_test += labels.size(0)\n",
" correct_test += (predicted == labels).sum().item()\n",
"\n",
" test_accuracy = 100 * correct_test / total_test\n",
" print(f\"Test Loss: {test_loss/len(test_loader):.4f}, Test Accuracy: {test_accuracy:.2f}%\")\n",
"\n",
" if test_accuracy > best_accuracy:\n",
" best_accuracy = test_accuracy\n",
" torch.save(model.state_dict(), SAVE_PATH)\n",
" print(f\"Best model saved with test accuracy: {best_accuracy:.2f}%\")\n",
"\n",
"print(\"Обучение завершено.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"\n",
"class ResidualBlock(nn.Module):\n",
" def __init__(self, in_channels, out_channels, stride=1):\n",
" super(ResidualBlock, self).__init__()\n",
" self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)\n",
" self.bn1 = nn.BatchNorm2d(out_channels)\n",
" self.relu = nn.ReLU(inplace=True)\n",
" self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)\n",
" self.bn2 = nn.BatchNorm2d(out_channels)\n",
"\n",
" self.shortcut = nn.Sequential()\n",
" if stride != 1 or in_channels != out_channels:\n",
" self.shortcut = nn.Sequential(\n",
" nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride),\n",
" nn.BatchNorm2d(out_channels)\n",
" )\n",
"\n",
" def forward(self, x):\n",
" out = self.relu(self.bn1(self.conv1(x)))\n",
" out = self.bn2(self.conv2(out))\n",
" out += self.shortcut(x)\n",
" out = self.relu(out)\n",
" return out\n",
"\n",
"class EnhancedCNN(nn.Module):\n",
" def __init__(self, num_classes):\n",
" super(EnhancedCNN, self).__init__()\n",
" self.conv_layers = nn.Sequential(\n",
" nn.Conv2d(1, 32, kernel_size=3, padding=1),\n",
" nn.LeakyReLU(0.1),\n",
" nn.BatchNorm2d(32),\n",
" nn.MaxPool2d(2, 2),\n",
" nn.Dropout2d(0.25),\n",
"\n",
" ResidualBlock(32, 64),\n",
" nn.MaxPool2d(2, 2),\n",
" nn.Dropout2d(0.25),\n",
"\n",
" ResidualBlock(64, 128),\n",
" nn.MaxPool2d(2, 2),\n",
" nn.Dropout2d(0.25),\n",
"\n",
" nn.AdaptiveAvgPool2d((1, 1))\n",
" )\n",
" self.fc_layers = nn.Sequential(\n",
" nn.Linear(128, 256),\n",
" nn.GELU(),\n",
" nn.Dropout(0.5),\n",
" nn.Linear(256, num_classes)\n",
" )\n",
"\n",
" def forward(self, x):\n",
" x = self.conv_layers(x)\n",
" x = x.view(x.size(0), -1)\n",
" x = self.fc_layers(x)\n",
" return x\n",
"\n",
"model = EnhancedCNN(len(CLASSES)).to(DEVICE)\n",
"criterion = nn.CrossEntropyLoss() # Или FocalLoss(), если необходимо\n",
"optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE, weight_decay=1e-4)\n",
"scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Epoch [1/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 2.9490, Train Accuracy: 9.62%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 2.2683, Test Accuracy: 31.25%\n",
"Best model saved with test accuracy: 31.25%\n",
"Улучшение найдено: 31.2500. Сохранение модели...\n",
"\n",
"Epoch [2/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 1.6123, Train Accuracy: 48.72%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.4703, Test Accuracy: 89.68%\n",
"Best model saved with test accuracy: 89.68%\n",
"Улучшение найдено: 89.6780. Сохранение модели...\n",
"\n",
"Epoch [3/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.6699, Train Accuracy: 79.42%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.2471, Test Accuracy: 93.47%\n",
"Best model saved with test accuracy: 93.47%\n",
"Улучшение найдено: 93.4659. Сохранение модели...\n",
"\n",
"Epoch [4/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.4096, Train Accuracy: 87.37%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.1863, Test Accuracy: 95.03%\n",
"Best model saved with test accuracy: 95.03%\n",
"Улучшение найдено: 95.0284. Сохранение модели...\n",
"\n",
"Epoch [5/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.2973, Train Accuracy: 91.12%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.1082, Test Accuracy: 96.83%\n",
"Best model saved with test accuracy: 96.83%\n",
"Улучшение найдено: 96.8277. Сохранение модели...\n",
"\n",
"Epoch [6/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.2404, Train Accuracy: 92.58%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.1003, Test Accuracy: 97.44%\n",
"Best model saved with test accuracy: 97.44%\n",
"Улучшение найдено: 97.4432. Сохранение модели...\n",
"\n",
"Epoch [7/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.1923, Train Accuracy: 94.24%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.0840, Test Accuracy: 97.63%\n",
"Best model saved with test accuracy: 97.63%\n",
"Улучшение найдено: 97.6326. Сохранение модели...\n",
"\n",
"Epoch [8/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.1699, Train Accuracy: 94.70%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.0862, Test Accuracy: 97.59%\n",
"Ранняя остановка через 9 эпох(и).\n",
"\n",
"Epoch [9/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.1481, Train Accuracy: 95.53%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.0703, Test Accuracy: 97.54%\n",
"Ранняя остановка через 8 эпох(и).\n",
"\n",
"Epoch [10/10]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Loss: 0.1311, Train Accuracy: 95.96%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Loss: 0.0719, Test Accuracy: 97.82%\n",
"Best model saved with test accuracy: 97.82%\n",
"Улучшение найдено: 97.8220. Сохранение модели...\n",
"Обучение завершено.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r"
]
}
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"from tqdm import tqdm\n",
"\n",
"# Предполагается, что следующие переменные уже определены:\n",
"# model, train_loader, test_loader, DEVICE, EPOCHS, SAVE_PATH, CLASSES\n",
"\n",
"# Определение функции ранней остановки\n",
"class EarlyStopping:\n",
" def __init__(self, patience=10, verbose=False, delta=0):\n",
" \"\"\"\n",
" Args:\n",
" patience (int): Количество эпох без улучшения перед остановкой.\n",
" verbose (bool): Выводить сообщения о сохранении модели.\n",
" delta (float): Минимальное улучшение для учёта.\n",
" \"\"\"\n",
" self.patience = patience\n",
" self.verbose = verbose\n",
" self.delta = delta\n",
" self.best_score = None\n",
" self.early_stop = False\n",
" self.best_accuracy = 0.0\n",
"\n",
" def __call__(self, score, model):\n",
" if self.best_score is None:\n",
" self.best_score = score\n",
" self.save_checkpoint(score, model)\n",
" elif score < self.best_score + self.delta:\n",
" self.patience -= 1\n",
" if self.verbose:\n",
" print(f\"Ранняя остановка через {self.patience} эпох(и).\")\n",
" if self.patience <= 0:\n",
" self.early_stop = True\n",
" else:\n",
" self.best_score = score\n",
" self.save_checkpoint(score, model)\n",
" self.patience = 10 # Сброс patience\n",
"\n",
" def save_checkpoint(self, score, model):\n",
" if self.verbose:\n",
" print(f\"Улучшение найдено: {score:.4f}. Сохранение модели...\")\n",
" torch.save(model.state_dict(), SAVE_PATH)\n",
" self.best_accuracy = score\n",
"\n",
"# Инициализация функции потерь, оптимизатора и планировщика\n",
"criterion = nn.CrossEntropyLoss() # Или используйте FocalLoss(), если необходимо\n",
"optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE, weight_decay=1e-4)\n",
"scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)\n",
"early_stopping = EarlyStopping(patience=10, verbose=True)\n",
"\n",
"best_accuracy = 0.0\n",
"\n",
"for epoch in range(1, EPOCHS + 1):\n",
" model.train()\n",
" running_loss = 0.0\n",
" correct_train = 0\n",
" total_train = 0\n",
"\n",
" print(f\"\\nEpoch [{epoch}/{EPOCHS}]\")\n",
" train_bar = tqdm(train_loader, desc=\"Training\", leave=False)\n",
"\n",
" for images, labels in train_bar:\n",
" images, labels = images.to(DEVICE), labels.to(DEVICE)\n",
" \n",
" optimizer.zero_grad()\n",
" outputs = model(images)\n",
" loss = criterion(outputs, labels)\n",
" loss.backward()\n",
" optimizer.step()\n",
"\n",
" running_loss += loss.item()\n",
" _, predicted = torch.max(outputs, 1)\n",
" total_train += labels.size(0)\n",
" correct_train += (predicted == labels).sum().item()\n",
"\n",
" current_loss = running_loss / (train_bar.n + 1)\n",
" current_acc = 100 * correct_train / total_train\n",
" train_bar.set_postfix(loss=f\"{current_loss:.4f}\", acc=f\"{current_acc:.2f}%\")\n",
"\n",
" train_loss = running_loss / len(train_loader)\n",
" train_accuracy = 100 * correct_train / total_train\n",
" print(f\"Train Loss: {train_loss:.4f}, Train Accuracy: {train_accuracy:.2f}%\")\n",
"\n",
" # Валидация\n",
" model.eval()\n",
" correct_test = 0\n",
" total_test = 0\n",
" test_loss = 0.0\n",
"\n",
" with torch.no_grad():\n",
" test_bar = tqdm(test_loader, desc=\"Testing\", leave=False)\n",
" for images, labels in test_bar:\n",
" images, labels = images.to(DEVICE), labels.to(DEVICE)\n",
"\n",
" outputs = model(images)\n",
" loss = criterion(outputs, labels)\n",
" test_loss += loss.item()\n",
"\n",
" _, predicted = torch.max(outputs, 1)\n",
" total_test += labels.size(0)\n",
" correct_test += (predicted == labels).sum().item()\n",
"\n",
" test_loss /= len(test_loader)\n",
" test_accuracy = 100 * correct_test / total_test\n",
" print(f\"Test Loss: {test_loss:.4f}, Test Accuracy: {test_accuracy:.2f}%\")\n",
"\n",
" # Обновление планировщика\n",
" scheduler.step()\n",
"\n",
" # Проверка и обновление лучших результатов\n",
" if test_accuracy > best_accuracy:\n",
" best_accuracy = test_accuracy\n",
" torch.save(model.state_dict(), SAVE_PATH)\n",
" print(f\"Best model saved with test accuracy: {best_accuracy:.2f}%\")\n",
"\n",
" # Применение ранней остановки\n",
" early_stopping(test_accuracy, model)\n",
" if early_stopping.early_stop:\n",
" print(\"Ранняя остановка активирована. Обучение завершено.\")\n",
" break\n",
"\n",
"print(\"Обучение завершено.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train Error Statistics: defaultdict(<class 'int'>, {6: 19, 12: 7, 1: 8, 10: 8, 7: 4, 20: 8, 3: 6, 2: 8, 9: 4, 19: 9, 11: 8, 16: 9, 17: 8, 0: 5, 13: 4, 18: 8, 21: 2, 8: 1, 5: 5, 4: 7, 14: 1})\n",
"Train Error Percentages: {6: 2.5299600532623168, 12: 0.8962868117797695, 1: 1.0062893081761006, 10: 1.0296010296010296, 7: 0.5161290322580645, 20: 1.0430247718383312, 3: 0.7905138339920948, 2: 1.06951871657754, 9: 0.5221932114882507, 19: 1.1583011583011582, 11: 1.0309278350515463, 16: 1.1450381679389312, 17: 1.0610079575596816, 0: 0.628140703517588, 13: 0.5263157894736842, 18: 1.0526315789473684, 21: 0.26246719160104987, 8: 0.13315579227696406, 5: 0.6459948320413437, 4: 0.9283819628647214, 14: 0.12919896640826875}\n",
"Test Error Statistics: defaultdict(<class 'int'>, {10: 2, 2: 3, 5: 3, 16: 4, 17: 4, 20: 8, 19: 3, 14: 1, 6: 5, 11: 3, 13: 4, 0: 3, 1: 5, 15: 2, 21: 3, 4: 3, 9: 4, 18: 2, 7: 3, 12: 2})\n",
"Test Error Percentages: {10: 1.092896174863388, 2: 1.4150943396226416, 5: 1.6129032258064515, 16: 2.2988505747126435, 17: 1.9417475728155338, 20: 4.145077720207254, 19: 1.639344262295082, 14: 0.5376344086021506, 6: 2.3923444976076556, 11: 1.6304347826086956, 13: 2.0, 0: 1.8292682926829267, 1: 3.0303030303030303, 15: 0.966183574879227, 21: 1.5151515151515151, 4: 1.4563106796116505, 9: 2.0618556701030926, 18: 1.0, 7: 1.6216216216216217, 12: 1.1173184357541899}\n"
]
}
],
"source": [
"import torch\n",
"from collections import defaultdict\n",
"\n",
"def calculate_error_statistics(model, loader, device):\n",
" \"\"\"\n",
" Вычисляет общую статистику ошибок для датасета (train_loader/test_loader).\n",
"\n",
" :param model: обученная модель\n",
" :param loader: DataLoader для вычисления статистики\n",
" :param device: устройство ('cuda' или 'cpu')\n",
" :return: словарь с количеством ошибок для каждого класса\n",
" \"\"\"\n",
" model.eval()\n",
" error_stats = defaultdict(int) # Хранение ошибок для каждого класса\n",
" total_samples = defaultdict(int) # Общее количество образцов для каждого класса\n",
"\n",
" with torch.no_grad():\n",
" for inputs, targets in loader:\n",
" inputs, targets = inputs.to(device), targets.to(device)\n",
" outputs = model(inputs)\n",
" predictions = torch.argmax(outputs, dim=1)\n",
"\n",
" for pred, target in zip(predictions, targets):\n",
" total_samples[target.item()] += 1\n",
" if pred.item() != target.item():\n",
" error_stats[target.item()] += 1\n",
"\n",
" error_percentages = {\n",
" cls: (errors / total_samples[cls] * 100) if total_samples[cls] > 0 else 0\n",
" for cls, errors in error_stats.items()\n",
" }\n",
"\n",
" return error_stats, error_percentages\n",
"\n",
"train_errors, train_error_percentages = calculate_error_statistics(model, train_loader, DEVICE)\n",
"test_errors, test_error_percentages = calculate_error_statistics(model, test_loader, DEVICE)\n",
"\n",
"print(\"Train Error Statistics:\", train_errors)\n",
"print(\"Train Error Percentages:\", train_error_percentages)\n",
"print(\"Test Error Statistics:\", test_errors)\n",
"print(\"Test Error Percentages:\", test_error_percentages)\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABdEAAACfCAYAAAAWP5CxAAAAOXRFWHRTb2Z0d2FyZQBNYXR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mT+zVg/9J3/yJ8Pb3/728Ja3vCX84A/+4MrPGmOMMV/PLB+AL3MtB+Dvete7wi/+4i9edc0dwh8cgF+5ciV8y7d8y1Vl4e666y75/B/2AJyMRqMQwh94wxtj/uixJboxX2Oejm5lxhhjzNOBu+++O/yVv/JXwn/4D/8h7O3thZe97GXh05/+dHjve98bXvOa14gH2TJbW1vh3LlzIcuyYy3hvvu7vzt84AMfCG9605vCJz7xifDN3/zNoSzL8Hu/93vhAx/4QPjYxz4m84vnP//54du+7dtEii2EEN72tretfIYPfehD4c1vfnN45jOfGZ7znOeEn/3Zn5Xrr3zlKx93FTfGGGO+3nn1q18d3vKWt4R3vvOd4d//+3//+N+v5QD87/29vxduuOGGlQfgv/zLvxze9a53he/7vu+Ta6PRKCwWi9Butx//21cOwF/wgheEEI4OwF/1qletPAC/dOlS2NnZieYQP/MzPxNCCFfdfzDG/OHxJroxTzGuR7cyY4wx5nrlZ37mZ8Ltt98e3vOe94QPfehD4cyZM+Ef/sN/GH7kR35k5fd6vd7K62mahl/6pV8K73jHO8L73ve+8KEPfSi0Wq1w++23hx/8wR98PMDoV3jZy14WXvKSl4S3ve1t4cEHHwzPfe5zw3ve857I84187nOfCyH8wYH7d3/3d0fXP/GJT3gT3RhjzHXD9XAA/rM/+7P
"text/plain": [
"<Figure size 1500x500 with 3 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import cv2\n",
"from matplotlib import pyplot as plt\n",
"\n",
"# Функция для упорядочивания точек в определенном порядке: верхний левый, верхний правый, нижний правый, нижний левый\n",
"def order_points(pts):\n",
" \"\"\"\n",
" Упорядочивает точки в порядке:\n",
" верхний левый, верхний правый, нижний правый, нижний левый\n",
" \"\"\"\n",
" rect = np.zeros((4, 2), dtype=\"float32\")\n",
"\n",
" # Сумма координат для нахождения верхнего левого и нижнего правого углов\n",
" s = pts.sum(axis=1)\n",
" rect[0] = pts[np.argmin(s)] # верхний левый\n",
" rect[2] = pts[np.argmax(s)] # нижний правый\n",
"\n",
" # Разница координат для нахождения верхнего правого и нижнего левого углов\n",
" diff = np.diff(pts, axis=1)\n",
" rect[1] = pts[np.argmin(diff)] # верхний правый\n",
" rect[3] = pts[np.argmax(diff)] # нижний левый\n",
"\n",
" return rect\n",
"\n",
"# Функция для получения перспективного преобразования\n",
"def get_transform(image, pts):\n",
" \"\"\"\n",
" Применяет перспективное преобразование к области, определенной точками pts в изображении.\n",
" Возвращает обрезанное и преобразованное изображение номерного знака.\n",
" \"\"\"\n",
" rect = order_points(pts)\n",
" (tl, tr, br, bl) = rect\n",
"\n",
" # Вычисляем ширину нового изображения\n",
" widthA = np.linalg.norm(br - bl)\n",
" widthB = np.linalg.norm(tr - tl)\n",
" maxWidth = max(int(widthA), int(widthB))\n",
"\n",
" # Вычисляем высоту нового изображения\n",
" heightA = np.linalg.norm(tr - br)\n",
" heightB = np.linalg.norm(tl - bl)\n",
" maxHeight = max(int(heightA), int(heightB))\n",
"\n",
" # Определяем точки назначения для преобразования\n",
" dst = np.array([\n",
" [0, 0],\n",
" [maxWidth - 1, 0],\n",
" [maxWidth - 1, maxHeight - 1],\n",
" [0, maxHeight - 1]\n",
" ], dtype=\"float32\")\n",
"\n",
" # Вычисляем матрицу перспективного преобразования\n",
" M = cv2.getPerspectiveTransform(rect, dst)\n",
"\n",
" # Применяем перспективное преобразование\n",
" warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))\n",
"\n",
" return warped\n",
"\n",
"# Функция для получения преобразованного номерного знака\n",
"def get_transformed_plate(image, bbox):\n",
" \"\"\"\n",
" Выполняет перспективное преобразование для извлечения номерного знака из изображения.\n",
" \"\"\"\n",
" plate = get_transform(image, bbox)\n",
" return plate\n",
"\n",
"# Список путей к изображениям\n",
"images = ['img/1.jpg', 'img/2.jpg', 'img/3.jpg']\n",
"\n",
"processed_plates = []\n",
"\n",
"for img_path in images:\n",
" image = cv2.imread(img_path)\n",
" \n",
" if image is None:\n",
" print(f\"Не удалось загрузить изображение по пути: {img_path}\")\n",
" processed_plates.append(None)\n",
" continue\n",
"\n",
" # Преобразуем изображение в оттенки серого\n",
" img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
" \n",
" # Применяем пороговое значение для выделения контуров\n",
" ret, thresh = cv2.threshold(img_gray, 100, 200, cv2.THRESH_TOZERO_INV)\n",
" \n",
" # Находим контуры на изображении\n",
" contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
"\n",
" plate_found = False # Флаг для отслеживания обнаружения номерного знака\n",
"\n",
" for cnt in contours:\n",
" x, y, w, h = cv2.boundingRect(cnt)\n",
" area = w * h\n",
" aspectRatio = float(w) / h\n",
"\n",
" # Фильтруем контуры по соотношению сторон и площади\n",
" if aspectRatio >= 3 and area > 600:\n",
" approx = cv2.approxPolyDP(cnt, 0.05 * cv2.arcLength(cnt, True), True)\n",
" \n",
" # Проверяем, имеет ли контур 4 угла и находится ли он достаточно далеко от края\n",
" if len(approx) <= 4 and x > 15:\n",
" # Получаем минимальный ограничивающий прямоугольник с поворотом\n",
" rect = cv2.minAreaRect(cnt)\n",
" box = cv2.boxPoints(rect)\n",
" box = np.intp(box)\n",
"\n",
" # Применяем перспективное преобразование для извлечения номерного знака\n",
" plate = get_transformed_plate(image, box)\n",
"\n",
" processed_plates.append(plate)\n",
" plate_found = True\n",
" break # Выходим из цикла после нахождения первого подходящего контура\n",
"\n",
" if not plate_found:\n",
" print(f\"Номерной знак не найден на изображении: {img_path}\")\n",
" processed_plates.append(None)\n",
"\n",
"# Отображаем обработанные номерные знаки с помощью Matplotlib\n",
"num_plates = len(processed_plates)\n",
"\n",
"# Определяем количество столбцов и строк для отображения\n",
"cols = min(num_plates, 3) # Ограничиваем до 3 столбцов\n",
"rows = (num_plates + cols - 1) // cols # Вычисляем необходимое количество строк\n",
"\n",
"fig, axs = plt.subplots(rows, cols, figsize=(5 * cols, 5 * rows))\n",
"axs = axs.flatten() if num_plates > 1 else [axs]\n",
"\n",
"for i, rotated_plate in enumerate(processed_plates):\n",
" ax = axs[i]\n",
" if rotated_plate is not None:\n",
" # Проверяем, является ли изображение цветным\n",
" if len(rotated_plate.shape) == 3:\n",
" rotated_plate_rgb = cv2.cvtColor(rotated_plate, cv2.COLOR_BGR2RGB)\n",
" ax.imshow(rotated_plate_rgb)\n",
" else:\n",
" ax.imshow(rotated_plate, cmap='gray')\n",
" else:\n",
" # Отображаем сообщение, если номерной знак не найден\n",
" ax.text(0.5, 0.5, 'Номер не найден', horizontalalignment='center', verticalalignment='center', fontsize=12, color='red')\n",
" \n",
" ax.set_title(f'Номер {i+1}')\n",
" ax.axis('off')\n",
"\n",
"# Скрываем неиспользуемые подграфики, если они есть\n",
"for j in range(i + 1, len(axs)):\n",
" fig.delaxes(axs[j])\n",
"\n",
"plt.tight_layout()\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABdEAAACfCAYAAAAWP5CxAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAACJYElEQVR4nO29edBtaX3X+6xpz/udzth9eqK7oWkG8QbEm5gUYIKh0ESqAhHFlERjpBI10SilWJKgKWMMFfBqnEjJUJBcSYQgJiWXWATLQqEouZgEEjoJ9HiGPsM77Hmvtdf9g8t59/fznHfv0ySkm7e/n6qu6uesd6/1rGdaz/D7fX9JXdd1MMYYY4wxxhhjjDHGGGNMRPpkZ8AYY4wxxhhjjDHGGGOMeariTXRjjDHGGGOMMcYYY4wx5gi8iW6MMcYYY4wxxhhjjDHGHIE30Y0xxhhjjDHGGGOMMcaYI/AmujHGGGOMMcYYY4wxxhhzBN5EN8YYY4wxxhhjjDHGGGOOwJvoxhhjjDHGGGOMMcYYY8wReBPdGGOM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jhhvsPxpjfP95EN+YpxnF0KzPGGGOOKz/7sz8b7r777vCud70rfPCDHwxnz54Nf//v//3woz/6oyt/t7W1tfJ6mqbhl37pl8Lb3va28J73vCd88IMfDJ1OJ9x9993hh37oh64HGP0KL3nJS8I3fuM3hre85S3hoYceCs95znPCu971rsjzjXz2s58NIXz5wP17vud7ousf+9jHvIlujDHm2HAcDsD
"text/plain": [
"<Figure size 1500x500 with 3 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import cv2\n",
"from matplotlib import pyplot as plt\n",
"\n",
"def order_points(pts):\n",
" \"\"\"\n",
" Упорядочивает точки в порядке:\n",
" верхний левый, верхний правый, нижний правый, нижний левый\n",
" \"\"\"\n",
" rect = np.zeros((4, 2), dtype=\"float32\")\n",
"\n",
" s = pts.sum(axis=1)\n",
" rect[0] = pts[np.argmin(s)] \n",
" rect[2] = pts[np.argmax(s)]\n",
"\n",
" diff = np.diff(pts, axis=1)\n",
" rect[1] = pts[np.argmin(diff)] \n",
" rect[3] = pts[np.argmax(diff)] \n",
"\n",
" return rect\n",
"\n",
"def get_transform(image, pts, padding=0.1):\n",
" \"\"\"\n",
" Применяет перспективное преобразование к области, определенной точками pts в изображении.\n",
" Уменьшает область захвата на заданный процент (padding).\n",
" Возвращает обрезанное и преобразованное изображение номерного знака.\n",
" \n",
" :param image: Исходное изображение\n",
" :param pts: Точки контура номерного знака (4 точки)\n",
" :param padding: Процент уменьшения области захвата (например, 0.1 для 10%)\n",
" \"\"\"\n",
" rect = order_points(pts)\n",
" (tl, tr, br, bl) = rect\n",
"\n",
" widthA = np.linalg.norm(br - bl)\n",
" widthB = np.linalg.norm(tr - tl)\n",
" maxWidth = max(int(widthA), int(widthB))\n",
"\n",
" heightA = np.linalg.norm(tr - br)\n",
" heightB = np.linalg.norm(tl - bl)\n",
" maxHeight = max(int(heightA), int(heightB))\n",
"\n",
" center = np.mean(rect, axis=0)\n",
"\n",
" vectors = rect - center\n",
"\n",
" rect_shrinked = rect - vectors * padding\n",
"\n",
" rect_shrinked[:, 0] = np.clip(rect_shrinked[:, 0], 0, image.shape[1] - 1)\n",
" rect_shrinked[:, 1] = np.clip(rect_shrinked[:, 1], 0, image.shape[0] - 1)\n",
"\n",
" dst = np.array([\n",
" [0, 0],\n",
" [maxWidth - 1, 0],\n",
" [maxWidth - 1, maxHeight - 1],\n",
" [0, maxHeight - 1]\n",
" ], dtype=\"float32\")\n",
"\n",
" M = cv2.getPerspectiveTransform(rect_shrinked, dst)\n",
"\n",
" warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))\n",
"\n",
" return warped\n",
"\n",
"def get_transformed_plate(image, bbox, padding=0.1):\n",
" \"\"\"\n",
" Выполняет перспективное преобразование для извлечения номерного знака из изображения.\n",
" Уменьшает область захвата на заданный процент (padding).\n",
" \n",
" :param image: Исходное изображение\n",
" :param bbox: Координаты ограничивающего прямоугольника (4 точки)\n",
" :param padding: Процент уменьшения области захвата (например, 0.1 для 10%)\n",
" :return: Преобразованное изображение номерного знака\n",
" \"\"\"\n",
" plate = get_transform(image, bbox, padding)\n",
" return plate\n",
"\n",
"images = ['img/1.jpg', 'img/2.jpg', 'img/3.jpg']\n",
"\n",
"processed_plates = []\n",
"\n",
"for img_path in images:\n",
" image = cv2.imread(img_path)\n",
" \n",
" if image is None:\n",
" print(f\"Не удалось загрузить изображение по пути: {img_path}\")\n",
" processed_plates.append(None)\n",
" continue\n",
"\n",
" img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
" \n",
" ret, thresh = cv2.threshold(img_gray, 100, 200, cv2.THRESH_TOZERO_INV)\n",
" \n",
" contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
"\n",
" plate_found = False \n",
"\n",
" for cnt in contours:\n",
" x, y, w, h = cv2.boundingRect(cnt)\n",
" area = w * h\n",
" aspectRatio = float(w) / h\n",
"\n",
" if aspectRatio >= 3 and area > 600:\n",
" approx = cv2.approxPolyDP(cnt, 0.05 * cv2.arcLength(cnt, True), True)\n",
" \n",
" if len(approx) <= 4 and x > 15:\n",
" rect = cv2.minAreaRect(cnt)\n",
" box = cv2.boxPoints(rect)\n",
" box = np.intp(box)\n",
"\n",
" \n",
" plate = get_transformed_plate(image, box, padding=0.1) \n",
"\n",
" processed_plates.append(plate)\n",
" plate_found = True\n",
" break \n",
"\n",
" if not plate_found:\n",
" print(f\"Номерной знак не найден на изображении: {img_path}\")\n",
" processed_plates.append(None)\n",
"\n",
"num_plates = len(processed_plates)\n",
"\n",
"cols = min(num_plates, 3)\n",
"rows = (num_plates + cols - 1) // cols \n",
"\n",
"fig, axs = plt.subplots(rows, cols, figsize=(5 * cols, 5 * rows))\n",
"if num_plates == 1:\n",
" axs = [axs] \n",
"else:\n",
" axs = axs.flatten()\n",
"\n",
"for i, rotated_plate in enumerate(processed_plates):\n",
" ax = axs[i]\n",
" if rotated_plate is not None:\n",
" if len(rotated_plate.shape) == 3:\n",
" rotated_plate_rgb = cv2.cvtColor(rotated_plate, cv2.COLOR_BGR2RGB)\n",
" ax.imshow(rotated_plate_rgb)\n",
" else:\n",
" ax.imshow(rotated_plate, cmap='gray')\n",
" else:\n",
" ax.text(0.5, 0.5, 'Номер не найден', horizontalalignment='center', verticalalignment='center', fontsize=12, color='red')\n",
" \n",
" ax.set_title(f'Номер {i+1}')\n",
" ax.axis('off')\n",
"\n",
"for j in range(i + 1, len(axs)):\n",
" fig.delaxes(axs[j])\n",
"\n",
"plt.tight_layout()\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAJvCAYAAAA6DRtsAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQV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9HRHjLW94CwBve8AZKKTzrWc+6SYnT4LvzB3/wB1y4cIHHPOYx3HDDDeMf5xz3ute9RkXLfD6naRr+6I/+6CYlcQNOnjwJwO///u9/2nKw173udZw4cYJv+IZvOPZ9X/EVX8H29vb4fe94xzvo+56nPOUp47ECPO1pT/t0l/Nm47u/+7uPmVTf6173QkRuUhJ4r3vdi49+9KOklMbX5vP5+N8XL17khhtu4P73vz8f/OAHuXjxIgB/+Id/SEqJH/zBHzz2eU95ylNuciyve93ruO9978upU6eOXY+v//qvJ+fMH//xH4/vfeUrX4mIfNZqJ1AvsQHOOb7yK78SEeF7v/d7x9dPnjzJXe96Vz74wQ+Or731rW/l8ssv5xGPeMT42mw24/u///tv8h2lFN761rceK7O7JXG0c+HQybDve97xjncAeuwhhGPHaq09plCbMGHChAkTJtzymErtJkyYMGHChM8zfvmXf5m73OUueO+57LLLuOtd73oTgsh7zxVXXHHstQ9/+MPc5ja3YWdn59jrX/zFXzz+HLR0z1rL3e9+9097DO973/uAjb/UJ2N3dxe
"text/plain": [
"<Figure size 1500x1000 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAJvCAYAAAA6DRtsAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9ebgsWVXmj3/2EBGZec655966NTJVYTEJ3Uo7AIqIDFJSCILYCiKTEw8ogg9qoygUSAuICD6g6EM/AiI2CCqijKLYtlOjjzZtizRTMdd8p3NOZkbE3nv9/lh7R+S5VWhJU/Dz2/Hq5dbNkyczhr137PWud73LiIgwYcKECRMmTJgwYcKECRMmTJgwYcIXGPZLfQATJkyYMGHChAkTJkyYMGHChAkT/r+JiXiaMGHChAkTJkyYMGHChAkTJkyYcItgIp4mTJgwYcKECRMmT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uoqnPe1pXHjhhfzWb/0W733ve2/yewohVcbY58LVV18NwLnnnvt5nU+MkW/5lm/hXve6Fz//8z/PO9/5Tp7znOcQQuB5z3seoCTgQx/6UN73vvfx5Cc/mbvc5S78/u//Po9//OM/r++cMGHChAkTJnyekAkTJkyYMGHCFwWvfvWrBZD3vOc9ct1118mnPvUpecMb3iDHjx+X+Xwun/70p0VE5PGPf7wA8sxnPvPQ77/lLW8RQJ7//Ocfev07vuM7xBgjH/nIR0RE5MMf/rBYa+URj3iExBgPvTelJCIie3t7cvToUfmBH/iBQz+/+uqrZXd3d3j95MmTAsiLX/ziz3lev/d7vyeA/M3f/M3nfM9//+//XQB5/etff+j1d77znYdev/baa6Wua3nIQx4yHKuIyE/91E8JII9//OM/53cUAPJDP/RDw7/f+973CiD/7t/9O+m6bnj90Y9+tBhj5MEPfvCh3/+6r/s6ufjiiw+9tlwub/Q9l112mXzZl33Z8O+rr75avPfy8Ic//ND7rrjiihsd+8/+7M/K1taWfOhDHzr03mc+85ninJNPfvKTw2tlPFx55ZU369yf85znDP9+znOeI4D84A/+4PBaCEFuc5vbiDFGXvjCFw6vnzx5Uubz+aHjfMlLXiKAvOUtbxleW61Wcpe73EUAee9733vo+x/72MfKfe9733/2GG+44QY5//zz5T73uc+/eD5XXnmlAPLqV796eK1cj6c+9anDayklechDHiJ1Xct1110nIiK/8zu/I4C87GUvG94XY5T73//+N/rMCRMmTJgwYcIth6nUbsKECRMmTPgi44EPfCDnnXcet73tbXnUox7F9vY2v/d7v8etb33rQ+978pOffOjfb3/723HO8SM/8iOHXn/GM56BiPCOd7wDgLe85S2klHj2s5+NtYcf9cYYAP7oj/6IU6dO8ehHP5rrr79++OOc4573vOegaJnP59R1zZ/+6Z/eqCSu4OjRowD84R/+4ecsB3vTm97E7u4u3/zN33zo+776q7+a7e3t4fve85730HUdT33qU4djBXj605/+uS7nzcbjHve4QybV97znPRGRG5UE3vOe9+RTn/oUIYThtfl8Pvz36dOnuf7667nvfe/Lxz72MU6fPg3AH//xHxNC4ClPecqhz3vqU596o2N505vexH3ucx+OHTt26Ho88IEPJMbIn/3Znw3vfc1rXoOIfN5qJ1AvsQLnHF/zNV+DiPB93/d9w+tHjx7lzne+86Hyt3e+853c+ta35mEPe9jw2mw24wd+4Adu9B0pJd75zncOZXY3hZQSj3nMYzh16hQvf/nLP+/zAQ51LiydDLuu4z3vec9w7FVVHTpWa+0hhdqECRMmTJgw4ZbHVGo3YcK
"text/plain": [
"<Figure size 1500x1000 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAJvCAYAAAA6DRtsAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9ebBsWVnmj3/WsIfMM9x7q6iBKhCwkElbaQdAERFQSkAmIVRUBJwIVAQCMFAUCqQFVAQDbDQwBBqxUWgFlFEUWnFoNKRthy8/pmKQrunWHc7Jk7n3XtPvj3etnXluFVBgFVWX3o9xpG6ePJl7XHu9z3qe51UppcSECRMmTJgwYcKECRMmTJgwYcKECTcy9M29ARMmTJgwYcKECRMmTJgwYcKECRO+PDERTxMmTJgwYcKECRMmTJgwYcKECRNuEkzE04QJEyZMmDBhwoQJEyZMmDBhwoSbBBPxNGHChAkTJkyYMGHChAkTJkyYMOEmwUQ8TZgwYcKECRMmTJgwYcKECRMmTLhJMBFPEyZMmDBhwoQJEyZMmDBhwoQJE24STMTThAkTJkyYMGHChAkTJkyYMGHChJsEE/E0YcKECRMmTJgwYcKECRMmTJgw4SbBRDxNmDBhwoQJEyZMmDBhwoQJEyZMuEkwEU8TJkyYMGHChLMCt7/97Xn84x9/c2/GLRpKKS677LKbezNuNHziE59AKcVrXvOam3tTJkyYMGHChAlfJCbiacKECRMmTPgS4TWveQ1KqfGnbVvudKc78dM//dNcddVVN/fmfdlAKcVP//RP39yb8f8s/vmf/xmlFB/4wAcAeNrTnsbXf/3Xc8455zCfz7nrXe/KZZddxmKxuJm3dMKECRMmTJjwpYC9uTdgwoQJEyZM+H8Nz3/+87nDHe5A13W8//3v55WvfCVvf/vb+Zd/+Rfm8/nNvXkTzmKsViusvXmnd29729s4//zz+aZv+iYA/v7v/5773Oc+POEJT6BtWz74wQ/yohe9iPe85z385V/+JVp/9nXQ293udqxWK6qq+lJt/oQJEyZMmDDhRsZEPE2YMGHChAlfYjzoQQ/iG7/xGwH4sR/7Mc4991x+/dd/nbe85S085jGPud6/OTg4YGtr60u5mRPOQrRte3NvAm9/+9t50IMehFIKgPe///3Xec8ll1zCM57xDD7wgQ9wr3vd67N+VlEGTpgwYcKECRPOXkxWuwkTJkyYMOFmxv3vf38ALr/8cgAe//jHs729zcc+9jEe/OAHs7Ozww/+4A8CQkA9/elP57a3vS1N03DnO9+ZX/u1XyOldJ3P/b3f+z3ucY97MJ/POXbsGN/2bd/Gu9/97kPvecc73sF97nMftra22NnZ4SEPeQj/+q//eug9V155JU94whO4zW1uQ9M03PrWt+bhD384n/jEJ8b3/MM//AOXXnopt7rVrZjNZtzhDnfgR37kRw59ToyRl73sZXz1V381bdtywQUX8MQnPpGTJ08eel9KiRe84AXc5ja3YT6fc7/73e862/SF4H3vex9KKf7wD/+Q5z3veVx88cXs7Ozw6Ec/mtOnT9P3PU996lM5//zz2d7e5glPeAJ93x/6jFe/+tXc//735/zzz6dpGu52t7vxyle+8jrfFWPksssu46KLLhq3/d/+7d+uN5/q1KlTPPWpTx3P5R3veEde/OIXE2M89L4rrriCD33oQzjnPu++npnxdNlll6GU4sMf/jA/9EM/xJEjRzjvvPP4xV/8RVJKfPrTn+bhD384u7u7XHjhhbzkJS+5zmd+8pOf5GEPexhbW1ucf/75PO1pT+Nd73oXSine9773XWef/uZv/oaHPOQhn3M7b3/724/v/1y4voyncn98/OMf59JLL2Vra4uLLrqI5z//+de5D6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+t+/fEf/3EC0t///d9/1vf81V/9VQLS61//+kOvv/Od7zz0+tVXX53quk4PechDxm1NKaWf//mfT0B63OMe91m/owBIP/VTPzX++73vfW8C0td8zdekYRjG1x/zmMckpVR60IMedOjvv/mbvznd7na3O/Tacrm8zvdceuml6Su/8ivHf1955ZXJWpse8YhHHHrfZZdddp1t/6Vf+qW0tbWVPvzhDx9677Oe9axkjEmf+tSnxtfK9XD55ZffoH1/7nOfO/77uc99bgLST/zET4yvee/TbW5zm6SUSi960YvG10+ePJlms9mh7XzJS16SgPTmN795fG21WqW73OUuCUjvfe97D33/Yx/72HTf+973Otv1t3/7twkYf+585ztf52+vD5dffnkC0qtf/erxtXI8nvzkJ4+vxRjTQx7ykFTXdbrmmmtSSin9j//xPxKQXvayl43vCyGk+9///tf5zAkTJkyYMGHCTYfJajdhwoQJEyZ8ifEd3/EdnHfeedz2trfl+7//+9ne3uaP//iPufjiiw+970lPetKhf7/97W/HGMPP/MzPHHr96U9/Oikl3vGOdwDw5je/mRgjz3nOc64T3Fxyd/7sz/6MU6dO8ZjHPIbjx4+PP8YY7nnPe46KltlsRl3XvO9977uOJa7g6NGjAPzpn/7pZ7WDvfGNb+TIkSN853d+56Hv+4Zv+Aa2t7fH73vPe97DMAw8+clPHrcV4KlPfepnO5w3GD/8wz98KKT6nve8Jyml61gC73nPe/LpT38a7/342mw2G//79OnTHD9+nPve9758/OMf5/Tp0wD8+Z//Od57fvInf/LQ5z35yU++zra88Y1v5D73uQ/Hjh07dDy+4zu+gxACf/mXfzm+9zWveQ0ppS9a7QSSJVZgjOEbv/EbSSnxoz/6o+PrR48e5c53vjMf//jHx9fe+c53cvHFF/Owhz1sfK1tW378x3/8Ot8RY+Sd73zn9drs7na3u/Fnf/ZnvPnNb+Znf/Zn2dra+g93tdvsXFg6GQ7DwHve855x26uqOrStWutDCrUJEyZMmDBhwk2PyWo
"text/plain": [
"<Figure size 1500x1000 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import cv2\n",
"from matplotlib import pyplot as plt\n",
"\n",
"def order_points(pts):\n",
" rect = np.zeros((4, 2), dtype=\"float32\")\n",
" s = pts.sum(axis=1)\n",
" rect[0] = pts[np.argmin(s)]\n",
" rect[2] = pts[np.argmax(s)]\n",
" diff = np.diff(pts, axis=1)\n",
" rect[1] = pts[np.argmin(diff)]\n",
" rect[3] = pts[np.argmax(diff)]\n",
" return rect\n",
"\n",
"def get_transform(image, pts, padding=0.1):\n",
" rect = order_points(pts)\n",
" (tl, tr, br, bl) = rect\n",
" widthA = np.linalg.norm(br - bl)\n",
" widthB = np.linalg.norm(tr - tl)\n",
" maxWidth = max(int(widthA), int(widthB))\n",
" heightA = np.linalg.norm(tr - br)\n",
" heightB = np.linalg.norm(tl - bl)\n",
" maxHeight = max(int(heightA), int(heightB))\n",
" center = np.mean(rect, axis=0)\n",
" vectors = rect - center\n",
" rect_shrinked = rect - vectors * padding\n",
" rect_shrinked[:, 0] = np.clip(rect_shrinked[:, 0], 0, image.shape[1] - 1)\n",
" rect_shrinked[:, 1] = np.clip(rect_shrinked[:, 1], 0, image.shape[0] - 1)\n",
" dst = np.array([\n",
" [0, 0],\n",
" [maxWidth - 1, 0],\n",
" [maxWidth - 1, maxHeight - 1],\n",
" [0, maxHeight - 1]\n",
" ], dtype=\"float32\")\n",
" M = cv2.getPerspectiveTransform(rect_shrinked, dst)\n",
" warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))\n",
" return warped\n",
"\n",
"def get_transformed_plate(image, bbox, padding=0.1):\n",
" plate = get_transform(image, bbox, padding)\n",
" return plate\n",
"\n",
"def process_and_display_plate(image_path, padding=0.1):\n",
" image = cv2.imread(image_path)\n",
" if image is None:\n",
" print(f\"Failed to load image at path: {image_path}\")\n",
" return\n",
" output_image = image.copy()\n",
" img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
" ret, thresh = cv2.threshold(img_gray, 100, 200, cv2.THRESH_TOZERO_INV)\n",
" contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
" plate_found = False\n",
" plate_image = None\n",
" plate_box = None\n",
" for cnt in contours:\n",
" x, y, w, h = cv2.boundingRect(cnt)\n",
" area = w * h\n",
" aspectRatio = float(w) / h\n",
" if aspectRatio >= 3 and area > 600:\n",
" approx = cv2.approxPolyDP(cnt, 0.05 * cv2.arcLength(cnt, True), True)\n",
" if len(approx) <= 4 and x > 15:\n",
" rect = cv2.minAreaRect(cnt)\n",
" box = cv2.boxPoints(rect)\n",
" box = np.intp(box)\n",
" plate_image = get_transformed_plate(image, box, padding)\n",
" plate_found = True\n",
" plate_box = box\n",
" break\n",
" if plate_found:\n",
" cv2.polylines(output_image, [plate_box], isClosed=True, color=(0, 255, 0), thickness=2)\n",
" height, width = image.shape[:2]\n",
" black_space_width = width // 2\n",
" black_space = np.zeros((height, black_space_width, 3), dtype=np.uint8)\n",
" if plate_found and plate_image is not None:\n",
" plate_height, plate_width = plate_image.shape[:2]\n",
" scale_factor = min(black_space_width / (plate_width + 200), height / plate_height)\n",
" new_plate_width = int(plate_width * scale_factor)\n",
" new_plate_height = int(plate_height * scale_factor)\n",
" resized_plate = cv2.resize(plate_image, (new_plate_width, new_plate_height), interpolation=cv2.INTER_AREA)\n",
" y_offset = (height - new_plate_height) // 2\n",
" black_space[y_offset:y_offset + new_plate_height, 0:new_plate_width] = resized_plate\n",
" placeholder_text = \"Text plate:\"\n",
" decoded_text = \"__________\"\n",
" font = cv2.FONT_HERSHEY_SIMPLEX\n",
" font_scale = 1\n",
" font_color = (255, 255, 255)\n",
" thickness = 2\n",
" line_type = cv2.LINE_AA\n",
" cv2.putText(black_space, placeholder_text, (new_plate_width + 20, y_offset + new_plate_height // 2), \n",
" font, font_scale, font_color, thickness, line_type)\n",
" cv2.putText(black_space, decoded_text, (new_plate_width + 20, y_offset + new_plate_height // 2 + 50), \n",
" font, font_scale, font_color, thickness, line_type)\n",
" else:\n",
" placeholder_text = \"Not found.\"\n",
" font = cv2.FONT_HERSHEY_SIMPLEX\n",
" font_scale = 1\n",
" font_color = (0, 0, 255)\n",
" thickness = 2\n",
" line_type = cv2.LINE_AA\n",
" text_size, _ = cv2.getTextSize(placeholder_text, font, font_scale, thickness)\n",
" text_x = (black_space_width - text_size[0]) // 2\n",
" text_y = height // 2\n",
" cv2.putText(black_space, placeholder_text, (text_x, text_y), \n",
" font, font_scale, font_color, thickness, line_type)\n",
" combined_image = np.hstack((output_image, black_space))\n",
" combined_image_rgb = cv2.cvtColor(combined_image, cv2.COLOR_BGR2RGB)\n",
" plt.figure(figsize=(15, 10))\n",
" plt.imshow(combined_image_rgb)\n",
" plt.axis('off')\n",
" plt.title(f'Processed Image: {image_path}')\n",
" plt.show()\n",
"\n",
"images = ['img/1.jpg', 'img/2.jpg', 'img/3.jpg']\n",
"\n",
"for img_path in images:\n",
" process_and_display_plate(img_path, padding=0.1)\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAEtCAYAAABJdaqWAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd3wU1doH8N+Z2ZJGEQiEGqpUBUUsKE1RqigqXrFRVLBwVa5iQ6WoF31RsReuCCooKti7XsWKFJWLiFgQkCIldAjJ7s6c94+Zc2YmuwkJsoD3/r5+Isnu7MyZ2dndzJPneY6QUkoQERERERERERHtZ8bBHgAREREREREREf13YuCJiIiIiIiIiIjSgoEnIiIiIiIiIiJKCwaeiIiIiIiIiIgoLRh4IiIiIiIiIiKitGDgiYiIiIiIiIiI0oKBJyIiIiIiIiIiSgsGnoiIiIiIiIiIKC0YeCIiIqI/zbZtFBQU4Lfffjvg204kEti4cSN+//33A75tI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6tS5cuEoC877779G3FxcWyXbt2smbNmjIWi1Vo/wYNGiQByMGDB6fc1zfffFNKKeXGjRtlJBKRp512mrQsSy/3yCOPSADy6aefTtr/VM+hHwA5ZswYKaWUN998szRNU7722mtlLiellDfccIOsWbOmbN++fcpzKdXjr7rqqqTb+/TpI/Pz85NuHz16tBRCBG7Lz8+XgwYN0j8XFhYmPW7u3LkSgHz22Wf1bbfffrsEIF955ZWk5W3bTrpt0KBBKcckZfL5t3HjRlmpUiXZq1cvCUB+8skn+r7SXisTJ05Mek349+3JJ59MOudSLSellI899piMRqOyW7dupY65NFdddVWFXktERETkYakdEREdsrp3747c3FzUr18f5513HnJycvDqq6/qvkIvv/wyOnXqhMMOOwwFBQX6q3v37rAsC5999llgfdu3b8eGDRvw73//G2+//TY6d+4c2FadOnUwY8YMfduSJUuwePFiXHjhhUlji8Vigaye0liWpccVi8X29VDANE1EIhEAgG3b2LJlCxKJBI455hh8++23+7xev3feeQemaeLqq68O3H7ddddBSol33303cPs//vGPQHbYRRddhFq1auHtt98OLBcKhTB8+HD9cyQSwfDhw7Fx40Z88803+7R/o0aNCvw8cuRImKapt/3RRx8hFovh2muvhWF4v+5cdtllqFy5ctIYK+KRRx7BhAkT8NBDD+GMM84oc9m1a9fi4Ycfxm233YacnJx93mZZynMu+kvG4vE4Nm/ejKZNm6Jq1aqB4zt79my0bdsW/fv3T1rHn50x8I477kCVKlWSzi/F/1pRX4WFhaWu7/XXX8eVV16JUaNGYcSIEWVuu7CwEOPHj8eIESPQoEGDP7UfREREVDEstSMiokPWo48+isMPPxyhUAi1atVC8+bNA0GEX375BYsXL0Zubm7Kx2/cuDHwc48ePTBv3jwAQM+ePfHiiy/q+wzDwAUXXIDHH38chYWFyMrKwowZM5CRkYEBAwYkrXvbtm3Iz8/f6z4sW7ZMj88wDDRt2hRjxozB+eefv/cDUMIzzzyD++67D8uWLUM8Hte3N2rUqMLrSmXVqlWoU6cOKlWqFLi9ZcuW+n7AC0C0aNEisJxpmmjWrFlS36A6deogOzs7cNvhhx8OwOlLdfzxxwMo3/4JIWAYBpo1axZYX5UqVVC7dm29bTXW5s2bB5aLRCJo3Lixvr+i3n33XSxcuBAAkvpZpTJmzBjUqVMHw4cPx6xZs/Zpm3uzbdu2vQa19uzZgwkTJmDq1KlYu3ZtoGfX9u3b9ffLly/H2Wefvd/HuGLFCjz55JN4/PHHkZG
"text/plain": [
"<Figure size 1500x500 with 3 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAEwCAYAAAD2PbvTAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd2AUZf4/8PczM9tSaaFDqIKABUFURIqiqHAqevgFUcEC2M7TUyzHKYh66mE9FRX1sIANUez1xK4IhxVEaUFBKZGekOzuzPP745lnSjYJAVnxfvd+eXsks7PTd5P55PP5PEJKKUFERERERERERLSHGXt7A4iIiIiIiIiI6P9PDDwREREREREREVFWMPBERERERERERERZwcATERERERERERFlBQNPRERERERERESUFQw8ERERERERERFRVjDwREREREREREREWcHAExERERERERERZQUDT0RERPSrOY6D0tJSrFix4jdfdzqdxvr16/HDDz/85usmIiIiotox8ERERES7Ze3atbjkkktQXFyMaDSKoqIidOnSBVu3bs36upcuXYoxY8agWbNmiEajaNKkCQ477DBIKbO+biIiIiKqO2tvbwAREVFVjzzyCM466yzv+1gshtatW+OYY47BNddcgyZNmuzFrSMAWLZsGQYMGIBUKoWLL74YBx10ECzLQiKRQG5ublbX/emnn+K4445DgwYNcNVVV6FLly4QQqCwsBBCiKyum/43OI6Dxx57DM899xw+//xzbNy4EW3btsXw4cNx+eWXIx6P7+1NJCIi+q8hJP80SEREvzM68DR58mS0bdsWFRUV+PDDD/H444+juLgY33zzDXJycvb2Zv5PO+qoo1B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UMPPYQRI0ZgzJgx2LZtGx5++GEMGjQIn332GQ488EAUFRWFlvfcc8/h+eefD01r3759jdsipcQJJ5yAuXPn4pxzzsGBBx6IN954A+PHj8eaNWtwxx13AAB++eUXVFRU4Pzzz8eRRx6J8847D8uXL8e9996LefPmYd68eYjFYt5yN23ahOOPPx6nnnoqRowYgWeeeQbnn38+otEozj777Drvn173ihUr8Ne//hUnn3wyLrvsMixYsABTpkzBN998g1deecULdkyaNAnXXXcdBg4ciPPPPx/fffcd7rvvPsyfPx8fffQRIpEIJkyYgHPPPTd0DseOHYsjjjhip+fu3HPPxbvvvou33noLXbp0qXG+zZs3ZwQud+boo4/GmWeeGZp22223YdOmTTW+Jnieq16L8+fPx8cff4zhw4ejZcuWKCkpwX333Yf+/ftj8eLFXunq9u3bccQRR+Dbb7/F2WefjYMOOgilpaV48cUXsXr1auy7776h9UybNg3ffvutd20AwP7771/jNt52221Yt25d3Q5CHU2cOBEPP/wwnn766VoDe+l02nu/7q61a9cCgPf+JCIiojqQREREvzPTp0+XAOTbb78tN2zYIH/88Uf51FNPyYYNG8pEIiFXr14tpZTy+uuvl7m5ufL7778Pvf6qq66SpmnKH374ocZ1XHDBBTIvL8/7/o033pAA5GuvvRaab//995f9+vXLeP3hhx8uBwwY4H2/cuVKCUBOnz7dm9avXz/ZtWvXXd7vlStXZjyXTqdlZWVlaNqmTZtkkyZN5Nlnn13t8iZOnCh35Uf9nDlzJAB5ww03hKb/8Y9/lEIIuWzZstByjzrqKJlOpzO2/+677/am9evXTwKQt912mzetsrJSHnjggbJx48YymUzu0v6NGjVKApCjR4+udl9feuklKaWU69evl9FoVB5zzDHStm1vvnvuuUcCkP/6178y9r+6cxgEQE6cOFFKKeXVV18tTdOUc+bMqXU+KaW84oorZOPGjWWPHj2qvZaqe/2FF16YMX3w4MGyuLg4Y/qECROkECI0rbi4WI4aNcr7vry8PON1n3zyiQQgH3vsMW/atddeKwHI5557LmN+x3Eypo0aNarabZIy8/pbv369zM/Pl8cdd5wEIOfOnes9V9N7ZcqUKRnvieC+PfDAAxnXXHXzSSnl1KlTZSwWkwMGDKhxm3dm4MCBsqCgQG7atGm3Xk9ERPS/iKV2RET0uzVw4EAUFRWhVatWGD58OPLy8vD88897fYVmzZqFI444AvXr10dpaan3GDhwIGzbxvvvvx9a3pYtW7Bu3Tr8+9//xiuvvIK+ffuG1tW8eXPMnDnTm/bNN9/gq6++wumnn56xbclkMpTVUxPbtr3tSiaTu3soYJomotEoADXi1saNG5FOp9GzZ08sXLhwt5cb9Oqrr8I0TVx88cWh6ZdddhmklHjttddC0//yl7+EssPOOOMMNGnSBK+88kpoPsuyMG7cOO/7aDSKcePGYf369fjPf/6zW/s3fvz40PeXXnopTNP01v32228jmUzikksugWH4v+6MGTMGBQUFGdu4K+655x7cdNNN+Oc//4kTTzyx1nnXrFmDu+++G9dccw3y8vJ2e521qcu1mEgkvK9TqRR++eUXdOjQAfXq1Qsd39mzZ+OAAw7wSsqCfm0z7euvvx6FhYUZ15cWfK/oR3l5eY3Le+GFF3DBBRdg/PjxuOiii2pdd3l5OSZPnoyLLroIrVu33q3t//vf/463334bN998M+rVq7dbyyAiIvpfxFI7IiL63br33nuxzz77wLIsNGnSBJ06dQoFEZYuXYqvvvoKRUVF1b5+/fr1oe8HDRqEefPmAQCOPfZYPP30095zhmFg5MiRuO+++1BeXo6cnBzMnDkT8Xgcw4YNy1j25s2bUVxcvNN9WLJkibd9hmGgQ4cOmDhxIk477bSdH4AqHn30Udx2221YsmQJUqmUN71t27a7vKzqrFq1Cs2bN0d+fn5o+r777us9D/gBiM6dO4fmM00THTt2zOgb1Lx5c+Tm5oam7bPPPgBUX6pDDz0UQN32TwgBwzDQsWPH0PIKCwvRrFkzb91
"text/plain": [
"<Figure size 1500x500 with 3 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAEvCAYAAAAEvQudAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd4AURfo+8Ke6J2yCBZaclixJBAEDKsETEeEEVFRMICrGM2e/CqIe+jNgDhhAxQyKWcET4yGCHAIiIFlQwpJh2d2Z7vr90V3V3TOzywA7gnfP525kt6enuzrNbL/z1ltCSilBRERERERERERUyYwD3QAiIiIiIiIiIvrvxMATERERERERERFlBANPRERERERERESUEQw8ERERERERERFRRjDwREREREREREREGcHAExERERERERERZQQDT0RERERERERElBEMPBERERERERERUUYw8ERERET7zbZtFBUVYfny5X/6uuPxODZs2IDVq1f/6esmIiIiooox8ERERET7ZN26dbjmmmtQWFiISCSCWrVqoW3btti+fXvG1/3rr7/i4osvRr169RCJRFCnTh0cffTRkFJmfN1ERERElL7QgW4AERFRogkTJuCCCy7Qv0ejUTRu3Bgnnngi7rjjDtSpU+cAto4AYOnSpejVqxdisRiuuuoqHH744QiFQsjOzkZubm5G1/3999+jb9++qFGjBm655Ra0bdsWQgjk5+dDCJHRddP/jueeew4TJ07EokWLsHXrVtSvXx89e/bEyJEj0aRJkwPdPCIior8MIfnVIBERHWRU4Gn06NFo2rQpSkpK8O233+KVV15BYWEhFixYgJycnAPdzP9pf/vb37By5Up8/fXXaNCgwZ+23rKyMhx22GGoWrUqpk6divz8/D9t3fS/5fLLL0dxcTEOPfRQVK9eHStWrMBzzz0Hy7Lw008/oX79+ge6iURERH8JzHgiIqKDVt++fdGlSxcAwEUXXYSCggI8/PDDeO+99zBkyJAD3Lr/XT/++CO++OILTJ069U8NOgHABx98gMWLF2PRokUMOlFGPfXUU0nTBg4ciC5duuDll1/GLbfccgBaRURE9NfDGk9ERPSXcfzxxwMAVqxYoadt3boV11xzDRo1aoRoNIoWLVrg/vvvh23bep7Fixfj+OOPR926dRGNRtGoUSNceuml2Lx5MwBg586dyM3NxdVXX520zjVr1sA0TYwZMyYwvWfP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TfeeGPg92uvvRamaep1f/755ygrK8M111wDw/D+3Ln44otRtWrVpDbujSeeeAJjxozBY489hgEDBlQ479q1a/H444/jjjvuQF5e3j6vsyLpnIvZ2dn651gshk2bNqFFixaoVq1aYP9OnjwZhx12GAYNGpS0jP0dMfDuu+9Gfn5+0vml+K8V9SguLi53ee+99x4uv/xy3HjjjbjyyisrXHdxcTFGjx6NK6+8Eo0bN96rdn/yySf4+OOP8dBDD6Fx48bYtWvXXr2eiIjofx272hER0UHrySefRKtWrRAKhVCnTh0ccsghgSDCr7/+innz5qFWrVopX79hw4bA73369MHMmTMBACeddBLefPNN/ZxhGDjnnHPw9NNPo7i4GDk5OXj11VeRlZWFwYMHJy1769atKCws3OM2LFq0SLfPMAy0aNECI0eOxNlnn73nHZDgpZdewkMPPYRFixYhFovp6U2bNt3rZaWyatUq1K9fH1WqVAlMb9OmjX4e8AIQrVu3DsxnmiZatmyZVDeofv36yM3NDUxr1aoVAKcu1VFHHQUgve0TQsAwDLRs2TKwvPz8fNSrV0+vW7X1kEMOCcwXiUTQrFkz/fz
"text/plain": [
"<Figure size 1500x500 with 3 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"\n",
"# Функция удаления мелких объектов\n",
"def bwareaopen(imgBW, areaPixels):\n",
" imgBWcopy = imgBW.copy()\n",
" contours, _ = cv2.findContours(imgBWcopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n",
" for contour in contours:\n",
" if cv2.contourArea(contour) < areaPixels:\n",
" cv2.drawContours(imgBWcopy, [contour], -1, 0, -1)\n",
" return imgBWcopy\n",
"\n",
"# Функция расширения объектов\n",
"def expand_characters(img, kernel_size=(2, 2), iterations=1):\n",
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)\n",
" return cv2.dilate(img, kernel, iterations=iterations)\n",
"\n",
"def selective_dilation(image, distance_threshold=10, iterations=1):\n",
" \"\"\"\n",
" Выполняет \"наращивание\" (дилатацию) символов, чтобы соединить близкие контуры.\n",
" \n",
" :param image: Бинарное изображение.\n",
" :param distance_threshold: Максимальное расстояние между контурами для их соединения.\n",
" :param iterations: Количество итераций для расширения.\n",
" :return: Обработанное изображение.\n",
" \"\"\"\n",
" # Копируем изображение\n",
" image_copy = image.copy()\n",
"\n",
" # Находим контуры\n",
" contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
"\n",
" # Создаём маску для временного соединения контуров\n",
" temp_mask = np.zeros_like(image)\n",
"\n",
" for i, cnt1 in enumerate(contours):\n",
" for j, cnt2 in enumerate(contours):\n",
" if i != j: # Сравниваем контуры только с другими\n",
" # Находим минимальное расстояние между точками двух контуров\n",
" min_distance = float(\"inf\")\n",
" for pt1 in cnt1:\n",
" for pt2 in cnt2:\n",
" dist = np.linalg.norm(np.array(pt1[0]) - np.array(pt2[0]))\n",
" if dist < min_distance:\n",
" min_distance = dist\n",
"\n",
" # Если расстояние меньше порога, соединяем контуры\n",
" if min_distance < distance_threshold:\n",
" # Рисуем оба контура на временной маске\n",
" cv2.drawContours(temp_mask, [cnt1], -1, 255, thickness=-1)\n",
" cv2.drawContours(temp_mask, [cnt2], -1, 255, thickness=-1)\n",
"\n",
" # Выполняем дилатацию только на временной маске\n",
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))\n",
" dilated_mask = cv2.dilate(temp_mask, kernel, iterations=iterations)\n",
"\n",
" # Объединяем исходное изображение с обработанной маской\n",
" result = cv2.bitwise_or(image_copy, dilated_mask)\n",
"\n",
" return result\n",
"\n",
"def imclearborder(imgBW, radius=1):\n",
" \"\"\"\n",
" Удаляет только те объекты, которые непосредственно касаются границ изображения,\n",
" если у них нет точек в пределах 30% от верхней или нижней границы.\n",
" \n",
" :param imgBW: Бинарное изображение (чёрно-белое, 0 и 255).\n",
" :param radius: Расстояние от границы, в пределах которого объект считается касающимся.\n",
" :return: Изображение с удалёнными объектами, касающимися границ.\n",
" \"\"\"\n",
" imgBWcopy = imgBW.copy()\n",
"\n",
" # Находим контуры\n",
" contours, _ = cv2.findContours(imgBWcopy.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n",
" imgRows, imgCols = imgBW.shape\n",
"\n",
" # Границы для проверки: 30% сверху и снизу\n",
" top_limit = int(imgRows * 0.3)\n",
" bottom_limit = int(imgRows * 0.7)\n",
"\n",
" # Список контуров, которые касаются границы\n",
" contourList = []\n",
"\n",
" for idx, cnt in enumerate(contours):\n",
" remove_contour = True # Предположим, что контур нужно удалить\n",
" for pt in cnt:\n",
" rowCnt = pt[0][1] # y-координата\n",
" colCnt = pt[0][0] # x-координата\n",
"\n",
" # Проверка на касание границы\n",
" check1 = (rowCnt >= 0 and rowCnt < radius) or (rowCnt >= imgRows - radius and rowCnt < imgRows)\n",
" check2 = (colCnt >= 0 and colCnt < radius) or (colCnt >= imgCols - radius and colCnt < imgCols)\n",
"\n",
" # Если точка находится в пределах 30% от верха или низа, не удаляем контур\n",
" if top_limit <= rowCnt <= bottom_limit:\n",
" remove_contour = False\n",
" break\n",
"\n",
" if check1 or check2:\n",
" continue # Проверяем следующую точку\n",
"\n",
" if remove_contour:\n",
" contourList.append(idx)\n",
"\n",
" # Удаляем найденные контуры\n",
" for idx in contourList:\n",
" cv2.drawContours(imgBWcopy, contours, idx, (0, 0, 0), -1)\n",
"\n",
" return imgBWcopy\n",
"\n",
"\n",
"\n",
"# Основная функция обработки одной пластины\n",
"def process_plate(plate):\n",
" # Преобразование в градации серого\n",
" gray_plate = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY)\n",
"\n",
" # Увеличение размера для улучшения качества\n",
" resized_plate = cv2.resize(gray_plate, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)\n",
"\n",
" # Фильтрация и бинаризация\n",
" gray = cv2.GaussianBlur(resized_plate, (3, 3), 0)\n",
" thresh = cv2.adaptiveThreshold(gray, 255,\n",
" cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\n",
" cv2.THRESH_BINARY_INV, 41, 10)\n",
"\n",
" # Удаление мелких областей\n",
" #expanded_image = expand_characters(thresh, kernel_size=(2, 2), iterations=1)\n",
" thresh = imclearborder(thresh, 5)\n",
" expanded_image = selective_dilation(thresh, distance_threshold=7, iterations=1)\n",
" processed_plate = bwareaopen(expanded_image, 20)\n",
"\n",
" # Поиск контуров символов\n",
" contours, _ = cv2.findContours(processed_plate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
"\n",
" # Сортировка контуров по их расположению (слева направо)\n",
" contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0])\n",
"\n",
" # Вырезание и нормализация каждого символа\n",
" characters = []\n",
" for ctr in contours:\n",
" x, y, w, h = cv2.boundingRect(ctr)\n",
" if w > 2 and h > 10: # Убираем шумы и маленькие контуры\n",
" char = processed_plate[y:y+h, x:x+w]\n",
" char_resized = cv2.resize(char, (28, 28)) # Подгонка под размер (например, 28x28 для модели)\n",
" characters.append(char_resized)\n",
" return processed_plate, characters\n",
"\n",
"# Пример использования с массивом processed_plates\n",
"for plate_index, plate in enumerate(processed_plates):\n",
" # Обработка пластинки\n",
" processed_plate, characters = process_plate(plate)\n",
"\n",
" # Вывод результата\n",
" plt.figure(figsize=(15, 5))\n",
"\n",
" # Исходная пластина\n",
" plt.subplot(1, 3, 1)\n",
" plt.title(\"Исходная картинка\")\n",
" plt.imshow(cv2.cvtColor(plate, cv2.COLOR_BGR2RGB)) # Преобразуем для корректного отображения\n",
" plt.axis('off')\n",
"\n",
" # Обработанная пластина\n",
" plt.subplot(1, 3, 2)\n",
" plt.title(\"Обработанная картинка\")\n",
" plt.imshow(processed_plate, cmap='gray')\n",
" plt.axis('off')\n",
"\n",
" plt.subplot(1, 3, 3)\n",
" plt.title(\"Выделенные символы\")\n",
" if characters:\n",
" symbols_grid = np.hstack(characters)\n",
" plt.imshow(symbols_grid, cmap='gray')\n",
" else:\n",
" plt.text(0.5, 0.5, 'Нет символов', ha='center', va='center')\n",
" plt.axis('off')\n",
"\n",
" plt.suptitle(f\"Результат обработки пластинки {plate_index + 1}\")\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved Original Plate to img\\plate1_original.jpg\n",
"Saved Processed Plate to img\\plate1_processed.jpg\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAEtCAYAAABJdaqWAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOydeZwdRbn+n+ruc87MZCYLEAibIYQdUTQKKPsiyKKyRAwIJiwCCgSvwHXDQBBEARVlkUUhCChXROSqgICEq8IFUUEUXIIGVPgJYUkgme2c7vr9Uf1Wv13TJ5kEjnDl+fIZMnNOL9W1dNf79Pu+Zay1FoQQQgghhBBCCCGEvMJEr3YBCCGEEEIIIYQQQsi/JxSeCCGEEEIIIYQQQkhHoPBECCGEEEIIIYQQQjoChSdCCCGEEEIIIYQQ0hEoPBFCCCGEEEIIIYSQjkDhiRBCCCGEEEIIIYR0BApPhBBCCCGEEEIIIaQjUHgihBBCCCGEEEIIIR2BwhMhhBBCCCGEEEII6QgUngghhLwuePzxx2GMwbx5817torxmuPvuu2GMwd133/1qF4WAfXRFbLDBBpg1a9arXQxCCCGErCQUngghhPyfZ968eTDG4Fe/+tWrXZTXBFIf8tPV1YVNNtkEJ5xwAp5++ulX5By33HILzjjjjFfkWJozzjijVPaenh5sscUWOO200/Diiy++4ucjDhEh5SeOY6y55pqYPn06/vCHP7zaxavk0UcfxRlnnIHHH398VNv/v//3//DJT34Su+66K/r6+ii6EkIIIf8ikle7AIQQQsi/gsmTJ2NgYAC1Wu3VLsq/jDPPPBNTpkzB4OAgfvGLX+DrX/86brnlFvz+979HT0/Pyzr2Lbfcgosvvrgj4hMAfP3rX0dvby+WLl2K22+/HWeffTbuuusu3HPPPTDGdOScrzavhT46e/ZsvP3tb0ez2cTDDz+MSy+9FHfffTd+//vfY9KkSa9auap49NFHMXfuXOyyyy7YYIMNVrj9n/70J3zxi1/ExhtvjK222gr/+7//2/lCEkIIIYTCEyGEkNcH4vnzemLvvffG2972NgDA0UcfjdVXXx1f/vKXcfPNN+OQQw55lUu3fKZPn4411lgDAHDcccfhoIMOwve//33cd999eMc73lG5T39//8sW1F5NXgt9dMcdd8T06dP935tuuik+8pGP4Fvf+hb+8z//81Us2ctn2rRpeO6557Daaqvhe9/7Ht7//ve/2kUihBBCXhcw1I4QQsjrgnb5c/74xz/i4IMPxsSJE9Hd3Y1NN90Un/nMZ0rbPPnkkzjyyCOx1lprodFoYMstt8SVV15Z2kZClb773e/i7LPPxnrrrYeuri7svvvueOyxx0rbLliwAAcddBAmTZqErq4urLfeepgxYwaWLFlS2u7aa6/FtGnT0N3djdVWWw0zZszA3//+91Wug9122w0AsHDhwrbb/PznP8f73/9+vOENb0Cj0cD666+P//iP/8DAwIDfZtasWbj44osBoBSeJWRZhgsuuABbbrklurq6sNZaa+HYY4/FCy+88IqVfZdddsEb3/hG/PrXv8ZOO+2Enp4efPrTnwYAPPPMMzjqqKOw1lproaurC29+85tx9dVXjzhmlmX46le/iq222gpdXV2YOHEi3v3ud48I2RxNO4ymTe+44w7ssMMOGD9+PHp7e7Hpppv6MgPVfXTWrFno7e3Fk08+if333x+9vb2YOHEiTjnlFKRpWirDc889h8MPPxxjx47F+PHjMXPmTPz2t799WXmjdtxxRwDAX/7yl9LnoxkTAHDhhRdiyy23RE9PDyZMmIC3ve1t+Pa3v126vipvJQm5bMe8efO8cLTrrrv6Pri80Lm+vj6sttpqy7tcQgghhHQAejwRQgh53fLwww9jxx13RK1WwzHHHIMNNtgAf/nLX/DDH/4QZ599NgDg6aefxnbbbQdjDE444QRMnDgRt956K4466ii8+OKL+NjHPlY65he+8AVEUYRTTjkFS5YswbnnnosPfvCDuP/++wEAw8PD2GuvvTA0NIQTTzwRkyZNwpNPPokf/ehHWLx4McaNGwcAOPvss/HZz34WBx98MI4++mgsWrQIF154IXbaaSc8+OCDGD9+/Epfr4gHq6++etttbrjhBvT39+MjH/kIVl99dfzyl7/EhRdeiH/84x+44YYbAADHHnssnnrqKdxxxx245pprRhzj2GOPxbx583DEEUdg9uzZWLhwIS666CI8+OCDuOeee1YplKyq7M899xz23ntvzJgxA4cddhjWWmstDAwMYJdddsFjjz2GE044AVOmTMENN9yAWbNmYfHixTjppJP8/kcddRTmzZuHvffeG0cffTRarRZ+/vOf47777vOeYqNph9G06SOPPIL99tsPb3rTm3DmmWei0Wjgsccewz333LPCa0/TFHvttRe23XZbnH/++bjzzjvxpS99CVOnTsVHPvIRAE5Ee8973oNf/vKX+MhHPoLNNtsMN998M2bOnLnSda2R/EkTJkzwn412TFxxxRWYPXs2pk+fjpNOOgmDg4N4+OGHcf/99+PQQw99WeXaaaedMHv2bHzta1/Dpz/9aWy++eYA4P8lhBBCyGsISwghhPwf56qrrrIA7AMPPNB2m4ULF1oA9qqrrvKf7bTTTravr88+8cQTpW2zLPO/H3XUUXbttde2zz77bGmbGTNm2HHjxtn+/n5rrbXz58+3AOzmm29uh4aG/HZf/epXLQD7u9/9zlpr7YMPPmgB2BtuuKFtWR9//HEbx7E9++yzS5//7ne/s0mSjPg8ROrjzjvvtIsWLbJ///vf7fXXX29XX311293dbf/xj3+Uyjx//ny/r1yP5pxzzrHGmFI9HX/88bZqGvHzn//cArDXXXdd6fPbbrut8vOQ008/3QKwf/rTn+yiRYvswoUL7WWXXWYbjYZda6217LJly6y11u68884WgL300ktL+19wwQUWgL322mv9Z8PDw/Yd73iH7e3ttS+++KK11tq77rrLArCzZ88eUQZp/9G2w2ja9Ctf+YoFYBctWtR2m6o+OnPmTAvAnnnmmaVt3/KWt9hp06b5v2+88UYLwF5wwQX+szRN7W677TbimFVIX7jyyivtokWL7FNPPWVvu+02u9FGG1ljjP3lL3/ptx3tmHjf+95nt9xyy+Wed+bMmXby5MkjPpd+oJk8ebKdOXOm//uGG24Y0X9Hy8vZlxBCCCErB0PtCCGEvC5ZtGgRfvazn+HII4/EG97whtJ3EuJjrcWNN96I97znPbDW4tlnn/U/e+21F5YsWYLf/OY3pX2POOII1Ot1/7eEKv31r38FAO/R9JOf/AT9/f2VZfv+97+PLMtw8MEHl845adIkbLzxxpg/f/6ornGPPfbAxIkTsf7662PGjBno7e3FTTfdhHXXXbftPt3d3f73ZcuW4dlnn8U73/lOWGvx4IMPrvCcN9xwA8aNG4d3vetdpbJPmzYNvb29oy77pptuiokTJ2LKlCk49thjsdFGG+HHP/5xKYdTo9HAEUccUdrvlltuwaRJk0o5rGq1GmbPno2lS5fif/7nfwAAN954I4wxOP3000ecW9p/tO0wmjYVD7Wbb74ZWZaNqg40xx13XOnvHXfc0fcpALjttttQq9Xw4Q9/2H8WRRGOP/74lTrPkUceiYkTJ2KdddbBu9/9bixZsgTXXHMN3v72twNYuTExfvx4/OMf/8ADDzyw0tdLCCGEkH8fGGpHCCHkdYkY7W984xvbbrNo0SIsXrwYl19+OS6//PLKbZ555pnS36GIJSFKkt9oypQp+PjHP44vf/nLuO6667Djjjvive99Lw477DAvYCxYsADWWmy88caV5xxtqNrFF1+MTTbZBEmSYK211sKmm26KKFr+O6e//e1vmDNnDv77v/97RE6mMAdVFQsWLMCSJUuw5pprVn4f1lc7brzxRowdOxa1Wg3rrbcepk6dOmKbddddtyTyAcATTzyBjTfeeMR1SgjWE088AcCF7q2zzjrLzfkz2nYYTZt+4AMfwDe+8Q0cffTR+OQnP4ndd98dBx54IKZ
"text/plain": [
"<Figure size 1500x500 with 3 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved Original Plate to img\\plate2_original.jpg\n",
"Saved Processed Plate to img\\plate2_processed.jpg\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAEwCAYAAAD2PbvTAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOydd6AdRdn/v7N7yj333jQgEJpJCL0oigJSAgiGqlIChmZCkSIQeAVUFAKhKk2UDgpBQNG8iLxKkRZE4QVRQJCiQQIq/IRQUm855+zO74/ZZ/fZOXuTm8ARXvl+9HBP9myZmZ3ZnfnO8zxjrLUWhBBCCCGEEEIIIYS8xwTvdwIIIYQQQgghhBBCyH8mFJ4IIYQQQgghhBBCSFug8EQIIYQQQgghhBBC2gKFJ0IIIYQQQgghhBDSFig8EUIIIYQQQgghhJC2QOGJEEIIIYQQQgghhLQFCk+EEEIIIYQQQgghpC1QeCKEEEIIIYQQQgghbYHCEyGEEEIIIYQQQghpCxSeCCGEfCh4+eWXYYzBjBkz3u+kfGB48MEHYYzBgw8++H4nhYB1dGmMGTMGU6ZMeb+TQQghhJBlhMITIYSQ//PMmDEDxhj84Q9/eL+T8oFAykM+HR0dWHfddXHsscfi9ddff0+uceedd+KMM854T86lOeOMM3Jp7+zsxIYbbohTTz0VCxYseM+vRxwiQsonDEOsvPLKmDhxIp5//vn3O3mFPPfcczjjjDPw8ssvD2r/+++/H4ceeijWXXdddHZ2Yq211sLhhx+O//f//l97E0oIIYR8yCm93wkghBBC/h2MHj0avb29KJfL73dS/m2ceeaZGDt2LPr6+vC73/0OV155Je688078+c9/Rmdn57s695133onLL7+8LeITAFx55ZXo7u7GokWLcM899+Ccc87BAw88gIcffhjGmLZc8/3mg1BHp06dik996lNoNBp4+umncdVVV+HBBx/En//8Z4waNep9S1cRzz33HKZPn47tt98eY8aMWer+X//61/H2229j3333xTrrrIOXXnoJl112GX71q1/hqaee+sDljxBCCPlPgcITIYSQDwVi+fNhY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fE73//e1x66aX45z//iZkzZwIAjjzySLz22mu49957ceONN7ac48gjj8SMGTNwyCGHYOrUqZgzZw4uu+wyPPnkk3j44YeXy5WsKO1vvfUWdt11V0yaNAkHHXQQVlllFfT29mL77bfHiy++iGOPPRZjx47FzJkzMWXKFMybNw/HH398evxhhx2GGTNmYNddd8Xhhx+OZrOJ3/72t3j00UdTS7HB3IfB3NNnn30We+yxBz760Y/izDPPRLVaxYsvvoiHH354qXmPogg777wztthiC1x44YW47777cNFFF2HcuHE4+uijATgR7XOf+xx+//vf4+ijj8b666+P22+/HZMnT17mstZI/KQRI0ak2wbbJq699lpMnToVEydOxPHHH4++vj48/fTTeOyxx3DAAQe8q3SNHz8eU6dOxfe//31885vfxAYbbAAA6d/BsmjRIixatCi1riOEEEJIG7CEEELI/3Guv/56C8A+/vjjA+4zZ84cC8Bef/316bbx48fbIUOG2FdeeSW3bxzH6ffDDjvMrrrqqvbNN9/M7TNp0iQ7bNgw29PTY621dtasWRaA3WCDDWx/f3+63/e+9z0LwD7zzDPWWmuffPJJC8DOnDlzwLS+/PLLNgxDe8455+S2P/PMM7ZUKrVs95HyuO++++zcuXPtP/7xD3vLLbfYFVdc0dZqNfvPf/4zl+ZZs2alx0p+NOedd541xuTK6ZhjjrFF3Yjf/va3FoC9+eabc9vvvvvuwu0+p59+ugVg//KXv9i5c+faOXPm2KuvvtpWq1W7yiqr2MWLF1trrd1uu+0sAHvVVVfljr/kkkssAHvTTTel2+r1uv30pz9tu7u77YIFC6y11j7wwAMWgJ06dWpLGuT+D/Y+DOaefve737UA7Ny5cwfcp6iOTp482QKwZ555Zm7fj3/843azzTZL/33rrbdaAPaSSy5Jt0VRZD/zmc+0nLMIqQvXXXednTt3rn3ttdfs3Xffbddee21rjLG///3v030H2ya+8IUv2I022miJ1508ebIdPXp0y3apB5rRo0fbyZMnp/+eOXNmS/1dVs466ywLwN5///3LfQ5CCCGELBm62hFCCPlQMnfuXDz00EM49NBD8ZGPfCT3m7j4WGtx66234nOf+xystXjzzTfTz84774z58+fjiSeeyB17yCGHoFKppP8WV6WXXnoJAFKLpl//+tfo6ekpTNvPf/5zxHGM/fbbL3fNUaNGYZ111sGsWbMGlceddtoJI0eOxJprrolJkyahu7sbt912G1ZfffUBj6nVaun3xYsX480338RWW20Fay2efPLJpV5z5syZGDZsGD772c/m0r7ZZpuhu7t70Glfb731MHLkSIwdOxZHHnkk1l57bdxxxx25GE7VahWHHHJI7rg777wTo0aNysWwKpfLmDp1KhYtWoTf/OY3AIBbb70VxhicfvrpLdeW+z/Y+zCYeyoWarfffjviOB5UGWiOOuqo3L+33XbbtE4BwN13341yuYwvf/nL6bYgCHDMMccs03UOPfRQjBw5Equtthp22WUXzJ8/HzfeeCM+9alPAVi2NjF8+HD885//xOOPP77M+f138NBDD2H69OnYb7/9UldOQgghhLz30NWOEELIhxIZtG+88cYD7jN37lzMmzcP11xzDa655prCfd54443cv30RS1yUJL7R2LFj8dWvfhUXX3wxbr75Zmy77bb4/Oc/j4MOOigVMGbPng1rLdZZZ53Caw7WVe3yyy/Huuuui1KphFVWWQXrrbdeS4wbn7///e+YNm0a/ud//qclJpMfg6qI2bNnY/78+Vh55ZULf/fLayBuvfVWDB06FOVyGWussQbGjRvXss/qq6+eE/k
"text/plain": [
"<Figure size 1500x500 with 3 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved Original Plate to img\\plate3_original.jpg\n",
"Saved Processed Plate to img\\plate3_processed.jpg\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJ4AAAEvCAYAAAAEvQudAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOy9eaAcRbn+/1T3zJw1GxAIm0kIO4JoFJAlgCBhlyViQDBhkaBA4CtwFdFAWL0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DhxIrbeemtceOGFuPfee3HRRRdh3Lhx+MIXvgDAiWj77LMPfv/73+MLX/gCNt54Y9x2222YMmXKUqe1RtZPGjFihD822DpxzTXXYPr06Zg0aRJOOOEE9PT04IknnsDDDz+MQw45ZLnCNWHCBEyfPh3f/va38dWvfhWbbLIJAPi/A/HWW2+hr68Pr732Gr7//e/jqaeeKgiAhBBCCFnBWEIIIeT/ONddd50FYB955JGm58ybN88CsNddd50/NmHCBDtkyBD7wgsvFM5N09S/P/LII+2aa65pX3vttcI5kydPtsOGDbOLFi2y1lo7Z84cC8Busskmtre31593ySWXWAD2ySeftNZa+9hjj1kAdvbs2U3D+vzzz9s4ju0555xTOP7kk0/aSqXScDxE0uPee++18+fPt//85z/tzTffbFdddVXb0dFh//WvfxXCPGfOHP9biY/mvPPOs8aYQjode+yxtqwb8Zvf/MYCsDfddFPh+F133VV6POT000+3AOxf//pXO3/+fDtv3jz7ne98x7a1tdk11ljDvvvuu9Zaa3fccUcLwF511VWF31988cUWgL3xxhv9sb6+Pvvxj3/cdnd327feestaa+19991nAdjp06c3hEHyf7D5MJg8/da3vmUB2Pnz5zc9p6yMTpkyxQKwZ555ZuHcD3/4w3b8+PH+8y233GIB2IsvvtgfS5LEfuITn2i4ZhlSFq699lo7f/58+9JLL9m77rrLrr/++tYYY3//+9/7cwdbJz71qU/ZzTbbbMD7TpkyxY4ePbrhuJQDzejRo+2UKVP859mzZzeU38EwceJEC8ACsLVazU6bNs0uXrx4qa5BCCGEkMFDVztCCCHvS+bPn48HHngARxxxBD7wgQ8UvhMXH2stbrnlFuyzzz6w1uK1117zr4kTJ2LhwoV49NFHC789/PDDUavV/GdxVXruuecAwFs0/fKXv8SiRYtKw/bTn/4UaZrioIMOKtxz1KhR2GCDDTBnzpxBxXHXXXfFyJEjse6662Ly5Mno7u7GrbfeirXXXrvpbzo6Ovz7d999F6+99hq23XZbWGvx2GOPLfGes2fPxrBhw/DJT36yEPbx48eju7t70GHfaKONMHLkSIwdOxbTpk3D+uuvj9tvv72whlNbWxsOP/zwwu/uuOMOjBo1qrCGVbVaxfTp0/HOO+/g17/+NQDglltugTEGp59+esO9Jf8Hmw+DyVOxULvtttuQpumg0kBzzDHHFD7vsMMOvkwBwF133YVqtYrPf/7z/lgURTj22GOX6j5HHHEERo4cibXWWgu77747Fi5ciBtuuAEf+9jHACxdnRg+fDj+9a9/4ZFHHlnq+LaSb3zjG7j77rvxve99D9tssw36+vpQr9dXdrAIIYSQ/1joakcIIeR9iQzaP/jBDzY9Z/78+ViwYAGuvvpqXH311aXnvPrqq4XPoYglLkqyvtHYsWPxpS99Cd/85jdx0003YYcddsC+++6LQw891AsYc+fOhbUWG2ywQek9B+uqdvnll2PDDTdEpVLBGmusgY022ghRNPCc0z/+8Q/MmDED//M//9OwJlO4BlUZc+fOxcKFC7H66quXfh+mVzNuueUWDB0
"text/plain": [
"<Figure size 1500x500 with 3 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from PIL import Image, ImageOps # Added for padding\n",
"\n",
"# 1. Function to add padding around symbols\n",
"def add_padding(img, padding=10):\n",
" \"\"\"\n",
" Adds padding around the image.\n",
"\n",
" :param img: Numpy array with the symbol image.\n",
" :param padding: Number of pixels for padding.\n",
" :return: Image with padding.\n",
" \"\"\"\n",
" # Ensure the image is in uint8 format\n",
" if img.dtype != np.uint8:\n",
" img = img.astype(np.uint8)\n",
" \n",
" pil_img = Image.fromarray(img)\n",
" padded_img = ImageOps.expand(pil_img, border=padding, fill=255)\n",
" return np.array(padded_img)\n",
"\n",
"# Define mask positions based on GOST standards\n",
"# These masks are defined as fractions of the image width and height\n",
"# Adjust these values based on the actual license plate layout and resolution\n",
"\n",
"# Mask for plates with 2 region digits (total 8 symbols)\n",
"mask_8 = [\n",
" (0.05, 0.25, 0.1, 0.5), # Символ 1: Буква\n",
" (0.17, 0.25, 0.1, 0.5), # Символ 2: Цифра\n",
" (0.29, 0.25, 0.1, 0.5), # Символ 3: Цифра\n",
" (0.41, 0.25, 0.1, 0.5), # Символ 4: Цифра\n",
" (0.53, 0.25, 0.1, 0.5), # Символ 5: Буква\n",
" (0.65, 0.25, 0.1, 0.5), # Символ 6: Буква\n",
" (0.80, 0.05, 0.1, 0.5), # Символ 7: Региональная цифра 1 (выше и меньше)\n",
" (0.90, 0.05, 0.1, 0.5), # Символ 8: Региональная цифра 2 (выше и меньше)\n",
"]\n",
"\n",
"\n",
"# Mask for plates with 3 region digits (total 9 symbols)\n",
"mask_9 = [\n",
" (0.05, 0.2, 0.1, 0.6), # Symbol 1: Letter\n",
" (0.17, 0.2, 0.1, 0.6), # Symbol 2: Digit\n",
" (0.29, 0.2, 0.1, 0.6), # Symbol 3: Digit\n",
" (0.41, 0.2, 0.1, 0.6), # Symbol 4: Digit\n",
" (0.53, 0.2, 0.1, 0.6), # Symbol 5: Letter\n",
" (0.65, 0.2, 0.1, 0.6), # Symbol 6: Letter\n",
" (0.77, 0.2, 0.1, 0.6), # Symbol 7: Region Digit 1\n",
" (0.89, 0.2, 0.1, 0.6), # Symbol 8: Region Digit 2\n",
" (1.01, 0.2, 0.1, 0.6) # Symbol 9: Region Digit 3\n",
"]\n",
"\n",
"import cv2\n",
"import numpy as np\n",
"from PIL import Image, ImageOps\n",
"\n",
"def resize_with_padding(image, size=28, padding=4, bg_color=0):\n",
" \"\"\"\n",
" Изменяет размер изображения до заданного размера с сохранением соотношения сторон\n",
" и добавлением отступов.\n",
" \n",
" :param image: Градация серого изображения символа (numpy array).\n",
" :param size: Целевой размер (ширина и высота) итогового изображения.\n",
" :param padding: Количество пикселей для отступов.\n",
" :param bg_color: Цвет фона (0 для чёрного, 255 для белого).\n",
" :return: Изображение размером size x size пикселей.\n",
" \"\"\"\n",
" # Преобразуем numpy array в PIL Image\n",
" pil_img = Image.fromarray(image)\n",
" \n",
" # Определяем текущие размеры\n",
" w, h = pil_img.size\n",
" \n",
" # Рассчитываем масштабирование\n",
" scale = (size - 2 * padding) / max(w, h)\n",
" new_w = int(w * scale)\n",
" new_h = int(h * scale)\n",
" \n",
" # Изменяем размер с сохранением соотношения сторон\n",
" pil_resized = pil_img.resize((new_w, new_h), Image.LANCZOS)\n",
" \n",
" # Создаём новое изображение с фоном\n",
" new_img = Image.new(\"L\", (size, size), color=bg_color)\n",
" \n",
" # Вычисляем позицию для центрирования\n",
" paste_x = (size - new_w) // 2\n",
" paste_y = (size - new_h) // 2\n",
" \n",
" # Вставляем изменённое изображение на фон\n",
" new_img.paste(pil_resized, (paste_x, paste_y))\n",
" \n",
" # Преобразуем обратно в numpy array\n",
" return np.array(new_img)\n",
"\n",
"\n",
"\n",
"def extract_symbols(image, masks):\n",
" \"\"\"\n",
" Extracts symbols from the image based on the provided masks.\n",
"\n",
" :param image: Grayscale image of the license plate.\n",
" :param masks: List of tuples containing mask definitions as (x_frac, y_frac, w_frac, h_frac).\n",
" :return: List of extracted and resized symbol images.\n",
" \"\"\"\n",
" img_h, img_w = image.shape\n",
" symbols = []\n",
" for bbox in masks:\n",
" x_frac, y_frac, w_frac, h_frac = bbox\n",
" x = int(x_frac * img_w)\n",
" y = int(y_frac * img_h)\n",
" w = int(w_frac * img_w)\n",
" h = int(h_frac * img_h)\n",
" \n",
" # Ensure the bounding box is within image boundaries\n",
" x_end = min(x + w, img_w)\n",
" y_end = min(y + h, img_h)\n",
" \n",
" symbol = image[y:y_end, x:x_end]\n",
" if symbol.size == 0:\n",
" # If the bounding box is out of image bounds, skip\n",
" continue\n",
" # Add padding\n",
" # Resize to 28x28 pixels\n",
" symbol_resized = resize_with_padding(symbol, size=28, padding=2, bg_color=255)\n",
" symbols.append(symbol_resized)\n",
" return symbols\n",
"\n",
"def is_symbol_present(symbol, threshold=250):\n",
" \"\"\"\n",
" Determines if a symbol is present based on the mean pixel intensity.\n",
"\n",
" :param symbol: Grayscale image of the symbol.\n",
" :param threshold: Intensity threshold to consider a symbol as present.\n",
" :return: Boolean indicating presence of a symbol.\n",
" \"\"\"\n",
" return np.mean(symbol) < threshold # Darker symbols have lower mean intensity\n",
"\n",
"# Main processing function\n",
"def process_plate(plate):\n",
" \"\"\"\n",
" Processes a single license plate image to extract individual symbols based on masks.\n",
"\n",
" :param plate: BGR image of the cropped license plate.\n",
" :return: Tuple containing the processed grayscale image and list of extracted symbols.\n",
" \"\"\"\n",
" # Convert to grayscale\n",
" gray_plate = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY)\n",
"\n",
" # Resize for consistent processing (adjust fx and fy as needed)\n",
" resized_plate = cv2.resize(gray_plate, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)\n",
" img_h, img_w = resized_plate.shape\n",
"\n",
" # Apply Gaussian blur to reduce noise\n",
" blurred = cv2.GaussianBlur(resized_plate, (3, 3), 0)\n",
"\n",
" # Extract symbols using both mask configurations\n",
" symbols_8 = extract_symbols(blurred, mask_8)\n",
" symbols_9 = extract_symbols(blurred, mask_9)\n",
"\n",
" # Determine which mask to use based on the presence of the third region digit\n",
" # Check if the 9th symbol (Region Digit 3) is present\n",
" if len(symbols_9) == 9 and is_symbol_present(symbols_9[-1]):\n",
" characters = symbols_9\n",
" else:\n",
" characters = symbols_8\n",
"\n",
" return blurred, characters\n",
"\n",
"# Example usage with processed_plates\n",
"# Assume processed_plates is a list of BGR images containing cropped license plates\n",
"# processed_plates = [cv2.imread('plate1.jpg'), cv2.imread('plate2.jpg'), ...]\n",
"\n",
"output_dir = \"img\"\n",
"\n",
"for plate_index, plate in enumerate(processed_plates):\n",
" # Ensure the plate image is valid\n",
" if plate is None:\n",
" print(f\"Plate {plate_index + 1} could not be loaded.\")\n",
" continue\n",
"\n",
" # Process the plate to extract symbols\n",
" processed_plate, characters = process_plate(plate)\n",
"\n",
" # Сохранение Original Plate\n",
" original_path = os.path.join(output_dir, f\"plate{plate_index + 1}_original.jpg\")\n",
" cv2.imwrite(original_path, plate)\n",
" print(f\"Saved Original Plate to {original_path}\")\n",
"\n",
" # Сохранение Processed Plate (Grayscale & Blurred)\n",
" processed_path = os.path.join(output_dir, f\"plate{plate_index + 1}_processed.jpg\")\n",
" cv2.imwrite(processed_path, processed_plate)\n",
" print(f\"Saved Processed Plate to {processed_path}\")\n",
"\n",
" # Display the results\n",
" plt.figure(figsize=(15, 5))\n",
"\n",
" # Original Plate\n",
" plt.subplot(1, 3, 1)\n",
" plt.title(\"Original Plate\")\n",
" plt.imshow(cv2.cvtColor(plate, cv2.COLOR_BGR2RGB))\n",
" plt.axis('off')\n",
"\n",
" # Processed Plate (Grayscale & Blurred)\n",
" plt.subplot(1, 3, 2)\n",
" plt.title(\"Processed Plate\")\n",
" plt.imshow(processed_plate, cmap='gray')\n",
" plt.axis('off')\n",
"\n",
" # Extracted Symbols\n",
" plt.subplot(1, 3, 3)\n",
" plt.title(\"Extracted Symbols\")\n",
" if characters:\n",
" # Arrange symbols in a horizontal grid\n",
" symbols_grid = np.hstack(characters)\n",
" plt.imshow(symbols_grid, cmap='gray')\n",
" else:\n",
" plt.text(0.5, 0.5, 'No Symbols Detected', ha='center', va='center', fontsize=12)\n",
" plt.axis('off')\n",
"\n",
" plt.suptitle(f\"License Plate Processing Result {plate_index + 1}\")\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\leonk\\AppData\\Local\\Temp\\ipykernel_17656\\2202792851.py:138: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
" model.load_state_dict(torch.load(SAVE_PATH, map_location=DEVICE))\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Модель успешно загружена.\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABdEAAAE8CAYAAAAvwPhFAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd4DUZP4G8CfJzM5sYVmEpStlpSigKAoq0gRBigqCHCLIAirY0BP9HTaalDtBxaOIFVBBFBUUkSIHWPCwImA90MVTPOlVtk3y/v5I3sybzGRZZBHL8/H22J3NZN68SWZnnvc7bzQhhAARERERERERERERESXQT3QDiIiIiIiIiIiIiIh+qxiiExEREREREREREREFYIhORERERERERERERBSAIToRERERERERERERUQCG6EREREREREREREREARiiExEREREREREREREFYIhORERERERERERERBSAIToRERERERERERERUQCG6EREREREREREREREARiiExER0Qm1detWaJqG2bNnn+im/GasWbMGmqZhzZo1J7ophD/PMdq2bVu0bdv2RDfjN2n06NHQNK1Uy86ePRuapmHr1q3Ht1G/Q0fTj0RERES/JQzRiYiI6LiRYdJHH310opvymyD7Q35Fo1HUr18fN998M7Zv314mj/HGG29g9OjRZbIulQy/5FdaWhpOP/103HvvvThw4ECZPx79dtWuXdtzLKSnp6N58+Z45plnTnTTflUTJkzAokWLTnQzPH6P++Zo+/HRRx/FlVdeiVNOOQWapiE3N/e4tY2IiIhICp3oBhAREdGfW61atZCfn49wOHyim/KrGTt2LOrUqYOCggK8++67ePTRR/HGG2/gs88+Q1pa2jGt+4033sD06dOPS5AO2AFWRkYGDh06hBUrVmD8+PFYtWoV1q5d+4etMP0zHqNH0rRpUwwfPhwA8L///Q9PPvkkBgwYgMLCQlx33XUnuHVl795778WIESM8t02YMAG9evVC9+7dPbf3798fffr0QSQS+RVbGPd72zdB/RjkH//4Bw4ePIjmzZvjf//73/FtHBEREZGDI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b7B48WKMHz8eALB9+3acd9550DQNN998M7Kzs7F06VIMHjwYBw4cwG233eZZ59///nfouo477rgD+/fvxwMPPICrr74a77//PgCgqKgInTp1QmFhIW655RZUrVoV27Ztw+uvv459+/ahfPnyAIDx48fjvvvuQ+/evXHttddi586dmDp1Klq3bo3169cjKyvrqLdXhlsVK1YMXGbBggU4fPgwbrjhBlSsWBEffPABpk6dih9++AELFiwAAAwZMgQ//vgj3nzzTTz77LMJ6xgyZAhmz56NgQMHYtiwYcjLy8O0adOwfv16rF279hdNV5Ks7bt370bnzp3Rp08f9OvXD1WqVEF+fj7atm2LLVu24Oabb0adOnWwYMEC5ObmYt++fbj11lvd+w8ePBizZ89G586dce211yIWi+Gdd97BunXr3Ar+0uyH0uzTzz//HN26dcMZZ5yBsWPHIhKJYMuWLVi7du0Rt900TXTq1AktWrTA5MmTsXLlSjz44IPIycnBDTfcAMAOKi+99FJ88MEHuOGGG9CwYUO8+uqrGDBgQKn699tvv8WiRYtw5ZVXok6dOti+fTsee+wxtGnTBl988QWqV6/uWf5IxzkAPPXUUxgyZAguuOAC3Hbbbfj2229x2WWX4aSTTsLJJ59cqnb5xWIx/PDDD6hQoYLn9tIec7Nnz8agQYPQqFEj3HXXXcjKysL69euxbNky9O3b111m4MCBOPfcczFx4kRs374djzzyCNauXes595YsWYK//OUvaNKkCSZOnIi9e/di8ODBqFGjRtK2z5s3DwcPHsSQIUOgaRoeeOABXHHFFfj2228Dz4lnn30W1157LZo3b47rr78eAJCTkxPYP6NHj8aYMWPQoUMH3HDDDfj666/x6KOP4sMPP0w49/bu3YtLLrkEV1xxBXr37o2XXnoJf/vb39CkSRN07ty5dDtEcSz7ZseOHejYsSOys7MxYsQIZGVlYevWrXjllVeOuh3JHG0/EhEREZ0wgoiIiOg4mTVrlgAgPvzww8Bl8vLyBAAxa9Ys97bWrVuLcuXKie+++86zrGVZ7veDBw8W1apVE7t27fIs06dPH1G+fHlx+PBhIYQQq1evFgDEaaedJgoLC93lHnnkEQFAbNq0SQghxPr16wUAsWDBgsC2bt26VRiGIcaPH++5fdOmTSIUCiXc7if7Y+XKlWLnzp3i+++/F/PnzxcVK1YUqamp4ocffvC0efXq1e595faoJk6cKDRN8/TTTTfdJJK9xHvnnXcEADF37lzP7cuWLUt6u9+oUaMEAPH111+LnTt3iry8PPHYY4+JSCQiqlSpIn7++WchhBBt2rQRAMTMmTM9958yZYoAIJ577jn3tqKiInH++eeLjIwMceDAASGEEKtWrRIAxLBhwxLaIPd/afdDafbpww8/LACInTt3Bi6T7BgdMGCAACDGjh3rWfass84SzZo1c39++eWXBQAxZcoU9zbTNMVFF12UsM5kCgoKhGmaCe2JRCKexy7tcV5UVCQqV64smjZt6lnu8ccfFwBEmzZtSmyPEELUqlVLdOzYUezcuVPs3LlTbNq0SfTv318AEDfddJO7XGmPuX379oly5cqJFi1aiPz8fM+ycp/Ldjdu3NizzOuvvy4AiJEjR7q3NWnSRNSsWVMcPHjQvW3NmjUCgKhVq5anHwGIihUrij179ri3v/rqqwKAWLx4sXubPP5V6enpYsCAAQn9I8/zvLw8IYQQO3bsECkpKaJjx46efTlt2jQBQDz99NPubfL8eeaZZ9zbCgsLRdWqVUXPnj0THsuvrPfNwoULj/gcnuz5Sojk583R9GNpHMt9iYiIiI4Gp3MhIiKi35SdO3fi7bffxqBBg3DKKad
"text/plain": [
"<Figure size 1500x500 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABdEAAAE/CAYAAACpVIrrAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd5zUZOIG8CfJzM42FhCWrrCsFAUURQFFWJAmRQVBDhCkCtjQn6iHjV7uxIIHIlZABQsqWAHlAAuKFRHrgYIFT0Cks22S9/dH8mbeZJJlaeLp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88MEHmDFjBn766ScsXLgQADB8+HD8/PPPeOONN/DEE08kzWP48OGYO3cuBg0ahJEjR2LTpk2YOXMm1q5di9WrVx/WcCVBZd+xYwc6deqE3r17o1+/fqhcuTLy8/PRunVrbNy4Eddccw1ycnKwcOFCDBw4ELt27cJ1113nvn/IkCGYO3cuOnXqhKFDhyIej+Ptt9/GmjVr3B78pdkOpdmmX3zxBbp27YrTTjsNEyZMQCwWw8aNG7F69eqDrrtpmujYsSOaNWuGu+66C8uXL8fdd9+N3NxcXHnllQDsoPLCCy/EBx98gCuvvBL169fHiy++iAEDBpSqfr/77jssXrwYl156KXJycrB161Y8+OCDyMvLw5dffolq1ap5pj9YOweARx99FMOHD8e5556L66+/Ht999x0uuuginHDCCTjxxBNLVS6/eDyOn376CeXLl/c8X9o2N3fuXAwePBgNGjTALbfcgnLlymHt2rVYunQp+vbt604zaNAgnH322Zg6dSq2bt2K++67D6tXr/bse6+++ir+9re/oVGjRpg6dSp27tyJIUOGoHr16oFlX7BgAfbu3Yvhw4dD0zTceeeduOSSS/Ddd9+F7hNPPPEEhg4diqZNm2LYsGEAgNzc3ND6GTduHMaPH4927drhyiuvxDfffIMHHngAH374YdK+t3PnTlxwwQW45JJL0KtXLzz33HP4+9//jkaNGqFTp06l2yCKI9k227ZtQ4cOHZCdnY3Ro0ejXLly2Lx5M1544YVDLkeQQ63HML/88gvS09P/p694ISIioj84QURERHSMzJkzRwAQH374Yeg0mzZtEgDEnDlz3OdatWolypQpI77//nvPtJZluT8PGTJEVK1aVfz666+eaXr37i3Kli0rDhw4IIQQYuXKlQKAOOWUU0RhYaE73X333ScAiPXr1wshhFi7dq0AIBYuXBha1s2bNwvDMMTkyZM9z69fv15EIpGk5/1kfSxfvlxs375d/Pjjj+Lpp58WFSpUEGlpaeKnn37ylHnlypXue+X6qKZOnSo0TfPU09VXXy2CvuK9/fbbAoCYP3++5/mlS5cGPu83duxYAUB88803Yvv27WLTpk3iwQcfFLFYTFSuXFns379fCCFEXl6eACBmz57tef/06dMFAPHkk0+6zxUVFYlzzjlHZGZmij179gghhFixYoUAIEaOHJlUBrn9S7sdSrNN7733XgFAbN++PXSaoDY6YMAAAUBMmDDBM+0ZZ5whmjRp4v7+/PPPCwBi+vTp7nOmaYrzzz8/aZ5BCgoKhGmaSeWJxWKeZZe2nRcVFYlKlSqJxo0be6Z76KGHBACRl5dXYnmEEKJmzZqiQ4cOYvv27WL79u1i/fr1on///gKAuPrqq93pStvmdu3aJcqUKSOaNWsm8vPzPdPKbS7L3bBhQ880r7zyigAgxowZ4z7XqFEjUaNGDbF37173uVWrVgkAombNmp56BCAqVKggfvvtN/f5F198UQAQL7/8svucbP+qjIwMMWDAgKT6kfv5pk2bhBBCbNu2TaSkpIgOHTp4tuXMmTMFAPHYY4+
"text/plain": [
"<Figure size 1500x500 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABdEAAAE/CAYAAACpVIrrAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd4DUVOIH8G+Sme0soCxdKStFAUVRQBAWpElRUZADBKkKFtAT/ImNXu7EggciVkAFCyI2RMqBDQUbCtgOdLHgHb3Ktpm83x/JS14yybI0sXw/dyOzM5nMS/KSmXzfmxdNCCFAREREREREREREREQJ9JNdACIiIiIiIiIiIiKi3yuG6EREREREREREREREIRiiExERERERERERERGFYIhORERERERERERERBSCIToRERERERERERERUQiG6EREREREREREREREIRiiExERERERERERERGFYIhORERERERERERERBSCIToRERERERERERERUQiG6ERERHRSbdmyBZqmYc6cOSe7KL8bb7/9NjRNw9tvv32yi0L469TRVq1aoVWrVie7GL9LY8eOhaZpJZp2zpw50DQNW7ZsObGF+gM6kvVIRERE9HvCEJ2IiIhOGBkmffLJJye7KL8Lcn3IW0pKCmrXro2bbroJ27ZtOy7v8eabb2Ls2LHHZV4qGX7JW1paGs466yzcfffd2L9//3F/P/r9ql69uqcupKeno3Hjxnj66adPdtF+U5MnT8Yrr7xysovh8UfcNkeyHvPy8jBo0CDUr18fpUuXRkZGBs455xw89NBDKCoqOrEFJSIior+0yMkuABEREf21VatWDXl5eYhGoye7KL+Z8ePHo0aNGsjPz8f777+PRx55BG+++SY2btyItLS0Y5r3m2++iYcffviEBOkA8MgjjyAjIwMHDx7EsmXLMGnSJKxcuRKrV6/+0/Yw/SvW0cNp2LAhRowYAQD473//iyeeeAL9+vVDQUEBrr322pNcuuPv7rvvxqhRozyPTZ48Gd27d0fXrl09j/ft2xc9e/ZEcnLyb1hC1x9t24StxyB5eXn48ssv0alTJ1SvXh26ruODDz7A3//+d6xduxbz588/8QUmIiKivySG6ERERHRSyR7ZfyUdO3bE+eefDwAYPHgwTj31VDzwwAN49dVX0atXr5NcuuJ1794d5cqVAwAMHToU3bp1w8svv4w1a9bgwgsvDHzNoUOHjrlx4GT6K9bRw6lSpQr69Onj/N2/f3/UrFkTDz744O8yqD1WkUgEkUjJTp0Mw4BhGCe4ROH+zNvmlFNOwZo1azyPDR06FKVLl8aMGTPwwAMPoGLFiiepdERERPRnxuFciIiI6KQKG2/6m2++QY8ePZCVlYXU1FTUqVMHd911l2earVu3YuDAgahQoQKSk5NRr149PPXUU55p5PjiL774IiZNmoSqVasiJSUFbdq0webNmz3Tbtq0Cd26dUPFihWRkpKCqlWromfPnti3b59numeffRaNGjVCamoqTjnlFPTs2RM//fTTUa+Diy++GACQm5sbOs17772Hq666CqeffjqSk5Nx2mmn4e9//zvy8vKcafr374+HH34YADxDOkimaWLatGmoV68eUlJSUKFCBQwZMgR79uw5bmVv1aoV6tevj08//RQtW7ZEWloa7rzzTgDA9u3bMWjQIFSoUAEpKSk455xzMHfu3IR5mqaJhx56CA0aNEBKSgqysrJwySWXJAwLVJLtUJJtunz5clx00UUoU6YMMjIyUKdOHafMQHAd7d+/PzIyMrB161Z07doVGRkZyMrKwsiRIxGPxz1l2LVrF/r27YvMzEyUKVMG/fr1wxdffFGicdZ3796NkSNHokGDBsjIyEBmZiY6duyIL774wjPdkdRzAHjssceQnZ2N1NRUNG7cGO+9916x5TicrKws1K1bF999953n8SOpc0uWLEFOTg5KlSqFzMxMXHDBBQk9ixcsWOBs83LlyqFPnz7YunVrwrwWLFiAs846CykpKahfvz4WLVqE/v37o3r16s40crved999zvpITk7GBRdcgI8//tgzP/9Y3pqm4ddff8XcuXOd/ax///4AwsdEnzlzJurVq4fk5GRUrlwZN954I/bu3euZRu4/X331FVq3bo20tDRUqVIF9957b9iqP6xj3TaffPIJOnTogHLlyiE1NRU1atTAwIEDnefDruFQkmsJFLcej4Tcrv71SURERHS8sCc6ERER/e6sX78eLVq0QDQaxXXXXYfq1avju+++w+uvv45JkyYBALZt24amTZtC0zTcdNNNyMrKwpIlSzBo0CDs378ft9xyi2ee//jHP6DrOkaOHIl9+/bh3nvvxdVXX421a9cCAAoLC9GhQwcUFBRg2LBhqFixIrZu3Yo33ngDe/fuRenSpQEAkyZNwj333IMePXpg8ODB2LFjB6ZPn46WLVti3bp1KFOmzBEvrwy3Tj311NBpFixYgEOHDuH666/Hqaeeio8++gjTp0/Hzz//jAULFgAAhgwZgl9++QXLly/HM888kzCPIUOGYM6cORgwYACGDx+O3NxczJgxA+vWrcPq1auPariSoLLv2rULHTt2RM+ePdGnTx9UqFABeXl5aNWqFTZv3oybbroJNWrUwIIFC9C/f3/s3bsXN998s/P6QYMGYc6cOejYsSMGDx6MWCyG9957D2vWrHF68JdkO5Rkm3755Zfo0qULzj77bIwfPx7JycnYvHkzVq9efdhlj8fj6NChA5o0aYL77rsPK1aswP3334/s7Gxcf/31AKyg8tJLL8VHH32E66+/HnXr1sWrr76Kfv36lWj9fv/993jllVdw1VVXoUaNGti2bRseffRR5OTk4KuvvkLlypU90x+ungPAk08+iSFDhqBZs2a45ZZb8P333+Oyyy7DKaecgtNOO61E5f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xPbt20VSUpJo3769Z1v
"text/plain": [
"<Figure size 1500x500 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from PIL import Image, ImageOps # Added for padding\n",
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"from torchvision import transforms\n",
"\n",
"# 1. Function to add padding around symbols\n",
"def add_padding(img, padding=10):\n",
" \"\"\"\n",
" Adds padding around the image.\n",
"\n",
" :param img: Numpy array with the symbol image.\n",
" :param padding: Number of pixels for padding.\n",
" :return: Image with padding.\n",
" \"\"\"\n",
" # Ensure the image is in uint8 format\n",
" if img.dtype != np.uint8:\n",
" img = img.astype(np.uint8)\n",
" \n",
" pil_img = Image.fromarray(img)\n",
" padded_img = ImageOps.expand(pil_img, border=padding, fill=255)\n",
" return np.array(padded_img)\n",
"\n",
"# Define mask positions based on GOST standards\n",
"# These masks are defined as fractions of the image width and height\n",
"# Adjust these values based on the actual license plate layout and resolution\n",
"\n",
"# Mask for plates with 2 region digits (total 8 symbols)\n",
"mask_8 = [\n",
" (0.03, 0.05, 0.12, 0.9), # Символ 1: Буква\n",
" (0.17, 0.05, 0.12, 0.9), # Символ 2: Цифра\n",
" (0.29, 0.05, 0.12, 0.9), # Символ 3: Цифра\n",
" (0.41, 0.05, 0.12, 0.9), # Символ 4: Цифра\n",
" (0.53, 0.05, 0.12, 0.9), # Символ 5: Буква\n",
" (0.65, 0.05, 0.12, 0.9), # Символ 6: Буква\n",
" (0.80, 0.05, 0.11, 0.9), # Символ 7: Региональная цифра 1 (выше и меньше)\n",
" (0.90, 0.05, 0.11, 0.9), # Символ 8: Региональная цифра 2 (выше и меньше)\n",
"]\n",
"\n",
"# Mask for plates with 3 region digits (total 9 symbols)\n",
"mask_9 = [\n",
" (0.05, 0.2, 0.1, 0.6), # Symbol 1: Letter\n",
" (0.17, 0.2, 0.1, 0.6), # Symbol 2: Digit\n",
" (0.29, 0.2, 0.1, 0.6), # Symbol 3: Digit\n",
" (0.41, 0.2, 0.1, 0.6), # Symbol 4: Digit\n",
" (0.53, 0.2, 0.1, 0.6), # Symbol 5: Letter\n",
" (0.65, 0.2, 0.1, 0.6), # Symbol 6: Letter\n",
" (0.77, 0.2, 0.1, 0.6), # Symbol 7: Region Digit 1\n",
" (0.89, 0.2, 0.1, 0.6), # Symbol 8: Region Digit 2\n",
" (1.01, 0.2, 0.1, 0.6) # Symbol 9: Region Digit 3\n",
"]\n",
"\n",
"def resize_with_padding(image, size=28, padding=4, bg_color=255):\n",
" \"\"\"\n",
" Изменяет размер изображения до заданного размера с сохранением соотношения сторон\n",
" и добавлением отступов.\n",
" \n",
" :param image: Градация серого изображения символа (numpy array).\n",
" :param size: Целевой размер (ширина и высота) итогового изображения.\n",
" :param padding: Количество пикселей для отступов.\n",
" :param bg_color: Цвет фона (0 для чёрного, 255 для белого).\n",
" :return: Изображение размером size x size пикселей.\n",
" \"\"\"\n",
" # Преобразуем numpy array в PIL Image\n",
" pil_img = Image.fromarray(image)\n",
" \n",
" # Определяем текущие размеры\n",
" w, h = pil_img.size\n",
" \n",
" # Рассчитываем масштабирование\n",
" scale = (size - 2 * padding) / max(w, h)\n",
" new_w = int(w * scale)\n",
" new_h = int(h * scale)\n",
" \n",
" # Изменяем размер с сохранением соотношения сторон\n",
" pil_resized = pil_img.resize((new_w, new_h), Image.LANCZOS)\n",
" \n",
" # Создаём новое изображение с фоном\n",
" new_img = Image.new(\"L\", (size, size), color=bg_color)\n",
" \n",
" # Вычисляем позицию для центрирования\n",
" paste_x = (size - new_w) // 2\n",
" paste_y = (size - new_h) // 2\n",
" \n",
" # Вставляем изменённое изображение на фон\n",
" new_img.paste(pil_resized, (paste_x, paste_y))\n",
" \n",
" # Преобразуем обратно в numpy array\n",
" return np.array(new_img)\n",
"\n",
"def extract_symbols(image, masks):\n",
" \"\"\"\n",
" Extracts symbols from the image based on the provided masks.\n",
"\n",
" :param image: Grayscale image of the license plate.\n",
" :param masks: List of tuples containing mask definitions as (x_frac, y_frac, w_frac, h_frac).\n",
" :return: List of extracted and resized symbol images.\n",
" \"\"\"\n",
" img_h, img_w = image.shape\n",
" symbols = []\n",
" for bbox in masks:\n",
" x_frac, y_frac, w_frac, h_frac = bbox\n",
" x = int(x_frac * img_w)\n",
" y = int(y_frac * img_h)\n",
" w = int(w_frac * img_w)\n",
" h = int(h_frac * img_h)\n",
" \n",
" # Ensure the bounding box is within image boundaries\n",
" x_end = min(x + w, img_w)\n",
" y_end = min(y + h, img_h)\n",
" \n",
" symbol = image[y:y_end, x:x_end]\n",
" if symbol.size == 0:\n",
" # If the bounding box is out of image bounds, skip\n",
" continue\n",
" # Add padding\n",
" # Resize to 28x28 pixels\n",
" symbol_resized = resize_with_padding(symbol, size=28, padding=2, bg_color=255)\n",
" symbols.append(symbol_resized)\n",
" return symbols\n",
"\n",
"def is_symbol_present(symbol, threshold=250):\n",
" \"\"\"\n",
" Determines if a symbol is present based on the mean pixel intensity.\n",
"\n",
" :param symbol: Grayscale image of the symbol.\n",
" :param threshold: Intensity threshold to consider a symbol as present.\n",
" :return: Boolean indicating presence of a symbol.\n",
" \"\"\"\n",
" return np.mean(symbol) < threshold # Darker symbols have lower mean intensity\n",
"\n",
"model = SimpleCNN(len(CLASSES)).to(DEVICE)\n",
"SAVE_PATH = \"best_model_5.pth\"\n",
"try:\n",
" model.load_state_dict(torch.load(SAVE_PATH, map_location=DEVICE))\n",
" model.eval()\n",
" print(\"Модель успешно загружена.\")\n",
"except Exception as e:\n",
" print(f\"Ошибка при загрузке модели: {e}\")\n",
"\n",
"# Define transformation for the input symbols\n",
"transform = transforms.Compose([\n",
" transforms.ToPILImage(),\n",
" transforms.Grayscale(),\n",
" transforms.Resize((28, 28)),\n",
" transforms.ToTensor(),\n",
" transforms.Normalize((0.5,), (0.5,)) # Нормализация, при необходимости измените\n",
"])\n",
"\n",
"# Main processing function\n",
"def process_plate(plate):\n",
" \"\"\"\n",
" Processes a single license plate image to extract individual symbols based on masks.\n",
"\n",
" :param plate: BGR image of the cropped license plate.\n",
" :return: Tuple containing the processed grayscale image and list of extracted symbols.\n",
" \"\"\"\n",
" # Convert to grayscale\n",
" gray_plate = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY)\n",
"\n",
" # Resize for consistent processing (adjust fx and fy as needed)\n",
" resized_plate = cv2.resize(gray_plate, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)\n",
" img_h, img_w = resized_plate.shape\n",
"\n",
" # Apply Gaussian blur to reduce noise\n",
" blurred = cv2.GaussianBlur(resized_plate, (3, 3), 0)\n",
"\n",
" # Extract symbols using both mask configurations\n",
" symbols_8 = extract_symbols(blurred, mask_8)\n",
" symbols_9 = extract_symbols(blurred, mask_9)\n",
"\n",
" # Determine which mask to use based on the presence of the third region digit\n",
" # Check if the 9th symbol (Region Digit 3) is present\n",
" if len(symbols_9) == 9 and is_symbol_present(symbols_9[-1]):\n",
" characters = symbols_9\n",
" else:\n",
" characters = symbols_8\n",
"\n",
" return blurred, characters\n",
"\n",
"for plate_index, plate in enumerate(processed_plates):\n",
" # Ensure the plate image is valid\n",
" if plate is None:\n",
" print(f\"Plate {plate_index + 1} could not be loaded.\")\n",
" continue\n",
"\n",
" # Process the plate to extract symbols\n",
" processed_plate, characters = process_plate(plate)\n",
"\n",
" # Predict symbols using the model\n",
" predictions = []\n",
" for symbol_img in characters:\n",
" # Предобработка изображения\n",
" input_tensor = transform(symbol_img).unsqueeze(0).to(DEVICE) # [1, 1, 28, 28]\n",
" \n",
" with torch.no_grad():\n",
" outputs = model(input_tensor)\n",
" _, predicted = torch.max(outputs, 1)\n",
" predicted_label = CLASSES[predicted.item()]\n",
" predictions.append(predicted_label)\n",
" \n",
" # Display the results\n",
" plt.figure(figsize=(15, 5))\n",
"\n",
" # Original Plate\n",
" plt.subplot(1, 4, 1)\n",
" plt.title(\"Original Plate\")\n",
" plt.imshow(cv2.cvtColor(plate, cv2.COLOR_BGR2RGB))\n",
" plt.axis('off')\n",
"\n",
" # Processed Plate (Grayscale & Blurred)\n",
" plt.subplot(1, 4, 2)\n",
" plt.title(\"Processed Plate\")\n",
" plt.imshow(processed_plate, cmap='gray')\n",
" plt.axis('off')\n",
"\n",
" # Extracted Symbols\n",
" plt.subplot(1, 4, 3)\n",
" plt.title(\"Extracted Symbols\")\n",
" if characters:\n",
" # Arrange symbols in a horizontal grid\n",
" symbols_grid = np.hstack(characters)\n",
" plt.imshow(symbols_grid, cmap='gray')\n",
" else:\n",
" plt.text(0.5, 0.5, 'No Symbols Detected', ha='center', va='center', fontsize=12)\n",
" plt.axis('off')\n",
"\n",
" # Recognized Symbols with Predictions\n",
" plt.subplot(1, 4, 4)\n",
" plt.title(\"Recognized Symbols\")\n",
" if characters and predictions:\n",
" # Создадим холст для отображения символов с метками\n",
" symbol_images = [resize_with_padding(sym, size=28, padding=0, bg_color=255) for sym in characters]\n",
" num_symbols = len(symbol_images)\n",
" # Создадим пустой холст для размещения символов и меток\n",
" canvas = np.ones((28 + 10, 28 * num_symbols + 10), dtype=np.uint8) * 255 # 10 пикселей отступа\n",
" for i, (sym, pred) in enumerate(zip(symbol_images, predictions)):\n",
" canvas[0:28, i*28 + 5:(i+1)*28 + 5] = sym # Расположим символы с отступом\n",
" # Добавим текст метки под символом\n",
" plt.text(i*28 + 14, 28 + 5, pred, ha='center', va='center', fontsize=8, color='blue')\n",
" plt.imshow(canvas, cmap='gray')\n",
" else:\n",
" plt.text(0.5, 0.5, 'No Symbols Detected', ha='center', va='center', fontsize=12)\n",
" plt.axis('off')\n",
"\n",
" plt.suptitle(f\"License Plate Processing and Recognition Result {plate_index + 1}\")\n",
" plt.tight_layout()\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\leonk\\AppData\\Local\\Temp\\ipykernel_17656\\1436905508.py:59: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
" model.load_state_dict(torch.load(SAVE_PATH, map_location=DEVICE))\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Модель успешно загружена.\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAB8YAAAFLCAYAAACk+eZsAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd5wV1f3/8ffd3bt3G01YQEApq4IFNaJgQ8ACgiUoahALxYJGISZqYkXAlogFvyh2ARVLsDcsBOzRmMRuNBjBREwEKQqyfef3B79zPfO5M3cXQdHr6/l47GN35s6dOefMmbl353NKIgiCQAAAAAAAAAAAAAAA5Ki8TZ0AAAAAAAAAAAAAAAC+SwTGAQAAAAAAAAAAAAA5jcA4AAAAAAAAAAAAACCnERgHAAAAAAAAAAAAAOQ0AuMAAAAAAAAAAAAAgJxGYBwAAAAAAAAAAAAAkNMIjAMAAAAAAAAAAAAAchqBcQAAAAAAAAAAAABATiMwDgAAAAAAAAAAAADIaQTGAQAAAHynFi9erEQioZkzZ27qpPxgPPfcc0okEnruuec2dVKgn04d7d+/v/r377+pk/GDNHHiRCUSiSZtO3PmTCUSCS1evPi7TdSP0PqUIwAAAAB83wiMAwAAAPjWXIDor3/966ZOyg+CKw/3U1RUpG222Uann366Pv/8841yjCeffFITJ07cKPvyuYCW+ykpKdF2222nCy64QF999d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zTV48GC99dZboe3Wp55L0s0336yKigoVFxerd+/eevHFF7OmozHl5eXq0aOH/vWvf4XWr0+dmzt3rvr166dmzZqpefPm2m233XT33XeHtpkzZ076nLdp00bHHnuslixZkrGvOXPmaLvttlNRUZF22GEHPfTQQxo1apS6dOmS3sad1yuvvDJdHqlUSrvttptef/310P7s3NiJREJff/21Zs2alb7O3JDecXOMT58+Xdtvv71SqZQ6dOig0047TatWrQpt466f999/XwMGDFBJSYk6duyoK664Iq7oG7Wh5+avf/2rBg0apDZt2qi4uFhdu3bVmDFj0q+7uvfcc8+F3hd3b/dlK8c4nTt3ztnRKQAAAABsWvQYBwAAAPC9e/vtt9W3b18lk0mdfPLJ6tKli/71r3/pscce06WXXipJ+vzzz7X77rsrkUjo9NNPV3l5uebOnasTTjhBX331lc4444zQPn//+98rLy9PZ511lr788ktdccUVOuaYY/Taa69JkmpqajRo0CBVV1dr3Lhxat++vZYsWaLHH39cq1atUosWLSRJl156qS688EIdddRROvHEE7Vs2TJNmzZN++yzj9544w21bNlyvfPrAlatW7eO3WbOnDlau3atTj31VLVu3Vp/+ctfNG3aNH366aeaM2eOJGns2LH67LPP9Oyzz+rOO+/M2MfYsWM1c+ZMjR49WuPHj9eiRYt03XXX6Y033tDLL7/8rYYKj0r78uXLNXjwYA0fPlzHHnus2rVrp8rKSvXv318fffSRTj/9dHXt2lVz5szRqFGjtGrVKv3qV79Kv/+EE07QzJkzNXjwYJ144omqq6vTiy++qFdffTXd074p56Ep5/S9997TwQcfrB133FGTJ09WKpXSRx99pJdffrnRvNfX12vQoEHq06ePrrzySs2bN09XXXWVKioqdOqpp0paF3w85JBD9Je//EWnnnqqevTooUceeUQjR45sUvl+/PHHevjhh3XkkUeqa9eu+vzzz3XTTTepX79+ev/999WhQ4fQ9o3Vc0m67bbbNHbsWO25554644wz9PHHH+vQQw/VZpttpi222KJJ6bLq6ur06aefqlWrVqH1Ta1zM2fO1JgxY7T99tvr3HPPVcuWLfXGG2/oqaee0ogRI9LbjB49Wrvttpsuv/xyff7557r22mv18ssvh669J554Qr/4xS/Us2dPXX755Vq5cqVOOOEEdezYMTLtd999t1avXq2xY8cqkUjoiiuu0OGHH66PP/449pq48847deKJJ6p37946+eSTJUkVFRWx5TNx4kRNmjRJ+++/v0499VR9+OGHuuGGG/T6669nXHsrV67UgQceqMMPP1xHHXWU7r//fv3ud79Tz549NXjw4KadEM+GnJulS5dq4MCBKi8v1znnnKOWLVtq8eLFevDBB9c7HVHWtxwBAAAA4DsVAAAAAMC3NGPGjEBS8Prrr8dus2jRokBSMGPGjPS6ffbZJ2jWrFnwySefhLZtaGhI/33CCScEm2++efDFF1+Ethk+fHjQokWLYO3atUEQBMGCBQsCScG2224bVFdXp7e79tprA0nBO++8EwRBELzxxhuBpGDOnDmxaV28eHGQn58fXHrppaH177zzTlBQUJCx3nLlMW/evGDZsmXBf/7zn+Dee+8NWrduHRQXFweffvppKM0LFixIv9flx3f55ZcHiUQiVE6nnXZaEPWv3IsvvhhICmbPnh1a/9RTT0Wuty666KJAUvDhhx8Gy5YtCxYtWhTcdNNNQSqVCtq1axd8/fXXQRAEQb9+/QJJwY033hh6/9SpUwNJwV133ZVeV1NTE+yxxx5BWVlZ8NVXXwVBEATz588PJAXjx4/PSIM7/009D005p9dcc00gKVi2bFnsNlF1dOTIkYGkYPLkyaFtf/aznwW9evVKLz/wwAOBpGDq1KnpdfX19cG+++6bsc8oVVVVQX19fUZ6UqlU6NhNrec1NTVB27Ztg5133jm03c033xxICvr165c1PUEQBJ07dw4GDhwYLFu2LFi2bFnwzjvvBMcdd1wgKTjttNPS2zW1zq1atSpo1qxZ0KdPn6CysjK0rTvnLt077LBDaJvHH388kBRMmDAhva5nz55Bp06dgtWrV6fXPffcc4GkoHPnzqFylBS0bt06WLFiRXr9I488EkgKHnvssfQ6V/99paWlwciRIzPKx13nixYtCoIgCJYuXRoUFhY
"text/plain": [
"<Figure size 2000x500 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {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XW1uqee+7RAQccoLvvvlsjR478/hMMAAAAIGsRGAcAAADwvXI9p39OBg0apN12202SdNJJJ6lFixa6/vrr9cQTT+iYY47ZzKnL7Mgjj1TLli0lSaeeeqqGDBmiRx99VG+99Zb23HPPyG1Wr1690QH/zennWEcb0q5dOx133HGp5REjRqhz58664YYbfpTB142Vl5envLzGPSLJzc1Vbm7u95yieNl8bvr3769///vfqXuQtPY+tMsuu2js2LEExgEAAABsFIZSBwAAAPC9ipu/+dNPP9XRRx+t8vJyFRUVqUuXLrroootC6yxYsECjRo1S69atVVBQoG7duunuu+8OrePm6/7Tn/6kK6+8Uu3bt1dhYaH2228/zZ07N7TunDlzNGTIELVp00aFhYVq3769hg4dquXLl4fWu++++9SjRw8VFRVpiy220NChQ/Wf//xng8tg3333lSTNmzcvdp1XX31VRx11lLbeemsVFBRoq6220m9+8xutWbMmtc6IESN08803S1JoOGWnvr5eEydOVLdu3VRYWKjWrVtr9OjRWrp06SZLe79+/bTjjjvq73//u/r06aPi4mJdeOGFkqSFCxfqxBNPVOvWrVVYWKidd95Z06ZNS9tnfX29brzxRnXv3l2FhYUqLy/XgQcemDYkf2POQ2PO6Ysvvqh99tlHzZo1U2lpqbp06ZJKsxRdR0eMGKHS0lItWLBAgwcPVmlpqcrLy3Xuueeqrq4ulIbFixfr+OOPV1lZmZo1a6bhw4fr/fffb9S85UuWLNG5556r7t27q7S0VGVlZRo0aJDef//90HrrU88l6fbbb1dFRYWKiorUs2dPvfrqqxnT0ZDy8nJ17dpVn3/+eej19alzM2fOVN++fdWkSROVlZVp99131/333x9aZ8aMGalz3rJlSx133HFasGBB2r5mzJihHXbYQYWFhdpxxx312GOPacSIEerYsWNqHXder7322lR5FBQUaPfdd9c777wT2p+dGzuRSGjVqlWaNm1a6jobMWKEpPg5xidPnqxu3bqpoKBAbdu21RlnnKFly5aF1nHXzyeffKL+/furuLhY7dq10zXXXBNX9A3a2HPzt7/9TQMHDlTLli1VVFSkTp06adSoUan3Xd17+eWXQ9vF3dt9mcoxSrdu3UJBcUkqKCjQQQcdpK+++korVqzIXBgAAAAAkAE9xgEAAAD84D744AP17t1byWRSp5xyijp27KjPP/9cTz31lK688kpJ0jfffKM99thDiURCZ555psrLyzVz5kydeOKJ+u6773T22WeH9vmHP/xBOTk5Ovfcc7V8+XJdc801OvbYY/X2229LkqqrqzVw4EBVVVVpzJgxatOmjRYsWKCnn35ay5YtU9OmTSVJV155pS655BIdffTROumkk7Ro0SJNmjRJffr00bvvvqtmzZqtd35dwKpFixax68yYMUOrV6/WaaedphYtWuivf/2rJk2apK+++kozZsyQJI0ePVpff/21XnzxRd17771p+xg9erSmTp2qkSNH6qyzztK8efN000036d1339Xrr7++QUOFR6V98eLFGjRokIYOHarjjjtOrVu31po1a9SvXz/NnTtXZ555pjp16qQZM2ZoxIgRWrZsmX7961+ntj/xxBM1depUDRo0SCeddJJqa2v16quv6q233kr1tG/MeWjMOf344491yCGHaKeddtL48eNVUFCguXPn6vXXX28w73V1dRo4cKB69eqla6+9VrNmzdJ1112niooKnXbaaZLWBh8PPfRQ/fWvf9Vpp52mrl276oknntDw4cMbVb5ffPGFHn/8cR111FHq1KmTvvnmG912223q27evPvnkE7Vt2za0fkP1XJLuuusujR49WnvttZfOPvtsffHFFzrssMO0xRZbaKuttmpUuqza2lp99dVXat68eej1xta5qVOnatSoUerWrZsuuOACNWvWTO+++66ee+45DRs2LLXOyJEjtfvuu+vqq6/WN998oxtvvFGvv/566Np75pln9Ktf/Urdu3fX1VdfraVLl+rEE09Uu3btItN+//33a8WKFRo9erQSiYSuueYaHXHEEfriiy9ir4l7771XJ510knr27KlTTjlFklRRURFbPpdddpnGjRun/fffX6eddpo+++wz3XLLLXrnnXfSrr2lS5fqwAMP1BFHHKGjjz5aDz/8sH7/+9+re/fuGjRoUONOiGdjzs3ChQs1YMAAlZeX6/zzz1ezZs00f/58Pfroo+udjijrW45x/ve//6m4uPgnPTIFAAAAgB+BAAAAAAA20JQpUwJJwTvvvBO7zrx58wJJwZQpU1Kv9enTJ2jSpEnw5Zdfhtatr69P/X3iiScGW265ZfDtt9+G1hk6dGjQtGnTYPXq1UEQBMHs2bMDScH2228fVFVVpda78cYbA0nBhx9+GARBELz77ruBpGDGjBmxaZ0/f36Qm5sbXHnllaHXP/zwwyAvLy/tdcuVx6xZs4JFixYF//nPf4IHH3wwaNGiRVBUVBR89dVXoTTPnj07ta3Lj+/qq68OEolEqJzOOOOMIOpfuVdffTWQFEyfPj30+nPPPRf5unXppZcGkoLPPvssWLRoUTBv3rzgtttuCwoKCoLWrVsHq1atCoIgCPr27RtICm699dbQ9hMnTgwkBffdd1/qterq6mDPPfcMSktLg++++y4IgiB46aWXAknBWWedlZYGd/4bex4ac05vuOGGQFKwaNGi2HWi6ujw4cMDScH48eND6/7iF78IevTokVp+5JFHAknBxIkTU6/V1dUF++67b9o+o1RWVgZ1dXVp6SkoKAgdu7H1vLq6OmjVqlWwyy67hNa7/fbbA0lB3759M6YnCIKgQ4cOwYABA4JFixYFixYtCj788MPg+OOPDyQFZ5xxRmq9xta5ZcuWBU2aNAl69eoVrFmzJrSuO+cu3TvuuGNonaeffjqQFIwdOzb1Wvfu3YP
"text/plain": [
"<Figure size 2000x500 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAB8YAAAFOCAYAAAD0NHffAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd7wU1f3/8ffee/d2mnIFAaVcFSyoEQUbzQJiC4oaxEKxgAVioiZWBERNxPpFsUZAxRLsDRUixm5MYjcajGAifr8CUgS5/c7vD35nPfPZmb1LUcz6ej4e+9i7s7Mz55w5c3bvfOackwiCIBAAAAAAAAAAAAAAADkqb3MnAAAAAAAAAAAAAACA7xOBcQAAAAAAAAAAA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Ki4t14IEH6tNPPw2tu2DBAg0ZMkRt27ZVcXGxOnTooKFDh2rVqlWh9e6991716NFDJSUl2mKLLTR06FD95z//2eAyOOCAAyRJCxcujF3n5Zdf1rHHHqttt91WRUVF2mabbfSrX/1KVVVVqXVGjBihm2++WZJCwyk7jY2NuuGGG7TzzjuruLhYbdq00ejRo7VixYpNlvZ+/fppl1120d/+9jf16dNHpaWluuiiiyRJS5Ys0SmnnKI2bdqouLhYu+22m2bOnJm2zcbGRt14443q3r27iouLVVFRoUMOOSRtSP5sjkM2x3Tu3Lnaf//91bJlS5WXl6tr166pNEvRdXTEiBEqLy/X4sWLNXjwYJWXl6uiokLnnXeeGhoaQmn4+uuvddJJJ6l58+Zq2bKlhg8frnfffTerecuXL1+u8847T927d1d5ebmaN2+uQYMG6d133w2ttz71XJJuv/12VVZWqqSkRD179tTLL7+cMR1NqaioULdu3fSvf/0rtHx96tycOXPUt29fNWvWTM2bN9dee+2V1gN49uzZqWPeunVrnXjiiVq8eHHatmbPnq2ddtpJxcXF2mWXXfToo49qxIgR6tSpU2odd1yvueaaVHkUFRVpr7320ltvvRXanp0bO5FI6Ntvv9XMmTNT59mIESMkxc8xPm3aNO28884qKipSu3btdNZZZ2nlypWhddz589FHH6l///4qLS1V+/btdfXVV8cVfZM29tj89a9/1cCBA9W6dWuVlJSoc+fOGjVqVOp9V/defPHF0Ofi2nZfpnJcH+642vIEAAAAgPVBj3EAAAAAP7j33ntPvXv3VjKZ1Omnn65OnTrpX//6l5588kldccUVkqSvvvpKe++9txKJhM4++2xVVFRozpw5OuWUU/TNN9/onHPOCW3zd7/7nfLy8nTeeedp1apVuvrqq3XCCSfozTfflCTV1tZq4MCBqqmp0dixY9W2bVstXrxYTz31lFauXKkWLVpIkq644gpdeumlOu6443Tqqadq6dKlmjp1qvr06aO3335bLVu2XO/8uoDVlltuGbvO7NmztXbtWp1xxhnacsst9Ze//EVTp07VF198odmzZ0uSRo8erS+//FJz587VPffck7aN0aNHa8aMGRo5cqTGjRunhQsX6qabbtLbb7+tV199dYOGCo9K+9dff61BgwZp6NChOvHEE9WmTRtVVVWpX79++vTTT3X22Werc+fOmj17tkaMGKGVK1fql7/8Zerzp5xyimbMmKFBgwbp1FNPVX19vV5++WW98cYbqZ722RyHbI7phx9+qMMPP1y77rqrJk2apKKiIn366ad69dVXm8x7Q0ODBg4cqF69eumaa67RvHnzdO2116qyslJnnHGGpHXBxyOOOEJ/+ctfdMYZZ6hbt256/PHHNXz48KzK97PPPtNjjz2mY489Vp07d9ZXX32l2267TX379tVHH32kdu3ahdZvqp5L0h/+8AeNHj1a++67r8455xx99tlnOvLII7XFFltom222ySpdVn19vb744gu1atUqtDzbOjdjxgyNGjVKO++8sy688EK1bNlSb7/9tp599lkNGzYstc7IkSO111576aqrrtJXX32lG2+8Ua+++mro3Hv66af1i1/8Qt27d9dVV12lFStW6JRTTlH79u0j037fffdp9erVGj16tBKJhK6++modffTR+uyzz2LPiXvuuUennnqqevbsqdNPP12SVFlZGVs+EyZM0MSJE3XQQQfpjDPO0CeffKJbbrlFb731Vtq5t2LFCh1yyCE6+uijddxxx+mhhx7Sb3/7W3Xv3l2DBg3K7oB4NubYLFmyRAMGDFBFRYUuuOACtWzZUosWLdIjjzyy3umIsr7l6NTW1uqbb75RVVWV/vrXv+qaa65Rx44dtd12222SdAEAAAD4iQoAAAAAYANNnz49kBS89dZbsessXLgwkBRMnz49taxPnz5Bs2bNgs8//zy0bmNjY+rvU045Jdh6662DZcuWhdYZOnRo0KJFi2Dt2rVBEATB/PnzA0nBjjvuGNTU1KTWu/HGGwNJwfvvvx8EQRC8/fbbgaRg9uzZsWldtGhRkJ+fH1xxxRWh5e+//35QUFCQttxy5TFv3rxg6dKlwX/+85/ggQceCLbccsugpKQk+OKLL0Jpnj9/fuqzLj++q666KkgkEqFyOuuss4Kof+VefvnlQFIwa9as0PJnn302crl12WWXBZKCTz75JFi6dGmwcOHC4LbbbguKioqCNm3aBN9++20QBEHQt2/fQFJw6623hj5/ww03BJKCe++9N7WstrY22GeffYLy8vLgm2++CYIgCF544YVAUjBu3Li0NLjjn+1xyOaYXn/99YGkYOnSpbHrRNXR4cOHB5KCSZMmhdb92c9+FvTo0SP1+uGHHw4kBTfccENqWUNDQ3DAAQekbTNKdXV10NDQkJaeoqKi0L6zree1tbXBVlttFey+++6h9W6//fZAUtC3b9+M6QmCIOjYsWMwYMCAYOnSpcHSpUuD999/PzjppJMCScFZZ52VWi/bOrdy5cqgWbNmQa9evYKqqqrQuu6Yu3TvsssuoXWeeuq
"text/plain": [
"<Figure size 2000x500 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from PIL import Image\n",
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"from torchvision import transforms\n",
"\n",
"transform = transforms.Compose([\n",
" transforms.ToPILImage(),\n",
" transforms.Grayscale(),\n",
" transforms.Resize((28, 28)), \n",
" transforms.ToTensor(),\n",
" transforms.Normalize((0.25,), (0.25,)) \n",
"])\n",
"\n",
"def extract_symbols(image, masks):\n",
" \"\"\"\n",
" Извлекает символы из изображения на основе предоставленных масок.\n",
"\n",
" :param image: Градация серого изображения номерного знака.\n",
" :param masks: Список кортежей с определениями масок как (x_frac, y_frac, w_frac, h_frac).\n",
" :return: Список извлечённых символов (numpy arrays).\n",
" \"\"\"\n",
" img_h, img_w = image.shape\n",
" symbols = []\n",
" for bbox in masks:\n",
" x_frac, y_frac, w_frac, h_frac = bbox\n",
" x = int(x_frac * img_w)\n",
" y = int(y_frac * img_h)\n",
" w = int(w_frac * img_w)\n",
" h = int(h_frac * img_h)\n",
" \n",
" x_end = min(x + w, img_w)\n",
" y_end = min(y + h, img_h)\n",
" \n",
" symbol = image[y:y_end, x:x_end]\n",
" if symbol.size == 0:\n",
" continue\n",
"\n",
" symbols.append(symbol)\n",
" return symbols\n",
"\n",
"def is_symbol_present(symbol, threshold=250):\n",
" \"\"\"\n",
" Определяет, присутствует ли символ на основе средней интенсивности пикселей.\n",
"\n",
" :param symbol: Градация серого изображения символа.\n",
" :param threshold: Порог интенсивности для определения наличия символа.\n",
" :return: Булево значение, указывающее на наличие символа.\n",
" \"\"\"\n",
" return np.mean(symbol) < threshold \n",
"\n",
"\n",
"model = SimpleCNN(len(CLASSES)).to(DEVICE)\n",
"SAVE_PATH = \"best_model_8.pth\"\n",
"try:\n",
" model.load_state_dict(torch.load(SAVE_PATH, map_location=DEVICE))\n",
" model.eval()\n",
" print(\"Модель успешно загружена.\")\n",
"except Exception as e:\n",
" print(f\"Ошибка при загрузке модели: {e}\")\n",
"\n",
"def process_plate(plate):\n",
" \"\"\"\n",
" Обрабатывает одно изображение номерного знака для извлечения отдельных символов на основе масок.\n",
"\n",
" :param plate: BGR изображение обрезанного номерного знака.\n",
" :return: Кортеж, содержащий обработанное градационное изображение и список извлечённых символов.\n",
" \"\"\"\n",
" gray_plate = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY)\n",
"\n",
" resized_plate = cv2.resize(gray_plate, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)\n",
" img_h, img_w = resized_plate.shape\n",
"\n",
" blurred = cv2.GaussianBlur(resized_plate, (3, 3), 0)\n",
"\n",
" symbols_8 = extract_symbols(blurred, mask_8)\n",
" symbols_9 = extract_symbols(blurred, mask_9)\n",
"\n",
" if len(symbols_9) == 9 and is_symbol_present(symbols_9[-1]):\n",
" characters = symbols_9\n",
" expected_types = expected_types_9\n",
" else:\n",
" characters = symbols_8\n",
" expected_types = expected_types_8\n",
"\n",
" return blurred, characters, expected_types\n",
"\n",
" \n",
"\n",
"expected_types_8 = ['letter', 'digit', 'digit', 'digit', 'letter', 'letter', 'digit', 'digit']\n",
"expected_types_9 = ['letter', 'digit', 'digit', 'digit', 'letter', 'letter', 'digit', 'digit', 'digit']\n",
"\n",
"digits = set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])\n",
"letters = set([c for c in CLASSES if c.isalpha()])\n",
"\n",
"mask_8 = [\n",
" (0.03, 0.05, 0.12, 0.9), # Символ 1: Буква\n",
" (0.16, 0.05, 0.12, 0.9), # Символ 2: Цифра\n",
" (0.28, 0.05, 0.12, 0.9), # Символ 3: Цифра\n",
" (0.40, 0.05, 0.12, 0.9), # Символ 4: Цифра\n",
" (0.52, 0.05, 0.12, 0.9), # Символ 5: Буква\n",
" (0.64, 0.05, 0.12, 0.9), # Символ 6: Буква\n",
" (0.79, 0.07, 0.10, 0.60), # Символ 7: Региональная цифра 1\n",
" (0.89, 0.07, 0.10, 0.60), # Символ 8: Региональная цифра 2 \n",
"]\n",
"\n",
"mask_9 = [\n",
" (0.05, 0.2, 0.1, 0.6), # Symbol 1: Letter\n",
" (0.17, 0.2, 0.1, 0.6), # Symbol 2: Digit\n",
" (0.29, 0.2, 0.1, 0.6), # Symbol 3: Digit\n",
" (0.41, 0.2, 0.1, 0.6), # Symbol 4: Digit\n",
" (0.53, 0.2, 0.1, 0.6), # Symbol 5: Letter\n",
" (0.65, 0.2, 0.1, 0.6), # Symbol 6: Letter\n",
" (0.77, 0.2, 0.1, 0.6), # Symbol 7: Region Digit 1\n",
" (0.89, 0.2, 0.1, 0.6), # Symbol 8: Region Digit 2\n",
" (1.01, 0.2, 0.1, 0.6) # Symbol 9: Region Digit 3\n",
"]\n",
"alls = []\n",
"alls_t = []\n",
"\n",
"for plate_index, plate in enumerate(processed_plates):\n",
"\n",
" if plate is None:\n",
" print(f\"Plate {plate_index + 1} could not be loaded.\")\n",
" continue\n",
"\n",
" processed_plate, characters, expected_types = process_plate(plate)\n",
"\n",
" predictions = []\n",
" transformed_symbols = []\n",
" input_tensors = []\n",
" for idx, (symbol_img, expected_type) in enumerate(zip(characters, expected_types)):\n",
"\n",
" input_tensor = transform(symbol_img).unsqueeze(0).to(DEVICE)\n",
" input_tensors.append(input_tensor)\n",
"\n",
" transformed_image = transform(symbol_img).squeeze().cpu().numpy()\n",
" transformed_symbols.append(transformed_image)\n",
"\n",
" with torch.no_grad():\n",
" outputs = model(input_tensor)\n",
" probabilities = torch.nn.functional.softmax(outputs, dim=1)\n",
" probs, indices = torch.sort(probabilities, descending=True)\n",
"\n",
" predicted_labels = [CLASSES[i] for i in indices[0]]\n",
"\n",
" if expected_type == 'letter':\n",
" for label in predicted_labels:\n",
" if label in letters:\n",
" predicted_label = label\n",
" break\n",
" else:\n",
" predicted_label = predicted_labels[0]\n",
" elif expected_type == 'digit':\n",
" for label in predicted_labels:\n",
" if label in digits:\n",
" predicted_label = label\n",
" break\n",
" else:\n",
" predicted_label = predicted_labels[0]\n",
" else:\n",
" predicted_label = predicted_labels[0]\n",
"\n",
" predictions.append(predicted_label)\n",
" alls.append(transformed_symbols)\n",
" alls_t.append(input_tensors)\n",
"\n",
" plt.figure(figsize=(20, 5))\n",
"\n",
"\n",
" plt.subplot(1, 4, 1)\n",
" plt.title(\"Original Plate\")\n",
" plt.imshow(cv2.cvtColor(plate, cv2.COLOR_BGR2RGB))\n",
" plt.axis('off')\n",
"\n",
" plt.subplot(1, 4, 2)\n",
" plt.title(\"Processed Plate\")\n",
" plt.imshow(processed_plate, cmap='gray')\n",
" plt.axis('off')\n",
"\n",
" plt.subplot(1, 4, 3)\n",
" plt.title(\"Extracted Symbols\")\n",
" if transformed_symbols:\n",
" symbols_grid = np.hstack(transformed_symbols)\n",
" plt.imshow(symbols_grid, cmap='gray')\n",
" else:\n",
" plt.text(0.5, 0.5, 'No Symbols Detected', ha='center', va='center', fontsize=12)\n",
" plt.axis('off')\n",
"\n",
" plt.subplot(1, 4, 4)\n",
" plt.title(\"Recognized Symbols\")\n",
" if transformed_symbols and predictions:\n",
"\n",
" num_symbols = len(transformed_symbols)\n",
" canvas = np.ones((28 + 10, 28 * num_symbols + 10), dtype=np.uint8) * 255 \n",
" for i, (sym, pred) in enumerate(zip(transformed_symbols, predictions)):\n",
" canvas[0:28, i*28 + 5:(i+1)*28 + 5] = sym \n",
" plt.text(i*28 + 14, 28 + 5, pred, ha='center', va='center', fontsize=8, color='blue')\n",
" plt.imshow(canvas, cmap='gray')\n",
" else:\n",
" plt.text(0.5, 0.5, 'No Symbols Detected', ha='center', va='center', fontsize=12)\n",
" plt.axis('off')\n",
" \n",
"\n",
" plt.suptitle(f\"License Plate Processing and Recognition Result {plate_index + 1}\")\n",
" plt.tight_layout()\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\leonk\\AppData\\Local\\Temp\\ipykernel_17656\\332507320.py:72: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
" model.load_state_dict(torch.load(SAVE_PATH, map_location=DEVICE))\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Модель успешно загружена.\n"
]
},
{
"ename": "NameError",
"evalue": "name 'symbols_9' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[51], line 141\u001b[0m\n\u001b[0;32m 138\u001b[0m processed_plate, characters \u001b[38;5;241m=\u001b[39m process_plate(plate)\n\u001b[0;32m 140\u001b[0m \u001b[38;5;66;03m# Выбор соответствующего списка ожидаемых типов\u001b[39;00m\n\u001b[1;32m--> 141\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m characters \u001b[38;5;241m==\u001b[39m \u001b[43msymbols_9\u001b[49m:\n\u001b[0;32m 142\u001b[0m expected_types \u001b[38;5;241m=\u001b[39m expected_types_9\n\u001b[0;32m 143\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
"\u001b[1;31mNameError\u001b[0m: name 'symbols_9' is not defined"
]
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from PIL import Image\n",
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"from torchvision import transforms\n",
"\n",
"# Предполагается, что CLASSES и DEVICE уже определены\n",
"# Например:\n",
"# CLASSES = ['A', 'B', 'C', ..., '0', '1', ..., '9']\n",
"# DEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"# Разделение классов на буквы и цифры\n",
"letters = [cls for cls in CLASSES if cls.isalpha()]\n",
"digits = [cls for cls in CLASSES if cls.isdigit()]\n",
"\n",
"# Создание словарей для быстрого доступа к индексам\n",
"letter_indices = {cls: idx for idx, cls in enumerate(CLASSES) if cls.isalpha()}\n",
"digit_indices = {cls: idx for idx, cls in enumerate(CLASSES) if cls.isdigit()}\n",
"\n",
"transform = transforms.Compose([\n",
" transforms.ToPILImage(),\n",
" transforms.Grayscale(),\n",
" transforms.Resize((28, 28)), \n",
" transforms.ToTensor(),\n",
" transforms.Normalize((0.25,), (0.25,)) \n",
"])\n",
"\n",
"def extract_symbols(image, masks):\n",
" \"\"\"\n",
" Извлекает символы из изображения на основе предоставленных масок.\n",
"\n",
" :param image: Градация серого изображения номерного знака.\n",
" :param masks: Список кортежей с определениями масок как (x_frac, y_frac, w_frac, h_frac).\n",
" :return: Список извлечённых символов (numpy arrays).\n",
" \"\"\"\n",
" img_h, img_w = image.shape\n",
" symbols = []\n",
" for bbox in masks:\n",
" x_frac, y_frac, w_frac, h_frac = bbox\n",
" x = int(x_frac * img_w)\n",
" y = int(y_frac * img_h)\n",
" w = int(w_frac * img_w)\n",
" h = int(h_frac * img_h)\n",
" \n",
" x_end = min(x + w, img_w)\n",
" y_end = min(y + h, img_h)\n",
" \n",
" symbol = image[y:y_end, x:x_end]\n",
" if symbol.size == 0:\n",
" continue\n",
"\n",
" symbols.append(symbol)\n",
" return symbols\n",
"\n",
"def is_symbol_present(symbol, threshold=250):\n",
" \"\"\"\n",
" Определяет, присутствует ли символ на основе средней интенсивности пикселей.\n",
"\n",
" :param symbol: Градация серого изображения символа.\n",
" :param threshold: Порог интенсивности для определения наличия символа.\n",
" :return: Булево значение, указывающее на наличие символа.\n",
" \"\"\"\n",
" return np.mean(symbol) < threshold \n",
"\n",
"# Определение модели (предполагается, что SimpleCNN определен)\n",
"model = SimpleCNN(len(CLASSES)).to(DEVICE)\n",
"SAVE_PATH = \"best_model_8.pth\"\n",
"try:\n",
" model.load_state_dict(torch.load(SAVE_PATH, map_location=DEVICE))\n",
" model.eval()\n",
" print(\"Модель успешно загружена.\")\n",
"except Exception as e:\n",
" print(f\"Ошибка при загрузке модели: {e}\")\n",
"\n",
"def process_plate(plate):\n",
" \"\"\"\n",
" Обрабатывает одно изображение номерного знака для извлечения отдельных символов на основе масок.\n",
"\n",
" :param plate: BGR изображение обрезанного номерного знака.\n",
" :return: Кортеж, содержащий обработанное градационное изображение и список извлечённых символов.\n",
" \"\"\"\n",
" gray_plate = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY)\n",
"\n",
" resized_plate = cv2.resize(gray_plate, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)\n",
" img_h, img_w = resized_plate.shape\n",
"\n",
" blurred = cv2.GaussianBlur(resized_plate, (3, 3), 0)\n",
"\n",
" symbols_8 = extract_symbols(blurred, mask_8)\n",
" symbols_9 = extract_symbols(blurred, mask_9)\n",
"\n",
" if len(symbols_9) == 9 and is_symbol_present(symbols_9[-1]):\n",
" characters = symbols_9\n",
" else:\n",
" characters = symbols_8\n",
"\n",
" return blurred, characters\n",
"\n",
"mask_8 = [\n",
" (0.03, 0.05, 0.12, 0.9), # Символ 1: Буква\n",
" (0.16, 0.05, 0.12, 0.9), # Символ 2: Цифра\n",
" (0.28, 0.05, 0.12, 0.9), # Символ 3: Цифра\n",
" (0.40, 0.05, 0.12, 0.9), # Символ 4: Цифра\n",
" (0.52, 0.05, 0.12, 0.9), # Символ 5: Буква\n",
" (0.64, 0.05, 0.12, 0.9), # Символ 6: Буква\n",
" (0.80, 0.05, 0.11, 0.6), # Символ 7: Региональная цифра 1\n",
" (0.90, 0.05, 0.11, 0.6), # Символ 8: Региональная цифра 2 \n",
"]\n",
"\n",
"mask_9 = [\n",
" (0.05, 0.2, 0.1, 0.6), # Symbol 1: Letter\n",
" (0.17, 0.2, 0.1, 0.6), # Symbol 2: Digit\n",
" (0.29, 0.2, 0.1, 0.6), # Symbol 3: Digit\n",
" (0.41, 0.2, 0.1, 0.6), # Symbol 4: Digit\n",
" (0.53, 0.2, 0.1, 0.6), # Symbol 5: Letter\n",
" (0.65, 0.2, 0.1, 0.6), # Symbol 6: Letter\n",
" (0.77, 0.2, 0.1, 0.6), # Symbol 7: Region Digit 1\n",
" (0.89, 0.2, 0.1, 0.6), # Symbol 8: Region Digit 2\n",
" (1.01, 0.2, 0.1, 0.6) # Symbol 9: Region Digit 3\n",
"]\n",
"\n",
"# Определение ожидаемых типов символов для mask_8 и mask_9\n",
"expected_types_8 = ['letter', 'digit', 'digit', 'digit', 'letter', 'letter', 'digit', 'digit']\n",
"expected_types_9 = ['letter', 'digit', 'digit', 'digit', 'letter', 'letter', 'digit', 'digit', 'digit']\n",
"\n",
"alls = []\n",
"alls_t = []\n",
"\n",
"for plate_index, plate in enumerate(processed_plates):\n",
"\n",
" if plate is None:\n",
" print(f\"Plate {plate_index + 1} could not be loaded.\")\n",
" continue\n",
"\n",
" processed_plate, characters = process_plate(plate)\n",
"\n",
" # Выбор соответствующего списка ожидаемых типов\n",
" if characters == symbols_9:\n",
" expected_types = expected_types_9\n",
" else:\n",
" expected_types = expected_types_8\n",
"\n",
" predictions = []\n",
" transformed_symbols = [] \n",
" input_tensors = []\n",
" for idx, symbol_img in enumerate(characters):\n",
" input_tensor = transform(symbol_img).unsqueeze(0).to(DEVICE) \n",
" input_tensors.append(input_tensor)\n",
"\n",
" transformed_image = transform(symbol_img).squeeze().cpu().numpy() \n",
" transformed_symbols.append(transformed_image)\n",
"\n",
" with torch.no_grad():\n",
" outputs = model(input_tensor) # Предполагается, что модель возвращает сырые логиты\n",
"\n",
" # Применяем softmax для получения вероятностей\n",
" probabilities = F.softmax(outputs, dim=1).squeeze()\n",
"\n",
" # Определение ожидаемого типа символа\n",
" expected_type = expected_types[idx]\n",
"\n",
" if expected_type == 'letter':\n",
" # Получение индексов классов-букв\n",
" class_indices = [CLASSES.index(cls) for cls in letters]\n",
" elif expected_type == 'digit':\n",
" # Получение индексов классов-цифр\n",
" class_indices = [CLASSES.index(cls) for cls in digits]\n",
" else:\n",
" # Если тип не определен, использовать все классы\n",
" class_indices = list(range(len(CLASSES)))\n",
"\n",
" # Извлечение вероятностей для соответствующих классов\n",
" if len(class_indices) == 0:\n",
" print(f\"Неизвестный тип символа: {expected_type}. Используются все классы.\")\n",
" class_indices = list(range(len(CLASSES)))\n",
"\n",
" filtered_probs = probabilities[class_indices]\n",
" if filtered_probs.numel() == 0:\n",
" print(f\"Нет классов для типа: {expected_type}.\")\n",
" predicted_label = \"Unknown\"\n",
" else:\n",
" # Получение индекса максимальной вероятности среди отфильтрованных\n",
" max_prob, max_idx = torch.max(filtered_probs, dim=0)\n",
" predicted_label = CLASSES[class_indices[max_idx.item()]]\n",
"\n",
" predictions.append(predicted_label)\n",
"\n",
" alls.append(transformed_symbols)\n",
" alls_t.append(input_tensors)\n",
"\n",
" plt.figure(figsize=(20, 5))\n",
"\n",
" plt.subplot(1, 4, 1)\n",
" plt.title(\"Original Plate\")\n",
" plt.imshow(cv2.cvtColor(plate, cv2.COLOR_BGR2RGB))\n",
" plt.axis('off')\n",
"\n",
" plt.subplot(1, 4, 2)\n",
" plt.title(\"Processed Plate\")\n",
" plt.imshow(processed_plate, cmap='gray')\n",
" plt.axis('off')\n",
"\n",
" plt.subplot(1, 4, 3)\n",
" plt.title(\"Extracted Symbols\")\n",
" if transformed_symbols:\n",
" symbols_grid = np.hstack(transformed_symbols)\n",
" plt.imshow(symbols_grid, cmap='gray')\n",
" else:\n",
" plt.text(0.5, 0.5, 'No Symbols Detected', ha='center', va='center', fontsize=12)\n",
" plt.axis('off')\n",
"\n",
" plt.subplot(1, 4, 4)\n",
" plt.title(\"Recognized Symbols\")\n",
" if transformed_symbols and predictions:\n",
"\n",
" num_symbols = len(transformed_symbols)\n",
" canvas = np.ones((28 + 20, 28 * num_symbols + 10), dtype=np.uint8) * 255 \n",
" for i, (sym, pred) in enumerate(zip(transformed_symbols, predictions)):\n",
" canvas[0:28, i*28 + 5:(i+1)*28 + 5] = sym \n",
" plt.text(i*28 + 14, 28 + 10, pred, ha='center', va='center', fontsize=12, color='blue')\n",
" plt.imshow(canvas, cmap='gray')\n",
" else:\n",
" plt.text(0.5, 0.5, 'No Symbols Detected', ha='center', va='center', fontsize=12)\n",
" plt.axis('off')\n",
"\n",
" plt.suptitle(f\"License Plate Processing and Recognition Result {plate_index + 1}\")\n",
" plt.tight_layout()\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAARQAAADECAYAAABeOd7iAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAbjUlEQVR4nO3de1BU9/k/8PciAVREQS6RBG8EjNfqmKSOGQVbkooajLHSmomNNYKx2tSk1YqNRUy8RSPx0hh1VLxkpopGa6ZpNDY2VWOnltaqTeKtEhUvsFExAoq45/eHPzas53lwj378arPv14wz8uGzh3POLg9nn+ezz3FZlmWBiMiAoLu9A0T07cGAQkTGMKAQkTEMKERkDAMKERnDgEJExjCgEJExDChEZA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7Li4OCxYswJgxY7xjtVJSUny217lzZ8ydO1fcXkJCgs/XAwcOtP3Svvrqqzhz5oztsdnZ2RgyZIjPWFZWlvhzfvvb36JXr164evUqioqKMHXqVFy4cAEffPCBOL/W1q1bsW3bNuzevbveebfj888/R0FBAdasWSPmLOoaMWIEunXrhoMHD6KwsNB7tSKJi4vzLmcoLy/H8uXL0bdvX+zcuROdO3d2vJ+1z0Hd19StuKMBJTExEZZloU2bNrYXpuTUqVOoqKjw+aU4dOgQAPhk6gGge/fu3mx5eno6SkpKMGvWLEyePNnnEq93797etwcZGRnYtWsXPvzww3oDyqlTpzBv3jzMmDEDTZo0cRxQ/H188+bNkZa
"text/plain": [
"<Figure size 200x200 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"def display_symbol(symbol_array, is_grayscale=True, title=\"Символ\"):\n",
" \"\"\"\n",
" Отображает символ из numpy массива.\n",
"\n",
" :param symbol_array: Numpy массив, представляющий изображение символа.\n",
" :param is_grayscale: Булево значение, указывающее, является ли изображение чёрно-белым.\n",
" :param title: Заголовок для графика.\n",
" \"\"\"\n",
" plt.figure(figsize=(2, 2)) # Устанавливаем размер фигуры\n",
"\n",
" if is_grayscale:\n",
" # Для чёрно-белых изображений используем cmap='gray'\n",
" plt.imshow(symbol_array, cmap='gray', interpolation='nearest')\n",
" else:\n",
" # Для цветных изображений предполагаем, что массив имеет форму (H, W, 3)\n",
" plt.imshow(symbol_array, interpolation='nearest')\n",
"\n",
" plt.title(title)\n",
" plt.axis('off') # Отключаем оси\n",
" plt.show()\n",
"\n",
"# Пример использования\n",
"\n",
"# Предполагается, что transformed_symbols уже определён и содержит хотя бы один символ\n",
"# Например:\n",
"# transformed_symbols = [numpy_array1, numpy_array2, ...]\n",
"\n",
"# Проверяем, что transformed_symbols не пуст\n",
"try:\n",
" # Извлекаем первый символ\n",
" first_symbol = alls[0][1]\n",
" \n",
" # Определяем, является ли изображение чёрно-белым или цветным\n",
" if len(first_symbol.shape) == 2:\n",
" is_grayscale = True\n",
" elif len(first_symbol.shape) == 3 and first_symbol.shape[2] == 3:\n",
" is_grayscale = False\n",
" else:\n",
" raise ValueError(\"Неподдерживаемый формат изображения.\")\n",
" \n",
" # Отображаем первый символ\n",
" display_symbol(first_symbol, is_grayscale=is_grayscale, title=\"Первый извлечённый символ\")\n",
" \n",
"except IndexError:\n",
" print(\"Список transformed_symbols пуст. Убедитесь, что символы были успешно извлечены.\")\n",
"except Exception as e:\n",
" print(f\"Произошла ошибка при отображении символа: {e}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'LETTERS' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[32], line 7\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# Инициализация модели\u001b[39;00m\n\u001b[1;32m----> 7\u001b[0m model_letters \u001b[38;5;241m=\u001b[39m SimpleCNN(\u001b[38;5;28mlen\u001b[39m(\u001b[43mLETTERS\u001b[49m))\u001b[38;5;241m.\u001b[39mto(DEVICE) \u001b[38;5;66;03m# Инициализация модели для букв\u001b[39;00m\n\u001b[0;32m 8\u001b[0m model_letters\u001b[38;5;241m.\u001b[39mload_state_dict(torch\u001b[38;5;241m.\u001b[39mload(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbest_model_letters.pth\u001b[39m\u001b[38;5;124m\"\u001b[39m, map_location\u001b[38;5;241m=\u001b[39mDEVICE)) \u001b[38;5;66;03m# Загрузка весов\u001b[39;00m\n\u001b[0;32m 9\u001b[0m model_letters\u001b[38;5;241m.\u001b[39meval()\n",
"\u001b[1;31mNameError\u001b[0m: name 'LETTERS' is not defined"
]
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"import torch\n",
"\n",
"# Инициализация модели\n",
"model_letters = SimpleCNN(len(LETTERS)).to(DEVICE) # Инициализация модели для букв\n",
"model_letters.load_state_dict(torch.load(\"best_model_letters.pth\", map_location=DEVICE)) # Загрузка весов\n",
"model_letters.eval()\n",
"\n",
"model_digits = SimpleCNN(len(DIGITS)).to(DEVICE) # Инициализация модели для цифр\n",
"model_digits.load_state_dict(torch.load(\"best_model_digits.pth\", map_location=DEVICE)) # Загрузка весов\n",
"model_digits.eval()\n",
"\n",
"def process_and_recognize_plate_with_processing(plate, model_digits, model_letters, transform, device=\"cpu\", padding=5):\n",
" \"\"\"\n",
" Обрабатывает пластину, распознаёт символы с дополнительной обработкой и возвращает текстовую строку.\n",
" \n",
" :param plate: Изображение пластины.\n",
" :param model_digits: Модель для распознавания цифр.\n",
" :param model_letters: Модель для распознавания букв.\n",
" :param transform: Преобразование изображения для модели.\n",
" :param device: Устройство для вычислений (\"cpu\" или \"cuda\").\n",
" :param padding: Отступ для символов.\n",
" :return: Обработанная пластина, список символов, распознанная строка.\n",
" \"\"\"\n",
" # Обработка пластины\n",
" processed_plate, characters = process_plate(plate)\n",
" \n",
" # Распознавание символов\n",
" recognized_text = \"\"\n",
" processed_characters = []\n",
" for i, char in enumerate(characters):\n",
" # Выбор модели для символа\n",
" if i in {0, 4, 5}: # 1-й, 5-й и 6-й символы (нумерация с 0)\n",
" model = model_letters\n",
" else:\n",
" model = model_digits\n",
" \n",
" # Распознавание символа\n",
" recognized_char, processed_char = predict_character_with_processing(\n",
" model, char, transform, device, padding=padding\n",
" )\n",
" recognized_text += recognized_char\n",
" processed_characters.append(processed_char)\n",
" \n",
" return processed_plate, processed_characters, recognized_text\n",
"\n",
"# Пример использования с массивом processed_plates\n",
"all_recognized_texts = []\n",
"for plate_index, plate in enumerate(processed_plates):\n",
" # Обработка пластины и распознавание символов\n",
" processed_plate, processed_characters, recognized_text = process_and_recognize_plate_with_processing(\n",
" plate, model_digits, model_letters, transform, device=DEVICE, padding=5\n",
" )\n",
"\n",
" # Сохранение распознанного текста\n",
" all_recognized_texts.append(recognized_text)\n",
"\n",
" # Визуализация результатов\n",
" plt.figure(figsize=(15, 5))\n",
"\n",
" # Исходная пластина\n",
" plt.subplot(1, 3, 1)\n",
" plt.title(\"Исходная картинка\")\n",
" plt.imshow(cv2.cvtColor(plate, cv2.COLOR_BGR2RGB)) # Преобразуем для корректного отображения\n",
" plt.axis('off')\n",
"\n",
" # Обработанная пластина\n",
" plt.subplot(1, 3, 2)\n",
" plt.title(\"Обработанная картинка\")\n",
" plt.imshow(processed_plate, cmap='gray')\n",
" plt.axis('off')\n",
"\n",
" # Выделенные символы и итоговый текст\n",
" plt.subplot(1, 3, 3)\n",
" plt.title(\"Выделенные символы\")\n",
" if processed_characters:\n",
" symbols_grid = np.hstack(processed_characters)\n",
" plt.imshow(symbols_grid, cmap='gray')\n",
" plt.xlabel(f\"Распознанный текст: {recognized_text}\")\n",
" else:\n",
" plt.text(0.5, 0.5, 'Нет символов', ha='center', va='center')\n",
" plt.axis('off')\n",
"\n",
" plt.suptitle(f\"Результат обработки пластинки {plate_index + 1} - {recognized_text}\")\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA6MUlEQVR4nO3deXRU9d3H8c9kMjPZw5YQUigUQgWVTRaLFgENm4hiXZBWJULBDe2iiFQroq1g5XmqjygqW1BcWbSCFBeEQikVXCIqFBER2YKEkIQkZJnMff7wZEpMgN+FTCa5eb/OmXPIzGd+853fXCbf+eXOvS7LsiwBAAAAcJyIcBcAAAAAIDRo9gEAAACHotkHAAAAHIpmHwAAAHAomn0AAADAoWj2AQAAAIei2QcAAAAcimYfAAAAcCiafQAAAMChaPYBAAAAh6LZb4R27typm2++We3bt1dUVJQSEhJ04YUX6oknntCxY8fCXV5YbNq0Sbfddpt69uwpj8cjl8sV7pIANGIul8vosnbt2jqta//+/br++ut11llnKT4+Xk2aNFGfPn20cOFCWZZVp7UAMBMZ7gJQt9566y1dc8018vl8uvHGG3XuueeqrKxM//znPzVp0iR98cUXeu6558JdZp1buXKl5s6dq65du6p9+/b68ssvw10SgEbshRdeqPLz888/r3fffbfa9Z07d67LspSTk6O9e/fq6quv1o9//GOVl5fr3XffVUZGhrZv365HHnmkTusBcGoui4/ijcauXbvUtWtXtW7dWu+//75atWpV5favvvpKb731ln7zm9+EqcLwOXjwoBISEhQdHa2JEyfqqaeeYpUKQL1R39+XRowYoTVr1ig/P19utzvc5QA4DrvxNCJ/+ctfVFhYqHnz5lVr9CUpLS2tSqPvcrn04IMPVsk89thjcrlcGjBgQPC6tWvXBv+knJWVVSW/b98+ud1uuVwuLVmyJHh9RkZGlT9FN23aVAMGDND69eur1fX000/rnHPOkc/nU2pqqm6//Xbl5eVVy33zzTcn/FP3qbRs2VLR0dGnzAFAffTdd99p3LhxatmypaKiotStWzctXLiwSuZk75E/fF+3q127diouLlZZWdlJcw8++OBJa8jMzAxmMzIyFBcXp6+//lpDhgxRbGysUlNT9dBDD1X70BMIBPT444/rnHPOUVRUlFq2bKmbb75ZR44cqVbDyebhm2++qZLNy8vT7373O7Vr104+n0+tW7fWjTfeqJycHEn//f13/O5U+/fvV7t27dSrVy8VFhZKksrKyvTAAw+oZ8+eSkxMVGxsrPr166c1a9ZUebzt27fr4osvVkpKinw+n9q0aaNbbrlFubm5wYzpWJXPc+bMmdXm4Nxzz63x9/gPdwsbPnx4jb3AmjVr1K9fPzVt2rTK/E2cOLHaYyH82I2nEVm+fLnat2+vCy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cXG2x7Lzeh//PCqdf/751rBhw6r1As8++6wlydq4cWOVMSVZt99++0mfC8KD3XgaiYKCAklSfHz8ad1/3759evLJJ/XHP/6xymrM8UaMGCGXy6U333xTkrR+/Xrt3btXo0aNqjEfCASUk5OjnJwcZWVl6fnnn1erVq2CXzh77733VFZWpt/+9reKiPjvpjp+/HglJCTorbfeqjJe5Z+PfT7faT1HAGiIVq5cqZSUFI0ePTp4ncfj0Z133hlcea9to0eP1rvvvquXXnpJv/zlLyUpZEdzO37XkMpdRcrKyvTee+9JkhYvXqzExEQNGjQo+DslJydHPXv2VFxcXLXdWyr/0hwVFXXSx126dKm6deumK6+8stptP9w9NBAI6MYbb9S///1vrVy5Uh06dKhyu9vtltfrDWZzc3Pl9/vVq1cvffzxx9XGz8/P18GDB7V69Wq99dZbuuiii057rOLi4irzkpOTo4qKipM+92XLlmnz5s2aMWNGtduOHj0qSWrevPlJx0D9QbPfSCQkJEj6739Su6ZOnarU1FTdfPPNJ8x4PB5df/31mj9/viRp/vz5uuqqq4KP/UN79uxRUlKSkpKS1KNHD+3cuVNLly4NfpjYvXu3JOmss86qcj+v16v27dsHb69UuR//iT6MAIAT7d69Wx07dqyyKCL990g9P3yvNJGdnV3l8sNGvm3btkpPT9fo0aP14osvqn379kpPT6/1hj8iIkLt27evct1Pf/pTSQruX79jxw7l5+crOTk5+Dul8lJYWKjvvvuuyv0r97dPTEw86WPv3LlT5557rlGd9913n1577TWVlpaquLi4xszChQvVtWtXRUVFqXnz5kpKStJbb72l/Pz8atkhQ4YoJSVF6enp6ty5s1599dXTHmvq1KnV5uU///nPCZ9LRUWF/vCHP+hXv/qVunbtWu32vn37SpImTZqkbdu2BT9AoP5in/1GIiEhQampqfr8889t33fbtm3KzMzUokWLatzv8Xhjx45Vjx49tH37di1evDi4yl+Tli1batGiRZK+X8WYP3++hg4dqn/+85/q0qWL7Tqzs7MlSSkpKbbvCwD4rx8exGHBggXKyMg4Yf7qq6/WnDlztG7dOg0ZMiTE1VUVCASUnJysF198scbbK/cpr1T5IaFdu3a1VsMHH3ygzMxMzZo1SxMmTFBWVlaVvzIvWrRIGRkZGjlypCZNmqTk5GS53W5Nnz5dO3furDbek08+qZycHG3dulXTp0/XLbfcEvx9aXesCRMm6Jprrqly3fjx40/4XObNm6dvvvlGb7/9do23X3DBBXrsscc0bdo0nX322Ubzg/Ci2W9ELrvsMj333HPauHFj8JO5iSlTpqh79+4n3B3neF26dFGPHj107bXXKikpSQMHDjzhn5CjoqKUnp4e/Pnyyy9Xs2bNNGvWLD377LNq27atpO+PTnD8yk5ZWZl27dpV5b6StHXrVrlcrmp/CQAAJ2vbtq22bNmiQCBQZXW/cvW28r3
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA7GklEQVR4nO3deXQUZb7/8U9n6ewLCQlJIIIsI8gmAjrIoKBREHCbURxUBEFABZ3rKCJuARfA5dzBKwqibCpuLHoVGBRZLsqPUdRBR0FkiWNAUUMge9LpdP3+8KSHmABPAZ1OKu/XOX0OVH/66W893Z3+dnV1lcuyLEsAAAAAHCck2AUAAAAACAyafQAAAMChaPYBAAAAh6LZBwAAAByKZh8AAABwKJp9AAAAwKFo9gEAAACHotkHAAAAHIpmHwAAAHAomn0AAADAoWj2m6A9e/Zo/Pjxatu2rSIjIxUfH6++ffvq6aefVllZWbDLq3c+n0+LFi3S5ZdfrszMTMXExKhLly569NFHVV5eHuzyADRBLpfL6LJx48Z6reuHH37QDTfcoDPOOENxcXFKTEzUOeeco8WLF8uyrHqtBYCZsGAXgPq1atUqXXPNNYqIiNCNN96oLl26yOPx6KOPPtKkSZP09ddfa968ecEus16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/58fffdd3rvvffqvP68887Tk08+qWnTpunMM880mh8EF81+EzJ06FDNmzdPW7Zs8X8yNzFlyhS
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvoAAAGGCAYAAAAKFyXyAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA6n0lEQVR4nO3deXhU5f3+8XsymZksk4VAQhJBkEVRVgW0iChoZHVtXYoWiQu4gLZWEXELaCtu/VYrFAXZVJTKolWkKCJUpKgUG9SCyBJlEVCW7PvM+f3hj6kxAZ6DTCacvF/XNdeVnLnnmc95ZpL5zJkz57gsy7IEAAAAwFGiIl0AAAAAgGOPRh8AAABwIBp9AAAAwIFo9AEAAAAHotEHAAAAHIhGHwAAAHAgGn0AAADAgWj0AQAAAAei0QcAAAAciEYfAAAAcCAa/UZoy5Ytuvnmm9WmTRvFxMQoMTFRvXv31jPPPKOysrJIlxcR06ZN03nnnafmzZvL5/PppJNO0vXXX6+vv/460qUBaGRcLpfRZcWKFfVa17fffqvf/OY3OuWUU5SQkKDk5GSdeeaZmj17tizLqtdaAJiJjnQBqF9vv/22rrzySvl8Pl133XXq1KmTKisr9eGHH2rMmDH673//q6lTp0a6zHr3n//8RyeddJIuueQSNWnSRHl5eZo2bZoWLVqkdevWKTMzM9IlAmgkXnrppRq/v/jii1q6dGmt5aeeemp9lqW9e/dqx44duuKKK3TiiSeqqqpKS5cuVXZ2tjZu3KhHH320XusBcGQui7fhjUZeXp66dOmiFi1a6P3331dGRkaN6zdv3qy3335bv/3tbyNUYcOydu1a9ejRQxMnTtS9994b6XIANFKjR4/W5MmTG+xW84svvljLly9XQUGB3G53pMsB8CPsutOIPPHEEyouLtb06dNrNfmS1K5duxpNvsvl0vjx42tknnzySblcLvXt2ze0bMWKFaGPknNzc2vkd+7cKbfbLZfLpfnz54eWZ2dn1/gIukmTJurbt69WrlxZq66//vWv6tixo3w+nzIzMzVq1Cjl5+fXyn399deH/Ij7aLRu3VqS6rwvAGgovvvuO914441q3ry5YmJi1LVrV82ePbtG5nD/H3/6P92u1q1bq7S0VJWVlYfNjR8//rA1zJo1K5TNzs6W3+/X1q1bNWDAAMXHxyszM1MPP/xwrTc8wWBQTz/9tDp27KiYmBg1b95cN998sw4cOFCrhsPNw0931czPz9edd96p1q1by+fzqUWLFrruuuu0d+9eSf977fvxLlTffvutWrdurR49eqi4uFiSVFlZqYceekjdu3dXUlKS4uPj1adPHy1fvrzG/W3cuFHnn3++0tPT5fP51LJlS91yyy3av39/KGM61sH1fOqpp2rNQadOnep8Df/prmBDhgypsw9Yvny5+vTpoyZNmtSYv9GjR9e6L0Qeu+40Im+99ZbatGmjs88++6hun5+fr4kTJx7y+piYGM2cOVPPPPNMaNns2bPl9XpVXl5eK9+sWTP9+c9/liTt2LFDzzzzjAYPHqzt27crOTlZ0g8vDBMmTFBWVpZuvfVWbdy4UVOmTNGaNWu0atUqeTyeWuOOHDlSffr0kSQtXLhQr7/+uvE67tu3T4FAQNu2bdPDDz8sSbrggguMbw8A9amsrEx9+/bV5s2bNXr0aJ100kmaN2+esrOzlZ+fX+sT2qFDh2rw4ME1lo0bN872fZaUlKi4uFj//Oc/NXPmTPXq1UuxsbFGt58yZYr8fn/o97y8PD300EO1coFAQAMHDtQvfvELPfHEE1qyZIlycnJUXV0d+v8sSTfffLNmzZql66+/XnfccYfy8vI0adIk/ec//znk68SP52Hx4sV69dVXa1xfXFysPn36aMOGDbrhhht0xhlnaO/evXrzzTe1Y8cONWvWrNaYBQUFGjRokDwejxYvXhxax8LCQr3wwgsaOnSoRowYoaKiIk2fPl0DBgzQJ598om7dukmSSkpK1KJFC1188cVKTEzUF198ocmTJ2vnzp166623bI31c33wwQdavHhxreV5eXkaMmSIMjIy9NBDDyk1NVWSNGzYsGNyvwgDC41CQUGBJcm69NJLjW8jycrJyQn9fs8991hpaWlW9+7drfPOOy+0fPny5ZYka+jQoVbTpk2tioqK0HXt27e3rrnmGkuSNW/evNDy4cOHW61atapxf1OnTrUkWZ988ollWZb13XffWV6v1+rfv78VCARCuUmTJlmSrBkzZtS4/aZNmyxJ1uzZs0PLcnJyLDtPc5/PZ0myJFlNmza1/vKXvxjfFgDCYdSoUYf8P/b0009bkqyXX345tKyystLq1auX5ff7rcLCQsuyLCsvL8+SZD355JO1xujYsWON/+lHMnHixND/SUnWBRdcYG3btu2Itzv4//j777+vsXzNmjWWJGvmzJmhZcOHD7ckWbfffntoWTAYtIYMGWJ5vd7QGCtXrrQkWXPmzKkx5pIlS+pc/tVXX1mSrKeeeiq07Mknn7QkWXl5eaFlDz30kCXJWrhwYa31CAaDlmX977Vv+fLlVnl5udW3b18rLS3N2rx5c418dXV1jddFy7KsAwcOWM2bN7duuOGGQ02XZVmWddttt1l+v9/2WHYe7x+vx0FnnXWWNWjQoFp9wPPPP29JslavXl1jTEnWqFGjDrsuiAx23WkkCgsLJUkJCQlHdfudO3fq2Wef1YMPPlhjS8yPXXzxxXK5XHrzzTclSStXrtSOHTt09dVX15kPBoPau3ev9u7dq9zcXL344ovKyMgIfcHsvffeU2VlpX73u98pKup/T9URI0YoMTFRb7/9do3xDn5s7PP5jmodJekf//iHFi9erD/96U868cQTVVJSctRjAUC4LV68WOnp6Ro6dGhomcfj0R133BHa4n6sDR06VEuXLtUrr7yia665RpLCdsS2H+8OcnD3kMrKSr333nuSpHnz5ikpKUkXXnhh6PVk79696t69u/x+f61dWg5+uhwTE3PY+12wYIG6du2qyy+/vNZ1P90dNBgM6rrrrtNHH32kxYsXq23btjWud7vd8nq9oez+/ftVXV2tHj166NNPP601fkFBgfbs2aNly5bp7bff1rnnnnvUY5WWltaYl7179yoQCBx23RcuXKg1a9boscceq3VdUVGRJKlp06aHHQMNB7vuNBKJiYmS/vdHaldOTo4yMzN1880319jX/sc8Ho9+85vfaMaMGbriiis0Y8YM/epXvwrd909t37499LGfJGVkZGjBggWhNxLffPONJOmUU06pcTuv16s2bdqErj/o4L70h3ojYqJfv36SpEGDBunSSy9Vp06d5Pf72fcQQIP0zTffqH379jU2hkj/OyLPT/9Pmti9e3eN35OSkmrsltOqVSu1atVK0g9N/8iRI5WVlaWNGzca775jIioqSm3atKmx7OSTT5ak0P70mzZtUkFBgdLS0uoc47vvvqvx+8H965OSkg5731u2bNGvfvUrozrvv/9+ffTRR3K5XCotLa0zM3v2bP3pT3/Sl19+qaqqqtDyk046qVZ2wIAB+vjjjyVJAwcO1N/+9rejHisnJ0c5OTm1ljdv3rzOOgOBgO677z5de+216tKlS63re/XqJUkaM2aMJk6cWOM1HA0TjX4jkZiYqMzMTH3xxRe2b7thwwbNmjVLL7/8cp37Ov7YDTfcoNNPP10bN27UvHnzQlv369K8eXO9/PLLkn7YgjFjxgwNHDhQH374oTp37my7zoMvTunp6bZvW5e2bdvq9NNP15w5c2j0ATQaPz1Yw8yZM5WdnX3I/BVXXKFp06bpgw8+0IABA8JcXU3BYFBpaWmaM2dOndf/tBE9+Abh4MEWjoWPP/5Ys2bN0qRJkzRy5Ejl5ubW+GT55ZdfVnZ2ti677DKNGTNGaWlpcrvdmjhxorZs2VJrvGeffVZ79+7V+vXrNXHiRN1yyy2h10q7Y40cOVJXXnlljWUjRow45LpMnz5dX3/9td555506rz/77LP15JNPasKECTrttNOM5geRRaPfiFx00UWaOnWqVq9eHXpXbmLcuHH
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvoAAAGGCAYAAAAKFyXyAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA6kUlEQVR4nO3deXhU5d3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+979Pui7l5eV66KGHdPvtt6tTp05Vru/Ro4ckady4cdqyZYv/zQPqL/bRbyDi4uKUnp6ub775xvZtt2zZovnz5+u1116rdl/HEw0dOlQXXHCBtm7dqsWLF/u37lcnNTVVr732mqSftmDMnTtXffv21SeffKKOHTvarvPAgQOSpCZNmti63XPPPafS0lLdeuut/hePPXv2SJKOHDminTt3Kj093b8VBQCc7OcHa5g3b54yMzNPmr/pppv00ksv6eOPP1afPn0CXF1lPp9PKSkpWrhwYbXXV+xDXqHif3yLFi1qrYbPPvtM8+fP1/Tp0zVixAhlZ2dX+mT5tddeU2Zmpm644QaNGzdOKSkpCg0N1eTJk7Vjx44q402bNk25ubnavHmzJk+erLvvvtv/Wml3rBEjRujmm2+utGz48OEnXZc5c+Zo586deu+996q9/pJLLtGzzz6rSZMm6fzzzzeaHwQXjX4Dcu2112r27NnasGGD/125iQkTJqhLly4n3QXnRB07dtQFF1ygW265RcnJybriiit
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA5z0lEQVR4nO3deXhU5d3/8c9kJjMJ2dhCCDVCISiIbIL4ICKgURDcWlesSCoitYK/WgVEq4haweV5xAcoCrJVrMjWVoHiglCRUqEqbiACYgsoVkSyAUlm5v794ZMpYwLcJzCZ5OT9uq65ruTMd+75njNnZj5z5sw5HmOMEQAAAADXSYh3AwAAAABig7APAAAAuBRhHwAAAHApwj4AAADgUoR9AAAAwKUI+wAAAIBLEfYBAAAAlyLsAwAAAC5F2AcAAABcirAPAAAAuBRhvx7asWOHRowYodatWyspKUnp6enq1auXnn76aR06dCje7cVFfn6+PB5PpUu7du3i3RqAeqaq16KqLmvWrKnRvr788kvdeOONOv3005WWlqaGDRuqR48emjdvnowxNdoLAHu+eDeAmrV8+XJdc801CgQCuummm3TmmWeqrKxMb7/9tkaPHq1PPvlEM2bMiHebcREIBPTcc89FTcvIyIhTNwDqq+effz7q/9///vd6/fXXK01v3759Tbalffv2affu3br66qt16qmnqry8XK+//rry8/O1detWPfroozXaDwA7HsPH8Xpj586d6tSpk0455RS9+eabys7Ojrp++/btWr58uf7f//t/ceowfvLz87V48WIVFxfHuxUAiDJy5EhNmzat1m49v+yyy7R69WoVFBTI6/XGux0AP8BuPPXI448/ruLiYs2aNatS0Jek3NzcqKDv8Xj04IMPRtU88cQT8ng86tu3b2TamjVrIl8rb9q0Kap+z5498nq98ng8Wrx4cWT6D3ebadSokfr27au1a9dW6ut3v/udOnTooEAgoBYtWuj222/XgQMHKtV98cUXR/2621YoFFJhYaF1PQDE27///W8NGzZMWVlZSkpKUufOnTVv3ryommO9Pv7wNd2pVq1a6eDBgyorKztm3YMPPnjMHubOnRupzc/PV2pqqj7//HP1799fKSkpatGihR566KFKH3rC4bAmT56sDh06KCkpSVlZWRoxYoS+++67Sj0cazl88cUXUbUHDhzQnXfeqVatWikQCOiUU07RTTfdpH379kn6z3vfkbtTffnll2rVqpW6d+8e2XhUVlamBx54QN26dVNGRoZSUlLUu3dvrV69Our+tm7dqgsuuEDNmzdXIBBQTk6OfvGLX2j//v2RGtuxKubzySefrLQMzjzzzCrfw3+4W9igQYOqzAGrV69W79691ahRo6jlN3LkyEr3hdqB3XjqkVdeeUWtW7fWueeeW63bHzhwQBMnTjzq9UlJSZozZ46efvrpyLR58+bJ7/fr8OHDleqbNm2qp556SpK0e/duPf300xo4cKB27dqlhg0bSvr+zWHChAnKy8vTbbfdpq1bt2r69OnauHGj1q1bp8TExErj3nrrrerdu7ckaenSpfrjH/9oNX8HDx5Uenq6Dh48qEaNGmnw4MF67LHHlJqaanV7AKhphw4dUt++fbV9+3aNHDlSP/7xj7Vo0SLl5+frwIE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J07Ujh07Ko03ZcoU7du3T5s3b9bEiRP1i1/8IvJe6XSsW2+9Vddcc03UtOHDhx91XmbNmqUvvvhCr776apXXn3vuuXriiSc0YcIEnXHGGVbLB/FH2K9HLr30Us2YMUPr16+PfDq3MW7cOHXp0uWou+McqWPHjuratauuvfZaZWZmql+/fkf
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA7DklEQVR4nO3de3RTZdr+8StNk7b0BC1taaWCUBVEOYjiiw6CWAHFA46K4qgUFE+gr6MgMv60oI7g4R1lQFFQDgLKcBqPDKgIozKMMiqeYBAQHEBREWhpgbZJnt8frmaoRXh2aUi7+/2slbXozpUn97OTkDs7O3t7jDFGAAAAAFwnJtoFAAAAAIgMmn0AAADApWj2AQAAAJei2QcAAABcimYfAAAAcCmafQAAAMClaPYBAAAAl6LZBwAAAFyKZh8AAABwKZp9AAAAwKVo9hugjRs36qabblKrVq0UHx+vlJQUnXXWWRo/frz27dsX7fKiJhQKadKkSerYsaMSEhKUnp6unj176tNPP412aQAaCI/HY3VZvnz5Ua3r22+/1TXXXKMTTzxRycnJaty4sbp06aIZM2bIGHNUawHgTGy0C8DR9cYbb+iKK65QXFycrrvuOp188skqLy/X+++/rxEjRujLL7/U5MmTo11mVAwePFizZ8/Wddddp2HDhqm0tFSffPKJfvjhh2iXBqCBmDlzZpW/X3jhBb311lvVlrdt2/ZolqUdO3Zo69atuvzyy3XssceqoqJCb731lgoKCrRu3To9/PDDR7UeAPY8ho/kDcamTZvUvn17NW/eXO+8846ys7OrXL9hwwa98cYb+t///d8oVRg9c+fO1ZVXXqmFCxfq0ksvjXY5ACBJGjZsmJ566qk6u/X8oosu0rJly1RUVCSv1xvtcgAcBLvxNCCPPvqoSkpK9Pzzz1dr9CUpLy+vSqPv8Xg0evToKpnHHntMHo9HPXr0CC9bvnx5+Kvl1atXV8lv27ZNXq9XHo9H8+fPDy8vKCio8pV0kyZN1KNHD7333nvV6nr66afVrl07xcXFKScnR0OHDtXu3bur5TZv3vyrX3kfzp/+9Cd16dJFl156qUKhkEpLSw97GwCIth9++EHXX3+9srKyFB8frw4dOmjGjBlVMof6v/GX/5871bJlS+3du1fl5eWHzI0ePfqQNUyfPj2cLSgoUFJSkr7++mv17t1biYmJysnJ0QMPPFDtQ08oFNKTTz6pdu3aKT4+XllZWbrpppu0a9euajUcaj1s3ry5Snb37t36/e9/r5YtWyouLk7NmzfXddddpx07dkj67/vegbtTffvtt2rZsqVOO+00lZSUSJLKy8t1//33q3PnzkpNTVViYqK6deumZcuWVbm/devWqWfPnmrWrJni4uKUm5urm2++WTt37gxnbMeqnOfjjz9ebR2cfPLJB33//uVuYX379j1oD7Bs2TJ169ZNTZo0qbL+hg0bVu2+UHewG08D8tprr6lVq1Y688wza3T73bt3a+zYsb96fXx8vKZNm6bx48eHl82YMUN+v1/79++vlm/atKmeeOIJSdLWrVs1fvx4XXDBBdqyZYsaN24s6ec3iDFjxig/P1+33HKL1q1bp0mTJmnVqlVasWKFfD5ftXFvvPFGdevWTZK0cOFC/fWvfz3kvIqLi/Xhhx/q1ltv1R/+8AdNmDBBJSUlOu644zRu3Dj179//sOsGAI62ffv2qUePHtqwYYOGDRum4447TvPmzVNBQYF2795d7VvaAQMG6IILLqiybNSoUY7vs7S0VCUlJfr73/+uadOmqWvXrkpISLC6/aRJk5SUlBT+e9OmTbr//vur5YLBoPr06aP/+Z//0aOPPqrFixersLBQgUBADzzwQDh30003afr06Ro0aJBuv/12bdq0SRMnTtQnn3zyq+8RB66HRYsW6aWXXqpyfUlJibp166a1a9dq8ODBOvXUU7Vjxw69+uqr2rp1q5o2bVptzKKiIp1//vny+XxatGhReI7FxcV67rnnNGDAAA0ZMkR79uzR888/r969e+vDDz9Ux44dJUmlpaVq3ry5LrroIqWkpOiLL77QU089pW3btum1115zNNaRevfdd7Vo0aJqyzdt2qS+ffsqOztb999/vzIyMiRJ1157ba3cLyLIoEEoKioykswll1xifRtJprCwMPz33XffbTIzM03nzp1N9+7dw8uXLVtmJJkBAwaY9PR0U1ZWFr7u+OOPN1dffbWRZObNmxdePnDgQNOiRYsq9zd58mQjyXz44YfGGGN++OEH4/f7Ta9evUwwGAznJk6caCSZqVOnVrn9+vXrjSQzY8aM8LLCwkJzuKf5xx9/bCSZ9PR0k5WVZZ5++mkze/Zs06VLF+PxeMzf/va3w64rAIiEoUOH/ur/YU8++aSRZGbNmhVeVl5ebrp27WqSkpJMcXGxMcaYTZs2GUnmscceqzZGu3btqvx/fjhjx441ksKXc8891/znP/857O0q/y/+8ccfqyxftWqVkWSmTZsWXjZw4EAjydx2223hZaFQyPTt29f4/f7wGO+9956RZGbPnl1lzMWLFx90+VdffWUkmccffzy87LHHHjOSzKZNm8LL7r//fiPJLFy4sNo8QqGQMea/73vLli0z+/fvNz169DCZmZlmw4YNVfKBQKDKe6IxxuzatctkZWWZwYMH/9rqMsYYc+utt5qkpCTHYzl5vA+cR6UzzjjDnH/++dV6gGeffdZIMitXrqwypiQzdOjQQ84F0cVuPA1EcXGxJCk5OblGt9+2bZsmTJig++67r8pWmQNddNFF8ng8evXVVyVJ7733nrZu3aorr7zyoPlQKKQdO3Zox44dWr16tV544QVlZ2eHf3j29ttvq7y8XHfccYdiYv77VB0yZIhSUlL0xhtvVBmv8mvkuLg4R3Or/Lr1p59+0iuvvKJbbrlFV199tZYuXar09HQ99NBDjsYDgKNh0aJFatasmQYMGBBe5vP5dPvtt4e3vNe2AQMG6K233tKLL76oq6++WpIidhS3A3cNqdxVpLy8XG+//bYkad68eUpNTdV5550Xfi/ZsWOHOnfurKSkpGq7t1R+wxwfH3/I+12wYIE6dOhw0N9v/XK30FAopOuuu07//Oc/tWjRIrVu3brK9V6vV36/P5zduXOnAoGATjvtNH388cfVxi8qKtL333+vpUuX6o033tDZZ59d47H27t1bZb3s2LFDwWDwkHNfuHChVq1apXHjxlW7bs+ePZKk9PT0Q46Buodmv4FISUmR9N8Xq1OFhYXKycnRTTfd9KsZn8+na665RlOnTpUkTZ06VZdddln4vn9py5YtysjIUEZGhjp16qSNGzdqwYIF4Q8T33zzjSTpxBNPrHI7v9+vVq1aha+vVLkf/699GPk1lV8/H3fccTrjjDPCy5OSknTRRRfpww8/VCAQcDQmAETaN998o+OPP77KxhDpv0fq+eX/kTa2b99e5fLLRr5FixbKz8/XgAEDNHv2bLVq1Ur5+fm13vDHxMSoVatWVZadcMIJkhTev379+vUqKipSZmZm+L2k8lJSUlLtSGqV+9unpqYe8r43btyok08+2arOe++9V3PnzlVZWZn27t170MyMGTPUvn17xcfHKz09XRkZGXrjjTdUVFRULdu7d281a9ZM+fn5atu2rf7yl7/UeKzCwsJq6+Xf//73r84lGAzqD3/4g373u9+pffv21a7v2rWrJGnEiBFau3Zt+AME6j722W8gUlJSlJOToy+++MLxbdeuXavp06dr1qxZB93/8UCDBw9Wp06dtG7dOs2bNy+8lf9gsrKyNGvWLEk/b82YOnWq+vTpo/fff1+nnHKK4zq3b98uSWrWrJmj2+Xk5ITr+aXMzExVVFSotLT0sG8QAFDf/fLgDdOmTVNBQcGv5i+//HJNmTJF7777rnr37h3h6qoKhULKzMzU7NmzD3p95T7llSo/JLRs2bLWavjggw80ffp0TZw4UTfeeKNWr15d5dvlWbNmqaCgQP369dOIESOUmZkpr9ersWPHauPGjdXGmzBhgnbs2KE1a9Zo7Nixuvnmm8Pvk07HuvH
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjku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6M6H3jgAb366qsqKytTSUlJtZm5c+eqW7duiomJUfPmzZWSkqJly5YpPz+/SnbAgAFKS0tTVlaWOnXqpL/+9a8nPFZubm6Vefnyyy+PuizBYFD333+/brjhBnXr1q3K7b1795Yk5eTkaMuWLeEPEKi/2Ge/kUhKSlJGRoY+//xz2/fdsmWL5syZo/nz51e73+ORRo4cqTPOOENbt27VokWLwlv5q9OyZUvNnz9f0k9bMWbNmqWBAwfqvffeU9euXW3XuW/fPklSWlqa7fseacGCBUpOTtall156UuMAQEP184M4zJ49W9nZ2UfNX3PNNXrxxRf17rvvasCAARGurrJQKKTU1FQtWLCg2tsr9imvUPEhoU2bNjVWwwcffKA5c+Zo6tSpGj16tPLy8ip9yzx//nxlZ2fryiuvVE5OjlJTU+V2uzVp0iTt2LGjynjPPvus9u/fr82bN2vSpEm67bbbwu+XdscaPXq0rr322krXjRo16qjLMnPmTH3zzTdauXJltbefe+65euqppzRx4kSdfvrpRvODukWz34hceumlmjFjhjZs2BD+ZG5i/Pjx6tGjx1F3xzlS165ddcYZZ2jo0KFKSUnRhRdeeNSvkGNiYpSVlRX+/+WXX65mzZpp6tS
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvoAAAGGCAYAAAAKFyXyAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA6FUlEQVR4nO3deZxT1f3/8XdmJsksmYVlBmaUgiyKIouiUGoRlJFFtFAXlLbKCAK1ot+2gEr96YC2BZfvt1JQFGUTLMpW60JxYalIEamKFKEIdFRAURGZhdmT8/vDBynpsJwLk8lw5/V8PPKAuXnn5NyTm+STk5t7PcYYIwAAAACuEhfrDgAAAACofRT6AAAAgAtR6AMAAAAuRKEPAAAAuBCFPgAAAOBCFPoAAACAC1HoAwAAAC5EoQ8AAAC4EIU+AAAA4EIU+gAAAIALUeg3QLt27dLo0aPVunVrJSYmKi0tTZdccommTp2qsrKyWHcvZhYtWqTvf//7ysjIUJMmTdSrVy+9+uqrse4WgAbG4/FYXdasWVOn/fr888/1s5/9TOecc45SU1OVkZGhbt26ad68eTLG1GlfANhJiHUHULdeffVVXX/99fL7/br55pt1/vnnq7KyUm+//bbGjx+vjz76SDNnzox1N+vctGnTdOedd2rgwIGaMmWKysvLNXfuXF111VVaunSprrnmmlh3EUADMX/+/Ii/n332Wb3xxhs1lp977rl12S3t379fe/bs0XXXXafvfe97qqqq0htvvKG8vDxt375dv//97+u0PwBOzGP4GN5gFBQUqFOnTjrzzDO1atUqZWdnR1y/c+dOvfrqq/qf//mfGPUwds4++2xlZGRow4YN8ng8kqSioiKdccYZuvzyy/WXv/wlxj0E0FCNGTNGjz/+eL2dNb/66qu1evVqFRYWKj4+PtbdAXAEdt1pQB5++GGVlJRo1qxZNYp8SWrbtm1Eke/xeDRx4sSIzCOPPCKPx6PevXuHl61Zsyb8VfKmTZsi8nv37lV8fLw8Ho+WLFkSXp6XlxfxFXSjRo3Uu3dvrV27tka/nnjiCXXo0EF+v185OTm6/fbbdfDgwRq5Tz755JhfcZ9IUVGRsrKyIrJpaWkKBAJKSko64e0BIFa++uorjRgxQs2aNVNiYqI6d+6sefPmRWSO9/r436/pTrVq1UqlpaWqrKw8bm7ixInH7cPcuXPD2by8PAUCAf373/9Wv379lJKSopycHD3wwAM1PvCEQiE99thj6tChgxITE9WsWTONHj1a3377bY0+HG8cPvnkk4jswYMH9atf/UqtWrWS3+/XmWeeqZtvvln79++X9J/3viN3ofr888/VqlUrXXTRRSopKZEkVVZW6v7771fXrl2Vnp6ulJQU9ezZU6tXr464v+3bt+vyyy9X8+bN5ff71aJFC/385z/XgQMHwhnbtg6v56OPPlpjDM4///yjvof/965gAwcOPGodsHr1avXs2VONGjWKGL8xY8bUuC/EHrvuNCAvv/yyWrdurR/84AcndfuDBw9q8uTJx7w+MTFRc+bM0dSpU8PL5s2bJ5/Pp/Ly8hr5pk2b6g9/+IMkac+ePZo6daquvPJK7d69WxkZGZK+e2OYNGmScnNzddttt2n79u2aMWOGNm7cqHXr1snr9dZod9SoUerZs6ckadmyZfrzn/98wnXr3bu3lixZomnTpunqq69WeXm5pk2bpsLCwgb5DQeA00NZWZl69+6tnTt3asyYMTrrrLO0ePFi5eXl6eDBgzVev4YOHaorr7wyYtmECRMc3+ehQ4dUUlKiv/3tb5ozZ4569OhhPSkyY8YMBQKB8N8FBQW6//77a+SCwaD69++v73//+3r44Ye1YsUK5efnq7q6Wg888EA4N3r0aM2dO1e33HKL7rzzThUUFGj69On64IMPjvk+ceQ4LF++XAsXLoy4vqSkRD179tS2bds0fPhwXXjhhdq/f79eeukl7dmzR02bNq3RZmFhoQYMGCCv16vly5eH17GoqEjPPPOMhg4dqpEjR6q4uFizZs1Sv3799O6776pLly6SpEOHDunMM8/U1VdfrbS0NG3ZskWPP/649u7dq5dfftlRW6fqrbfe0vLly2ssLygo0MCBA5Wdna37779fmZmZkqSbbrqpVu4XUWDQIBQWFhpJZtCgQda3kWTy8/PDf991110mKyvLdO3a1fTq1Su8fPXq1UaSGTp0qGnSpImpqKgIX9euXTvzk5/8xEgyixcvDi8fNmyYadmyZcT9zZw500gy7777rjHGmK+++sr4fD7Tt29fEwwGw7np06cbSWb27NkRt9+xY4eRZObNmxdelp+fb2w28y+//NL06dPHSApfmjZtav7+97+f8LYAEE233377MV/HHnvsMSPJLFiwILyssrLS9OjRwwQCAVNUVGSMMaagoMBIMo888kiNNjp06BDxmn4ikydPjnit7NOnj/nss89OeLvDr8dff/11xPKNGzcaSWbOnDnhZcOGDTOSzB133BFeFgqFzMCBA43P5wu3sXbtWiPJPPfccxFtrlix4qjLP/74YyPJPProo+FljzzyiJFkCgoKwsvuv/9+I8ksW7asxnqEQiFjzH/e+1avXm3Ky8tN7969TVZWltm5c2dEvrq6OuJ90Rh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uC644AJt375dixcvDs/uH02zZs20YMECSd/NYMyePVv9+/fX22+/rY4dOzru5+E3p+bNmzu63b///W+tWLGixmFFGzdurB/+8Idat26d474AwOnqvw/WMGfOHOXl5R0zf9111+npp5/WW2+9pX79+kW5d5FCoZCysrL03HPPHfX6/y5ED39AaNWqVa31YcOGDZo7d66mT5+uUaNGadOmTRHfLC9YsEB5eXkaPHiwxo8fr6ysLMXHx2vy5MnatWtXjfamTZum/fv3a+vWrZo8ebJ+/vOfh98rnbY1atQoXX/99RHLRo4cecx
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torchvision.transforms as transforms\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Классы символов\n",
"#CLASSES = \"DABEKMHOPCTYX0123456789\"\n",
"#NUM_CLASSES = len(CLASSES)\n",
"\n",
"# Загрузка модели и весов\n",
"#DEVICE = \"cpu\" # Или \"cuda\", если доступен GPU\n",
"#model = SimpleCNN(NUM_CLASSES).to(DEVICE)\n",
"#model.load_state_dict(torch.load(\"best_model.pth\", map_location=DEVICE)) # Загрузка весов\n",
"#model.eval()\n",
"\n",
"# Преобразование для входных данных\n",
"transform = transforms.Compose([\n",
" # Изменение яркости (0.5 - уменьшение, >1 - увеличение)\n",
" transforms.ToTensor(),\n",
" #transforms.ColorJitter(brightness=0.5),\n",
" #transforms.Normalize((0.5,), (0.5,)) # Нормализация значений пикселей\n",
"])\n",
"\n",
"# Функция для получения топ-3 предсказаний\n",
"def get_top_predictions(output, top_k=3):\n",
" probabilities = torch.softmax(output, dim=1).squeeze() # Преобразование в вероятности\n",
" top_probs, top_indices = torch.topk(probabilities, top_k)\n",
" return top_probs.tolist(), [CLASSES[idx] for idx in top_indices.tolist()]\n",
"\n",
"# Функция распознавания символов с выводом графиков\n",
"def recognize_and_plot_characters(characters, model, device=\"cpu\"):\n",
" \"\"\"\n",
" Распознает символы из списка изображений characters и строит графики топ-3 предсказаний.\n",
"\n",
" :param characters: Список символов (массивы numpy 28x28).\n",
" :param model: Загруженная модель для распознавания.\n",
" :param device: Устройство для вычислений (\"cpu\" или \"cuda\").\n",
" \"\"\"\n",
" for i, char in enumerate(characters):\n",
" # Преобразование символа в тензор\n",
" char_tensor = transform(char).unsqueeze(0).to(device) # Добавляем batch размерности\n",
" \n",
" # Прогон через модель\n",
" with torch.no_grad():\n",
" output = model(char_tensor)\n",
" top_probs, top_classes = get_top_predictions(output)\n",
"\n",
" # Вывод символа и графика топ-3 предсказаний\n",
" plt.figure(figsize=(8, 4))\n",
"\n",
" # Символ\n",
" plt.subplot(1, 2, 1)\n",
" plt.imshow(char, cmap='gray')\n",
" plt.axis('off')\n",
" plt.title(f\"Символ {i + 1}\")\n",
"\n",
" # Топ-3 предсказания\n",
" plt.subplot(1, 2, 2)\n",
" plt.barh(top_classes, top_probs, color='blue')\n",
" plt.xlabel(\"Вероятность\")\n",
" plt.ylabel(\"Класс\")\n",
" plt.title(\"Топ-3 предсказания\")\n",
" plt.gca().invert_yaxis() # Инвертируем ось, чтобы лучший класс был сверху\n",
"\n",
" plt.tight_layout()\n",
" plt.show()\n",
"\n",
"# Использование функции\n",
"if 'characters' in locals() and len(transformed_symbols) > 0:\n",
" recognize_and_plot_characters(alls[1], model, device=DEVICE)\n",
"else:\n",
" print(\"Список characters пуст.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Топ-10 предсказаний:\n",
"1: B (вероятность: 0.3549)\n",
"2: P (вероятность: 0.2256)\n",
"3: 7 (вероятность: 0.0604)\n",
"4: E (вероятность: 0.0508)\n",
"5: 2 (вероятность: 0.0501)\n",
"6: A (вероятность: 0.0425)\n",
"7: M (вероятность: 0.0313)\n",
"8: 8 (вероятность: 0.0289)\n",
"9: 3 (вероятность: 0.0235)\n",
"10: O (вероятность: 0.0192)\n"
]
},
{
"data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b+iOO+5QtWrVdMMNN5QpVnlJTU3VsGHD1LlzZ6WkpCgjI0OzZs3SZ599po0bN6patWqSpG+//VZ/+MMfVK9ePU2YMEGRkZF644031L9/f7311lu6/vrry7TdH3/8sVixRpIyMzPVtWtXDRkyRKGhoVq+fLkmTJigkJAQjRs3zpPL5ZdfrpiYGP3tb39TaGio/vWvf6lHjx766KOP1LVrV0nSoUOHtG7dOoWEhGjUqFFq0qSJlixZojvvvFOHDh3yek8le6j9sWPHNGrUKOXk5GjWrFn64x//qK+//lrx8fGSSv9+PfbYY9q+fbuuv/56ffnllz4vy9y5c6f69++vfv36ef2181w/A2X1yiuv6Ouvvy423/R7DQAIbCdOnFCPHj30448/avTo0WrUqJEWL16soUOH6siRI8XOowYPHqyrr77aa56vc7Pfw+12a8uWLRo+fHixZV26dNHKlSt17NgxRUdHnzHOVVddpdtvv91r3tNPP63ffvutWNvSnCecy2+oy+XSv//9b8/rO+64o1ib1NRUDR8+XK1bt9bEiRNVrVo1bdy4UcuXL9fNN9/sc99efvllPfTQQ3r66ae92ixevFjZ2dm6++67VbNmTa1du1bPPvusfvnlFy1evNjTLisrS9dff70aNmyoEydOKDU1VTfccIPS0tLUpUuXMsX6vR544IFStTty5IhSUlKMbReADxbgw8svv2xJsr766iufy7t37261bt3aa54kS5K1bt06z7xdu3ZZYWFh1vXXX++ZN2LECKtOnTrWwYMHvda/6aabrNjYWCs7O9tr/oMPPmhJ8mrfunVrq3v37p7XBQUFVqNGjawGDRpYv/32m9f6brfb899DhgyxGjRoYFmWZa1bt86KioqyBgwYYBUWFnqtc3oOlmVZr7/+uiXJ+vjjj330yMk8YmJirNGjR5c51uTJky1J1oEDB7zafvXVV5Yk6+WXX/baj8jIyGJxFy9ebEmyPvzwQ8uyLCsvL8+qXbu21aZNG+vEiROedkuXLrUkWZMmTfLMu/LKK622bdtaOTk5nnlut9u69NJLrWbNmpW4z5ZlWTt37iyW48CBA602bdpYiYmJ1pAhQ864fqtWrax+/fp5Xvfv399yOp3Wjh07PPN+/fVXKzo62rriiis88xo0aGBJslJTUz3zCgoKrCuvvNJyuVyeY6Yov/DwcOuXX37xtP3yyy8tSdZ9993nmVeW9z4rK8vq1KmT1bp1a+vo0aPWhx9+6On/I0eOWK1atbI6d+5cLGZpPwNF8RYvXlwsp8jISK9+LfrM7ty507Isy8rJybHq169v9enTp9h783veawBAYBo1apRV0qn/zJkzLUnWq6++6pmXl5dndevWzYqKirIyMzMtyzr5e/rkk08Wi3H6uVlpHDhwwJJkTZ48ucRljz76aLFls2fPtiRZ33///RnjS7JGjRpVbH7fvn0954OWVbbzhLL+ht58881WVFSU17zTf8OPHDliRUdHW127dvU6XyuKXaR79+6ePn7//fetkJAQa9y4ccW26etcJiUlxXI4HNauXbuKLSuyf/9+S5L11FNPlTlWac9NT98Py7KsZcuWWZKs5OTkYsfo6cfH3/72N6t27dpWx44dy3y8ASgdLjeDUd26dVPHjh09r+vXr6/rrrtOK1asUGFhoSzL0ltvvaVrrrlGlmXp4MGDnqnoCVUbNmzwipmTkyNJCgsLK3G7Gzdu1M6dO/XXv/7VMzKmiK+ROj/99JP69u2rDh066JVXXlFQkPdHITw83Gv7Bw8e9Nwg7/T8CgsLdfDgQe3atUvPPPOMMjMzdfnll59TLEk6fPiwV78cPXq0xP0+td3
"text/plain": [
"<Figure size 1200x600 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import torch\n",
"from torchvision import transforms\n",
"from PIL import Image\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# Предполагается, что CLASSES и DEVICE определены ранее\n",
"# Например:\n",
"# CLASSES = ['A', 'B', 'C', ..., 'Z', '0', '1', ..., '9']\n",
"# DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"\n",
"# Путь к изображению\n",
"image_path = \"img/7-viva4.jpg\"\n",
"transform = transforms.Compose([\n",
" transforms.Resize((28, 28)), # Изменяем размер до 28x28\n",
" transforms.ToTensor(), # Преобразуем в тензор\n",
" transforms.Normalize(mean=[0.5], std=[0.5]) # Нормализация: среднее 0.5, стандартное отклонение 0.5\n",
" ])\n",
"\n",
"transform1 = transforms.Compose([\n",
" transforms.ToTensor(), # Преобразуем в тензор\n",
" #transforms.Normalize(mean=[0.5], std=[0.5]) # Нормализация: среднее 0.5, стандартное отклонение 0.5\n",
" ])\n",
"# Предобработка изображения для чёрно-белого изображения 28x28\n",
"def preprocess_image(image_path):\n",
" # Загрузка изображения\n",
" image = Image.open(image_path).convert('L') # Преобразование в чёрно-белое\n",
" # Применение трансформаций\n",
" image_tensor = transform(image)\n",
" \n",
" # Добавление batch dimension\n",
" image_tensor = image_tensor.unsqueeze(0)\n",
" return image_tensor\n",
"\n",
"# Загрузка и обработка изображения\n",
"image_tensor = preprocess_image(image_path).to(DEVICE)\n",
"char_tensor = transform1(transformed_symbols[3]).unsqueeze(0).to(DEVICE)\n",
"\n",
"# Загрузка модели (предполагается, что модель уже определена)\n",
"# model = SimpleCNN(len(CLASSES)).to(DEVICE)\n",
"# Загрузка весов модели\n",
"# model.load_state_dict(torch.load(SAVE_PATH, map_location=DEVICE))\n",
"# model.eval()\n",
"\n",
"# Убедитесь, что модель находится в режиме оценки\n",
"model.eval()\n",
"\n",
"# Добавление вывода изображения и графика топ-10 предсказаний\n",
"def plot_image_and_predictions(image_tensor, classes):\n",
" \"\"\"\n",
" Отображает изображение и график топ-10 предсказаний модели.\n",
" \n",
" :param image_tensor: Тензор изображения после предобработки.\n",
" :param top_probs: Топ-10 вероятностей предсказаний.\n",
" :param top_indices: Индексы топ-10 классов.\n",
" :param classes: Список названий классов.\n",
" \"\"\"\n",
" # Обратная нормализация для отображения изображения\n",
" # Если нормализация была (mean=0.5, std=0.5), то обратная:\n",
" # x = x * std + mean\n",
"\n",
" # Предсказание\n",
" with torch.no_grad():\n",
" output = model(image_tensor) # Получение выхода модели\n",
" probabilities = torch.softmax(output, dim=1) # Преобразование выходов в вероятности\n",
" top10_probabilities, top10_indices = torch.topk(probabilities, 10, dim=1) # Получение топ-10\n",
" \n",
" top_probs = top10_probabilities.squeeze(0) # Удаление batch-дименшена\n",
" top_indices = top10_indices.squeeze(0) # Удаление batch-дименшена\n",
"\n",
" print(\"Топ-10 предсказаний:\")\n",
" for i in range(10):\n",
" predicted_class = top_indices[i].item()\n",
" probability = top_probs[i].item()\n",
" print(f\"{i + 1}: {CLASSES[predicted_class]} (вероятность: {probability:.4f})\")\n",
" \n",
" unnormalize = transforms.Normalize(\n",
" mean=[-0.5 / 0.5],\n",
" std=[1 / 0.5]\n",
" )\n",
" image_tensor_unnorm = unnormalize(image_tensor.squeeze(0)).cpu().numpy()\n",
" \n",
" # Переводим тензор в формат [H, W]\n",
" image_np = image_tensor_unnorm.squeeze(0)\n",
" image_np = np.clip(image_np, 0, 1) # Ограничиваем значения для корректного отображения\n",
" \n",
" # Получение топ-10 классов и их вероятностей\n",
" top_classes = [classes[idx] for idx in top_indices]\n",
" top_probs = top_probs.cpu().numpy()\n",
" \n",
" # Создание фигуры с двумя подграфиками\n",
" fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))\n",
" \n",
" # Отображение изображения\n",
" ax1.imshow(image_np, cmap='gray')\n",
" ax1.set_title(\"Предсказанное изображение\")\n",
" ax1.axis('off')\n",
" \n",
" # Отображение графика топ-10 предсказаний\n",
" ax2.barh(range(10), top_probs, color='skyblue') # Инвертируем для отображения сверху вниз\n",
" ax2.set_yticks(range(10))\n",
" ax2.set_yticklabels(top_classes)\n",
" ax2.invert_yaxis() # Самый высокий показатель сверху\n",
" ax2.set_xlabel('Вероятность')\n",
" ax2.set_title('Топ-10 предсказаний')\n",
" \n",
" # Добавление вероятностей на график\n",
" for i, v in enumerate(top_probs):\n",
" ax2.text(v + 0.01, i, f\"{v:.2f}\", color='blue', va='center')\n",
" \n",
" plt.tight_layout()\n",
" plt.show()\n",
"\n",
"# Вызов функции для отображения\n",
"plot_image_and_predictions(image_tensor, CLASSES)\n"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA/0AAAMWCAYAAACjvOYhAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAABuWklEQVR4nO3deZQeVZ0//k9n64SEnUSWwRAWQRDRL4oKYsIAZlREEHRgIrsYRWV03JeBgKN83WVwww0YBUZBR0RFlGERt3FB3BgVEfQnekD2LSGQ1O8PT/pL0/dC3VQ/3f3cfr3O4Ry9favqVj1Vt55PqvtdA03TNAEAAABUZ8p4DwAAAADoDUU/AAAAVErRDwAAAJVS9AMAAEClFP0AAABQKUU/AAAAVErRDwAAAJVS9AMAAEClFP0AAABQKUX/JLds2bIYGBhYq2XPPPPMGBgYiBtuuGF0B/UQN9xwQwwMDMSZZ57Zs20A0DtHHnlkbLXVVuM9DIAJa2BgIJYtWzbew6Biiv4+9qtf/Spe8pKXxBZbbBGDg4Ox+eabx5IlS+JXv/rVeA8NgIi4/vrr41WvelU87nGPi3XWWSfWWWed2HHHHeOVr3xl/PznPx/v4QHQI2sejj30v3nz5sVee+0VF1100XgPj0lm2ngPgLXzpS99KQ499NDYaKON4phjjokFCxbEDTfcEJ/+9Kfj/PPPj//8z/+MAw888FHX8/a3vz3e/OY3r9UYDjvssDjkkENicHBwrZYHqNlXv/rV+Md//MeYNm1aLFmyJHbZZZeYMmVK/PrXv44vfelL8bGPfSyuv/76mD9//ngPFYAeOfnkk2PBggXRNE3cdNNNceaZZ8Zzn/vcuPDCC2O//faLiIjly5fHtGnKMnrH2dWHrrvuujjssMNi6623jm9/+9sxd+7coZ/98z//c+y5555x2GGHxc9//vPYeuutk+u49957Y/bs2TFt2rS1nmSmTp0aU6dOXatlAWp23XXXxSGHHBLz58+P//7v/47NNtts2M/f/e53x0c/+tGYMiX/C3dr5mkA+tdznvOceMpTnjL0/4855ph4zGMeE+eee+5Q0T9z5swxH1fTNLFixYqYNWvWmG+bsefX+/vQe9/73rjvvvviE5/4xLCCPyJik002idNPPz3uvffeeM973hMR/+/v9q+55pr4p3/6p9hwww3jmc985rCfPdTy5cvj+OOPj0022STWXXfd2H///ePGG28c8fdGqb/p32qrrWK//faL73znO7HbbrvFzJkzY+utt47/+I//GLaN2267LV7/+tfHzjvvHHPmzIn11lsvnvOc58TPfvazUTxSAOPjPe95T9x7771xxhlnjCj4IyKmTZsWxx9/fGy55ZYR8be/e58zZ05cd9118dznPjfWXXfdWLJkSUREXHnllfGiF70oHvvYx8bg4GBsueWW8drXvjaWL18+tL4zzjgjBgYG4qc//emIbb3rXe+KqVOnxo033hgREddee20cdNBBsemmm8bMmTPj7/7u7+KQQw6JO++8c9hyn/vc52K33XaLddZZJzbccMN41rOeFd/85jeHfn7BBRfE8573vNh8881jcHAwttlmm3jHO94Rq1atetTjs3r16vjQhz4UO+20U8ycOTMe85jHxNKlS+P2229vcXQB+tcGG2wQs2bNGvbQ7eHfsdd8P//d734XRx55ZGywwQax/vrrx1FHHRX33XffsPWdccYZ8fd///cxb968GBwcjB133DE+9rGPjdjumu/oF198cTzlKU+JWbNmxemnnx4LFy6MXXbZJTnW7bffPhYvXjw6O8648qS/D1144YWx1VZbxZ577pn8+bOe9azYaqut4mtf+9qw9he96EWx3Xbbxbve9a5omia7/iOPPDK+8IUvxGGHHRZPf/rT44orrojnPe95rcf3u9/9Lg4++OA45phj4ogjjojPfOYzceSRR8auu+4aO+20U0RE/P73v48vf/nL8aIXvSgWLFgQN91009DEc80118Tmm2/eensAE81Xv/rV2HbbbeNpT3ta62UefPDBWLx4cTzzmc+M973vfbHOOutERMR5550X9913X7ziFa+IjTfeOH74wx/GaaedFn/605/ivPPOi4iIgw8+OF75ylfG2WefHU9+8pOHrffss8+ORYsWxRZbbBErV66MxYsXx/333x+vfvWrY9NNN40bb7wxvvrVr8Ydd9wR66+/fkREnHTSSbFs2bLYfffd4+STT44ZM2bE//zP/8Sll14az372syPib//wO2fOnPiXf/mXmDNnTlx66aVxwgknxF133RXvfe97H3Ffly5dGmeeeWYcddRRcfzxx8f1118fH/7wh+OnP/1pfPe7343p06e3Pm4AE9mdd94Zt9xySzRNEzfffHOcdtppcc8998RLXvKSR132xS9+cSxYsCBOOeWUuOqqq+JTn/pUzJs3L9797ncP9fnYxz4WO+20U+y///4xbdq0uPDCC+O4446L1atXxytf+cph6/vNb34Thx56aCxdujSOPfbY2H777WPOnDlx7LHHxi9/+ct4whOeMNT3Rz/6Ufz2t7+Nt7/97aN3MBg/DX3ljjvuaCKiecELXvCI/fbff/8mIpq77rqrOfHEE5uIaA499NAR/db8bI2f/OQnTUQ0r3nNa4b1O/LII5uIaE488cShtjPOOKOJiOb6668faps/f34TEc23v/3tobabb765GRwcbF73utcNta1YsaJZtWrVsG1cf/31zeDgYHPyyScPa4uI5owzznjE/QWYKO68884mIpoDDjhgxM9uv/325q9//evQf/fdd1/TNE1zxBFHNBHRvPnNbx6xzJo+D3XKKac0AwMDzR/+8IehtkMPPbTZfPPNh82tV1111bA59Kc//WkTEc15552XHf+1117bTJkypTnwwANHzNOrV69+xHEtXbq0WWeddZoVK1YMtR1xxBHN/Pnzh/7/lVde2UREc/bZZw9b9hvf+EayHaAfrfme/PD/BgcHmzPPPHNY34d/x17z/fzoo48e1u/AAw9sNt5442Ftqbl48eLFzdZbbz2sbc139G984xvD2u+4445m5syZzZve9KZh7ccff3wze/bs5p577mm9z0xcfr2/z9x9990REbHuuus+Yr81P7/rrruG2l7+8pc/6vq/8Y1vRETEcccdN6z91a9+desx7rjjjsN+C2Hu3Lmx/fbbx+9///uhtsHBwaG/ZV21alXceuutMWfOnNh+++3jqquuar0tgIlmzbw7Z86cET9btGhRzJ07d+i/j3zkI8N+/opXvGLEMg/9e8t77703brnllth9992jaZphv85/+OGHx5///Oe47LLLhtrOPvvsmDVrVhx00EEREUNP8i+++OIRvyK6xpe//OVYvXp1nHDCCSMyBx7652APHdfdd98dt9xyS+y5555x3333xa9//evkuiP+9psL66+/fuy7775xyy23DP236667xpw5c4aNH6DffeQjH4lvfetb8a1vfSs+97nPxV577RUvfelL40tf+tKjLvvw7+577rln3HrrrcO+3z90Ll7zWwULFy6M3//+9yP+bGvBggUjfl1//fXXjxe84AVx7rnnDv0m8KpVq+Lzn/98HHDAAbJlKqHo7zNrivk1xX9O6h8HFixY8Kjr/8Mf/hBTpkwZ0XfbbbdtPcbHPvaxI9o23HDDYX+ruXr16vjgBz8Y2223XQwODsYmm2wSc+fOjZ///OcjJiiAfrJm3r3nnntG/Oz0008f+uL3cNOmTYu/+7u/G9H+xz/+MY488sjYaKONYs6cOTF37txYuHBhRMSw+XLfffeNzTbbLM4+++yI+Ns8e+6558YLXvCCoTEtWLAg/uVf/iU+9alPxSabbBKLFy+Oj3zkI8PWc91118WUKVNixx13fMT9/NWvfhUHHnhgrL/++rHeeuvF3Llzh35d9ZHm8WuvvTbuvPPOmDdv3rB/AJk7d27cc889cfPNNz/idgH6yW677Rb77LNP7LPPPrFkyZL42te+FjvuuGO86lWvipUrVz7isg//Tr3hhhtGRAz7Tv3d73439tlnn5g9e3ZssME
"text/plain": [
"<Figure size 1200x800 with 6 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Топ-10 предсказаний:\n",
"1: Y (вероятность: 0.2801)\n",
"2: 2 (вероятность: 0.1730)\n",
"3: 9 (вероятность: 0.0611)\n",
"4: M (вероятность: 0.0609)\n",
"5: P (вероятность: 0.0556)\n",
"6: X (вероятность: 0.0436)\n",
"7: 8 (вероятность: 0.0414)\n",
"8: H (вероятность: 0.0380)\n",
"9: 0 (вероятность: 0.0329)\n",
"10: 1 (вероятность: 0.0279)\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABIsAAAJOCAYAAAA3eodTAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiw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ybb2B9//FG33367rrnmGoWEhOj666/XlClTzOt9+/ZVTEyMyz7vvvuufH19NWHCBLNty5Yt6tu3r6pWraqgoCBFRUWpf//+OnLkiMu+06dPV4MGDRQeHq6QkBA1aNBAb7/9tsuYvNbKOs6LQ7h169bJx8dHc+bMcTmO0NDQbMf/4Ycfup1avnDhQjVu3FglSpRQRESE7r//fh08eDDb/r/88ovuuecelS5dWkFBQWrSpIk+/fTTbOMutmfPnmw9StKgQYPk4+Ojvn37mm0fffSRmjVrptKlS6tEiRKqVauWXnrpJVmW5bLvxo0b1bFjR4WFhSk0NFS33nqrVq9e7TJm9erVaty4sR577DHzd6levXp688033fb36quvatKkSapcubJKlCih1q1ba+vWrS5j8/r1GjlypIKCgvT999+bbQkJCdnO//fff6+goCCNHDnSZf+8fA9k1fvwww+znfPQ0FCX85r10dE9e/aYbZmZmbr++uvdfm0u92sNACheDh06pAEDBigyMlJBQUFq0KCB5s6d6zIm6/doTou7azJ3Lr4Gy8mHH36opk2bmqBIkmrVqqVbb71VH3zwwSX3z63XC3vIz/WBlL/fnX379nX7/hf+7s7yf//3f2rdurVKliypsLAwNW3aVPPmzTOvt2nTJts5Hj9+vHx9fV3Gffvtt+revbsqVaokh8Oh6OhoDR06VGfOnHHZd+zYsapTp45CQ0MVFhamG2+8UYsXL3YZk9da+bkmdXcca9euNefmQlkfU3Q4HGrcuLFq166tV155JV9/3wBcGWYWwVZff/21FixYoCeeeEIOh0NvvPGGOnTooDVr1ph7HCUnJ+vGG2804VLZsmX1f//3fxowYIBSU1P15JNPuq09ffp088vo4v/xlqSVK1eqc+fOKl++vIYMGaKoqCht375dS5YsyfFmxytWrFD//v01ePBgjRgxwqXW77//rn79+ikqKkrbtm3TzJkztW3bNq1evdr8Qjtx4oTat2+vatWqybIsffDBB3rwwQdVqlQp3X333fmqVVDmzJmjfv36qWnTpoqPj1dycrKmTJmi77//Xhs3blSpUqUkSdu2bdM//vEPVaxYUSNGjFBISIg++OADde3aVYsWLVK3bt3y9b47d+7MFtpIUmpqqpo3b64+ffooICBAy5Yt04gRI+Tv769hw4aZXm6++WaFhYXp6aefVkBAgP773/+qTZs2+vrrr9W8eXNJ0pEjR7Ru3Tr5+/tr0KBBqlatmhYvXqyHHnpIR44ccfmaSs6p+CdOnNCgQYN09uxZTZkyRbfccot++uknRUZGSsr71+vFF1/Ujh071K1bN/34449uP665e/dude3aVZ07d3b5V9DL/R7Ir3feeUc//fRTtu12f60BAN7pzJkzatOmjXbu3KnBgwerSpUqWrhwofr27atjx45lu37q1auXbr/9dpdt7q7JrkRmZqa2bNmi/v37Z3utWbNmWrFihU6cOKGSJUvmWue2227TAw884LLttdde019//ZVtbF6uDy7nd6fD4dBbb71l1h988MFsY+bMmaP+/furbt26GjlypEqVKqWNGzdq2bJl+uc//+n22GbPnq1nn31Wr732msuYhQsX6vTp03r00UdVpkwZrVmzRlOnTtWBAwe0cOFCM+7UqVPq1q2bYmJidObMGc2ZM0d33323EhMT1axZs3zVulLPPPNMnsYdO3ZM8fHxtr0vgDywADdmz55tSbLWrl3r9vXWrVtbdevWddkmyZJkrVu3zmzbu3evFRQUZHXr1s1sGzBggFW+fHkrJSXFZf97773XCg8Pt06fPu2yfdSoUZYkl/F169a1Wrdubdb//vtvq0qVKlblypWtv/76y2X/zMxM8+c+ffpYlStXtizLstatW2eFhoZa3bt3tzIyMlz2ubgHy7Ks999/35JkffPNN27OyPk+wsLCrMGDB+e71pgxYyxJ1uHDh13Grl271pJkzZ492+U4QkJCstVduHChJcn66quvLMuyrPT0dKtcuXJWvXr1rDNnzphxS5YssSRZo0ePNttuvfVWq379+tbZs2fNtszMTKtly5ZWjRo1cjxmy7Ks3bt3Z+uxR48eVr169azo6GirT58+ue5fp04dq3Pnzma9a9euVmBgoLVr1y6z7Y8//rBKlixptWrVymyrXLmyJcmaM2eO2fb3339bt956q+VwOMzfmaz+SpQoYR04cMCM/fHHHy1J1tChQ822/HztT506ZTVp0sSqW7eudfz4ceurr74y5//YsWNWnTp1rKZNm2armdfvgax6CxcuzNZTSEiIy3nN+p7dvXu3ZVmWdfbsWatSpUpWx44ds31truRrDQDwLoMGDbJyuuSfPHmyJcl69913zbb09HSrRYsWVmhoqJWammpZ1vnfo6+88kq2Ghdfk+XF4cOHLUnWmDFjcnzthRdeyPbatGnTLEnWL7/8kmt9SdagQYOybe/UqZO5DrSs/F0f5Pd35z//+U8rNDTUZdvFv7uPHTtmlSxZ0mrevLnLdVpW7SytW7c25/jzzz+3/P39rWHDhmV7T3fXMPHx8ZaPj4+1d+/ebK9lOXTokCXJevXVV/NdK6/XpBcfh2VZ1tKlSy1JVocOHbL9Hb3478fTTz9tlStXzmrcuHG+/74BuDx8DA22atGihRo3bmzWK1WqpC5dumj58uXKyMiQZVlatGiR7rjjDlmWpZSUFLNkPdFqw4YNLjXPnj0rSQoKCsrxfTdu3Kjdu3frySefNDNlsribufP777+rU6dOatiwod555x35+rp+K5QoUcLl/VNSUswN9S7uLyMjQykpKdq7d68mTZqk1NRU3XzzzZdVS5KOHj3qcl6OHz+e43FfOC4lJSXbEyLWrVu
"text/plain": [
"<Figure size 1200x600 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import torch\n",
"from torchvision import transforms\n",
"from PIL import Image\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import cv2 as cv\n",
"\n",
"# Предполагается, что CLASSES и DEVICE определены ранее\n",
"# Например:\n",
"# CLASSES = ['A', 'B', 'C', ..., 'Z', '0', '1', ..., '9']\n",
"# DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"\n",
"# Путь к изображению\n",
"image_path = \"img/7-bad1.png\"\n",
"\n",
"# Определение трансформаций\n",
"transform = transforms.Compose([\n",
" transforms.Resize((28, 28)), # Изменяем размер до 28x28\n",
" transforms.ToTensor(), # Преобразуем в тензор\n",
" transforms.Normalize(mean=[0.5], std=[0.5]) # Нормализация: среднее 0.5, стандартное отклонение 0.5\n",
"])\n",
"\n",
"transform1 = transforms.Compose([\n",
" transforms.ToTensor(), # Преобразуем в тензор\n",
" #transforms.Normalize(mean=[0.5], std=[0.5]) # Нормализация: среднее 0.5, стандартное отклонение 0.5\n",
"])\n",
"\n",
"def preprocess_image(image_path):\n",
" # Шаг 1: Загрузка изображения с помощью OpenCV\n",
" src = cv.imread(image_path, cv.IMREAD_COLOR)\n",
" assert src is not None, f\"Ошибка при открытии изображения по пути: {image_path}\"\n",
"\n",
" # Шаг 2: Преобразование в оттенки серого\n",
" if len(src.shape) != 2:\n",
" gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)\n",
" else:\n",
" gray = src\n",
"\n",
" # Инверсия цветов\n",
" gray = cv.bitwise_not(gray)\n",
"\n",
" # Бинаризация изображения\n",
" bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 15, -2)\n",
"\n",
" # Извлечение горизонтальных и вертикальных линий\n",
" horizontal = np.copy(bw)\n",
" vertical = np.copy(bw)\n",
"\n",
" # Обработка горизонтальных линий\n",
" horizontal_size = horizontal.shape[1] // 30\n",
" horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))\n",
" horizontal = cv.erode(horizontal, horizontalStructure)\n",
" horizontal = cv.dilate(horizontal, horizontalStructure)\n",
"\n",
" # Обработка вертикальных линий\n",
" vertical_size = vertical.shape[0] // 30\n",
" verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, vertical_size))\n",
" vertical = cv.erode(vertical, verticalStructure)\n",
" vertical = cv.dilate(vertical, verticalStructure)\n",
"\n",
" # Инверсия вертикальных линий\n",
" vertical = cv.bitwise_not(vertical)\n",
"\n",
" # Выделение краев\n",
" edges = cv.adaptiveThreshold(vertical, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 3, -2)\n",
" kernel = np.ones((2, 2), np.uint8)\n",
" edges = cv.dilate(edges, kernel)\n",
"\n",
" # Сглаживание вертикальных линий\n",
" smooth = np.copy(vertical)\n",
" smooth = cv.blur(smooth, (2, 2))\n",
" rows, cols = np.where(edges != 0)\n",
" vertical[rows, cols] = smooth[rows, cols]\n",
"\n",
" # Финальная обработка: использование сглаженных вертикальных линий\n",
" final_processed = vertical\n",
"\n",
" # (Опционально) Визуализация этапов обработки\n",
" \n",
" titles = ['Original', 'Grayscale', 'Binary', 'Horizontal Lines', 'Vertical Lines', 'Smoothed Vertical']\n",
" images = [src, gray, bw, horizontal, vertical, smooth]\n",
"\n",
" plt.figure(figsize=(12, 8))\n",
" for i in range(len(images)):\n",
" plt.subplot(2, 3, i + 1)\n",
" plt.imshow(images[i], cmap='gray' if len(images[i].shape) == 2 else None)\n",
" plt.title(titles[i])\n",
" plt.axis('off')\n",
"\n",
" plt.tight_layout()\n",
" plt.show()\n",
" \n",
"\n",
" # Преобразование обработанного изображения в формат PIL для применения трансформаций\n",
" final_image = Image.fromarray(final_processed)\n",
"\n",
" # Применение трансформаций\n",
" image_tensor = transform(final_image)\n",
"\n",
" # Добавление batch dimension\n",
" image_tensor = image_tensor.unsqueeze(0)\n",
"\n",
" return image_tensor\n",
"\n",
"# Загрузка и обработка изображения\n",
"image_tensor = preprocess_image(image_path).to(DEVICE)\n",
"\n",
"# Обработка символов (предполагается, что transformed_symbols определен)\n",
"# char_tensor = transform1(transformed_symbols[3]).unsqueeze(0).to(DEVICE)\n",
"\n",
"# Загрузка модели (предполагается, что модель уже определена)\n",
"# model = SimpleCNN(len(CLASSES)).to(DEVICE)\n",
"# Загрузка весов модели\n",
"# model.load_state_dict(torch.load(SAVE_PATH, map_location=DEVICE))\n",
"# model.eval()\n",
"\n",
"# Убедитесь, что модель находится в режиме оценки\n",
"model.eval()\n",
"\n",
"# Добавление вывода изображения и графика топ-10 предсказаний\n",
"def plot_image_and_predictions(image_tensor, classes):\n",
" \"\"\"\n",
" Отображает изображение и график топ-10 предсказаний модели.\n",
" \n",
" :param image_tensor: Тензор изображения после предобработки.\n",
" :param classes: Список названий классов.\n",
" \"\"\"\n",
" # Предсказание\n",
" with torch.no_grad():\n",
" output = model(image_tensor) # Получение выхода модели\n",
" probabilities = torch.softmax(output, dim=1) # Преобразование выходов в вероятности\n",
" top10_probabilities, top10_indices = torch.topk(probabilities, 10, dim=1) # Получение топ-10\n",
" \n",
" top_probs = top10_probabilities.squeeze(0) # Удаление batch-дименшена\n",
" top_indices = top10_indices.squeeze(0) # Удаление batch-дименшена\n",
"\n",
" print(\"Топ-10 предсказаний:\")\n",
" for i in range(10):\n",
" predicted_class = top_indices[i].item()\n",
" probability = top_probs[i].item()\n",
" print(f\"{i + 1}: {CLASSES[predicted_class]} (вероятность: {probability:.4f})\")\n",
" \n",
" # Обратная нормализация для отображения изображения\n",
" unnormalize = transforms.Normalize(\n",
" mean=[-0.5 / 0.5],\n",
" std=[1 / 0.5]\n",
" )\n",
" image_tensor_unnorm = unnormalize(image_tensor.squeeze(0)).cpu().numpy()\n",
" \n",
" # Переводим тензор в формат [H, W]\n",
" image_np = image_tensor_unnorm.squeeze(0)\n",
" image_np = np.clip(image_np, 0, 1) # Ограничиваем значения для корректного отображения\n",
" \n",
" # Получение топ-10 классов и их вероятностей\n",
" top_classes = [classes[idx] for idx in top_indices]\n",
" top_probs = top_probs.cpu().numpy()\n",
" \n",
" # Создание фигуры с двумя подграфиками\n",
" fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))\n",
" \n",
" # Отображение изображения\n",
" ax1.imshow(image_np, cmap='gray')\n",
" ax1.set_title(\"Предсказанное изображение\")\n",
" ax1.axis('off')\n",
" \n",
" # Отображение графика топ-10 предсказаний\n",
" ax2.barh(range(10), top_probs, color='skyblue') # Инвертируем для отображения сверху вниз\n",
" ax2.set_yticks(range(10))\n",
" ax2.set_yticklabels(top_classes)\n",
" ax2.invert_yaxis() # Самый высокий показатель сверху\n",
" ax2.set_xlabel('Вероятность')\n",
" ax2.set_title('Топ-10 предсказаний')\n",
" \n",
" # Добавление вероятностей на график\n",
" for i, v in enumerate(top_probs):\n",
" ax2.text(v + 0.01, i, f\"{v:.2f}\", color='blue', va='center')\n",
" \n",
" plt.tight_layout()\n",
" plt.show()\n",
"\n",
"# Вызов функции для отображения\n",
"plot_image_and_predictions(image_tensor, CLASSES)\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA1qUlEQVR4nO3dd3hUZd7/8c+kIyGhhUA0EkMRREBAYdGlR4qIYBcbEUQs4LMuBmT9SUDdBcvzCAsIokBQUKTEQlksSBQRAZFiQQQ2KEVURFIIpM39+8Mrs8yGcgYymeTO+3Vdc+mc+c4933OYzHzmnjPnuIwxRgAAAACsExToBgAAAAD4B2EfAAAAsBRhHwAAALAUYR8AAACwFGEfAAAAsBRhHwAAALAUYR8AAACwFGEfAAAAsBRhHwAAALAUYR8AAACwFGG/Ctq9e7eGDRumxMRERUREKCoqSldddZUmT56sY8eOBbq9gNiwYYMefPBBtWvXTqGhoXK5XIFuCUAV5nK5HF0yMjLKta8DBw7ozjvv1MUXX6waNWqoZs2aat++vebOnStjTLn2AsCZkEA3gPK1fPly3XzzzQoPD9fdd9+tSy+9VAUFBfr000+VkpKib775RjNnzgx0m+VuxYoVeuWVV9SqVSslJibq+++/D3RLAKqw1157zev6q6++qg8++KDU8ubNm5dnWzp06JD27dunm266SRdeeKEKCwv1wQcfKDk5WTt27NA//vGPcu0HwJm5DB/Fq4zMzEy1atVKF1xwgT766CM1aNDA6/Zdu3Zp+fLl+p//+Z8AdRg4P//8s6KiolStWjUNHz5c06ZNY5YKQIVR0V+X+vXrp9WrVysrK0vBwcGBbgfACdiNpwp59tlnlZubq1mzZpUK+pLUuHFjr6Dvcrk0btw4r5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WeDxM//PCDJOniiy/2ul9YWJgSExM9t5co2Y//VB9GAMBGP/zwg5o0aeI1KSL950g9//1a6cTBgwe9Lv8d5Bs2bKikpCQNHDhQ8+fPV2JiopKSkso88AcFBSkxMdFrWdOmTSXJs3/9zp07lZWVpXr16nneU0ouubm5+uWXX7zuX7K/fXR09Gkfe/fu3br00ksd9fn4449r4cKFys/PV15e3klr5s6dq1atWikiIkJ16tRRTEyMli9frqysrFK1vXr1Uv369ZWUlKTmzZvrzTffPOuxUlNTS22X77777pTrUlxcrL/97W+644471KpVq1K3d+zYUZKUkpKi7du3ez5AoOJin/0qIioqSnFxcfr66699vu/27duVlpamefPmnXS/xxMNHjxYbdq00Y4dO7Ro0SLPLP/JxMbGat68eZL+mMWYPXu2evfurU8//VQtW7b0uc+DBw9KkurXr+/zfQEA//HfB3GYM2eOkpOTT1l/00036eWXX9Ynn3yiXr16+bk7b263W/Xq1dP8+fNPenvJPuUlSj4kJCQklFkP69evV1pamqZOnar77rtPW7Zs8fqWed68eUpOTtaAAQOUkpKievXqKTg4WBMmTNDu3btLjTdlyhQdOnRI3377rSZMmKD777/f837p61j33Xefbr75Zq9lQ4cOPeW6zJo1S3v27NF777130tuvvPJKPffccxo/frwuueQSR9sHgUXYr0KuvfZazZw5U+vWrfN8MndizJgxuuyyy065O86JWrZsqTZt2uiWW25RTEyMunXrdsqvkCMiIpSUlOS5ft1116l27dqaOnWqXnrpJTVs2FDSH0cnOHFmp6CgQJmZmV73laRvv/1WLper1DcBAGCzhg0batu2bXK73V6z+yWztyWvpb744IMPvK63aNHitPUlM/onm1k+F263W//
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACX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T06NEiPZS0H3788UeP2mPHjulvf/ubkpKSFBISolq1amngwIE6dOiQpP+9/p19OtWBAweUlJSk1q1b6/jx45J+n1R66qmn1KpVK0VHRys8PFwdOnTQmjVrPB5v586duu666xQfH6+QkBDVrl1b999/v44cOeKusTpW4fN88cUXi+yDJk2aFPs6/sfTwnr16lVsFlizZo06dOigatWqeey/YcOGFXks+B6n8VQiH3zwgerWrVtssLXi2LFjGjt27DlvDw0N1YwZMzRx4kT3tlmzZsnpdBZ77nvNmjX10ksvSZL27duniRMn6vrrr9fevXtVtWpVSb+/MIwZM0YpKSl64IEHtHPnTk2dOlWbNm3S+vXrFRwcXGTce++9Vx06dJAkLVq0SO+9994FPd/MzEx3nwDgr06dOqXOnTtr9+7dGjZsmC677DLNnz9fqampOnbsWJF3awcMGKDrr7/eY9uoUaO8fswTJ07o+PHj+uSTTzRjxgy1b99eYWFhlu4/depURUREuK9nZGToqaeeKlJXUFCgHj166KqrrtILL7yg5cuXKz09Xfn5+Xr66afddffdd59mzpypu+66Sw8//LAyMjI0efJkff311+d8rTh7Pyxbtkxz5szxuP348ePq0KGDduzYocGDB+vKK6/UoUOHtHjxYu3bt6/Y14asrCz17NlTwcHBWrZsmfs5Zmdn64033tCAAQM0ZMgQ5eTkaNq0aerevbs2btyoFi1aSJJOnDihWrVq6YYbblBUVJS+/fZbTZkyRfv379cHH3zg1VgX69NPP9WyZcuKbM/IyFCvXr2UkJCgp556SjExMZKkO++8s1QeF2XAoFLIysoykkyfPn0s30eSSU9Pd19/9NFHTWxsrGnVqpXp1KmTe/uaNWuMJDNgwABTo0YNk5ub676tfv365vbbbzeSzPz5893bBw0aZOrUqePxeK+99pqRZDZu3GiMMea3334zTqfTdOvWzRQUFLjrJk+ebCSZ6dOne9x/165dRpKZNWuWe1t6erq50MM8JSXFREVFmaNHj17Q/QGgtAwdOvScv8smTJhgJJm33nrLvS0vL8+0b9/eREREmOzsbGOMMRkZGUaSGT9+fJExGjdu7PF7/XzGjh1rJLkvXbp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hmJiYrR06VJlZWUVqe3evbvi4+OVkpKiRo0a6d13373gsdLT04vsl//+97/nfC4FBQX6xz/+oTvuuEPNmjUrcnv79u0lSWlpadqxY4f7Dwj4L87ZrySioqKUmJiob7/91uv77tixQzNnztRbb71V7HmPZxs8eLBatmypnTt3av78+e5Z/uLExcXprbfekvT7LMb06dPVo0cPffbZZ2ratKnXfRaeYx8fH+/1fQutWrVKAwcOVK9evfTKK69c8DgAUJH9cRGHGTNmKDU19Zz1t9xyi15//XV9+umn6t69exl358nlcik2NlZvv/12sbcXnlNeqPCPhKSkpFLr4YsvvtDMmTM1efJk3XvvvdqyZYvHu8xvvfWWUlNT1bdvX6WlpSk2NlaBgYEaO3as9uzZU2S8SZMm6dChQ9q+fbvGjh2r+++/3/166e1Y9957r2699VaPbUOGDDnnc5k2bZp+/PFHrVixotjb//SnP2n8+PEaM2aMrrjiCkv7B75F2K9Eevfurddee00bNmxw/2VuxahRo9SiRYtzno5ztqZNm6ply5bq16+
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA4BklEQVR4nO3deXhU5f3+8XuyI1nYIdEYVkWRTTaRIiiRRVBpXZCKEqWgFrBVQMR+NWJbQVELBQqyoyjKZiuIKCJRpMgmixREwFgBQWXLwpJtnt8f/jJlTAjPQCYzOXm/risX5MxnnvmcJzNn7jlz5ozLGGMEAAAAwHFCAt0AAAAAAP8g7AMAAAAORdgHAAAAHIqwDwAAADgUYR8AAABwKMI+AAAA4FCEfQAAAMChCPsAAACAQxH2AQAAAIci7AMAAAAORdivgPbt26eHHnpI9evXV1RUlGJjY9WhQwdNmDBBp0+fDnR7ATF9+nR16tRJtWvXVmRkpOrVq6cHHnhA3377baBbA1DBuFwuq5+0tLQy7ev7779Xv379dOWVVyomJkZVqlRR27ZtNXfuXBljyrQXAPbCAt0AytZ7772nu+66S5GRkbr//vt1zTXXKDc3V5999plGjBih//znP5o2bVqg2yxzW7ZsUb169XTbbbepatWqSk9P1/Tp07Vs2TJt27ZNCQkJgW4RQAXx+uuve/3+2muvaeXKlUWWX3XVVWXZlo4cOaIDBw7ozjvv1OWXX668vDytXLlSKSkp2r17t55//vky7QeAHZfh5XiFkZ6ermbNmumyyy7Txx9/rPj4eK/L9+7dq/fee09/+MMfAtRhcNm8ebNat26tMWPG6Mknnwx0OwAqqCFDhmjy5MlBu/f81ltv1erVq5WRkaHQ0NBAtwPgFziMpwJ58cUXlZ2drZkzZxYJ+pLUsGFDr6Dvcrn07LPPetWMGzdOLpdLnTt39ixLS0vzvK28detWr/qDBw8qNDRULpdLixYt8ixPSUnxeju6atWq6ty5s9asWVOkr3/84x9q0qSJIiMjlZCQoMGDB+vEiRNF6r799ttzvt19IerWrStJxd4WAASLH3/8UQMGDFDt2rUVFRWl5s2ba+7cuV41JW0ff7lN91XdunV16tQp5ebmllj37LPPltjDnDlzPLUpKSmKjo7WN998o27duqly5cpKSEjQc889V+RFj9vt1vjx49WkSRNFRUWpdu3aeuihh3T8+PEiPZQ0D788bPPEiRN67LHHVLduXUVGRuqyyy7T/fffryNHjkj633Pf2YdTff/996pbt65at26t7OxsSVJubq6eeeYZtWrVSnFxcapcubI6duyo1atXe93e7t27ddNNN6lOnTqKjIxUYmKiHn74YR07dsxTYztW4Xq+9NJLRebgmmuuKfY5/JeHhfXs2bPYHLB69Wp17NhRVatW9Zq/IUOGFLktBAcO46lAli5dqvr16+v666+/oOufOHFCY8aMOeflUVFRmj17tiZMmOBZNnfuXEVEROjMmTNF6mvUqKG//e1vkqQDBw5owoQJuuWWW7R//35VqVJF0s9PDqNHj1ZycrIeeeQR7d69W1OmTNHGjRu1du1ahYeHFx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yuVx69913JUlr1qzRgQMH1KdPn2Lr3W63jhw5oiNHjmjr1q167bXXFB8f7/nQ2UcffaTc3Fz98Y9/VEjI/+6qAwcOVGxsrN577z2v8QrfQo6MjLygdZSk999/X8uXL9fLL7+syy+/XCdPnrzgsQDA35YvX646deqob9++nmXh4eF69NFHPXveS1vfvn21cuVKvfnmm/rtb38rSX47k9vZh4YUHiqSm5urjz76SJK0cOFCxcXF6eabb/Y8nxw5ckStWrVSdHR0kcNbCt9ljoqKKvF2Fy9erObNm+vXv/51kct+eWio2+3W/fffr88//1zLly9XgwYNvC4PDQ1VRESEp/bYsWPKz89X69at9cUXXxQZPyMjQz/88INWrVql9957TzfccMMFj3Xq1CmveTly5IgKCgpKXPclS5Zo48aNGjt2bJHLsrKyJEnVq1cvcQwEFw7jqSBiY2Ml/e+B6qvU1FQlJCTooYce8jr2/mzh4eHq16+fZs2apTvvvFOzZs3SHXfc4bntX9q/f7/nLUBJio+P1+LFiz0vJv773/9Kkq688kqv60VERKh+/fqeywsVHlt/rhcjNm688UZJUo8ePXT77bfrmmuuUXR0NMciAghK//3vf9WoUSOvHSLS/87U88vtpI3Dhw97/R4XF+d1iE5SUpKSkpIk/Rz8Bw0apOTkZO3evdv6UB4bISEhql+/vteyK664QpI8x9fv2bNHGRkZqlWrVrFj/Pjjj16/Fx5vHxcXV+Jt79u3T3fccYdVn3/605/0+eefy+Vy6dSpU8XWzJ07Vy+//LK++uor5eXleZbXq1evSG23bt20fv16SVL37t319ttvX/BYqampSk1NLbK8du3axfZZUFCgp556Svfee6+aNWtW5PL27dtLkkaMGKExY8Z4PYcjeBH2K4jY2FglJCRox44dPl93165dmjNnjubNm1fssY9ne/DBB9WyZUvt3r1bCxcu9OzlL07t2rU1b948ST/vyZg1a5a6d++uzz77TE2bNvW5z8InqDp16vh83eI0aNBALVu21BtvvEHYB1Bh/PIEDrNnz1ZKSso56++8805Nnz5dn376qbp16+bn7ry53W7VqlVLb7zxRrGX/zKMFr5IKDwBQ2lYv3695syZo0mTJmnQoEHaunWr1zvM8+bNU0pKinr37q0RI0aoVq1aCg0N1ZgxY7Rv374i402cOFFHjhzRzp07NWbMGD388MOe50pfxxo0aJDuuusur2UDBw4857rMnDlT3377rT744INiL7/++us1btw4jR49WldffbXV/CDwCPsVSK9evTRt2jStW7fO8+rcxqhRo9SiRYtzHo5ztqZNm6ply5a
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvoAAAGGCAYAAAAKFyXyAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA3T0lEQVR4nO3deXxTZb7H8W/aNC10Q6ClVJBFQHYQBAcZBLWyiOKOUxXp6IAL6NULFRlHFp0RFe8IFxBB2dwQEFC2YUQsggwIgxYGQTarAoLI1tIC3fLcP7yNxLZwUpsmPf28X6+8NCe/PPk9aTj55uTkHIcxxggAAACArYQEugEAAAAA5Y+gDwAAANgQQR8AAACwIYI+AAAAYEMEfQAAAMCGCPoAAACADRH0AQAAABsi6AMAAAA2RNAHAAAAbIigDwAAANgQQb8K2rdvnx588EE1btxYERERiomJUdeuXTVx4kSdOXMm0O0FXH5+vlq2bCmHw6GXX3450O0AqGIcDoely5o1ayq0rx9++EH33nuvLrvsMkVHR6tGjRrq3Lmz5syZI2NMhfYCwBpnoBtAxVq+fLnuvPNOhYeH67777lPr1q2Vl5enzz77TKmpqfrqq680ffr0QLcZUJMmTdL3338f6DYAVFFvvfWW1/U333xTq1atKra8RYsWFdmWjh49qgMHDuiOO+7QJZdcovz8fK1atUopKSnatWuXnn/++QrtB8CFOQwfw6uMjIwMtW3bVvXq1dMnn3yiunXret2+d+9eLV++XP/1X/8VoA4D78iRI2rWrJmGDRumUaNGafz48Ro+fHig2wJQhQ0dOlR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ZNat++vSQpJydH9erV00033aSYmBht375dU6ZM0cGDB7V06VKfxvqt1q5dqxUrVhRbnpGRob59+6pu3boaNWqU4uLiJEkDBgwol8eFHxhUCZmZmUaSufnmmy3fR5IZPXq05/qTTz5p4uPjTceOHU337t09y9PS0owkk5ycbGrVqmVyc3M9tzVt2tTcfffdRpJZsGCBZ/nAgQNNgwYNvB5v+vTpRpLZtGmTMcaYI0eOGJfLZXr27GkKCws9dZMnTzaSzMyZM73uv2fPHiPJzJkzx7Ns9OjRxsrL3O12m86dO5vk5GRjjDEZGRlGkhk/fvwF7wsA/jRkyJBS12MTJkwwkszbb7/tWZaXl2e6dOlioqKiTFZWljHm/Ou0Vq1aea3TL2TcuHFGkudy3XXXme+///6C9ytaH//0009eyzdv3mwkmVmzZnmWDRw40Egyjz76qGeZ2+02ffv2NS6XyzPGunXrjCTzzjvveI25cuXKEpfv3r3bSDIvv/yyZ9n48eONJJORkeFZNmrUKCPJLFq0qNg83G63MeaX9760tDRz9uxZ06NHDxMfH2/27t3rVV9QUOD1vmiMMSdOnDB16tQx999/f2lPlzHGmEceecRERUX5PJYvf+9z51HkyiuvNH369CmWA6ZNm2YkmQ0bNniNKckMGTLkvHNBYLDrThWRlZUlSYqOji7T/Q8ePKhJkybpmWee8doSc66bbrpJDodDS5YskSStW7dOBw4c0F133VVivdvt1tGjR3X06FGlp6frzTffVN26dT0/MPv444+Vl5enxx9/XCEhv7xUBw0apJiYGC1fvtxrvKKvjcPDw32e3+zZs/Wf//xHL774os/3BYBAWbFihRISEpScnOxZFhYWpscee8yzxb28JScna9WqVXr33Xd19913S5Lfjth27u4gRbuH5OXl6eOPP5YkLViwQLGxsbr++us97ydHjx5Vx44dFRUVVWyXlqJvlyMiIs77uAsXLlS7du106623Frvt19/4ut1u3Xfffdq4caNWrFihSy+91Ov20NBQuVwuT+3x48dVUFCgK664Ql988UWx8TMzM/Xjjz9q9erVWr58ua6++uoyj3X69Gmv5+Xo0aMqLCw879wXLVqkzZs364UXXih226lTpyRJtWrVOu8YCB4E/SoiJiZG0i//SH01evRoJSYm6sEHHyy1JiwsTPfee69mzpwpSZo5c6Zuv/12z2P/2v79+xUXF6e4uDhdfvnl2rdvnxYuXOj5IPHdd99Jki677DKv+7lcLjVu3Nhze5Gi/fZL+yBSmqysLI0cOVKpqamqX7++T/cFgED67rvv1LRpU6+NIdIvR+T59XrSisOHD3tdfh3iGzRooKSkJCUnJ+udd95R48aNlZSUVO5hPyQkRI0bN/Za1qxZM0ny7E+/Z88eZWZmKj4+3vN+UnTJzs7WkSNHvO5ftH99bGzseR973759at26taU+n376ac2fP1+5ubk6ffp0iTVz5sxR27ZtFRERoVq1aikuLk7Lly9XZmZmsdpevXopISFBSUlJatGihebNm1fmsUaPHl3sefn6669LnUthYaH+/Oc/65577lHbtm2L3d6lSxdJUmpqqnbu3On58IDgxT76VURMTIwSExO1fft2n++7c+dOzZ49W2+//XaJ+zqe6/7779fll1+uXbt2acGCBZ6t+yWpU6eO3n77bUk/b8GYOXOmevfurc8++0xt2rTxuc/Dhw9LkhISEny638svv6y8vDzdddddnjePAwcOSJJOnDihb7/9VomJiZ6tKABgZ78+WMOsWbOUkpJSav0dd9yh119/XWvXrlWvXr383J03t9ut+Ph4vfPOOyXeXrQPeZGidXzDhg3LrYfPP/9cs2fP1uTJkzV48GClp6d7fbP89ttvKyUlRbfccotSU1MVHx+v0NBQjRs3Tvv27Ss23qRJk3T06FHt2LFD48aN00MPPeR5r/R1rMGDB+vOO+/0WjZo0KBS5zJjxgx9++23+uc//1ni7VdddZXGjx+vsWPHqmXLlpaeHwQWQb8KufHGGzV9+nRt2LDB86ncipEjR6p9+/al7oJzrjZt2ujyyy9X//79FRcXp2uuuabUr44jIiKUlJTkud6vXz/VrFl
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAv4AAAGGCAYAAAAD/IWIAAAAOXRFWHRTb2Z0d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89pOPHjxfp4Xy3w7fffutTe+LECT322GNKTExUeHi4ateurfvuu09Hjx6V9L/3v19ucvX9998rMTFRbdq08a5Mys3N1dNPP63WrVsrNjZWlSpVUocOHbRq1Sqf69u9e7duvPFG1apVS+Hh4apTp45+97vf6dixY94aq2MVzvOVV14pchs0bdq02PfxX2861rNnz2KzwKpVq9ShQwdVqVLF5/YbOnRoketCYGFTHwdZtGiRkpKSdN1115Xo8idOnFB6evo5z4+IiNCMGTP02muveZfNmjVLYWFhOnv2bJH66tWr69VXX5UkHTx4UK+99pp69OihAwcOqHLlypJ+fpMYM2aMUlJS9Pvf/167d+/W5MmTtXHjRq1bt06hoaFFxh08eLA6dOggSVq4cKH+/ve/W5rf6dOnFRMTo9OnT6tKlSrq37+/XnrpJUVFRVm6PAD4w5kzZ9SpUyft27dPQ4cO1ZVXXql58+YpNTVVJ06cKPItbv/+/dWjRw+fZaNHj7Z9nadOnVJ2drb+9a9/acaMGUpOTlZkZKSly0+ePNnntTUjI0NPP/10kbqCggJ169ZN//d//6eXX35Zy5YtU1pamvLz8/Xss8966x566CHNnDlT999/vx555BFlZGRo4sSJ2rJlyznfK355OyxdulRz5szxOT87O1sdOnTQrl27NHDgQF1zzTU6evSoPvzwQx08eFDVq1cvMqbb7Vb37t0VGhqqpUuXeueYlZWlt99+W/3799egQYN08uRJTZs2TV27dtWGDRvUsmVLSdKpU6dUu3Zt9erVSzExMdq+fbsmTZqkQ4cOadGiRbbGulhr1qzR0qVLiyzPyMhQz549FR8fr6efflo1atSQJN17772lcr0oYwaO4Ha7jSRz6623Wr6MJJOWlub9//HHHzdxcXGmdevWpmPHjt7lq1atMpJM//79TbVq1UxOTo73vAYNGpi77rrLSDLz5s3zLh8wYICpW7euz/VNmTLFSDIbNmwwxhjzww8/mLCwMNOlSxdTUFDgrZs4caKRZKZPn+5z+b179xpJZtasWd5laWlpxsrD/IknnjCjRo0y77//vpkzZ44ZMGCAkWTat29v8vLyLnh5AChLQ4YMOedr2fjx440kM3v2bO+y3Nxck5ycbKKiokxWVpYxxpiMjAwjyYwbN67IGE2aNPF5Xb+Q9PR0I8l7uummm8x///vfC16u8DX5xx9/9Fm+ceNGI8nMmDHDu6zwdXjYsGHeZR6Px/Ts2dOEhYV5x1i7dq2RZN59912fMZctW1bs8j179hhJ5pVXXvEuGzdunJFkMjIyvMuefvppI8ksXLiwyDw8Ho8x5n/vf6tWrTJnz541nTp1MnFxcWbfvn0+9fn5+T7vjcYYc/z4cVOzZk0zcODAc91cxhhjHn74YRMVFWV7LDv39y/nUejaa6813bt3L5IF3nrrLSPJrF+/3mdMSWbIkCHnnQv8j019HKJw85Xo6OgSXf7QoUOaMGGCnnrqqXOuAe/Vq5dcLpc+/PBDSdLatWt18OBB9e3bt9h6j8ejo0eP6ujRo9q6dav+9re/KT4+3vtjtU8++US5ubl69NFHFRT0v4fqoEGDFBMToyVLlviMV/gVc3h4uO35paena+zYserTp4/69eunmTNn6oUXXtC6det8NlECgECzdOlS1apVS/379/cuCw0N1SOPPOJdI1/a+vfvrxUrVui9997TXXfdJUlltle4X24+Urg5SW5urj755BNJ0rx58xQbG6vOnTt731OOHj2q1q1bKyoqqsgmMIXfQEdERJz3ehcsWKAWLVrotttuK3Lerzch9Xg8uu+++/T5559r6dKlqlevns/5wcHBCgsL89YeO3ZM+fn5atOmjTZv3lxkfLfbrSNHjmjlypVasmSJrr/++hKPdfr0aZ/b5ejRoyooKDjv3BcuXKiNGzdq7NixRc47efKkJKlatWrnHQOBieDvEDExMZL+94S1Ky0tTQkJCXrooYfOWRMaGqp77rlH06dPlyRNnz5dt99+u/e6f+3AgQOqUaOGatSooVatWmn//v1asGCB94PFd999J0m6+uqrfS4XFhampKQk7/mFCrf7L61Ncx577DEFBQV531wAIBB99913atCggc8KEul/e/z59WulFZmZmT6nX4f6unXrKiUlRf3799e7776rpKQkpaSklHr4DwoKUlJSks+yq666SpK82+Pv3btXbrdbcXFx3veUwlN2drZ++OEHn8sXbp9/ob227d+/X02bNrXU55NPPqkPPvhAOTk5On36dLE1s2bNUvPmzRUREaFq1aqpRo0aWrJkidxud5Harl27qlatWkpJSVGjRo30/vvvl3istLS0IrfL119/fc65FBQU6E9/+pPuvvtuNW/evMj5ycnJkqSRI0dq165d3g8TqBjYxt8hYmJilJCQoO3bt9u+7K5duzRz5kzNnj272O0kf2ngwIFq1aqVdu/erXnz5nnX/henZs2amj17tqSf125Mnz5d3bp102effaZmzZrZ7jMzM1OSVKtWLduXLU5kZKSqVavm86MqAHCCX+8AYsaMGUpNTT1n/R133KGpU6dqzZo16tq1axl358vj8SguLk7vvvtusecXboNeqPADQ2JiYqn18MUXX2jmzJmaOHGiBg8erK1bt/p8+zx79mylpqaqd+/eGjlypOLi4hQcHKz09HTt37+/yHgTJkzQ0aNHtXPnTqWnp+t3v/ud9/3S7liDBw/WnXfe6bNs0KBB55zLtGnT9O233+rjjz8u9vzrrrtO48aN05gxY9S4cWNLtw8CB8HfQW6++WZNmTJF69ev935it2L06NFq2bLlOTfZ+aVmzZqpVatW6tOnj2rUqKEbbrjhnF8zR0REKCUlxfv/LbfcoqpVq2rixIl66623VLduXUk
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAwcAAAGGCAYAAAAuK/0bAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA5j0lEQVR4nO3deXiU1fnG8XuyTBLIwpYQojEswYLseylFtgBBELUqiBsRCy6grQIi+tMIbQXUS6GAIMimIsiSuoEIIhRKUUBZqqQYMCqLQSCQhSXbnN8fTqaOCfAOZJgs3891zQV555kzz5k1d84779iMMUYAAAAAqjw/XzcAAAAAoHwgHAAAAACQRDgAAAAA4EQ4AAAAACCJcAAAAADAiXAAAAAAQBLhAAAAAIAT4QAAAACAJMIBAAAAACfCAQAAAABJhIMq6cCBA3rggQfUsGFDBQcHKzw8XF26dNG0adN09uxZX7fnMw6HQ7NmzVLr1q0VEhKi2rVrq2fPntq9e7evWwNQxdhsNkunjRs3XtG+jhw5orvvvlu/+c1vFBYWpho1aqhjx45atGiRjDFXtBcA3hHg6wZwZa1atUq33367goKCdO+996p58+bKz8/Xv/71L40dO1Zff/215syZ4+s2fWLYsGFavHix7r33Xo0aNUqnT5/Wzp079dNPP/m6NQBVzJtvvun28xtvvKF169aV2N60adMr2ZaOHz+uQ4cO6bbbbtM111yjgoICrVu3TklJSdq3b5+ef/75K9oPgLJnM0T9KiM9PV0tW7bU1VdfrU8//VT16tVzO3///v1atWqV/vSnP/moQ99ZtmyZBg8erJSUFN1yyy2+bgcA3IwaNUozZ84st3+dv/HGG7VhwwZlZWXJ39/f1+0AuAzsVlSFvPDCC8rNzdW8efNKBANJio+PdwsGNptNzz33nFvNiy++KJvNpu7du7u2bdy40bXEvWvXLrf6w4cPy9/fXzabTStWrHBtT0pKclsar1mzprp3767NmzeX6OvVV19Vs2bNFBQUpJiYGI0cOVKnTp0qUffdd9+dd+n9Yl5++WV17NhRt9xyixwOh06fPn3RywBAefHTTz/p/vvvV926dRUcHKxWrVpp0aJFbjUXeo389eu6p+rXr68zZ84oPz//gnXPPffcBXtYuHChqzYpKUmhoaH69ttv1bdvX1WvXl0xMTGaOHFiiZDkcDg0depUNWvWTMHBwapbt64eeOABnTx5skQPF7odvvvuO7faU6dO6bHHHlP9+vUVFBSkq6++Wvfee6+OHz8u6X/vf7/cvevIkSOqX7++2rdvr9zcXElSfn6+nn32WbVr104RERGqXr26unbtqg0bNrhd3759+9SzZ09FR0crKChIsbGxevDBB5WZmemqsTpW8TxfeumlErdB8+bNS30f//Vuav379y/1d4ENGzaoa9euqlmzptvtN2rUqBLXhYqH3YqqkA8++EANGzbU7373u0u6/KlTpzRp0qTznh8cHKwFCxZo2rRprm2LFi2S3W7XuXPnStTXqVNHr7zyiiTp0KFDmjZtmm644QYdPHhQNWrUkPTzG8mECROUkJCghx56SPv27dOsWbO0fft2bdmyRYGBgSXGHTFihLp27SpJSklJ0T/+8Y8Lzis7O1vbtm3Tww8/rKeeekrTp09Xbm6uGjRooMmTJ2vQoEEXvW0AwFfOnj2r7t27a//+/Ro1apQaNGig5cuXKykpSadOnSqxGjxkyBDdcMMNbtvGjx/v8XWePn1aubm5+uc//6kFCxaoc+fOCgkJsXT5WbNmKTQ01PVzenq6nn322RJ1RUVFSkxM1G9/+1u98MILWrNmjZKTk1VYWKiJEye66h544AEtXLhQ9913nx599FGlp6drxowZ2rlz53nfK355O6xevVpLlixxOz83N1ddu3ZVamqqhg0bprZt2+r48eN6//33dejQIdWpU6fEmFlZWerXr58CAwO1evVq1xyzs7P1+uuva8iQIRo+fLhycnI0b9489e3bV9u2bVPr1q0lSadPn9bVV1+tG2+8UeHh4frqq680c+ZMHT58WB988IFHY12uTZs2afXq1SW2p6enq3///qpXr56effZZRUZGSpLuueeeMrlelAMGVUJWVpaRZG666SbLl5FkkpOTXT8/8cQTJioqyrRr185069bNtX3Dhg1GkhkyZIipXbu2ycvLc53XuHFjc+eddxpJZvny5a7tQ4cONXFxcW7XN2fOHCPJbNu2zRhjzE8//WTsdrvp06ePKSoqctXNmDHDSDLz5893u3xaWpqRZBYtWuTalpycbC72MP/yyy+NJFO7dm1Tt25d8+qrr5rFixebjh07GpvNZj766KOL3lYA4E0jR44872vZ1KlTjSTz1ltvubbl5+ebzp07m9DQUJOdnW2MMSY9Pd1IMi+++GKJMZo1a+b2un4xkyZNMpJcp169epkffvjhopcrfk0+duyY2/bt27cbSWbBggWubUOHDjWSzCOPPOLa5nA4TP/+/Y3dbneNsXnzZiPJLF682G3MNWvWlLr9m2++MZLMSy+95Nr24osvGkkmPT3dte3ZZ581kkxKSkqJeTgcDmPM/97/NmzYYM6dO2e6d+9uoqKizP79+93qCwsL3d4bjTHm5MmTpm7dumbYsGHnu7mMMcY8/PDDJjQ01OOxPL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3++EhMT9a9//UstWrTwuM+MjAxJUnR0tEeXi4mJcfXza1FRUSooKNDp06cv+kYCAJXFrw9asWDBAiUlJZ23/rbbbtPcuXO1adMm9e3b18vduXM4HIqKitLixYtLPb94n/hixaGifv36ZdbD559/roULF2rGjBkaMWKEdu3a5baK/dZbbykpKUk333yzxo4dq6ioKPn7+2vSpEk6cOBAifGmT5+u48ePa+/evZo0aZIefPBB1/ulp2ONGDFCt99+u9u24cOHn3cu8+bN03fffaePP/6
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvsAAAGGCAYAAADl1U7MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA7YElEQVR4nO3dd3RUZf7H8c+khzRaQohAEIKCNOkbXIoSIYC9gIhCQIoKlhUishbKrguKZ5WfsNhoAoIgrKuCIFIUEWkKLFXAiPSehFDS5vn94cksYwLcAYZJbt6vc+ZI7v3OM9+5GWc+88zNMw5jjBEAAAAA2/HzdQMAAAAAvIOwDwAAANgUYR8AAACwKcI+AAAAYFOEfQAAAMCmCPsAAACATRH2AQAAAJsi7AMAAAA2RdgHAAAAbIqwDwAAANgUYb8U2r17t/r3768aNWooJCREkZGRuuWWWzR27FidPXvW1+35hMPhuODl9ttv93V7AEqZiz0nnX9Zvnz5Ne3rwIEDeuSRR3TjjTcqIiJCZcuWVfPmzTV16lQZY65pLwCsCfB1A7i25s+frwcffFDBwcHq0aOH6tWrp5ycHH333XdKTU3Vli1b9N577/m6zWtu2rRphbatW7dOY8eOVfv27X3QEYDS7I/PSR9++KEWL15caHudOnWuZVs6duyY9u3bpwceeEDVqlVTbm6uFi9erJSUFO3YsUP/+Mc/rmk/AC7NYXgrXmqkpaWpQYMGqlKlipYuXarKlS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ECeXl5alp06b68ccfC42fkZGhw4cPa8mSJZo/f75at2592WOdOXPG7bgcO3ZM+fn5F73v8+bN09q1azV69OhC+06dOiVJqlChwkXHQPFB2C8lIiMjJf3vf1JPDRs2THFxcerfv/8FawIDA/XII49o0qRJkqRJkybp/vvvd932H+3du1fR0dGKjo5Wo0aNtHv3bs2dO9f1ZmLPnj2SpBtvvNHtekFBQapRo4Zrf4GC8/gv9GbEqrlz5+rcuXOcwgOgRNizZ49q1arlNiki/W+lnj8+V1px6NAht8sfg3x8fLySkpLUrVs3zZgxQzVq1FBSUtJVD/x+fn6qUaOG27YbbrhBklzn1+/cuVMZGRmKiYlxvaYUXLKysnTkyBG36xecbx8VFXXR2969e7fq1atnqc8XX3xRs2fPVnZ2ts6cOVNkzdSpU9WgQQOFhISoQoUKio6O1vz585WRkVGotkOHDoqNjVVSUpLq1Kmjjz/++LLHGjZsWKHjsn379gvel/z8fP31r39V9+7d1aBBg0L7ExMTJUmpqanatm2b6w0Eii/O2S8lIiMjFRcXp82bN3t83W3btmnKlCmaPn16kec9nq93795q1KiRduzYoTlz5rhm+YtSqVIlTZ8+XdLvsxiTJk1ScnKyvvvuO9WvX9/jPg8dOiRJio2N9fi655sxY4aioqJ0xx13XNE4AFBS/XERh8mTJyslJeWC9Q888IDef/99ffvtt+rQoYOXu3PndDoVExOjGTNmFLm/4JzyAgVvEqpXr37Veli9erWmTJmicePGqV+/ftqwYYPbp8zTp09XSkqK7rnnHqWmpiomJkb+/v4aNWqUdu/eXWi8t99+W8eOHdPWrVs1atQoPf74467XS0/H6tevnx588EG3bX379r3gfZk4caJ+/fVXLVq0qMj9LVu21JgxYzRixAjddNNNlo4PfIuwX4rccccdeu+997Rq1SrXO3Mrhg4dqptvvvmCp+Ocr379+mrUqJG6dOmi6Oho3XrrrRf8CDkkJERJSUmun++66y6VL19e48aN07vvvqv4+HhJv69OcP7MTk5OjtLS0tyuK0lbt26Vw+E
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvoAAAGGCAYAAAAKFyXyAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA0RUlEQVR4nO3deXhT1dr+8TudoRNTC61wqFAURQYBQVQEpTIIKg6gOFFRQAV99UBFjj8tqMci+r7qAUVRJsGJ6TiBzKCIiBwUUEEErAoIIiIdGDpl/f7wag6xpexA26Sr38915ZLs/WTl2bsxubOys+MyxhgBAAAAsEqQvxsAAAAAUP4I+gAAAICFCPoAAACAhQj6AAAAgIUI+gAAAICFCPoAAACAhQj6AAAAgIUI+gAAAICFCPoAAACAhQj6AAAAgIUI+tXQzp07NXToUDVp0kQRERGKiYnRxRdfrBdeeEFHjx71d3t+M3v2bF144YWqVauW6tatqy5dumjBggX+bgtANeNyuRxdVq1aVal9/fLLL7r11lt19tlnKzo6WrVq1VKHDh00Y8YMGWMqtRcAzoT4uwFUrgULFqhfv34KDw/X7bffrvPOO0/5+fn69NNPlZaWpm+//VaTJ0/2d5uVbsKECbr//vvVu3dvjRs3TseOHdP06dPVp08fzZs3T9ddd52/WwRQTcycOdPr+uuvv66lS5eWWH7OOedUZls6cOCAdu/erRtuuEF/+9vfVFBQoKVLlyo1NVXbtm3TU089Van9ADg5l+FteLWRmZmpVq1aqWHDhlqxYoUSEhK81u/YsUMLFizQ//zP//ipQ/8566yzVKtWLa1bt04ul0uSlJ2drTPOOEOXX3653nvvPT93CKC6Gj58uF588cWAnTW/6qqrtHLlSmVlZSk4ONjf7QA4DofuVCPjx49Xbm6upkyZUiLkS1JycrJXyHe5XBozZoxXzTPPPCOXy6WuXbt6lq1atcrzUfLGjRu96vfs2aPg4GC5XC7NnTvXszw1NdXrI+jatWura9euWr16dYm+XnrpJbVo0ULh4eFKTEzUsGHDdOjQoRJ1P/744wk/4j6Z7OxsxcfHe9XGxMQoKipKNWrUOOntAcBf9u/frzvvvFP169dXRESEWrdurRkzZnjVlPX8+NfndF8lJSXpyJEjys/PL7NuzJgxZfYwffp0T21qaqqioqL0ww8/qEePHoqMjFRiYqIef/zxEm943G63nn/+ebVo0UIRERGqX7++hg4dqj/++KNED2Xthx9//NGr9tChQ3rwwQeVlJSk8PBwNWzYULfffrsOHDgg6b+vfccfQvXLL78oKSlJ7du3V25uriQpPz9fjz32mNq1a6fY2FhFRkaqc+fOWrlypdf9bdu2TZdffrkaNGig8PBwNWrUSHfffbcOHjzoqXE6VvF2PvvssyX2wXnnnVfqa/hfDwXr3bt3qTlg5cqV6ty5s2rXru21/4YPH17ivuB/HLpTjXzwwQdq0qSJLrroolO6/aFDh5SRkXHC9REREZo2bZpeeOEFz7IZM2YoLCxMx44dK1Ffr149Pffcc5Kk3bt364UXXtCVV16pXbt2qVatWpL+fGEYO3asUlJSdM8992jbtm2aNGmS1q9frzVr1ig0NLTEuEOGDFHnzp0lSfPnz9e///3vk25b165dNXfuXE2YMEFXXXWVjh07pgkTJigrK6tafsIBoGo4evSounbtqh07dmj48OE688wzNWfOHKWmpurQoUMlnr8GDBigK6+80mvZ6NGjfb7Pw4cPKzc3Vx9//LGmTZumTp06OZ4UmTRpkqKiojzXMzMz9dhjj5WoKyoqUs+ePXXhhRdq/PjxWrRokdLT01VYWKjHH3/cUzd06FBNnz5dd9xxh+6//35lZmZq4sSJ+uqrr074OnH8fli4cKHeeustr/W5ubnq3Lmztm7dqkGDBqlt27Y6cOCA3n//fe3evVv16tUrMWZWVpZ69eql0NBQLVy40LON2dnZeu211zRgwAANHjxYOTk5mjJlinr06KEvvvhCbdq0kSQdPnxYDRs21FVXXaWYmBh98803evHFF7Vnzx598MEHPo11uj755BMtXLiwxPLMzEz17t1bCQkJeuyxxxQXFydJuu2228rlflEBDKqFrKwsI8lcc801jm8jyaSnp3uuP/TQQyY+Pt60a9fOdOnSxbN85cqVRpIZMGCAqVu3rsnLy/Osa9asmbn55puNJDNnzhzP8oEDB5rGjRt73d/kyZONJPPFF18YY4zZv3+/CQsLM927dzdFRUWeuokTJxpJZurUqV633759u5FkZsyY4VmWnp5unDzMf/31V9OtWzcjyXOpV6+e+eyzz056WwCoSMOGDTvh89jzzz9vJJlZs2Z5luXn55tOnTqZqKgok52dbYwxJjMz00gyzzzzTIkxWrRo4fWcfjIZGRlez5XdunUzP//880lvV/x8/Ntvv3ktX79+vZFkpk2b5lk2cOBAI8ncd999nmVut9v07t3bhIWFecZYvXq1kWTeeOMNrzEXLVpU6vLvv//eSDLPPvusZ9kzzzxjJJnMzEzPsscee8xIMvPnzy+xHW632xjz39e+lStXmmPHjpmuXbua+Ph4s2PHDq/6wsJCr9dFY4z5448/TP369c2gQYNOtLuMMcbce++9JioqyuexfPl7H78dxTp27Gh69epVIge88sorRpJZu3at15iSzLBhw8rcFvgHh+5UE9nZ2ZKk6OjoU7r9nj17NGHCBD366KNeMzHHu+qqq+RyufT+++9LklavXq3du3frxhtvLLXe7XbrwIEDOnDggDZu3KjXX39dCQkJni+YLVu2TPn5+XrggQcUFPTfh+rgwYMVExNT4ow4xR8bh4eH+7x9NWvW1Nlnn62BAwdqzpw5mjp1qhISEnTddddpx44dPo8HAJVh4cKFatCggQYMGOBZFhoaqvvvv98z417eBgwYoKVLl+rNN9/UzTffLEkVdsa24w8HKT48JD8/X8uWLZMkzZkzR7Gxsbriiis8rycHDhxQu3btFBUVVeKQluJPlyMiIsq833nz5ql169a69tprS6z76+Ggbrdbt99+uz7//HMtXLhQTZs29VofHByssLAwT+3BgwdVWFio9u3b68svvywxflZWln799VctX75cCxYs0KWXXnrKYx05csRrvxw4cEBFRUVlbvv8+fO1fv16jRs3rsS6nJwcSVLdunXLHAOBg0N3qomYmBhJ//2f1Ffp6elKTEzU0KFDvY61P15oaKhuvfVWTZ06VTfccIOmTp2q66+/3nPff7Vr1y7Px36SlJCQoHnz5nneSPz000+SpLPPPtvrdmFhYWrSpIlnfbHi4/ZP9EakLP369VNISIjn41FJuuaaa9SsWTM98sgjeuedd3weEwAq2k8//aRmzZp5TYZI/z0jz1+fJ53Yt2+f1/XY2Fivw3IaN26sxo0bS/oz9A8ZMkQpKSnatm1buX6nKSgoSE2aNPFadtZZZ0mS53j67du3KysrS/Hx8aWOsX//fq/rxcfXx8bGlnnfO3fu1PXXX++oz0ceeUSff/65XC6Xjhw5UmrNjBkz9L//+7/67rvvVFBQ4Fl+5plnlqjt0aOH1q1bJ0nq2bNnidcfX8ZKT09Xenp6ieX169cvtc+ioiL94x//0C233KJWrVqVWN+pUydJUlpamjIyMrxewxGYCPrVRExMjBITE/XNN9/4fNutW7dq+vTpmjVrVqnHOh5v0KBBOv/887Vt2zbNmTPHM7tfmvr162vWrFmS/pzBmDp1qnr27KlPP/1ULVu29LnP4henBg0a+HS7H374QYsWLSpxWtE6derokksu0Zo1a3zuBQCqqr+erGHatGlKTU09Yf0NN9ygV199VZ988ol69OhRwd15c7vdio+P1xtvvFHq+r8G0eI3CElJSeXWw7p16zR9+nRNnDhRQ4YM0caNG70+WZ41a5ZSU1PVt29fpaWlKT4+XsHBwcrIyNDOnTtLjDdhwgQdOHBAW7ZsUUZGhu6++27Pa6WvYw0ZMkT9+vXzWjZ48OATbsuUKVP0448/avHixaWuv+iii/T
"text/plain": [
"<Figure size 800x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torchvision.transforms as transforms\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from PIL import Image, ImageOps\n",
"import cv2\n",
"\n",
"# Классы символов\n",
"CLASSES = \"ABEKMHOPCTYX0123456789\"\n",
"NUM_CLASSES = len(CLASSES)\n",
"\n",
"# Загрузка модели и весов\n",
"DEVICE = \"cpu\" # Или \"cuda\", если доступен GPU\n",
"model = SimpleCNN(NUM_CLASSES).to(DEVICE)\n",
"model.load_state_dict(torch.load(\"best_model.pth\", map_location=DEVICE)) # Загрузка весов\n",
"model.eval()\n",
"\n",
"# Преобразование для входных данных\n",
"transform = transforms.Compose([\n",
" transforms.ToTensor(),\n",
" transforms.Normalize((0.5,), (0.5,)) # Нормализация значений пикселей\n",
"])\n",
"\n",
"# Функция для получения топ-3 предсказаний\n",
"def get_top_predictions(output, top_k=3):\n",
" probabilities = torch.softmax(output, dim=1).squeeze() # Преобразование в вероятности\n",
" top_probs, top_indices = torch.topk(probabilities, top_k)\n",
" return top_probs.tolist(), [CLASSES[idx] for idx in top_indices.tolist()]\n",
"\n",
"# Функция для добавления отступов\n",
"def add_padding(img, padding=10):\n",
" \"\"\"\n",
" Добавляет отступы вокруг изображения.\n",
"\n",
" :param img: Массив numpy с изображением символа.\n",
" :param padding: Количество пикселей для отступа.\n",
" :return: Изображение с отступами.\n",
" \"\"\"\n",
" pil_img = Image.fromarray(img)\n",
" padded_img = ImageOps.expand(pil_img, border=padding, fill=0)\n",
" return np.array(padded_img)\n",
"\n",
"# Функция для утончения линий\n",
"def thin_lines(img, kernel_size=(2, 2), iterations=1):\n",
" \"\"\"\n",
" Утончает линии на изображении с помощью морфологической операции эрозии.\n",
"\n",
" :param img: Массив numpy с изображением символа.\n",
" :param kernel_size: Размер ядра эрозии.\n",
" :param iterations: Количество итераций эрозии.\n",
" :return: Изображение с утонченными линиями.\n",
" \"\"\"\n",
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)\n",
" eroded_img = cv2.erode(img, kernel, iterations=iterations)\n",
" return eroded_img\n",
"\n",
"# Функция распознавания символов с выводом графиков\n",
"# Функция для изменения размера изображения\n",
"# Функция для изменения размера изображения\n",
"def resize_to_model_input(img, target_size=(28, 28)):\n",
" \"\"\"\n",
" Изменяет размер изображения до требуемого для модели.\n",
"\n",
" :param img: Массив numpy с изображением символа.\n",
" :param target_size: Размер, до которого нужно привести изображение.\n",
" :return: Изображение с измененным размером.\n",
" \"\"\"\n",
" pil_img = Image.fromarray(img)\n",
" resized_img = pil_img.resize(target_size, Image.Resampling.LANCZOS) # Используем LANCZOS вместо ANTIALIAS\n",
" return np.array(resized_img)\n",
"\n",
"# Остальной код остается без изменений\n",
"\n",
"\n",
"# Функция распознавания символов с исправлением размера\n",
"def recognize_and_plot_characters(characters, model, device=\"cpu\", padding=5):\n",
" \"\"\"\n",
" Распознает символы из списка изображений characters и строит графики топ-3 предсказаний.\n",
"\n",
" :param characters: Список символов (массивы numpy 28x28).\n",
" :param model: Загруженная модель для распознавания.\n",
" :param device: Устройство для вычислений (\"cpu\" или \"cuda\").\n",
" :param padding: Отступы вокруг символа.\n",
" \"\"\"\n",
" for i, char in enumerate(characters):\n",
" # Добавление отступов\n",
" char_padded = add_padding(char, padding=padding)\n",
"\n",
" \n",
" char_selective = selective_dilation(char_padded, distance_threshold=4, iterations=1)\n",
" char_selective = thin_lines(char_selective, kernel_size=(2,2), iterations=1)\n",
" # Изменение размера для модели\n",
" char_resized = resize_to_model_input(char_selective, target_size=(28, 28))\n",
"\n",
" # Преобразование символа в тензор\n",
" char_tensor = transform(char_resized).unsqueeze(0).to(device) # Добавляем batch размерности\n",
" \n",
" # Прогон через модель\n",
" with torch.no_grad():\n",
" output = model(char_tensor)\n",
" top_probs, top_classes = get_top_predictions(output)\n",
"\n",
" # Вывод символа и графика топ-3 предсказаний\n",
" plt.figure(figsize=(8, 4))\n",
"\n",
" # Символ\n",
" plt.subplot(1, 2, 1)\n",
" plt.imshow(char_resized, cmap='gray')\n",
" plt.axis('off')\n",
" plt.title(f\"Символ {i + 1}\")\n",
"\n",
" # Топ-3 предсказания\n",
" plt.subplot(1, 2, 2)\n",
" plt.barh(top_classes, top_probs, color='blue')\n",
" plt.xlabel(\"Вероятность\")\n",
" plt.ylabel(\"Класс\")\n",
" plt.title(\"Топ-3 предсказания\")\n",
" plt.gca().invert_yaxis() # Инвертируем ось, чтобы лучший класс был сверху\n",
"\n",
" plt.tight_layout()\n",
" plt.show()\n",
"\n",
"\n",
"# Использование функции\n",
"if 'characters' in locals() and len(characters) > 0:\n",
" recognize_and_plot_characters(characters, model, device=DEVICE, padding=5)\n",
"else:\n",
" print(\"Список characters пуст.\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}