{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "kernelspec": {
      "display_name": "Python [conda env:ml]",
      "language": "python",
      "name": "conda-env-ml-py"
    },
    "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.8.3"
    },
    "colab": {
      "name": "findMatches.ipynb",
      "provenance": [],
      "toc_visible": true
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "gQuHuQ1UVDc1"
      },
      "source": [
        "!git clone https://github.com/derInformatiker/AIcrowd-AIBlitz7-Solution.git\n",
        "!pip install -r AIcrowd-AIBlitz7-Solution/challenge5/requirements.txt\n",
        "!pip install aicrowd-cli==0.1\n",
        "!pip install trianglesolver==1.2"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lDvPLxS9VEDd"
      },
      "source": [
        "###RESTART RUNTIME TO USE NEW PACKAGES"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SbcrS4b5VDZq",
        "outputId": "337fb7ee-1514-4e4a-98e0-31b48c3219e5",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "API_KEY = \"\"  # Please enter your API Key from [https://www.aicrowd.com/participants/me]\n",
        "!aicrowd login --api-key $API_KEY"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\u001b[32mAPI Key valid\u001b[0m\n",
            "\u001b[32mSaved API Key successfully!\u001b[0m\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dwZpO7rMVDYB"
      },
      "source": [
        "!aicrowd dataset download --challenge image-correction\n",
        "!rm -rf data\n",
        "!mkdir data\n",
        "\n",
        "!unzip -q test.zip  -d data/test\n",
        "!unzip -q train.zip  -d data/train"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hqayFgsPWJk2",
        "outputId": "b2104e2d-8547-4939-ef3b-282236632a0c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        }
      },
      "source": [
        "import shutil\n",
        "\n",
        "shutil.copy('AIcrowd-AIBlitz7-Solution/challenge5/load.py','load.py')\n",
        "shutil.copy('AIcrowd-AIBlitz7-Solution/challenge5/matching.py','matching.py')"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'matching.py'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "izvmLnJ1TCRp"
      },
      "source": [
        "import cv2, os\n",
        "import math\n",
        "from glob import glob\n",
        "import numpy as np\n",
        "from trianglesolver import solve, degree\n",
        "import random\n",
        "from load import *\n",
        "from matching import *"
      ],
      "execution_count": 11,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3kahHiaLTCRr"
      },
      "source": [
        "def run2(mode, d,out = np.array([])):\n",
        "    (start, last, label), image, black = preprocess(mode,d)\n",
        "    y, x = start.shape[:2]\n",
        "    x_num = int(512/x)\n",
        "    y_num = int(512/y)\n",
        "    if out.shape[0] == 0:\n",
        "        out = np.zeros((x_num,y_num,y,x,3))\n",
        "\n",
        "        start_right, image, _ = findN2(start,image,2,right)\n",
        "        start_bottom, image, _ = findN2(start,image,2,bottom)\n",
        "\n",
        "        last_top, image, _ = findN2(last,image,2,top)\n",
        "        last_left, image, _ = findN2(last,image,2,left)\n",
        "\n",
        "        out[0][0] = start\n",
        "        out[-1][-1] = last\n",
        "        out[0][1] = start_bottom\n",
        "        out[1][0] = start_right\n",
        "        out[-1][-2] = last_top\n",
        "        out[-2][-1] = last_left\n",
        "\n",
        "    if image.shape[0] != 0:\n",
        "        for _ in range(4):\n",
        "            filled, nCounts = getNei(out)\n",
        "            nei = np.zeros(nCounts.shape)\n",
        "            nei[nCounts > 1] = 1\n",
        "\n",
        "            for i in range(x_num):\n",
        "                for j in range(y_num):\n",
        "                    if nei[i][j] == 1:\n",
        "                        p_images = []\n",
        "                        scores = []\n",
        "                        remove_index = []\n",
        "                        for d in range(4):\n",
        "                            try:\n",
        "                                img = getImgNei(out,i,j,d)\n",
        "                                out_image, image, confidence, removeIndex = findN2(img,image,2,(d+2)%4,remove = False)\n",
        "                                if d == top or d == bottom:\n",
        "                                    confidence = confidence\n",
        "                                p_images.append(out_image)\n",
        "                                scores.append(confidence)\n",
        "                                remove_index.append(removeIndex)\n",
        "                            except (IndexError,ValueError):\n",
        "                                pass\n",
        "                        index = scores.index(min(scores))\n",
        "                        p_image =  p_images[index]\n",
        "                        out[i][j] =  p_images[index]\n",
        "                        image = np.delete(image,remove_index[index],axis = 0)\n",
        "    img = arangeNew(out)\n",
        "    return img, label\n",
        "\n",
        "def run3(mode, d,out = np.array([])):\n",
        "    (start, last, label), image, black = preprocess(mode,d)\n",
        "    y, x = start.shape[:2]\n",
        "    x_num = int(512/x)\n",
        "    y_num = int(512/y)\n",
        "    if out.shape[0] == 0:\n",
        "        out = np.zeros((x_num,y_num,y,x,3))\n",
        "\n",
        "        start_right, image, _ = findN2(start,image,2,right)\n",
        "        start_bottom, image, _ = findN2(start,image,2,bottom)\n",
        "\n",
        "        last_top, image, _ = findN2(last,image,2,top)\n",
        "        last_left, image, _ = findN2(last,image,2,left)\n",
        "\n",
        "        out[0][0] = start\n",
        "        out[-1][-1] = last\n",
        "        out[0][1] = start_bottom\n",
        "        out[1][0] = start_right\n",
        "        out[-1][-2] = last_top\n",
        "        out[-2][-1] = last_left\n",
        "\n",
        "    if image.shape[0] != 0:\n",
        "        for _ in range(4):\n",
        "            filled, nCounts = getNei(out)\n",
        "            nei = np.zeros(nCounts.shape)\n",
        "            nei[nCounts > 0] = 1\n",
        "\n",
        "            for i in range(x_num):\n",
        "                for j in range(y_num):\n",
        "                    if nei[i][j] == 1:\n",
        "                        p_images = []\n",
        "                        scores = []\n",
        "                        remove_index = []\n",
        "                        for d in range(4):\n",
        "                            try:\n",
        "                                img = getImgNei(out,i,j,d)\n",
        "                                out_image, image, confidence, removeIndex = findN2(img,image,2,(d+2)%4,remove = False)\n",
        "                                if d == top or d == bottom:\n",
        "                                    confidence = confidence\n",
        "                                p_images.append(out_image)\n",
        "                                scores.append(confidence)\n",
        "                                remove_index.append(removeIndex)\n",
        "                            except (IndexError,ValueError):\n",
        "                                pass\n",
        "                        index = scores.index(min(scores))\n",
        "                        p_image =  p_images[index]\n",
        "                        out[i][j] =  p_images[index]\n",
        "                        image = np.delete(image,remove_index[index],axis = 0)\n",
        "    img = arangeNew(out)\n",
        "    return img, label"
      ],
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4oE2KTjITCRs"
      },
      "source": [
        "def run(mode, d,out = np.array([])):\n",
        "    (start, last, label), imagee, black = preprocess(mode,d)\n",
        "    y, x = start.shape[:2]\n",
        "    x_num = int(512/x)\n",
        "    y_num = int(512/y)\n",
        "    \n",
        "    image = imagee.copy()\n",
        "    start_down, image = findN(start,image,x_num,right,asList = True)\n",
        "    outs = []\n",
        "    for starting_image in start_down:\n",
        "        out, image = findN(starting_image,image,y_num,bottom,last)\n",
        "        outs.append(out)\n",
        "    h = np.concatenate((outs),axis = 1)\n",
        "    \n",
        "    image = imagee.copy()\n",
        "    start_down, image = findN(start,image,y_num,bottom,asList = True)\n",
        "    outs = []\n",
        "    for starting_image in start_down:\n",
        "        out, image = findN(starting_image,image,x_num,right,last)\n",
        "        outs.append(out)\n",
        "    v = np.concatenate((outs),axis = 0) \n",
        "    \n",
        "    s = run2(mode,d)[0]\n",
        "    f = run3(mode,d)[0]\n",
        "    out = np.mean((h,v,s,f),axis = 0).astype('uint8')\n",
        "    #out = s\n",
        "    img = fillup(out)\n",
        "    \n",
        "    return img, label"
      ],
      "execution_count": 13,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Oe2sKe31TCRt"
      },
      "source": [
        "if not os.path.exists('submission'):\n",
        "    os.makedirs('submission/Labels')\n",
        "\n",
        "for i in range(5000):\n",
        "    out, label = run('test',i)\n",
        "    (start, last, label), image, black = preprocess('train',i)\n",
        "    out = np.mean((out,label),axis = 0)\n",
        "    out = cv2.cvtColor(out.astype(np.uint16),cv2.COLOR_RGB2BGR)\n",
        "    cv2.imwrite(f'submission/Labels/{i}.jpg',out,[int(cv2.IMWRITE_JPEG_QUALITY), 50])"
      ],
      "execution_count": 16,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m9jhO99nTCRt"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}