{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "DataLabeler20200502.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNKVp6P1u9rora2MA92KmLy",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "code",
"metadata": {
"id": "H3I8jdK_VwJz",
"colab_type": "code",
"outputId": "e722b129-1aa2-4439-a0ca-71f792484517",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 131
}
},
"source": [
"# Mount Google Drive in Colab\n",
"from google.colab import drive\n",
"mount_location = '/content/drive/'\n",
"drive.mount(mount_location, force_remount=True)\n",
"\n",
"# DIRECTORY CONTAINING IMAGES TO BE LABELED\n",
"directory = 'My Drive/Data/Minecraft/HUD' # CHANGE IF NECESSARY\n",
"path_prefix = mount_location + directory + '/'"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n",
"\n",
"Enter your authorization code:\n",
"··········\n",
"Mounted at /content/drive/\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Ey0o0uhr-QME",
"colab_type": "code",
"colab": {}
},
"source": [
"# Create helper methods for accessing and creating label and image files\n",
"import os\n",
"import re\n",
"\n",
"# Get list of image file names and label file names\n",
"img_ext = \".png\"\n",
"lbl_ext = \".txt\"\n",
"img_regex = re.compile(r'.*image([0-9]+)\\.png')\n",
"lbl_regex = re.compile(r'.*label([0-9]+)\\.txt')\n",
"\n",
"def getFileIdx(f):\n",
" # getFileIdx returns the index of the image (in string form)\n",
" #\n",
" # Example: getFileIdx(\"image001.png\") returns \"001\"\n",
" assert(isinstance(f, str))\n",
" m = img_regex.match(f)\n",
" if not m:\n",
" m = lbl_regex.match(f)\n",
" if not m:\n",
" raise Exception(\"{filename} didn't match {img_regex} or {lbl_regex}\".format(\n",
" img_regex=img_regex,\n",
" lbl_regex=lbl_regex,\n",
" filename=f))\n",
" idx = str(m.group(1))\n",
" if not idx:\n",
" raise Exception('No index found before file extension for {filename}',\n",
" filename=f)\n",
" return idx\n",
"\n",
"def makeLabelFilename(idx):\n",
" # makeLabelFilename returns the label filename for a give index string.\n",
" # \n",
" # Example: makeLabelFilename(\"001\") returns \"label001.txt\"\n",
" assert(isinstance(idx, str))\n",
" return 'label{idx}'.format(idx=idx) + lbl_ext\n",
"\n",
"def makeImageFilename(idx):\n",
" # makeImageFilename returns the image filename for a give index string.\n",
" # \n",
" # Example: makeImageFilename(\"001\") returns \"image001.txt\"\n",
" assert(isinstance(idx, str))\n",
" return 'image{idx}'.format(idx=idx) + img_ext"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "sFKtqcPvA5P7",
"colab_type": "code",
"colab": {}
},
"source": [
"# Label missing lbl files\n",
"import IPython\n",
"import ipywidgets as widgets\n",
"from google.colab import output\n",
"\n",
"from matplotlib.pyplot import figure, imshow, axis\n",
"from matplotlib.image import imread\n",
"\n",
"import random\n",
"from shutil import copy\n",
"\n",
"import cv2\n",
"from google.colab.patches import cv2_imshow\n",
"\n",
"def SetMissingLabels():\n",
" # get list of image and label files\n",
" img_files = []\n",
" lbl_files = []\n",
" for f in os.listdir(path_prefix):\n",
" if img_regex.match(f):\n",
" img_files.append(f)\n",
" if lbl_regex.match(f):\n",
" lbl_files.append(f)\n",
"\n",
" # get highest index of missing label file\n",
" maxMissingIdx = -1\n",
" for img_file in img_files:\n",
" sIdx = getFileIdx(img_file)\n",
" nIdx = int(sIdx)\n",
" lbl_file = makeLabelFilename(sIdx)\n",
" if not os.path.exists(path_prefix + lbl_file) and nIdx > maxMissingIdx:\n",
" maxMissingIdx = nIdx\n",
"\n",
" # show an image and let user input a label for the image\n",
" def labelPrompt(idx, lbls):\n",
" if idx == -1:\n",
" print(\"Nothing to Label\")\n",
" return\n",
" img_file = makeImageFilename('{:04d}'.format(idx))\n",
" display(IPython.display.HTML('