wko-projekt/API/yolo.ipynb

826 lines
1.8 MiB
Plaintext
Raw Normal View History

2023-02-01 00:34:29 +01:00
{
"cells": [
{
"cell_type": "markdown",
"id": "4b1d82bd",
"metadata": {},
"source": [
"# LICENSE PLATE DETECTION"
]
},
{
"cell_type": "markdown",
"id": "9b54ab67",
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "775443e2f8e6d780f7310e57d00701e7",
"grade": true,
"grade_id": "cell-53e7c09d33eaba20",
"locked": false,
"points": 6,
"schema_version": 3,
"solution": true,
"task": false
}
},
"source": [
"### YOLO V3"
]
},
{
"cell_type": "code",
2023-02-01 01:35:26 +01:00
"execution_count": null,
2023-02-01 00:34:29 +01:00
"id": "61ac9526",
2023-02-01 01:35:26 +01:00
"metadata": {
"pycharm": {
"is_executing": true
2023-02-01 00:34:29 +01:00
}
2023-02-01 01:35:26 +01:00
},
"outputs": [],
2023-02-01 00:34:29 +01:00
"source": [
"!git clone https://github.com/roboflow-ai/keras-yolo3"
]
},
{
"cell_type": "code",
2023-02-01 01:35:26 +01:00
"execution_count": null,
2023-02-01 00:34:29 +01:00
"id": "5d7010e7",
2023-02-01 01:35:26 +01:00
"metadata": {
"pycharm": {
"is_executing": true
2023-02-01 00:34:29 +01:00
}
2023-02-01 01:35:26 +01:00
},
"outputs": [],
2023-02-01 00:34:29 +01:00
"source": [
"!curl -L \"https://app.roboflow.com/ds/hTj8Pr7g7U?key=q9kdROYojM\" > roboflow.zip; unzip roboflow.zip; rm roboflow.zip"
]
},
{
"cell_type": "code",
2023-02-01 01:35:26 +01:00
"execution_count": null,
2023-02-01 00:34:29 +01:00
"id": "6989cf92",
2023-02-01 01:35:26 +01:00
"metadata": {
"pycharm": {
"is_executing": true
2023-02-01 00:34:29 +01:00
}
2023-02-01 01:35:26 +01:00
},
"outputs": [],
2023-02-01 00:34:29 +01:00
"source": [
"!wget https://pjreddie.com/media/files/yolov3.weights"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e981c95d",
"metadata": {},
"outputs": [],
"source": [
"from keras.layers import ELU, PReLU, LeakyReLU"
]
},
{
"cell_type": "code",
2023-02-01 01:35:26 +01:00
"execution_count": null,
2023-02-01 00:34:29 +01:00
"id": "d8924aad",
2023-02-01 01:35:26 +01:00
"metadata": {
"pycharm": {
"is_executing": true
2023-02-01 00:34:29 +01:00
}
2023-02-01 01:35:26 +01:00
},
"outputs": [],
2023-02-01 00:34:29 +01:00
"source": [
"!python keras-yolo3/convert.py keras-yolo3/yolov3.cfg yolov3.weights model_data/yolo.h5"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4038756b",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [],
"source": [
"\"\"\"\n",
"Self-contained Python script to train YOLOv3 on your own dataset\n",
"\"\"\"\n",
"\n",
"import numpy as np\n",
"import keras.backend as K\n",
"from keras.layers import Input, Lambda\n",
"from keras.models import Model\n",
"from keras.optimizers import Adam\n",
"from keras.callbacks import TensorBoard, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\n",
"\n",
"from yolo3.model import preprocess_true_boxes, yolo_body, tiny_yolo_body, yolo_loss\n",
"from yolo3.utils import get_random_data\n",
"\n",
"\n",
"def _main():\n",
" annotation_path = './train/_annotations.txt' # path to Roboflow data annotations\n",
" log_dir = './logs/000/' # where we're storing our logs\n",
" classes_path = './train/_classes.txt' # path to Roboflow class names\n",
" anchors_path = './model_data/yolo_anchors.txt'\n",
" class_names = get_classes(classes_path)\n",
" print(\"-------------------CLASS NAMES-------------------\")\n",
" print(class_names)\n",
" print(\"-------------------CLASS NAMES-------------------\")\n",
" num_classes = len(class_names)\n",
" anchors = get_anchors(anchors_path)\n",
"\n",
" input_shape = (256,256) # multiple of 32, hw default = (416,416)\n",
"\n",
" is_tiny_version = len(anchors)==6 # default setting\n",
" if is_tiny_version:\n",
" model = create_tiny_model(input_shape, anchors, num_classes,\n",
" freeze_body=2, weights_path='./model_data/tiny_yolo_weights.h5')\n",
" else:\n",
" model = create_model(input_shape, anchors, num_classes,\n",
" freeze_body=2, weights_path='./model_data/yolo.h5') # make sure you know what you freeze\n",
"\n",
" logging = TensorBoard(log_dir=log_dir)\n",
" checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5',\n",
" monitor='val_loss', save_weights_only=True, save_best_only=True, period=3)\n",
" reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1)\n",
" early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)\n",
"\n",
" val_split = 0.2 # set the size of the validation set\n",
" with open(annotation_path) as f:\n",
" lines = f.readlines()\n",
" np.random.seed(10101)\n",
" np.random.shuffle(lines)\n",
" np.random.seed(None)\n",
" num_val = int(len(lines)*val_split)\n",
" num_train = len(lines) - num_val\n",
"\n",
" # Train with frozen layers first, to get a stable loss.\n",
" # Adjust num epochs to your dataset. This step is enough to obtain a not bad model.\n",
" if True:\n",
" model.compile(optimizer=Adam(lr=1e-3), loss={\n",
" # use custom yolo_loss Lambda layer.\n",
" 'yolo_loss': lambda y_true, y_pred: y_pred})\n",
"\n",
" batch_size = 16\n",
" print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))\n",
" model.fit_generator(data_generator_wrapper(lines[:num_train], batch_size, input_shape, anchors, num_classes),\n",
" steps_per_epoch=max(1, num_train//batch_size),\n",
" validation_data=data_generator_wrapper(lines[num_train:], batch_size, input_shape, anchors, num_classes),\n",
" validation_steps=max(1, num_val//batch_size),\n",
" epochs=500,\n",
" initial_epoch=0,\n",
" callbacks=[logging, checkpoint])\n",
" model.save_weights(log_dir + 'trained_weights_stage_1.h5')\n",
"\n",
" # Unfreeze and continue training, to fine-tune.\n",
" # Train longer if the result is not good.\n",
" if True:\n",
" for i in range(len(model.layers)):\n",
" model.layers[i].trainable = True\n",
" model.compile(optimizer=Adam(lr=1e-4), loss={'yolo_loss': lambda y_true, y_pred: y_pred}) # recompile to apply the change\n",
" print('Unfreeze all of the layers.')\n",
"\n",
" batch_size = 16 # note that more GPU memory is required after unfreezing the body\n",
" print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))\n",
" model.fit_generator(data_generator_wrapper(lines[:num_train], batch_size, input_shape, anchors, num_classes),\n",
" steps_per_epoch=max(1, num_train//batch_size),\n",
" validation_data=data_generator_wrapper(lines[num_train:], batch_size, input_shape, anchors, num_classes),\n",
" validation_steps=max(1, num_val//batch_size),\n",
" epochs=100,\n",
" initial_epoch=50,\n",
" callbacks=[logging, checkpoint, reduce_lr, early_stopping])\n",
" model.save_weights(log_dir + 'trained_weights_final.h5')\n",
"\n",
" # Further training if needed.\n",
"\n",
"\n",
"def get_classes(classes_path):\n",
" '''loads the classes'''\n",
" with open(classes_path) as f:\n",
" class_names = f.readlines()\n",
" class_names = [c.strip() for c in class_names]\n",
" return class_names\n",
"\n",
"def get_anchors(anchors_path):\n",
" '''loads the anchors from a file'''\n",
" with open(anchors_path) as f:\n",
" anchors = f.readline()\n",
" anchors = [float(x) for x in anchors.split(',')]\n",
" return np.array(anchors).reshape(-1, 2)\n",
"\n",
"\n",
"def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,\n",
" weights_path='./model_data/yolo.h5'):\n",
" '''create the training model'''\n",
" K.clear_session() # get a new session\n",
" image_input = Input(shape=(None, None, 3))\n",
" h, w = input_shape\n",
" num_anchors = len(anchors)\n",
"\n",
" y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \\\n",
" num_anchors//3, num_classes+5)) for l in range(3)]\n",
"\n",
" model_body = yolo_body(image_input, num_anchors//3, num_classes)\n",
" print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))\n",
"\n",
" if load_pretrained:\n",
" model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)\n",
" print('Load weights {}.'.format(weights_path))\n",
" if freeze_body in [1, 2]:\n",
" # Freeze darknet53 body or freeze all but 3 output layers.\n",
" num = (185, len(model_body.layers)-3)[freeze_body-1]\n",
" for i in range(num): model_body.layers[i].trainable = False\n",
" print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))\n",
"\n",
" model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',\n",
" arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(\n",
" [*model_body.output, *y_true])\n",
" model = Model([model_body.input, *y_true], model_loss)\n",
"\n",
" return model\n",
"\n",
"def create_tiny_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,\n",
" weights_path='./model_data/tiny_yolo_weights.h5'):\n",
" '''create the training model, for Tiny YOLOv3'''\n",
" K.clear_session() # get a new session\n",
" image_input = Input(shape=(None, None, 3))\n",
" h, w = input_shape\n",
" num_anchors = len(anchors)\n",
"\n",
" y_true = [Input(shape=(h//{0:32, 1:16}[l], w//{0:32, 1:16}[l], \\\n",
" num_anchors//2, num_classes+5)) for l in range(2)]\n",
"\n",
" model_body = tiny_yolo_body(image_input, num_anchors//2, num_classes)\n",
" print('Create Tiny YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))\n",
"\n",
" if load_pretrained:\n",
" model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)\n",
" print('Load weights {}.'.format(weights_path))\n",
" if freeze_body in [1, 2]:\n",
" # Freeze the darknet body or freeze all but 2 output layers.\n",
" num = (20, len(model_body.layers)-2)[freeze_body-1]\n",
" for i in range(num): model_body.layers[i].trainable = False\n",
" print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))\n",
"\n",
" model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',\n",
" arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.7})(\n",
" [*model_body.output, *y_true])\n",
" model = Model([model_body.input, *y_true], model_loss)\n",
"\n",
" return model\n",
"\n",
"def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes):\n",
" '''data generator for fit_generator'''\n",
" n = len(annotation_lines)\n",
" i = 0\n",
" while True:\n",
" image_data = []\n",
" box_data = []\n",
" for b in range(batch_size):\n",
" if i==0:\n",
" np.random.shuffle(annotation_lines)\n",
" image, box = get_random_data(annotation_lines[i], input_shape, random=True)\n",
" image_data.append(image)\n",
" box_data.append(box)\n",
" i = (i+1) % n\n",
" image_data = np.array(image_data)\n",
" box_data = np.array(box_data)\n",
" y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)\n",
" yield [image_data, *y_true], np.zeros(batch_size)\n",
"\n",
"def data_generator_wrapper(annotation_lines, batch_size, input_shape, anchors, num_classes):\n",
" n = len(annotation_lines)\n",
" if n==0 or batch_size<=0: return None\n",
" return data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes)\n",
"\n",
"if __name__ == '__main__':\n",
" _main()"
]
},
{
"cell_type": "markdown",
"id": "214e7684",
"metadata": {},
"source": [
"# Prepare image to ocr"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "abe450c6",
"metadata": {},
"outputs": [],
"source": [
"import cv2 as cv\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 218,
"id": "f650aacf",
"metadata": {},
"outputs": [],
"source": [
"def grayscale(image):\n",
" return cv.cvtColor(image, cv.COLOR_BGR2GRAY)\n",
"\n",
"def noise_removal(image):\n",
" import numpy as np\n",
" kernel = np.ones((1, 1), np.uint8)\n",
" image = cv.dilate(image, kernel, iterations=1)\n",
" kernel = np.ones((1, 1), np.uint8)\n",
" image = cv.erode(image, kernel, iterations=1)\n",
" image = cv.morphologyEx(image, cv.MORPH_CLOSE, kernel)\n",
" image = cv.medianBlur(image, 3)\n",
" return (image)\n",
"\n",
"def thin_font(image):\n",
" import numpy as np\n",
" image = cv.bitwise_not(image)\n",
" kernel = np.ones((2,2),np.uint8)\n",
" image = cv.erode(image, kernel, iterations=1)\n",
" image = cv.bitwise_not(image)\n",
" return (image)\n",
"\n",
"def thick_font(image):\n",
" import numpy as np\n",
" image = cv.bitwise_not(image)\n",
" kernel = np.ones((2,2),np.uint8)\n",
" image = cv.dilate(image, kernel, iterations=1)\n",
" image = cv.bitwise_not(image)\n",
" return (image)\n",
"\n",
"def remove_borders(image):\n",
" contours, heiarchy = cv.findContours(image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)\n",
" cntsSorted = sorted(contours, key=lambda x:cv.contourArea(x))\n",
" cnt = cntsSorted[-1]\n",
" x, y, w, h = cv.boundingRect(cnt)\n",
" crop = image[y:y+h, x:x+w]\n",
" return (crop)"
]
},
{
"cell_type": "code",
"execution_count": 231,
"id": "9df02404",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAApcAAAEzCAYAAABkCSj0AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOx9d5hU1f3+e6fP7tI7SG8qoIig6FesRLArKGhsqEFEDQFLokZNbCFqjIoNFYMaDWJssaOigooFQUWkCahILyJsmZ16f3/s7z37uWfu7M4sOzszy32fh4edmVvOPfecz3k/9RimaZpw4MCBAwcOHDhw4KAe4Mp1Axw4cODAgQMHDhw0Hjjk0oEDBw4cOHDgwEG9wSGXDhw4cODAgQMHDuoNDrl04MCBAwcOHDhwUG9wyKUDBw4cOHDgwIGDeoNDLh04cODAgQMHDhzUGxxy6cCBAwcOHDhw4KDe4JBLBw4cOHDgwIEDB/UGh1w6cODAgQMHDhw4qDd40j3QMIxstsPBXg7DMGAYBhKJhPoMANxAyjAM6JtJ2X0HAB5P1bCOx+PweDyIRqOW391uN+LxeL0/gwMHDhw42Hvgcrng8XgQiUQAWNctfQ0DAK/Xi1gspr5zuVyWNY9roMvlgmmatR5nGIY6Vv8s7yuvy7/5+yWXXIILLrgApmnC5XLB6/UiGo0iFovB4/Fg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p0aJFOOGEEwAA77zzDgYOHAjAWpJPXs9uz/B04ZBLB40GUnDss88+ePLJJxGNRnH99dfj66+/zm3jHDhw4EADrUe0jEkZxlhAoPadaAidDHm9XktcoLyetLrp0Mk
"text/plain": [
"<Figure size 643.75x287.5 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"image_file = './img/img00.png'\n",
"img = cv.imread(image_file)\n",
"gray_image = grayscale(img)\n",
"thresh, im_bw = cv.threshold(gray_image, 100, 150, cv.THRESH_BINARY)\n",
"no_noise = noise_removal(im_bw)\n",
"# eroded_image = thin_font(no_noise)\n",
"# dilated_image = thick_font(eroded_image)\n",
"no_borders = remove_borders(no_noise)\n",
"cv.imwrite(\"temp/no_borders.jpg\", no_borders)\n",
"display('temp/no_borders.jpg')"
]
},
{
"cell_type": "code",
"execution_count": 169,
"id": "68bb5c6b",
"metadata": {},
"outputs": [],
"source": [
"def display(im_path):\n",
" dpi = 80\n",
" im_data = plt.imread(im_path)\n",
"\n",
" height, width = im_data.shape[:2]\n",
" \n",
" # What size does the figure need to be in inches to fit the image?\n",
" figsize = width / float(dpi), height / float(dpi)\n",
"\n",
" # Create a figure of the right size with one axes that takes up the full figure\n",
" fig = plt.figure(figsize=figsize)\n",
" ax = fig.add_axes([0, 0, 1, 1])\n",
"\n",
" # Hide spines, ticks, etc.\n",
" ax.axis('off')\n",
"\n",
" # Display the image.\n",
" ax.imshow(im_data, cmap='gray')\n",
"\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 170,
"id": "c57a2af5",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArUAAAGLCAYAAADZOGoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9aYxtS3YeiH0rIvbe5+Rw5/f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VU1gIAvolNmQscmcEZERJ5mrzAzmqMOnjJwC2N1mKwZDysrOzo0HAb/VuIfqRZhRY2tavWjCyBp1ol/JLMaMyQw7vy76vbJR59T55f5o5fFS7ogntJ4Ps/O2otP05xxoyXrxSoKRU6MqM6Y4InQdQi/yauh6Bbeh6FEik422bmXtZk6IkxhAjuSa7Mxrq3tKn2MfW/njjn3kyBzYtr+hY5SbPWOe6VSIiHbcRJ5H1XlV3i2Z8fZ7McYLTPPyHk1ntkerI5b3sDxmcv4xQe9jg9r16gBENkAAiMFZ3CQF1DYKqf1Nys7Yws05Y5oSOEaxhAvgEhAsM6PLgZq9BdlsznkYY1uZ3VCVAhjeeQzDIMo2RURlg9xyMem1zKqogwkRcqhb15GJmno3jhZMKAEg3wi+uVJqx0Y+X8G0MHRzNpYEPc9BiRoOXoGhuc+9UwPBHosInfOzhUHElVHTH0ZGisK6sjLx5IDsfN0ARch4AOpe1u+0LiVAQQBJ+AI5EiveOQE37OGoQ9eEMZAjMByYVWDlrGPPgAJ3Hzyc60WwMOReOUE+xaI89Vo2V143eSbAZ1IlruAc+8CQMhAqsM2QKGBWf+xZl3PZCoClELowp83hUgWU+8Boy+w
"text/plain": [
"<Figure size 673.75x375 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(image_file)"
]
},
{
"cell_type": "code",
"execution_count": 171,
"id": "d52ff256",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArUAAAGLCAYAAADZOGoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9WawuSZIeiH3m7hHx/2e5ee/NPbO2rL2K7H2pZldvQ5DDRcJohKE0EjkiMRAxxAygF0mApHnUAx8EQuCTpNkIjKSHIQYDaCQOG+R0c+lmL1XVVV3dXU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1+NZoelUZBznQNfjAK7F663qWNavatAsaZJQGNo1hocyVSUNSCxGG/gq0y7RrDICahVeTX0PZwzxS6K1eUkgF6FTu8JnjIoA5xjkdlxnpGzRh1jVBkbG4+prPVpnDEeRszzVLyz+/1hsfemaVoays4hhK46WHQRd76TaBNR8V4SCOQYCKF4wGVNLgFq3/cYNhuELojBQA7Oe/ig650a2UOrdWfrujFsbMwXckcdJHB6D6gAqv3ewqgTqw6Av7Q/nHNwarZcJYvqwYvzL95ZATSL5Mr5BEgSGV7h4plvvejTNMmcW0Q5i07J5rBDu+cIXVc15awGvO3VtTxuZXk1fC+D1/Zodc5Vz3lJ1hRnoKsRIP2xe7bf3tueNUOlGuK2P9vrmYHSYr7WYbGW7bZGDWetI75rMPyvczw2qN3v9ovFaTe2BrXrhzQvLdisOHmAbgiywYoQ4mL5KlIEQbx35GwjV+AiigDITMgpllCCWdXmjSIWT0EIQSz+aZbQvz6HhWDlYLUecgGXxTMEkdAG88wjlDkXL4kBFWY
"text/plain": [
"<Figure size 673.75x375 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"inverted_image = cv.bitwise_not(img)\n",
"cv.imwrite(\"temp/inverted.jpg\", inverted_image)\n",
"display(\"temp/inverted.jpg\")"
]
},
{
"cell_type": "code",
"execution_count": 172,
"id": "e653440c",
"metadata": {},
"outputs": [],
"source": [
"def grayscale(image):\n",
" return cv.cvtColor(image, cv.COLOR_BGR2GRAY)"
]
},
{
"cell_type": "code",
"execution_count": 203,
"id": "0985148f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gray_image = grayscale(img)\n",
"cv.imwrite(\"temp/gray.jpg\", gray_image)"
]
},
{
"cell_type": "code",
"execution_count": 204,
"id": "b54b6db2",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArUAAAGLCAYAAADZOGoBAAAAOX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RgYGEiYh6DN5USpG1wVVmFAjuP26DvHRNqUFZfTMntGeeBPztkgpsnZoxwo9fY8Zc4LZZ6AlnrqKLr7sHJkUDsyMlJQMru7u8lwdTJGDnJ1jaCWSsijQRGRIq5STKyPoIIAQaBJE0sj7f2iwuCyAr1T9bNMIVCJqj0qOY5TxZc0vXQSzMMMPpU7rzWbzZTDxOcETt0bpzDouvpOpetATtep1PW/5jOi6NGKyQW69R55TcajUqnE5uZmdpnKlSBpeZjCULvukDgvlL3HtniNdTq/Vir7udUuO+JFPROxD0rZPxluBw9SGDQS6iPp5MadQJ18zXZoTJnfzDZ6e3sLIFnXtRSsujwfLyIOGAQHgu6g5JwwLzS+Pp9uWHJg2Q2pG1tGoBzAuWOrdgjqc6kjuTG4TmE9HEcuLYPAI+Jg/rzLjebHaSYecMed4xJNpAddn5JWLgfUteQ9RVtJS+l26jDyvgNGLi0riqqoKnMme3p6Uj54X19fIY1MdTkftlqtAtjnSqN+BDS1v4FAlI44nU3KGQMLXE7mXDAS/aBgQXxdJiPsp7+n3/6ueNtBtvOf85Wvwno7HogpG6/4hIEtX62t1WpprmWbGIlX/8mjapNzwuCaFw8AUZ/knBrSO+d85sZKHmGfWJ9jmbL+6l5Oz+ZwEOfG72lslM2cTv+/KUcGteyUjJUmnR1VyS0hMFKj3CIRnobUGU1Mz5w9bgzLKeN2u53yhtQH3/wlgejv70+TISZ
"text/plain": [
"<Figure size 673.75x375 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(\"temp/gray.jpg\")"
]
},
{
"cell_type": "code",
"execution_count": 254,
"id": "90df13c4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 254,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"thresh, im_bw = cv.threshold(gray_image, 170, 210, cv.THRESH_BINARY)\n",
"cv.imwrite(\"temp/bw_image.jpg\", im_bw)"
]
},
{
"cell_type": "code",
"execution_count": 255,
"id": "d6f7feb2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wOIxAIKALK24o+EzGVTmBU5ldffYXrr78+4X0QIZROYlQnXUKGG264ASNHjnQ4jdH/Xbt2YeDAgVi3bl3Cu+CSZZ/Ph2g0ivz8fBXqi8ojcwr67Pf7te9DmlLQsfvvvx+LFi3CQw89pH7j/SkYDDrItMfjQV5eniMyg3wvBga5DENqDQwMDFKATsKWTOqWDDL0EkFHkiWZ4edK21J+3K3+uvtaloXDDjsMZ5xxhuv1OvTp0wf169cHUCFBpsgBHo8HmzZt0taDmwsQmSKJNH9m+Z0Tf7c2b9GiBU466SQ88MADCeYLbnB7j+3atcPBBx+sjaYQj8cxe/bsBLtdAifr/Dn4cf4OiZDq6qP7DQBWr16tTBW4RJffh39P1k/T2ZQZGGQrDKk1MDAwSAE6JyROsNIhBCShS0Zqdd8luCq+sussy0JRUZFDNc/rc+KJJ+KWW25xVdFXVo9oNAqfz6c87ivDggULEAgEUFBQgHg8jjlz5igiZtt7ohT06NHDQcikhJE7ntG1HTt2xPXXX4/p06dj4cKFCIVCCcQ4mZRdJ+FOZSOjI8QdO3Z0RDbgfUeSWsApxeZSV36+3LzIzQrXAMRiMWXGwduxa9eu2LFjBzZu3FjpezIwyCUYUmtgYGCQAmTIJimJ09lXuoFSmpLkUXetlABKAscdgQjJyovH43jrrbfQqlUrx7mk9qdYrJR1KhVIswkyH+AmE7Jt6PiQIUPwzDPPYMSIEdi0aRP69OnjeN6CggKsWrVKkXDe3tQO1JaS7DZp0gRfffUVunTpghUrVqhydeRY2rHScb/f75AmU7sTYSSyzK/jdsTXX389/vKXvzjKp4QORDjpXrxv0e9kjhAOhxPIKrUrmU3QpoL
"text/plain": [
"<Figure size 673.75x375 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(\"temp/bw_image.jpg\")"
]
},
{
"cell_type": "code",
"execution_count": 207,
"id": "fe31b009",
"metadata": {},
"outputs": [],
"source": [
"def noise_removal(image):\n",
" import numpy as np\n",
" kernel = np.ones((1, 1), np.uint8)\n",
" image = cv.dilate(image, kernel, iterations=1)\n",
" kernel = np.ones((1, 1), np.uint8)\n",
" image = cv.erode(image, kernel, iterations=1)\n",
" image = cv.morphologyEx(image, cv.MORPH_CLOSE, kernel)\n",
" image = cv.medianBlur(image, 3)\n",
" return (image)"
]
},
{
"cell_type": "code",
"execution_count": 208,
"id": "4e873cc8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 208,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"no_noise = noise_removal(im_bw)\n",
"cv.imwrite(\"temp/no_noise.jpg\", no_noise)"
]
},
{
"cell_type": "code",
"execution_count": 209,
"id": "a993d061",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArUAAAGLCAYAAADZOGoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOx9d5hUVbb9upU7kKNIFBVEiY35GVBRMfIUMIxhnlkxIYiiP8fwRiUOYxgdx1FHB3NCHJ+RMaAzI0qmkSwgOUnq7sr390d/6/S+p241VQ1NVXef9X18dFXdfE9YZ++197Zs27ZhYGBgYGBgYGBgUIfhyfUFGBgYGBgYGBgYGOwrDKk1MDAwMDAwMDCo8zCk1sDAwMDAwMDAoM7DkFoDAwMDAwMDA4M6D0NqDQwMDAwMDAwM6jwMqTUwMDAwMDAwMKjzMKTWwMDAwMDAwMCgzsOQWgMDAwMDAwMDgzoPQ2oNDAwMDAwMDAzqPHyZbmhZVm1eh0EDhmVZ0AvbeTwe2LYN27bh9XqRSCRydHUGBgYGuYdlWbAsC8lk0vH9wIED8d5778Hn88G2bSQSCdi2jbKyMrRr106Nr6Z4qEFdRyZt2FhqDXIONlTLsuDxVDZJDtwej8cQWgMDgwYP27bVuOj1etX3Ho8HPp8P0WhUjaU//PADzjrrL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OczOY1G1OoWSO85g8prQ2f/TRR5g7d64ivf/617+wZ88eWJaFYDCIu+66C6eccgqaNGniIPWSyDN7QSwWw+rVq7F8+XLH9dm2jZ07d+L777/HY489hhtvvBEtW7Z0BM3yupLJJKLRqJq
"text/plain": [
"<Figure size 673.75x375 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(\"temp/no_noise.jpg\")"
]
},
{
"cell_type": "code",
"execution_count": 210,
"id": "e175dc1d",
"metadata": {},
"outputs": [],
"source": [
"def thin_font(image):\n",
" import numpy as np\n",
" image = cv.bitwise_not(image)\n",
" kernel = np.ones((2,2),np.uint8)\n",
" image = cv.erode(image, kernel, iterations=1)\n",
" image = cv.bitwise_not(image)\n",
" return (image)"
]
},
{
"cell_type": "code",
2023-02-01 01:35:26 +01:00
"execution_count": null,
2023-02-01 00:34:29 +01:00
"id": "c77d5076",
2023-02-01 01:35:26 +01:00
"metadata": {
"pycharm": {
"is_executing": true
2023-02-01 00:34:29 +01:00
}
2023-02-01 01:35:26 +01:00
},
"outputs": [],
2023-02-01 00:34:29 +01:00
"source": [
"eroded_image = thin_font(no_noise)\n",
"cv.imwrite(\"temp/eroded_image.jpg\", eroded_image)"
]
},
{
"cell_type": "code",
"execution_count": 212,
"id": "0517c085",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArUAAAGLCAYAAADZOGoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOx9d5xU5fX+c6fvLlVAmgh2isKC2CMKdowFRbG3mG+ssdE0mmJioqCixm6KCWpUxG6wBLHFREVcBMQCUgQhKNVld/r8/tjf8+65Z+7szsIuM7O8z+fDh52Z2+9bnvec55zjZDKZDCwsLCwsLCwsLCxKGL5CX4CFhYWFhYWFhYXF1sK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UqVPNb88//zzuuece2y4sLIoEWhYkyW1Tx/FMJoPrr78exx13nCvgS5Izv9+PHj16GE0qF7nPPPMMbr75ZrzyyitmHolEIgBgCKoMPKOmltZSaaVNJBJIpVKGXHJOjMfjJiia9/z444/j4IMPRiAQMGMZxzFeRzQaRSaTMde8ceNGjBw50pzzgAMOwG233WaO4WVRlZ4syvV4rvLycsyaNQtnnHEGVq9e7VpQyIW/lR9YlCqaJD/Q+llpiWUnr6ysRDgcNr+TjGxNB5FuHC+LsV1Ntn6MGzcOoVDIfNbpczp16oQhQ4YAgFl4RaNRPPjgg1meAmuBsLDY9vCSHujvrrzySrRr1y6v4x1//PHYZ599AMAQTM4R7OdehpB+/fphhx12MN+TqBLpdNpICuR3JK+8XoJ/M7MD/54xYwamT5+OUCiEq6++GsOGDUP79u0bNNDQepvJZPDVV1/hsccewye
"text/plain": [
"<Figure size 673.75x375 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(\"temp/eroded_image.jpg\")"
]
},
{
"cell_type": "code",
"execution_count": 213,
"id": "9b077539",
"metadata": {},
"outputs": [],
"source": [
"def thick_font(image):\n",
" import numpy as np\n",
" image = cv.bitwise_not(image)\n",
" kernel = np.ones((2,2),np.uint8)\n",
" image = cv.dilate(image, kernel, iterations=1)\n",
" image = cv.bitwise_not(image)\n",
" return (image)"
]
},
{
"cell_type": "code",
2023-02-01 01:35:26 +01:00
"execution_count": null,
2023-02-01 00:34:29 +01:00
"id": "25bbdacd",
2023-02-01 01:35:26 +01:00
"metadata": {
"pycharm": {
"is_executing": true
2023-02-01 00:34:29 +01:00
}
2023-02-01 01:35:26 +01:00
},
"outputs": [],
2023-02-01 00:34:29 +01:00
"source": [
"dilated_image = thick_font(no_noise)\n",
"cv.imwrite(\"temp/dilated_image.jpg\", dilated_image)"
]
},
{
"cell_type": "code",
"execution_count": 215,
"id": "cff53158",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAA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17IxwO45577sGjjz6KvXv3phy7U6dO6NKli4McErJ8t5TWkbS+++67WLJkiRqT/v3vf6tAMampHT58OEaNGoW+ffs69ido6d20aRPWrFnjuCdebzKZxKJFizB9+nRceeWVGDBggLJUk8TSuksi6/F4cPTRR2PcuHF4+OGHa/VODAwKAVkpvnDVVVehbdu2+Oijj/Dss8+axPgGdYJjjz0WTZo0UZ9lcAdQOXDffvvtqKiowLvvvov169c7CK/cz8DAID/RuXNntGzZUn3W+7n8ftCgQZg4cSIAOAKkJLkjioqKMGHCBDz//PPYu3dvymJ3/fr1WL16NX70ox+p8+mZGEgyKY+wLAsrVqzASy+9hHfeecchgSJ4rN69e+PKK6/E8OHD1TFJoAEoi/HKlSuxa9eulAwM9DTx85NPPomePXuirKzMEVBGMq/ff+fOnTFu3Dg89NBDmb0QA4MCQlayH3z88cd4//33MWXKFFdRf20sZ9IFJSUOBg0DlmXh9ddfxxlnnKGij8PhsHIBckD3+XyIxWJ48skncffdd6u2drAiDQYGBrmBx+NRgU8ejwePPPIILrnkEoculGD/lsFcLEIQDAYVqdTTSJI4RqNR9OnTBxs3bkxJCWbbNs4++2y8/PLLymIaCAQcOl5pweX1de/eHRs2bHC9L5mX/euvv0b79u2VVZXEWG7n8/nQo0cPrFu3rtpiEdThTps2Dddcc43K8MIAWqb9krltLctCJBJBx44dEQ6HzeLeoOBQ59kP3PIFuoHJreuSQNxxxx246667sH//fhx77LEIhUJ1dmy
"text/plain": [
"<Figure size 673.75x375 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(\"temp/dilated_image.jpg\")"
]
},
{
"cell_type": "code",
"execution_count": 216,
"id": "18cd2910",
"metadata": {},
"outputs": [],
"source": [
"def remove_borders(image):\n",
" contours, heiarchy = cv.findContours(image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)\n",
" cntsSorted = sorted(contours, key=lambda x:cv.contourArea(x))\n",
" cnt = cntsSorted[-1]\n",
" x, y, w, h = cv.boundingRect(cnt)\n",
" crop = image[y:y+h, x:x+w]\n",
" return (crop)"
]
},
{
"cell_type": "code",
"execution_count": 217,
"id": "40842784",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAApcAAAEzCAYAAABkCSj0AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOx9d5hU1f3+e6fP7tI7SG8qoIig6FesRLArKGhsqEFEDQFLokZNbCFqjIoNFYMaDWJssaOigooFQUWkCahILyJsmZ16f3/s7z37uWfu7M4sOzszy32fh4edmVvOPfecz3k/9RimaZpw4MCBAwcOHDhw4KAe4Mp1Axw4cODAgQMHDhw0Hjjk0oEDBw4cOHDgwEG9wSGXDhw4cODAgQMHDuoNDrl04MCBAwcOHDhwUG9wyKUDBw4cOHDgwIGDeoNDLh04cODAgQMHDhzUGxxy6cCBAwcOHDhw4KDe4JBLBw4cOHDgwIEDB/UGh1w6cODAgQMHDhw4qDd40j3QMIxstsPBXg7DMGAYBhKJhPoMANxAyjAM6JtJ2X0HAB5P1bCOx+PweDyIRqOW391uN+LxeL0/gwMHDhw42Hvgcrng8XgQiUQAWNctfQ0DAK/Xi1gspr5zuVyWNY9roMvlgmmatR5nGIY6Vv8s7yuvy7/5+yWXXIILLrgApmnC5XLB6/UiGo0iFovB4/FgwIABcLvdFg5YUlJSa9+kTS4dOGhI6MTRjkSmIpuchPoEs7uuAwcOHDhwkClItmioIOnj+kKyxu8kAeTvqdY43aiir1mmaSojifydf8vPHo8H1157LeLxOFwuF9xuNwAgFovB5XLhyCOPxMCBAy3tBaDIaJ37J929xR3LpYNsojbLpd3xpmmiV69eaNq0KcLhMJYvX67Ol8fxOvo9HDhw4MCBg7qARIzk0u12J5E7Ejlp9HC73RbLYSpIzqWTTcMw4PF4EI/HkUgk0LFjR7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+wFqUMrjYMA+Xl5erY0047DVOmTFFChMcyKxyA0lLj8bgSNFLDdeDAgQMH9Qt9xxo9OUf3NOkuZ0LGSh5wwAH4+9//jng8jkAgoLb3LS4uVrWRCdM0EQgE1H2A6tAo6QGTa44ey8l28xyuMfSsBQIBhEIhi+vcgT0ccukgp5CC5ddff8W9996LK6+8Eq+++irWrVuHHTt2WOJ5Nm3ahK+++goDBgzAY489hnA4jM8//9xyLTsSaZeN6MCBAwcO6gd6co1dwo38zufz4YorrrC4zqV72+12o2fPnjjssMNUfUg9dtKuVI5MAtV3p9HXAUk25bX0OE22SZJiBzXDSehxkDeg9jh37lxce+21+PLLLwFYk34Yczlr1iyceOKJ2L17t6W+mV7PErAKPSkoHDhw4MBB/ULGt7dv3x6tWrWyrVVZXFyMd999V8XKyx1rTNNU7nNpDKCbPRqNKouitDomEglL4XCfz4dwOAyPx2ObRCTjLGVdTl6LJFcnxnu75dIpou6gIKCXitBJIb+Tn2WRXn0Iy+vZ/e7AgQMHDuoHMrlFl7VTp07FxIkTU5bwSSQS8Pv9iEQilu0HWa+SrmnWq5QubP7G+MxAIICKigpFGBnX6fP5VLKorIMpLZZ6JjsAC2HNdfkhOwut3TEMSagt4Sid69UEh1w6aFSw035r+uzAgQMHuYCuMJME6XGJuqfFTp7J2o4AFNHifXg9fUs/2RbAPlxIFhq3u69+vL7zTnFxMVauXJmSfPl8PmU1tOMQep1Lu+Qe/Tv9eLv+S3XNdK5bUxuzDd3IwvvG43FFktkePXFp0aJFOOGEEwAA77zzDgYOHAjAWpJPXs9uz/B04ZBLB40GUnDss88+ePLJJxGNRnH99dfj66+/zm3jHDhw4EADrUe0jEkZxlhAoPadaAidDHm9XktcoLyetLrp0Mk
"text/plain": [
"<Figure size 643.75x287.5 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"no_borders = remove_borders(no_noise)\n",
"cv.imwrite(\"temp/no_borders.jpg\", no_borders)\n",
"display('temp/no_borders.jpg')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.16"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}