wko/wko-10.ipynb

1716 lines
5.7 MiB
Plaintext
Raw Normal View History

2023-01-16 09:35:16 +01:00
{
2023-01-30 13:54:58 +01:00
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"id": "nMai_g_dwTOS",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nMai_g_dwTOS",
"outputId": "6a94ccca-7390-459d-e5a6-7cc5c98e6364"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cloning into 'wko'...\n",
"remote: Enumerating objects: 102, done.\u001b[K\n",
"remote: Counting objects: 100% (102/102), done.\u001b[K\n",
"remote: Compressing objects: 100% (100/100), done.\u001b[K\n",
"remote: Total 102 (delta 2), reused 102 (delta 2)\u001b[K\n",
"Receiving objects: 100% (102/102), 126.67 MiB | 734.00 KiB/s, done.\n",
"Resolving deltas: 100% (2/2), done.\n"
]
}
],
"source": [
"!git clone https://git.wmi.amu.edu.pl/s444417/wko"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "EuRiyPQMw0RP",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EuRiyPQMw0RP",
"outputId": "a6afa182-1b77-4915-baae-56938b01d14a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/content/wko\n"
]
}
],
"source": [
"%cd /content/wko"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "XeRQ_YVew3g2",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XeRQ_YVew3g2",
"outputId": "4488091a-21ef-456a-9288-27d82963f928"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/content/wko\n"
]
}
],
"source": [
"!pwd"
]
},
{
"cell_type": "markdown",
"id": "3c8a4b52",
"metadata": {
"id": "3c8a4b52"
},
"source": [
"![Logo 1](img/aitech-logotyp-1.jpg)\n",
"<div class=\"alert alert-block alert-info\">\n",
"<h1> Widzenie komputerowe </h1>\n",
"<h2> 10. <i>Metody głębokiego uczenia (2)</i> [laboratoria]</h2> \n",
"<h3>Andrzej Wójtowicz (2021)</h3>\n",
"</div>\n",
"\n",
"![Logo 2](img/aitech-logotyp-2.jpg)"
]
},
{
"cell_type": "markdown",
"id": "783d6d64",
"metadata": {
"id": "783d6d64"
},
"source": [
"W poniższym materiale zobaczymy w jaki sposób korzystać z wytrenowanych modeli sieci neuronowych w zagadnieniach związanych z wykrywaniem wielu obiektów, szacowaniem pozy człowieka, wykrywaniem i rozpoznawaniem tekstu oraz super rozdzielczością.\n",
"\n",
"Uwaga: realizacja poniższych treści będzie wymagała pobrania ok. 700 MB danych.\n",
"\n",
"Na początku załadujmy niezbędne biblioteki:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "ef18510f",
"metadata": {
"id": "ef18510f"
},
"outputs": [],
"source": [
"import cv2 as cv\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"id": "e45afc56",
"metadata": {
"id": "e45afc56"
},
"source": [
"Pobrane pliki będziemy zapisywać w katalogu `dnn`:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "7aac31ef",
"metadata": {
"id": "7aac31ef"
},
"outputs": [],
"source": [
"!mkdir -p dnn"
]
},
{
"cell_type": "markdown",
"id": "f792eb4f",
"metadata": {
"id": "f792eb4f"
},
"source": [
"# Wykrywanie obiektów\n",
"\n",
"## SSD\n",
"\n",
"W poprzednich materiałach korzystaliśmy z [SSD](https://arxiv.org/pdf/1512.02325.pdf) do wykrywania wielu twarzy na zdjęciu. W poniższym przykładzie możemy zobaczyć użycie do wykrywania wielu obiektów - sieć została wytrenowana na zbiorze [Common Objects in Context](https://cocodataset.org/) (COCO). Użyjemy modelu dostępnego dla frameworku [Tensorflow](https://github.com/tensorflow/models/tree/master/research/object_detection) (inne modele możemy znaleźć w [Detection Model Zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md)):"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "aa10b6fa",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aa10b6fa",
"outputId": "0277eb03-d371-4993-fd11-2bd3c4b275f3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dnn/ssd_mobilenet_v 100%[===================>] 179.22M 252MB/s in 0.7s \n"
]
}
],
"source": [
"!wget -q --show-progress -O dnn/ssd_mobilenet_v2_coco_2018_03_29.tar.gz http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz\n",
"!cd dnn && tar xzf ssd_mobilenet_v2_coco_2018_03_29.tar.gz && rm ssd_mobilenet_v2_coco_2018_03_29.tar.gz"
]
},
{
"cell_type": "markdown",
"id": "99ec1efa",
"metadata": {
"id": "99ec1efa"
},
"source": [
"Pobraliśmy model i generujemy konfigurację:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "eac9a8da",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "eac9a8da",
"outputId": "f0fe207f-d984-4ca2-c7c4-af60c082994c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dnn/ssd_mobilenet_v 100%[===================>] 17.88K --.-KB/s in 0.001s \n",
"dnn/ssd_mobilenet_v 100%[===================>] 9.82K --.-KB/s in 0s \n",
"tf_text_graph_ssd.py:273: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
" for label in ['ClassPredictor', 'BoxEncodingPredictor' if box_predictor is 'convolutional' else 'BoxPredictor']:\n",
"tf_text_graph_ssd.py:278: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
" if box_predictor is 'convolutional':\n",
"tf_text_graph_ssd.py:311: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
" if box_predictor is 'convolutional':\n",
"Scale: [0.200000-0.950000]\n",
"Aspect ratios: [1.0, 2.0, 0.5, 3.0, 0.333299994469]\n",
"Reduce boxes in the lowest layer: True\n",
"Number of classes: 90\n",
"Number of layers: 6\n",
"box predictor: convolutional\n",
"Input image size: 300x300\n"
]
}
],
"source": [
"!wget -q --show-progress -O dnn/ssd_mobilenet_v2_coco_2018_03_29/tf_text_graph_ssd.py https://raw.githubusercontent.com/opencv/opencv/4.5.3/samples/dnn/tf_text_graph_ssd.py\n",
"!wget -q --show-progress -O dnn/ssd_mobilenet_v2_coco_2018_03_29/tf_text_graph_common.py https://raw.githubusercontent.com/opencv/opencv/4.5.3/samples/dnn/tf_text_graph_common.py\n",
"!cd dnn/ssd_mobilenet_v2_coco_2018_03_29 && python3 tf_text_graph_ssd.py --input frozen_inference_graph.pb --output net.pbtxt --config pipeline.config"
]
},
{
"cell_type": "markdown",
"id": "232e2987",
"metadata": {
"id": "232e2987"
},
"source": [
"Wczytujemy model:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "9b4180e5",
"metadata": {
"id": "9b4180e5"
},
"outputs": [],
"source": [
"model = cv.dnn.readNetFromTensorflow(\"dnn/ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb\",\n",
" \"dnn/ssd_mobilenet_v2_coco_2018_03_29/net.pbtxt\")"
]
},
{
"cell_type": "markdown",
"id": "0bbfd2a4",
"metadata": {
"id": "0bbfd2a4"
},
"source": [
"Pobieramy i wczytujemy etykiety klas obiektów:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "17335a42",
"metadata": {
"id": "17335a42"
},
"outputs": [],
"source": [
"!wget -q -O - https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/mscoco_complete_label_map.pbtxt | grep display_name | grep -o '\".*\"' | tr -d '\"' > dnn/ssd_mobilenet_v2_coco_2018_03_29/coco-labels.txt"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "662e1a33",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "662e1a33",
"outputId": "e8749ad7-6f3f-4d79-a98f-418d4121159a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"91 ['background', 'person', 'bicycle', 'car', 'motorcycle']\n"
]
}
],
"source": [
"with open('dnn/ssd_mobilenet_v2_coco_2018_03_29/coco-labels.txt', 'r') as f_fd:\n",
" classes = f_fd.read().splitlines()\n",
" \n",
"print(len(classes), classes[:5])"
]
},
{
"cell_type": "markdown",
"id": "94cace8a",
"metadata": {
"id": "94cace8a"
},
"source": [
"Spróbujemy sprawdzić jakie obiekty znajdują się na poniższym zdjęciu:"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "91834aba",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 295
},
"id": "91834aba",
"outputId": "d1b3927e-9842-4462-db61-a3d004817e1e"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAa8AAAEWCAYAAADRrhi8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9z45lS7Lm9TMz97Ujs6ovogUtJGDAACQGjJDgCZCYMQUeoEc8AM/SA8Y8QUs8QDPpMUigFhKikRB06/btWycz9nJ3MwZmvtaOPKeqQXDUp3RjVcWJzIwde68/5mafffaZuUQEn8fn8Xl8Hp/H5/HndOi/7BP4PD6Pz+Pz+Dw+j/+3x2fw+jw+j8/j8/g8/uyOz+D1eXwen8fn8Xn82R2fwevz+Dw+j8/j8/izOz6D1+fxeXwen8fn8Wd3fAavz+Pz+Dw+j8/jz+741YKXiPynIvI/icg/EpH/+tf6nM/j8/g8Po/P42/eIb9Gn5eIGPA/A/8J8I+Bfwj8FxHxP/7//mGfx+fxeXwen8ffuOPXyrz+I+AfRcT/EhEn8N8C/9mv9Fmfx+fxeXwen8ffsKP9Su/7bwL/28vf/zHwH7++QET+LvB368//4Ze3B4IgAiAEmRG6Ox6Be+ARUJnia8K4fyf/n9/rJ4jI64vyiO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gnwx1mTUn9MRJXJ45bJF7uARRWFIZBCNlZnUdmZzOnM4cwRrBSMEb+mc0Sgjjgul57Wkk0qqKRfVK6evmueUJ/Bzak2kgp1uA4xK7Yt2jDsg5
"text/plain": [
"<Figure size 504x504 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"image = cv.imread('img/messi5.jpg')\n",
"plt.figure(figsize=[7,7])\n",
"plt.imshow(image[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "43774ae3",
"metadata": {
"id": "43774ae3"
},
"source": [
"Sieć zwraca nam listę obiektów z oznaczeniem współrzędnych na zdjęciu oraz identyfikatorem obiektu (ustawiliśmy próg odcięcia na 0.5):"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "84652c91",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 417
},
"id": "84652c91",
"outputId": "8035b80f-5621-4b02-e3af-63f918a8730e"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAsYAAAHECAYAAAA6SOwjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9za5sy9IdNCJy1jrn2limQwtoWAgeAZ4AiR5d4AHc4gF4Fjdo8wSWeANkt0ECWUgI06P1fZ/vvXtVZgSNMSIyZ621zwXZVz4SNXXqrLVrVc2ZvxEjRvykZSbe1/t6X+/rfb2v9/W+3tf7+v/75f+2G/C+3tf7el/v6329r/f1vt7X7+F6A+P39b7e1/t6X+/rfb2v9/W+8AbG7+t9va/39b7e1/t6X+/rfQF4A+P39b7e1/t6X+/rfb2v9/W+ALyB8ft6X+/rfb2v9/W+3tf7el8A3sD4fb2v9/W+3tf7el/v6329LwB/RWBsZv+Fmf2vZvYvzOy/+2s95329r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h0rAh4EMink4iwDNgg6WIJZNzlp7shYjVOadM5TyM/ew+bUwmkoYF/aJ7XCtmdbMsCECuQbsiDEDzl0NmWHh8RnCTEpFvNYJHcg2XoCPCuLOJCctuO9WX7vTtrz3lo41JA2Qy4hpEQ8CFSr2RcIswQ30C48/prAeN/DuA/NrN/BALi/wrAf/OzD5sZLeqLrIcblbVL8CIOQImtrLbSSgmTJYD8VaF997vu1p/fwiFrt9w+m9pQZfHe7xHaWK2fj/YGgU5P7O7H2
"text/plain": [
"<Figure size 864x864 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"height, width, _ = image.shape\n",
"\n",
"image_blob = cv.dnn.blobFromImage(image=image, scalefactor=1, size=(300, 300), mean=(0,0,0), \n",
" swapRB=True, crop=False)\n",
"\n",
"model.setInput(image_blob)\n",
"detections = model.forward()\n",
"\n",
"image_out = image.copy()\n",
"\n",
"for i in range(detections.shape[2]):\n",
" confidence = detections[0, 0, i, 2]\n",
" if confidence > 0.5:\n",
"\n",
" box = detections[0, 0, i, 3:7] * np.array([width, height, width, height])\n",
" (x1, y1, x2, y2) = box.astype('int')\n",
" \n",
" class_id = int(detections[0, 0, i, 1])\n",
"\n",
" cv.rectangle(image_out, (x1, y1), (x2, y2), (0, 255, 0), 6)\n",
" label = '{:} ({:.3f})'.format(classes[class_id], confidence)\n",
" label_size, base_line = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.65, 1)\n",
" cv.rectangle(image_out, (x1, y1 - label_size[1]), (x1 + label_size[0], y1 + base_line), \n",
" (255, 255, 255), cv.FILLED)\n",
" cv.putText(image_out, label, (x1, y1), cv.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0))\n",
" \n",
"plt.figure(figsize=[12,12])\n",
"plt.imshow(image_out[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "3fa16e91",
"metadata": {
"id": "3fa16e91"
},
"source": [
"## YOLOv4"
]
},
{
"cell_type": "markdown",
"id": "27ce3522",
"metadata": {
"id": "27ce3522"
},
"source": [
"Innym popularnym modelem do wykrywania obiektów jest [You Only Look Once](https://github.com/AlexeyAB/darknet) (YOLO). Porównując YOLO do innych sieci, model ten nie analizuje poszczególnych regionów, ale patrzy na obraz całościowo, co w pewien sposób stanowi balans między szybkością a precyzją. Ze względu na tę cechę model ten dobrze nadaje się do wykrywania obiektów w czasie rzeczywistym. Model powinien dobrze sobie radzić gdy zostanie mu przedstawiona nieznana wcześniej reprezentacja obiektu (np. zacieniony) lub gdy obiekt znajduje się w otoczeniu innych nieoczekiwanych obiektów.\n",
"\n",
"YOLO jest dostępne w kilku wersjach, natomiast my sprawdzimy jak sobie radzi wersja kompaktowa:"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "4c3e7fb1",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4c3e7fb1",
"outputId": "828fa7fa-e873-44d0-e424-299c041bc039"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dnn/yolo_v4_tiny/yo 100%[===================>] 23.13M 154MB/s in 0.2s \n",
"dnn/yolo_v4_tiny/yo 100%[===================>] 3.16K --.-KB/s in 0s \n",
"dnn/yolo_v4_tiny/co 100%[===================>] 625 --.-KB/s in 0s \n"
]
}
],
"source": [
"!mkdir -p dnn/yolo_v4_tiny\n",
"!wget -q --show-progress -O dnn/yolo_v4_tiny/yolov4-tiny.weights https://github.com/AlexeyAB/darknet/releases/download/yolov4/yolov4-tiny.weights\n",
"!wget -q --show-progress -O dnn/yolo_v4_tiny/yolov4-tiny.cfg https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny.cfg\n",
"!wget -q --show-progress -O dnn/yolo_v4_tiny/coco.names https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/coco.names"
]
},
{
"cell_type": "markdown",
"id": "9497b09c",
"metadata": {
"id": "9497b09c"
},
"source": [
"Wczytujemy model:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "e8cc6a3a",
"metadata": {
"id": "e8cc6a3a"
},
"outputs": [],
"source": [
"model = cv.dnn.readNetFromDarknet(\"dnn/yolo_v4_tiny/yolov4-tiny.cfg\", \n",
" \"dnn/yolo_v4_tiny/yolov4-tiny.weights\")"
]
},
{
"cell_type": "markdown",
"id": "df331450",
"metadata": {
"id": "df331450"
},
"source": [
"Wczytujemy etykiety obiektów:"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "8f01d354",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8f01d354",
"outputId": "a15a642c-3de2-47a6-8446-f44c7fd86fa2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"80 ['person', 'bicycle', 'car', 'motorbike', 'aeroplane']\n"
]
}
],
"source": [
"with open('dnn/yolo_v4_tiny/coco.names', 'r') as f_fd:\n",
" classes = f_fd.read().splitlines()\n",
" \n",
"print(len(classes), classes[:5])"
]
},
{
"cell_type": "markdown",
"id": "3fc5e3fc",
"metadata": {
"id": "3fc5e3fc"
},
"source": [
"Przetestujemy działanie na poniższym zdjęciu:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "df65dee0",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 432
},
"id": "df65dee0",
"outputId": "60720513-7cb9-47b1-961d-7db9a4757774"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAU0AAAGfCAYAAADbBoDgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9eehlW3bf91l773Pu8BtqfFVv7snd7rYlGUXBchKwBcHYCQkygZgoJoMJiJD4j4AJMeSP/BMSB0KIIRAiiEn8T6yQQAZFYDJgt5EtW0rLUkdDS3rq97req1fjr37THc45e6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BECKlwKBbgB3ZRgWjhOA3QiAQ6IvSiJGoYFtD7DJqsbiWyahtBvw8K00pjEEhhEaNBDRt5M5773J6eYqKcvPmDa5evYqoOLDvXAIb/9sD516+E9mojfVjfZ5CZZMU633StxaIGKUqD3UzGbVKZ6UUXLEIElAtFeRgNm145doxFwa6WnhgNrgFFxQivr41Ea20vOAn5Nk89Rxdx3Dlxv85iKKKDQM2rLDLM4TB4w/9yhNSQgIdAPFswDRFG8jRMCIhQg5C1kAsYLlQSsZsV+t9Wl5O0HxG3PdBjapVrpfWbwyQgImRBTqJDBLJBEwiIQqdGZarH3KcTTd8zc28C4SN4zoQSBgpSDX3t+g1vosiHiEXc/NcqAPPqtnu/NBSCrpac3QwZz1c8OjsDEnC+dk5X/zSl5jPZtVBzjN0qz1m7uW7JAKIbqh7IUWyGiG4HmLU4GUFy6wVJCtwSqnrRypZvVBUoDniQuacdOdMeiN6S
"text/plain": [
"<Figure size 504x504 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"image = cv.imread('img/pedestrians.jpg')\n",
"plt.figure(figsize=[7,7])\n",
"plt.imshow(image[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "9fbb6325",
"metadata": {
"id": "9fbb6325"
},
"source": [
"Podczas korzystania z tego modelu musimy się zmierzyć z kilkoma subtelnościami. Model wykorzystuje framework Darknet, więc musimy wskazać, że chodzi nam o predykcje pochodzące z ostatniej warstwy. Dodatkowo mamy kilka progów odcięcia do zdefiniowania, tj. miarę obiektowości (*objectness*), pewności (*confidence*) oraz tłumienia niemaksymalnego aby ograniczyć występowanie nakładających się na siebie ramek z wykrytymi obiektami (por. [`cv.dnn.NMSBoxes()`](https://docs.opencv.org/4.5.3/d6/d0f/group__dnn.html#ga9d118d70a1659af729d01b10233213ee)). Poniżej mamy wynik działania:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "d8450888",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 391
},
"id": "d8450888",
"outputId": "510a045e-0a2f-4c6c-bc8f-a201065cdbaa"
},
"outputs": [
{
"ename": "IndexError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-32-9712370d1845>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetInput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage_blob\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mdetections\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetLayerNames\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetUnconnectedOutLayers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mimage_out\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mimage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-32-9712370d1845>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetInput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage_blob\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mdetections\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetLayerNames\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetUnconnectedOutLayers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mimage_out\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mimage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mIndexError\u001b[0m: invalid index to scalar variable."
]
}
],
"source": [
"height, width, _ = image.shape\n",
"\n",
"image_blob = cv.dnn.blobFromImage(image=image, scalefactor=1/255, size=(416, 416), mean=(0,0,0), \n",
" swapRB=True, crop=False)\n",
"\n",
"model.setInput(image_blob)\n",
"detections = model.forward([model.getLayerNames()[i[0] - 1] for i in model.getUnconnectedOutLayers()])\n",
"\n",
"image_out = image.copy()\n",
"\n",
"class_ids = []\n",
"confidences = []\n",
"boxes = []\n",
"\n",
"for out in detections:\n",
" for detection in out:\n",
" if detection[4] > 0.5: # objectness thr.\n",
" scores = detection[5:]\n",
" class_id = np.argmax(scores)\n",
" confidence = scores[class_id]\n",
" if confidence > 0.5: # confidence thr.\n",
" center_x = int(detection[0] * width)\n",
" center_y = int(detection[1] * height)\n",
" b_width = int(detection[2] * width)\n",
" b_height = int(detection[3] * height)\n",
"\n",
" b_left = int(center_x - b_width / 2)\n",
" b_top = int(center_y - b_height / 2)\n",
" class_ids.append(class_id)\n",
" confidences.append(float(confidence))\n",
" boxes.append([b_left, b_top, b_width, b_height])\n",
"\n",
"indices = cv.dnn.NMSBoxes(boxes, confidences, score_threshold=0.5, nms_threshold=0.5)\n",
"for i in indices:\n",
" idx = i[0]\n",
" box = boxes[idx]\n",
" x1 = box[0]\n",
" y1 = box[1]\n",
" x2 = box[0] + box[2]\n",
" y2 = box[1] + box[3]\n",
" cv.rectangle(image_out, (x1, y1), (x2, y2), (0, 255, 0), 6)\n",
" label = '{:} ({:.3f})'.format(classes[class_ids[idx]], confidences[idx])\n",
" \n",
" label_size, base_line = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.65, 1)\n",
" cv.rectangle(image_out, (x1, y1 - label_size[1]), (x1 + label_size[0], y1 + base_line), \n",
" (255, 255, 255), cv.FILLED)\n",
" cv.putText(image_out, label, (x1, y1), cv.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0))\n",
" \n",
"plt.figure(figsize=[12,12])\n",
"plt.imshow(image_out[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "41e32b8e",
"metadata": {
"id": "41e32b8e"
},
"source": [
"# Szacowanie pozy człowieka\n",
"\n",
"Kolejnym interesującym zagadnieniem jest szacowanie pozy człowieka (ang. *human pose estimation*) na podstawie zdjęcia. Celem jest tutaj wykrycie charakterystycznych punktów orientacyjnych, które mogą potem zostać wykorzystane np. treningu sportowego, kontroli gestów, korekcji postawy, itp. W tym celu wykorzystamy [OpenPose](https://github.com/CMU-Perceptual-Computing-Lab/openpose)."
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "6a3fedf2",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6a3fedf2",
"outputId": "85c361b2-32df-4922-f7ba-009c8399a70c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dnn/openpose/pose_i 100%[===================>] 196.41M 61.1MB/s in 3.7s \n",
"dnn/openpose/pose_d 100%[===================>] 31.46K --.-KB/s in 0.002s \n"
]
}
],
"source": [
"!mkdir -p dnn/openpose\n",
"!wget -q --show-progress -O dnn/openpose/pose_iter_160000.caffemodel http://posefs1.perception.cs.cmu.edu/Users/tsimon/Projects/coco/data/models/mpi/pose_iter_160000.caffemodel\n",
"!wget -q --show-progress -O dnn/openpose/pose_deploy_linevec_faster_4_stages.prototxt https://raw.githubusercontent.com/CMU-Perceptual-Computing-Lab/openpose/master/models/pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
]
},
{
"cell_type": "markdown",
"id": "851c965b",
"metadata": {
"id": "851c965b"
},
"source": [
"Wczytujemy model:"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "d55edcb8",
"metadata": {
"id": "d55edcb8"
},
"outputs": [],
"source": [
"model = cv.dnn.readNetFromCaffe(\"dnn/openpose/pose_deploy_linevec_faster_4_stages.prototxt\",\n",
" \"dnn/openpose/pose_iter_160000.caffemodel\")"
]
},
{
"cell_type": "markdown",
"id": "c2c701c3",
"metadata": {
"id": "c2c701c3"
},
"source": [
"Będziemy chcieli przeanalizować poniższe zdjęcie:"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "aeaed6eb",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 295
},
"id": "aeaed6eb",
"outputId": "19e5b8f8-a816-4e26-c0e1-83d3cb1482a7"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAa8AAAEWCAYAAADRrhi8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9z45lS7Lm9TMz97Ujs6ovogUtJGDAACQGjJDgCZCYMQUeoEc8AM/SA8Y8QUs8QDPpMUigFhKikRB06/btWycz9nJ3MwZmvtaOPKeqQXDUp3RjVcWJzIwde68/5mafffaZuUQEn8fn8Xl8Hp/H5/HndOi/7BP4PD6Pz+Pz+Dw+j/+3x2fw+jw+j8/j8/g8/uyOz+D1eXwen8fn8Xn82R2fwevz+Dw+j8/j8/izOz6D1+fxeXwen8fn8Wd3fAavz+Pz+Dw+j8/jz+741YKXiPynIvI/icg/EpH/+tf6nM/j8/g8Po/P42/eIb9Gn5eIGPA/A/8J8I+Bfwj8FxHxP/7//mGfx+fxe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ib6ma0AjMq0FV4lJ2LkH7iTxy/SvCKiCki/xXw3wEG/DcR8T/8sdeLCI9udMsUfflizJkLpSD3poo+ctz34ax8vWs6vEJ1NCUzByU8szGBdPiejsKa8XZ0vhwHj6PRRDCFRRDTUZGkgnwx1mTUn9MRJXJ45bJF7uARRWFIZBCNlZnUdmZzOnM4cwRrBSMEb+mc0Sgjjgul57Wkk0qqKRfVK6evmueUJ/Bzak2kgp1uA4xK7Yt2jDsg5
"text/plain": [
"<Figure size 504x504 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"image = cv.imread(\"img/messi5.jpg\")\n",
"plt.figure(figsize=[7,7])\n",
"plt.imshow(image[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "00a42dd5",
"metadata": {
"id": "00a42dd5"
},
"source": [
"Zdefinujemy poniżej połączenia pomiędzy 15 punktami orientacyjnymi:"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "894acae5",
"metadata": {
"id": "894acae5"
},
"outputs": [],
"source": [
"pose_points_n = 15\n",
"pose_pairs = [[0,1], [1,2], [2,3], [3,4], [1,5], [5,6], [6,7], [1,14], [14,8], [8,9], [9,10], [14,11], [11,12], [12,13]]"
]
},
{
"cell_type": "markdown",
"id": "5a8a5028",
"metadata": {
"id": "5a8a5028"
},
"source": [
"W wyniku otrzymujemy mapy prawodpodobieństwa występowania danego punktu orientacyjnego:"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "24ca95c6",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 115
},
"id": "24ca95c6",
"outputId": "54ab09e1-4ae2-49c2-8133-5b879e1277ef"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABGoAAACxCAYAAACcAVv5AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsI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dS6UTbFYftxeEpX73a81pEmPTXOJ5/uA7cBOGOwN5QU2ELaGTgIngdmLwJuEduXqrOxZ6ZWapVZgJfur9JjamQxqpFsXYBfg1E54Mccvxx7hnz73ff7o3teZYiPvvns/HM3BtykMJpcKP9PZHUh7ywOX4EY/vDzAu/kdfPPgv4ZByG2cZv5Mb6hR8ypwKtapiHeDqpE+8lTZFyeeI+HD8Z1wYpD5l3t5Y/DxQj0r4DzAWYptInnRXu0S48VehxdGrSM54ax3jYbFCmKDwUwnSG8viCr1G3EbyhhwPfRlL24PY9xGwurSSUL/dmOKUJNtguIKP9tUtjXywjcGQvEifCd07YdngC/DE48dYRujXGYLI9zPhR8+CN/YBid6CWHN6Qa9TlUvXb+vlvcnPS6l+6EuFoZ7vveRQY1EnuJdpD+Fk8Nwchd8O8ebQweLF1+TAG8VHumLLye+7SduYZiD8zvha9tvTVbn1/aG9jAJcJEwcx2luOKiWsku2PbTGuYSjxHI98HJPjjZnXg+LuWuJbhLS/9cchJT6WLLADDbGvGeLHbUfKm2pfZU6zQ/Bi6xj7sEI30wEk+8m6N4LHfcqmJII7i9v4lFYQsHMzwEPAOPP/Yq/47/wB7eYpRtnGYP//k3/gVvnj8I5/vh2hDhZkit8yk1xnLnNMlVM1F6blPLroX2rslmUCMt+IUeIayWeRPGesODOUJdi
"text/plain": [
"<Figure size 1440x216 with 15 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"height, width, _ = image.shape\n",
"\n",
"image_blob = cv.dnn.blobFromImage(image, 1.0/255, (368, 368), (0, 0, 0), swapRB=False, crop=False)\n",
"model.setInput(image_blob)\n",
"\n",
"output = model.forward()\n",
"\n",
"plt.figure(figsize=(20,3))\n",
"for i in range(pose_points_n):\n",
" prob_map = output[0, i, :, :]\n",
" disp_map = cv.resize(prob_map, (width, height), cv.INTER_LINEAR)\n",
" plt.subplot(2, 8, i+1)\n",
" plt.axis('off')\n",
" plt.imshow(disp_map, cmap='jet', vmin=0, vmax=1)"
]
},
{
"cell_type": "markdown",
"id": "c8be6dc1",
"metadata": {
"id": "c8be6dc1"
},
"source": [
"Przeskalowujemy wyniki do rozmiarów obrazu wejściowego i przy pomocy [`cv.minMaxLoc()`](https://docs.opencv.org/4.5.3/d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707) znajdujemy wartość maksymalną (dodatkowo sprawdzamy czy wartość prawdopodobieństwa jest odpowiednio duża):"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "a3163987",
"metadata": {
"id": "a3163987"
},
"outputs": [],
"source": [
"scale_x = width / output.shape[3]\n",
"scale_y = height / output.shape[2]\n",
"\n",
"points = []\n",
"\n",
"for i in range(pose_points_n):\n",
" prob_map = output[0, i, :, :]\n",
" \n",
" _, prob, _, point = cv.minMaxLoc(prob_map)\n",
" \n",
" x = scale_x * point[0]\n",
" y = scale_y * point[1]\n",
"\n",
" if prob > 0.1: # thr.\n",
" points.append((int(x), int(y)))\n",
" else:\n",
" points.append(None)"
]
},
{
"cell_type": "markdown",
"id": "f1f8cac3",
"metadata": {
"id": "f1f8cac3"
},
"source": [
"Możemy teraz nanieść punkty na obraz i połączyć je w szkielet"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "fcbda6c6",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 208
},
"id": "fcbda6c6",
"outputId": "2a53717b-290a-4633-9667-10216e49e99e"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABIQAAAFfCAYAAAA/Exd9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOx9za4tyXLWFxFZa5++F5AthsAABBJPAE+AxIwp8AAe8QA8iweMeQJLPIMHyAOQQBYTYIaMsX27z16VEcHgi8iqtU+37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NepsNK3ks6JASGhGDCA5he3sFiJWQWfzAaQ5SHClXFMxJiWkCHfOYRmHNimGBKwiCYArgRtC7Pa106cemVMQANgZhANKHmcAfgjhQ+4xOlX+5yr4D4Xd5YHfRVBTCDSmIocBj3MHlHkfrRy7zpZ6K/EH8BG4NtDLYx2E/RxmAbg20MtjHYt4bBfi6D0O8D+Eci8vdBIPIvAfzrn/qwQPDpIBhRSWhZ7sjYAQ2nYK7PZ2ZZHZ1KOTtUktPdFkz3ek8vD8BSns0EqkCBAdHSK1lWWnTAFpABOHi/hw3YGDCVF69PM8qcTo+G04L+PifO6Xie57KeigiQ17OMcEgaFS6MocqqMNG1iUQokDUTGgKEID14HXGEZwnCAm4AzgQg1j6U26RnOaUEIhyPJIUBkFCjlTIikNKB2FgOPFkK8B6K7dcH6rUV0gcp4U3wwL9baAXaDJplDV8QTLLuhXVdKmsKX0mHFCCLGYgz4WeDEkHSvQS4cO5KmB468DAC3kAigt4mUaNwNSnAl5DSPA5akD0CiaTgqnlI1QInDQQEW
"text/plain": [
"<Figure size 1440x1440 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"image_points = image.copy()\n",
"image_skeleton = image.copy()\n",
"\n",
"for i, p in enumerate(points):\n",
" cv.circle(image_points, p, 8, (255, 255, 0), thickness=-1, lineType=cv.FILLED)\n",
" cv.putText(image_points, \"{}\".format(i), p, cv.FONT_HERSHEY_SIMPLEX, 1, (0,255,255), 2, lineType=cv.LINE_AA)\n",
"\n",
"\n",
"for pair in pose_pairs:\n",
" part_a = pair[0]\n",
" part_b = pair[1]\n",
"\n",
" if points[part_a] and points[part_b]:\n",
" cv.line(image_skeleton, points[part_a], points[part_b], (0, 255, 255), 4)\n",
" cv.circle(image_skeleton, points[part_a], 7, (255, 255, 0), thickness=-1, lineType=cv.FILLED)\n",
"\n",
"plt.figure(figsize=(20,20))\n",
"plt.subplot(121)\n",
"plt.imshow(image_points[:,:,::-1])\n",
"plt.subplot(122)\n",
"plt.imshow(image_skeleton[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "ea3421fd",
"metadata": {
"id": "ea3421fd"
},
"source": [
"# Wykrywanie i rozpoznawanie tekstu\n",
"\n",
"W kolejnym przykładzie zobaczymy jak możemy wykryć na zdjęciu tekst przy pomocy [DB](https://github.com/MhLiao/DB) oraz rozpoznać go przy pomocy [CRNN](https://arxiv.org/pdf/1507.05717.pdf)."
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "5ef81ed2",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5ef81ed2",
"outputId": "9e5e74ea-366d-48ba-e59f-2147ac9a057f"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Downloading...\n",
"From: https://drive.google.com/uc?export=dowload&id=19YWhArrNccaoSza0CfkXlA8im4-lAGsR\n",
"To: /content/wko/dnn/DB_TD500_resnet50.onnx\n",
"100%|██████████| 101M/101M [00:01<00:00, 79.4MB/s] \n",
"Downloading...\n",
"From: https://drive.google.com/uc?export=dowload&id=12diBsVJrS9ZEl6BNUiRp9s0xPALBS7kt\n",
"To: /content/wko/dnn/crnn_cs.onnx\n",
"100%|██████████| 64.9M/64.9M [00:00<00:00, 67.7MB/s]\n",
"Downloading...\n",
"From: https://drive.google.com/uc?export=dowload&id=1oKXxXKusquimp7XY1mFvj9nwLzldVgBR\n",
"To: /content/wko/dnn/alphabet_94.txt\n",
"100%|██████████| 188/188 [00:00<00:00, 260kB/s]\n"
]
}
],
"source": [
"import gdown\n",
"\n",
"for url, output in [('https://drive.google.com/uc?export=dowload&id=19YWhArrNccaoSza0CfkXlA8im4-lAGsR', 'dnn/DB_TD500_resnet50.onnx'), \n",
" ('https://drive.google.com/uc?export=dowload&id=12diBsVJrS9ZEl6BNUiRp9s0xPALBS7kt', 'dnn/crnn_cs.onnx'),\n",
" ('https://drive.google.com/uc?export=dowload&id=1oKXxXKusquimp7XY1mFvj9nwLzldVgBR', 'dnn/alphabet_94.txt')]:\n",
" gdown.download(url, output, quiet=False)"
]
},
{
"cell_type": "markdown",
"id": "72721bc5",
"metadata": {
"id": "72721bc5"
},
"source": [
"Będziemy pracować na poniższym zdjęciu:"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "86e3f889",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 432
},
"id": "86e3f889",
"outputId": "92443850-051f-4798-882f-de73be17cb07"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASUAAAGfCAYAAAAZNsRkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9ya9lyX3n94nhTHd+Y85ZA8kqkhJFqdmSGg3bpNSWILsNy2r3wm3vG174D/Af4pUW3njh3hmGDXUbBtyATcGQqMEUySpWVtaQ8/DGO997zokILyLinPNevqymgGZ3Qsgg6+V7954hxm/8ft/fEMI5x9vytrwtb8ubUuS/7wq8LW/L2/K2dMtbUHpb3pa35Y0qb0HpbXlb3pY3qrwFpbflbXlb3qjyFpTelrflbXmjyltQelvelrfljSq/FFASQvyBEOITIcR9IcR//8t4x9vytrwtfzeL+LftpySEUMA94PeAx8CPgH/mnPvo3+qL3pa35W35O1l+GZLSbwH3nXOfO+dK4F8Af/hLeM/b8ra8LX8Hi/4lPPMW8Kjz92Pgty9fJIT458A/D39+Typ5+XvE697Q/cK11zsc3bscQJAE/feXHyLCd+F6ZxFCAC5+c/Xr/SU4wFiHUAqQeKnTfxGf09TEOZS8+DzRftvWuVPf7mfOOaRK2D+4TpJktF+3d3fv6RYXfsa+6faLQ3BZWL74GBeuv6IfLv0iuj+cu3iLE1z8RIT3+E9lp/4u3Otc2x8XqvBVwn3Tqe5Cu1zzlR+XPBX0co0UAmuMf7erWC7OSZKcbVnR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1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"image = cv.imread('img/road-sign.jpg')\n",
"\n",
"plt.figure(figsize=(5,7))\n",
"plt.imshow(image[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "ec7d3ce4",
"metadata": {
"id": "ec7d3ce4"
},
"source": [
"Wczytujemy obsługiwany alfabet:"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "5d27f129",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5d27f129",
"outputId": "a7668afc-97ca-4b2c-8565-f3b20782e613"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"94 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e']\n"
]
}
],
"source": [
"with open('dnn/alphabet_94.txt', 'r') as f_fd:\n",
" alphabet = f_fd.read().splitlines()\n",
" \n",
"print(len(alphabet), alphabet[:15])"
]
},
{
"cell_type": "markdown",
"id": "d3373c60",
"metadata": {
"id": "d3373c60"
},
"source": [
"OpenCV posiada gotowe API dla sieci DB poprzez [`cv.dnn_TextDetectionModel_DB()`](https://docs.opencv.org/4.5.3/db/d0f/classcv_1_1dnn_1_1TextDetectionModel__DB.html):"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "b3c3bfc2",
"metadata": {
"id": "b3c3bfc2"
},
"outputs": [],
"source": [
"text_detector = cv.dnn_TextDetectionModel_DB(\"dnn/DB_TD500_resnet50.onnx\")\n",
"\n",
"text_detector.setBinaryThreshold(0.4).setPolygonThreshold(0.5)\n",
"text_detector.setInputParams(scale=1.0/255, size=(640, 640), \n",
" mean=(122.67891434, 116.66876762, 104.00698793), swapRB=True)"
]
},
{
"cell_type": "markdown",
"id": "31300a5f",
"metadata": {
"id": "31300a5f"
},
"source": [
"W wyniku otrzymujemy ramki, na których występuje tekst (choć jak widzimy, są też wyniki fałszywie pozytywne):"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "d14502d4",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 432
},
"id": "d14502d4",
"outputId": "b2605c56-9228-4b1c-e775-8e2b6b4017b8"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASUAAAGfCAYAAAAZNsRkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9ya9lyX3n94nhTHd+Y85ZA8kqkhJFqdmSGg3bpNSWILsNy2r3wm3vG174D/Af4pUW3njh3hmGDXUbBtyATcGQqMEUySpWVtaQ8/DGO997zokILyLinPNevqymgGZ3Qsgg6+V7954hxm/8ft/fEMI5x9vytrwtb8ubUuS/7wq8LW/L2/K2dMtbUHpb3pa35Y0qb0HpbXlb3pY3qrwFpbflbXlb3qjyFpTelrflbXmjyltQelvelrfljSq/FFASQvyBEOITIcR9IcR//8t4x9vytrwtfzeL+LftpySEUMA94PeAx8CPgH/mnPvo3+qL3pa35W35O1l+GZLSbwH3nXOfO+dK4F8Af/hLeM/b8ra8LX8Hi/4lPPMW8Kjz92Pgty9fJIT458A/D39+Typ5+XvE697Q/cK11zsc3bscQJAE/feXHyLCd+F6ZxFCAC5+c/Xr/SU4wFiHUAqQeKnTfxGf09TEOZS8+DzRftvWuVPf7mfOOaRK2D+4TpJktF+3d3fv6RYXfsa+6faLQ3BZWL74GBeuv6IfLv0iuj+cu3iLE1z8RIT3+E9lp/4u3Otc2x8XqvBVwn3Tqe5Cu1zzlR+XPBX0co0UAmuMf7erWC7OSZKcbVnRG4xQKuH58xcYYDgaY+ua7WaNc2Ctoa5rtmVJbWqMczjrSLSmyHOkENTGtv3oQErp56JzyDAnjfHPAf99bWqstRhjEQikFCRpQqI14LDW4qxDKvXqmLvY/bFfw7p4zdzw7QYb5th2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Qjb4uUAKEkhjrMFHqEwIlJEhHIhNGo128cZMLKttlC2oXdEKTY5MuqHWiJYE6ql7bRgfYDpjJBrSiCtZRxYQIINSOqeh0cASWCI7x/R5IW0CyTmCCqudsBMyWjYo1kwGMpZANYFkX3hMkAn+9l4KlEjgnGOaC6/sFUtZepTSWNC04evGCw2s3mS3X3Lp9F+ss1jgePn7GcrFCmprF+RGr9YLhYAQOEpUghcBUNa627O7vMhkPSRONlCoAocFKiZSKylUcHR/R7/eZTackWYYUjl6RUxQZ1lpmiyknp8cMBgOuHx6S5wVpKrh75w7XDg85n884OzujriyPHj3l2bOX7O7uMhmPybMUaxx5npOTUdaGJ89fcLC351kfa6nrmjRJSZIUrbSX8AibsfVc6na7ZbMpqes6UBF+fIwxVGWFTj0wpUlCmiYkWlHkPaqqZrVaUZYlz54+A6kQWtHr5Rzu7CAFKKEoRI6q/x2CEn6C/AnwJ3+be4SSDTfh14rnH1QQ/+UlVeXCworgFdZPq7JYlFKNrh5VAITEdcHKgeSi6idlqyJ2iw3qnjUGsF6dUglVbb3agEQlCp2kZD0H1Rblaj9JRVBNnF8soQItgIRFJaWMMhoCv/tlgxGDwdirfcIFItddUUd3QYV6bX8H1aghJMNn3X/ByzxO4FXW5jrfP1J5idI561XMKLU5f0/DYRGBztfXWt9XTnbByKt71hH+je/z4+D5p069AiC1qmjThfiJ0FEBncM5g0Tw/s1d+qlFypTtaknR61HbmizvUQze59NP73Ntfx/hBLPZnM+++JJEJ7x35zZKCIajMYlKEAK01mip0ShEInn3zm1G/YLUEzjYMKccBifg6OgIY/2czLKMNElIpPIbmzGUlVeTyrLibDanNg4lJIPhgN2dXYbDAXmRcW1/j7queXl0zMnpCY8fPeH46
"text/plain": [
"<Figure size 360x504 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"boxes, confs = text_detector.detect(image)\n",
"\n",
"image_out = image.copy()\n",
"\n",
"cv.polylines(image_out, boxes, True, (255, 0, 255), 4)\n",
"\n",
"plt.figure(figsize=(5,7))\n",
"plt.imshow(image_out[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "3c3eae71",
"metadata": {
"id": "3c3eae71"
},
"source": [
"W kolejnym kroku przygotowujemy model do rozpoznawania tekstu przy pomocy [`cv.dnn_TextRecognitionModel()`](https://docs.opencv.org/4.5.3/de/dee/classcv_1_1dnn_1_1TextRecognitionModel.html):"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "d6b29f6a",
"metadata": {
"id": "d6b29f6a"
},
"outputs": [],
"source": [
"text_recognizer = cv.dnn_TextRecognitionModel(\"dnn/crnn_cs.onnx\")\n",
"text_recognizer.setDecodeType(\"CTC-greedy\")\n",
"text_recognizer.setVocabulary(alphabet)\n",
"text_recognizer.setInputParams(scale=1/127.5, size=(100, 32), mean=(127.5, 127.5, 127.5), swapRB=True)"
]
},
{
"cell_type": "markdown",
"id": "a17f6437",
"metadata": {
"id": "a17f6437"
},
"source": [
"Każdą wykrytą ramkę rzutujemy na rozmiar 100x32 i wykrywamy tekst:"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "d6909f83",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "d6909f83",
"outputId": "a42e5f2f-610d-4165-f056-aa78e3b4e77b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SIGNAL\n",
"RED\n",
"you\n",
"HEREON\n",
"STOP\n",
"Fe\n",
"a\n",
"a\n"
]
}
],
"source": [
"for box in boxes:\n",
" vertices = np.asarray(box).astype(np.float32)\n",
" output_size = (100, 32)\n",
" target_vertices = np.array([\n",
" [0, output_size[1] - 1],\n",
" [0, 0],\n",
" [output_size[0] - 1, 0],\n",
" [output_size[0] - 1, output_size[1] - 1]],\n",
" dtype=\"float32\")\n",
" rotation_matrix = cv.getPerspectiveTransform(vertices, target_vertices)\n",
" cropped_roi = cv.warpPerspective(image, rotation_matrix, output_size)\n",
" \n",
" result = text_recognizer.recognize(cropped_roi)\n",
" print(result)"
]
},
{
"cell_type": "markdown",
"id": "e0b4b3c0",
"metadata": {
"id": "e0b4b3c0"
},
"source": [
"# Super rozdzielczość\n",
"\n",
"Podczas zwiększania rozdzielczości brakujące piksele muszą być w jakiś sposób interpolowane. Przy niewielkich powiększeniach zwykle wystarczą nam tradycyjne metody, jednak jeśli pracujemy z obrazem w niskiej rozdzielczości i chcemy go znacząco powiększyć, to chcielibyśmy również uzyskać wysoką jakość np. poprzez uwzględnienie informacji z otoczenia pikseli. Problematyka ta dotyczy zagadnienia super rozdzielczości (ang. *super-resolution*).\n",
"\n",
"W [artykule](https://arxiv.org/pdf/1902.06068.pdf) z 2020 r. możemy znaleźć porównanie dostępnych w tamtym czasie modeli (zob. wykres na str. 15); np. możemy zobaczyć, że model [EDSR](https://github.com/Saafke/EDSR_Tensorflow) radzi sobie całkiem nieźle, aczkolwiek kosztem sporego narzutu obliczeniowego (por. również benchmarki [OpenCV](https://github.com/opencv/opencv_contrib/blob/master/modules/dnn_superres/README.md)). Przetestujemy EDSR na powiększeniu 4-krotnym:"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "6b9f6be9",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6b9f6be9",
"outputId": "50afd240-7700-49aa-c5ad-ad29e31abc54"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dnn/EDSR_x4.pb 100%[===================>] 36.79M 182MB/s in 0.2s \n"
]
}
],
"source": [
"!wget -q --show-progress -O dnn/EDSR_x4.pb https://raw.githubusercontent.com/Saafke/EDSR_Tensorflow/master/models/EDSR_x4.pb"
]
},
{
"cell_type": "markdown",
"id": "92f09e43",
"metadata": {
"id": "92f09e43"
},
"source": [
"Przy pomocy [`cv.dnn_superres.DnnSuperResImpl_create()`](https://docs.opencv.org/4.5.3/d8/d11/classcv_1_1dnn__superres_1_1DnnSuperResImpl.html) przygotowujemy model:"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "368ca179",
"metadata": {
"id": "368ca179"
},
"outputs": [],
"source": [
"sr = cv.dnn_superres.DnnSuperResImpl_create()\n",
"sr.readModel('dnn/EDSR_x4.pb')\n",
"sr.setModel('edsr', 4)"
]
},
{
"cell_type": "markdown",
"id": "9e0169f3",
"metadata": {
"id": "9e0169f3"
},
"source": [
"Następnie zwiększy rozdzielczość zadanego obrazu (operacja może zająć trochę czasu):"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "45c89529",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "45c89529",
"outputId": "f4b99d64-f525-433f-9a24-7c91e3a8fee9"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAt0AAAVxCAYAAABbTjDpAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9yZMkSXbm93tq5kusuVRmZS3dVd1Ad2MRgOQA5IDkzAiEy3ARoQivXO5z4o0X/iO8zIFHCkV44WlESBEeSA5J4QAkMJhBdwOoru5Gd1dX5RoZmy9mqo8HVTVTW9zDIzIi0iPTvqpIdzNTU1Uz1+XTT58+FVVlwIABAwYMGDBgwIABNwfztjMwYMCAAQMGDBgwYMC7joF0DxgwYMCAAQMGDBhwwxhI94ABAwYMGDBgwIABN4yBdA8YMGDAgAEDBgwYcMMYSPeAAQMGDBgwYMCAATeMgXQPGDBgwIABAwYMGHDDuHXSLSL/kYj8lYh8ISL/zW2nP2DAgAEDBgwYMGDAbUNu00+3iGTAXwP/EPgl8CfAf66qP7y1TAwYMGDAgAEDBgwYcMu4baX77wJfqOqXqroE/gfgP73lPAwYMGDAgAEDBgwYcKvIbzm9T4FfJMe/BP5oVWBjjOYmu2JScomw27IrZ5rnm87T5dISad7TOuzAT6Ak8epFqUgdcMCAjd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yQtxS0Hu2l25gg/ap00R0tfoB7yzp3jZcpdDVbvlE/E5VxkTSXfsITvt8lZRwt7H5DEDn/mrKmdDJRxLSp3RHwl0T7ziltWqDn0b1H+rn+4lEDa0FZa2Um
"text/plain": [
"<Figure size 1800x1800 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"image = cv.imread('img/parrot.jpg')\n",
"\n",
"image_EDSR = sr.upsample(image)\n",
"\n",
"plt.figure(figsize=(25,25))\n",
"plt.subplot(211)\n",
"plt.imshow(image[:,:,::-1])\n",
"plt.subplot(212)\n",
"plt.imshow(image_EDSR[:,:,::-1]);"
]
},
{
"cell_type": "markdown",
"id": "3c157587",
"metadata": {
"id": "3c157587"
},
"source": [
"# Zadanie 1\n",
"\n",
"Przy pomocy biblioteki [MediaPipe](https://google.github.io/mediapipe/solutions/selfie_segmentation.html) dokonaj podmień tło w selfie `img/selfie-man.jpg` na `img/selfie-background.jpg` (możesz również odbić obraz w poziomie)."
]
},
{
"cell_type": "markdown",
"id": "d6116f61",
"metadata": {
"id": "d6116f61"
},
"source": [
"![Wynik działania programu](img/selfie-out.png)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "HOfeBql1xwLx",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HOfeBql1xwLx",
"outputId": "63cb837e-a42e-4ac6-a1d1-013abd74df46"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Collecting mediapipe\n",
" Downloading mediapipe-0.9.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (33.0 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m33.0/33.0 MB\u001b[0m \u001b[31m20.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from mediapipe) (1.21.6)\n",
"Requirement already satisfied: opencv-contrib-python in /usr/local/lib/python3.8/dist-packages (from mediapipe) (4.6.0.66)\n",
"Requirement already satisfied: absl-py in /usr/local/lib/python3.8/dist-packages (from mediapipe) (1.3.0)\n",
"Collecting flatbuffers>=2.0\n",
" Downloading flatbuffers-23.1.4-py2.py3-none-any.whl (26 kB)\n",
"Requirement already satisfied: protobuf<4,>=3.11 in /usr/local/lib/python3.8/dist-packages (from mediapipe) (3.19.6)\n",
"Requirement already satisfied: attrs>=19.1.0 in /usr/local/lib/python3.8/dist-packages (from mediapipe) (22.2.0)\n",
"Requirement already satisfied: matplotlib in /usr/local/lib/python3.8/dist-packages (from mediapipe) (3.2.2)\n",
"Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->mediapipe) (2.8.2)\n",
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->mediapipe) (3.0.9)\n",
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.8/dist-packages (from matplotlib->mediapipe) (0.11.0)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->mediapipe) (1.4.4)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/dist-packages (from python-dateutil>=2.1->matplotlib->mediapipe) (1.15.0)\n",
"Installing collected packages: flatbuffers, mediapipe\n",
" Attempting uninstall: flatbuffers\n",
" Found existing installation: flatbuffers 1.12\n",
" Uninstalling flatbuffers-1.12:\n",
" Successfully uninstalled flatbuffers-1.12\n",
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
"tensorflow 2.9.2 requires flatbuffers<2,>=1.12, but you have flatbuffers 23.1.4 which is incompatible.\u001b[0m\u001b[31m\n",
"\u001b[0mSuccessfully installed flatbuffers-23.1.4 mediapipe-0.9.0.1\n"
]
}
],
"source": [
"!pip3 install mediapipe"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "jguEGbukxzH5",
"metadata": {
"id": "jguEGbukxzH5"
},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import mediapipe as mp\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "WdQbZcfyx0oB",
"metadata": {
"id": "WdQbZcfyx0oB"
},
"outputs": [],
"source": [
"tmp = mp.solutions.selfie_segmentation\n",
"\n",
"tmp1 = tmp.SelfieSegmentation()"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "nstJpwD5x2G5",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 419
},
"id": "nstJpwD5x2G5",
"outputId": "474c9ec6-587d-42e2-e6e8-be247d0c7222"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjwAAAGSCAYAAADn8RuZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9e8ytW3cX9BtzPmu9797nfN9ppUL8mvohVBDrpXhPMF5K1FDUqlEj/gENyEVjvDWIl4iVIEjQVEwxrYBBQUHTiBZNlDTUpEnBREIRtYmCQj+gWj/anu9c9n7f9cw5/GOMMeeY85nzWevde59LT56x8+611vPMy5i3MX5jzBsxMw466KCDDjrooIM+yxQ+aQYOOuiggw466KCDPmo6AM9BBx100EEHHfSZpwPwHHTQQQcddNBBn3k6AM9BBx100EEHHfSZpwPwHHTQQQcddNBBn3k6AM9BBx100EEHHfSZpwPwHHTQQR85EdHfRUR/zv3+uUT0Q0T0HhH9c0T0XUT0b7xi2kxEX/+KcX8BEf2fRPQ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bAATad0X+cUAc8ZlTXiMCYDWKUtdRArgnMEqZFPKgMrSVAxIyT+EAIRWVsZAQKzyco+eCnRmAOpNAp+Rof0q8uqagbEHekaAZS+ta3z4vGYG5S389sbrnpG6R1cBT5tBWyj/m5lBqtw4MxKlohgNJFla4olhYGVQBJAJmRiJgcAAFYVICJmwpiyDhxghMEABBFbQVQeW8ZHKsGQrgfwfAgIzGIScGTHG8jb7gTkAP0WxqXKnEKqV4+rAhMmyLBsk24MWTz3g8QO5f9bzp4aegB0oOIQIM
"text/plain": [
"<Figure size 720x720 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"man = cv2.imread('img/selfie-man.jpg')\n",
"\n",
"plt.figure(figsize=[10, 10])\n",
"\n",
"plt.title(\"Man's selfie before\")\n",
"plt.axis('off')\n",
"plt.imshow(man[:,:,::-1])\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "w6WJV2nXx2v9",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 238
},
"id": "w6WJV2nXx2v9",
"outputId": "e65923df-a38f-47e4-f453-e979681b40d7"
},
"outputs": [
{
"data": {
"text/plain": [
"(-0.5, 749.5, 499.5, -0.5)"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABNoAAAGSCAYAAAA1uKzsAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9e7Bvy3bX9R3d87fW2uee+wi5gCRgoiEg0RQU+CApkJSgaKpSwZRQYMJDSkslRVkWYhVBkFSEGKBUHgWx1BhiENQYXkbUGDEBTFIlKBUDBggmJCE8bsI99zz23us3u4d/jDG6e/bsnr/f2mefs/c5G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ul+s5yCPaXC6Xy+VyuVwul8vlcrlcrucgB20ul8vlcrlcLpfL5XK5XC7Xc9BytPOr/9SfYQIhgBCIwMSgQAhMYAaICCESiAgAQCEgEHCKEadTxBICiBnnNSNlxvm8IhOwLAE3S8QSCQEBUgZwWiKWGLDEiFMMWGLATSTcnhbcLguIgBACiKRMCgxmBgMAE8BAzhmAbtdRsTlnMDNSSuVnXVdwzsg5I+l+OVbqZUNq+8/yGwDEhhCEVVqa3OQlNnBJ0/4udaDafrbP9tvnZVkQYyzf2+NNVl9mgEPNNwSAQEAGzjnh6XnF4/uMN+7v8eR+RWZCBte8tD1rXWsZvYjkh5mn6Wq+MuP8aJ/9PsqnT2dt2e5rh0GPvo9kZfbpe3tsf/t7ZPOsDn0fmumaodyzvGbH9rZeaudrbO3T9G3S77u2nFE+IMZtjPjwB+7w0VdfxQce3WCJNLCPwZwBkF4L3PWBgNrN676cgXPOuF/PON8n3J/PuD9nPE0Za044p4xzzliz3MfKddbkIXcxQoyEGAhLJ
"text/plain": [
"<Figure size 1584x1584 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"man_in_rgb = cv2.cvtColor(man, cv2.COLOR_BGR2RGB)\n",
"\n",
"prob = tmp1.process(man_in_rgb)\n",
"\n",
"plt.figure(figsize=[22,22])\n",
"\n",
"plt.subplot(121);plt.imshow(man[:,:,::-1])\n",
"plt.title(\"Original Image\")\n",
"plt.axis('off')\n",
"plt.subplot(122)\n",
"plt.imshow(prob.segmentation_mask, cmap='gray')\n",
"plt.title(\"Probability Map\")\n",
"plt.axis('off')"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "kNTBzXv4x5Ds",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 238
},
"id": "kNTBzXv4x5Ds",
"outputId": "604f6ced-bab9-4a69-c7c1-81a44f9f1b09"
},
"outputs": [
{
"data": {
"text/plain": [
"(-0.5, 749.5, 499.5, -0.5)"
]
},
"execution_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WAfwBXBbNBv2d/ckYz7X7EgTm/QQA6GINZZVSIvp0Ivo5+vGnNW37O/pHINDvC4noN11ij8vl+uCWgzaX6wUXM38/M/+Vye7/EMDvIqJXIQsP/Kl7FvPzAXwzZE6M7wDwh5n5Wwfp3gkBYT8NGVb5k6hzbvx2yPDTv0SyKuc3Q+fXuIu0rp8HGaL605rnZ5857H8D8FcBfBdksts/qnn9GQC/H8CfVJu+G8Cv2cnnMwB8NOQH0Z8B8F8x8zfvpP9MAFcAvldt/QbIXHcul8vlcrlcH8r6JiJ6DcAHAPweAJ/FzN9zj3zu+1v3awD8YkwemI7EzH+Fmb9/sP17IdDuOwD8mOb7/zRJfimA79T6fiOAL2TmH+jy+EEIbPtiIvKFs1yut7loPN+5y+VyuVwul8vlcrlcH3wios8E8PnMfJ/hqi6Xy/VU8og2l8vlcrlcLpfL5XK9LUREDyGRcH/kedvicrk+NOWgzeVyuVwul8vlcrlcH/TS+YF/AjLE848/Z3NcLteHqHzoqMvlcrlcLpfL5XK5XC6Xy/UM5BFtLpfL5XK5XC6Xy+VyuVwu1zOQgzaXy+VyuVwul8vlcrlcLpfrGWjZ2/kV3/StTCAEEAIRmBgUCIEJzAARIUQCEQEAKAQEAg4x4nCIWEIAMeO4ZqTMOB5XZAKWJeBqiVgiISBAygAOS8QSA5YYcYgBSwy4ioTrw4LrZQEREEIAkZRJgcHMYABgAhjIOQPQ7ToqNucMZkZKqfyt6wrOGTlnJN0vx0q9bEht/15eAUBsCEFYpaXJTV5iA5c07WupA9X2s322394vy4IYY/ncHm+y+jIDHGq+IQAEAjJwzAlPjise3Wa8dnuLx7crMhMyuOal7VnrWsvoRSR/zDxNV/MF+hR9GXv59OmsLdt97
"text/plain": [
"<Figure size 1584x1584 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"binary_mask = prob.segmentation_mask > 0.9\n",
"\n",
"plt.figure(figsize=[22,22])\n",
"plt.subplot(121)\n",
"plt.imshow(man[:,:,::-1])\n",
"plt.title(\"Man's selfie before\")\n",
"plt.axis('off')\n",
"plt.subplot(122);plt.imshow(binary_mask, cmap='gray')\n",
"plt.title(\"Binary Mask\")\n",
"plt.axis('off')"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "3ObKOunIx-aG",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 238
},
"id": "3ObKOunIx-aG",
"outputId": "bb5e4872-c442-4612-9d22-229d1803f24e"
},
"outputs": [
{
"data": {
"text/plain": [
"(-0.5, 749.5, 499.5, -0.5)"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABNoAAAGSCAYAAAA1uKzsAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9e7Qtz1bX951Vvfbe53d/915eRgTxyiMQgw9E4lswGJOAKJFoBBOBCCjJQGN8kURFIPiKISADMzBoNAFBAgMQIYkOh4DIS0XBFw4BUdGLwAXuvb/HOWevrpr5Y85ZVV2rqtfa+3d+5/wk83vGPnut7uqqWdXVvXt+elYVMTNcLpfL5XK5XC6Xy+VyuVwu1ytTeNYGuFwul8vlcrlcLpfL5XK5XD8R5KDN5XK5XC6Xy+VyuVwul8vlegJy0OZyuVwul8vlcrlcLpfL5XI9ATloc7lcLpfL5XK5XC6Xy+VyuZ6AHLS5XC6Xy+VyuVwul8vlcrlcT0AO2lwul8vlcrlcLpfL5XK5XK4nIAdtLpfrVRcR/XIi+pfN9/cjou8koheI6LcT0RcQ0R+4Z95MRO9zz2N/CRF9DxG9SET/yX3ycLlcLpfL5XJtRUT/DxF93M7+P0dEn/U0bWrK/nQi+uJXId9/RkT/wZPO99UWEX0DEX3is7bD5fqJpOVZG+ByuU5FRP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DNk0to/yMx/dSf9x0KGYPwjtfUrIHPduVwul8vlcj01PaXnRdM3AvhRfUlp3wnA35mk/2sA/iGAf01EbzmXOTP/KICPgCzW9aMAfi+Aj2BmO/YPQKLKfhwyd9mXNMe+DJn4/5tJVkidzfH7FwH8Bs3jNwH4KJ0/7
"text/plain": [
"<Figure size 1584x1584 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"binary_mask_2 = np.dstack((binary_mask,binary_mask,binary_mask))\n",
"\n",
"without_bck = np.where(binary_mask_2, man, 255) \n",
"\n",
"plt.figure(figsize=[22,22])\n",
"plt.subplot(121)\n",
"plt.imshow(man[:,:,::-1])\n",
"plt.title(\"Man's selfie before\")\n",
"plt.axis('off')\n",
"plt.subplot(122)\n",
"plt.imshow(without_bck[:,:,::-1])\n",
"plt.title(\"Man without background\")\n",
"plt.axis('off')"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "8NjvRPqjyAI2",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 171
},
"id": "8NjvRPqjyAI2",
"outputId": "cd33968c-b874-4155-bccf-a4282db16d56"
},
"outputs": [
{
"data": {
"text/plain": [
"(-0.5, 749.5, 499.5, -0.5)"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABNoAAAEPCAYAAACUQKlxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9e/RlyVXf99lVdc699/fq7pnRjEZCSAYhzBuSGJJl/Ah+EOMkYBJicBwgEDD2coITYkycFYKJH/EiLEKMvYwJCa9gY4jtQJzEXiweFiCEeUhYCEtYQoyERpqZ7p7u3+vee6pq549dde753b6/X3ePBg2I+q7V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PjvE4qs9CwQR+Vos5M5leD/wmqIabzZWw4uKRrQ1XApVfYeq/uwlP/8Z4OtF5BgLbPn3X+BpPgr4YSyOzRuAv6WqP7pjvyNsor2NuVXeZBMn5y9g8uafFsvK+cM8oC/+FKWtX4ZJoG+XMr/4Pof9X8DPAW/CAlR/eynrHwJ/Hfh7pU5vAf7IFeV8AfAazIj5h8B/r6o/fMX+Xwj0wFtLXX8Ai6XQ0NDQ0PDi4odE5AS4iwU+/iJV/aUXUM4LfW5+F/AJXLL4sguq+rOq+o4d29+KkXZvwAyMTwB+crLL7wLeWNr7g8BXquo7t8p4CiPbvkZEWhKehoaGhg8imn12pX32TzAX1beX+iy52tXz+8vfmyLy8+Vzs7EaXhTI7riGDQ0NDQ0NDQ0NLzVE5AuBL1fVF+Ku2tDQ0NDQ0NDQ8EFGU7Q1NDQ0NDQ0NPwmhIjsYQqFv/NS16WhoaGhoaGhoeHB0Ii2hoaGhoaGhobfZCixbJ7FXDy/9yWuTkNDQ0NDQ0NDwwOiuY42NDQ0NDQ0NDQ0NDQ0NDQ0NDS8C
"text/plain": [
"<Figure size 1584x1584 with 3 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"new_background = cv2.imread('img/selfie-background.jpg')\n",
"\n",
"fin = np.where(binary_mask_2, man, new_background) \n",
"fin = cv2.flip(fin, 1)\n",
"\n",
"\n",
"plt.figure(figsize=[22,22])\n",
"plt.subplot(131);plt.imshow(man[:,:,::-1])\n",
"plt.title(\"Man's selfie before\")\n",
"plt.axis('off')\n",
"plt.subplot(132)\n",
"plt.imshow(binary_mask, cmap='gray')\n",
"plt.title(\"Binary Mask\")\n",
"plt.axis('off')\n",
"plt.subplot(133)\n",
"plt.imshow(fin[:,:,::-1])\n",
"plt.title(\"Man's selfie after\")\n",
"plt.axis('off')"
]
}
],
"metadata": {
"author": "Andrzej Wójtowicz",
"colab": {
"provenance": []
},
"email": "andre@amu.edu.pl",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"lang": "pl",
"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.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]"
},
"subtitle": "10. Metody głębokiego uczenia w widzeniu komputerowym [laboratoria]",
"title": "Widzenie komputerowe",
"vscode": {
"interpreter": {
"hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c"
}
},
"year": "2021"
},
"nbformat": 4,
"nbformat_minor": 5
2023-01-16 09:35:16 +01:00
}