{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 , 80.1ms\n",
      "Speed: 3.0ms preprocess, 80.1ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 806392 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 , 80.1ms\n",
      "Speed: 2.0ms preprocess, 80.1ms inference, 0.0ms postprocess per image at shape (1, 3, 480, 640)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 742276 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 , 100.1ms\n",
      "Speed: 2.0ms preprocess, 100.1ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 740580 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 , 76.1ms\n",
      "Speed: 1.0ms preprocess, 76.1ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 800144 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 , 85.1ms\n",
      "Speed: 2.0ms preprocess, 85.1ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 830504 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 , 74.3ms\n",
      "Speed: 2.0ms preprocess, 74.3ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 730384 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 , 87.6ms\n",
      "Speed: 1.0ms preprocess, 87.6ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 724324 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 , 73.1ms\n",
      "Speed: 2.0ms preprocess, 73.1ms inference, 1.0ms postprocess per image at shape (1, 3, 480, 640)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 751732 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from config import NUM_CLASSES, CRNN_WEIGHTS_PATH, YOLO_WEIGHTS_PATH\n",
    "from license_plate_recognizer import LicensePlateRecognizer\n",
    "\n",
    "def process_images_in_folder(folder_path, yolo_weights_path, crnn_weights_path):\n",
    "    lpr = LicensePlateRecognizer(\n",
    "        yolo_model_path=yolo_weights_path,\n",
    "        crnn_model_path=crnn_weights_path,\n",
    "        num_classes=NUM_CLASSES,\n",
    "        device=\"cpu\"\n",
    "    )\n",
    "    \n",
    "    image_files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]\n",
    "    if not image_files:\n",
    "        print(\"В указанной папке нет изображений.\")\n",
    "        return\n",
    "\n",
    "    for image_file in image_files:\n",
    "        image_path = os.path.join(folder_path, image_file)\n",
    "        image = cv2.imread(image_path)\n",
    "        if image is None:\n",
    "            print(f\"Не удалось загрузить изображение: {image_file}\")\n",
    "            continue\n",
    "        \n",
    "        detections = lpr.detect_and_recognize_frame(image, padding=5)\n",
    "        \n",
    "        display_image = image.copy()\n",
    "\n",
    "        for det in detections:\n",
    "            x1, y1, x2, y2 = det[\"bbox\"]\n",
    "            text = det[\"text\"]\n",
    "\n",
    "            cv2.rectangle(display_image, (x1, y1), (x2, y2), (0, 255, 0), 2)\n",
    "            cv2.putText(\n",
    "                display_image, text, (x1, y1 - 10),\n",
    "                cv2.FONT_HERSHEY_SIMPLEX,\n",
    "                0.7, (0, 255, 0), 2\n",
    "            )\n",
    "\n",
    "        height, width, _ = display_image.shape\n",
    "        black_bar_width = 300\n",
    "        black_bar = np.zeros((height, black_bar_width, 3), dtype=np.uint8)\n",
    "\n",
    "        y_start = 40\n",
    "        for i, det in enumerate(detections):\n",
    "            txt = det[\"text\"]\n",
    "            cv2.putText(\n",
    "                black_bar,\n",
    "                f\"Plate #{i+1}: {txt}\",\n",
    "                (10, y_start),\n",
    "                cv2.FONT_HERSHEY_SIMPLEX,\n",
    "                0.7, (255, 255, 255), 2\n",
    "            )\n",
    "            y_start += 40\n",
    "\n",
    "        display_image = np.hstack((display_image, black_bar))\n",
    "\n",
    "        display_image_rgb = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB)\n",
    "        plt.figure(figsize=(12, 8))\n",
    "        plt.imshow(display_image_rgb)\n",
    "        plt.title(f\"Processed: {image_file}\")\n",
    "        plt.axis(\"off\")\n",
    "        plt.show()\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    folder_path = \"img\"  # Укажите путь к папке с изображениями\n",
    "\n",
    "    process_images_in_folder(folder_path, YOLO_WEIGHTS_PATH, CRNN_WEIGHTS_PATH)\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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
