Skip to content

Instantly share code, notes, and snippets.

@wrignj08
Created September 26, 2019 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrignj08/06e69f6068a25967127b729eb03f8fcc to your computer and use it in GitHub Desktop.
Save wrignj08/06e69f6068a25967127b729eb03f8fcc to your computer and use it in GitHub Desktop.
This is a Jupyter Notebook for converting VGG VIA annotations into raster masks for fastai
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Befor you start you need to set up a couple things.\n",
"Make a new folder, within that folder you need 3 things:\n",
"1 All of your images in a folder called \"images\"\n",
"2 Your VGG VIA anotations exported as a csv\n",
"3 A \"codes.txt\" document with your anotation classes\n",
"the codes.txt should look like this\n",
"Void\n",
"cat\n",
"dog\n",
"\n",
"The code below maps the codes.txt anotations to raster RGB values ect Void = 0,0,0 cat = 1,1,1\n",
"\n",
"Once you have run this code you should be able to use it as an input to the Camvid notebook.\n",
"You will also need to change the val/train split in the Camvid notebook from using the valid.txt file to using a random splitter and the remove the part that adds the \"_P\" to the labeled images.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image, ImageDraw\n",
"import csv\n",
"from tkinter import Tk\n",
"from tkinter.filedialog import askopenfilename\n",
"import os\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"#open a dialog to find the VGG2 csv file\n",
"#if you have data in VGG1 format open it and VGG2 and save it back out\n",
"Tk().withdraw()\n",
"VIA_csv = askopenfilename()\n",
"#print(VIA_csv)\n",
"directory = os.path.dirname(VIA_csv)+\"/\"\n",
"#print(directory)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Void', 'square', 'turkey', 'valley', 'roaded', 'ditch', 'shed', 'tree', 'outcrop', 'tank', 'yard', 'golf', 'crop', 'salt', '']\n"
]
}
],
"source": [
"#load in codes file for converting poly types into colour codes\n",
"with open(directory+'codes.txt') as f:\n",
" codes = f.readlines()\n",
" codes = [s.strip('\\n') for s in codes]\n",
" print(codes)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Creation of the directory %s failed\n"
]
}
],
"source": [
"#make labels folder\n",
"try:\n",
" os.mkdir(directory+\"labels/\")\n",
"except OSError:\n",
" print (\"Creation of the directory %s failed\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"#loads the VIA v2 CSV file\n",
"with open(VIA_csv) as csvfile:\n",
" readCSV = csv.reader(csvfile, delimiter=',')\n",
"\n",
" count = 0\n",
" list_of_types=[]\n",
" list_of_polygons=[]\n",
" last_filename=\"\"\n",
" images_folder_dir = (directory+\"images/\")\n",
" #print(images_folder_dir)\n",
" for row in readCSV:\n",
" #below skips the first row empty rows and rows with missing images\n",
" #print(image_dir)\n",
" if (count != 0) and (len(row[5])!= 2) and (os.path.isfile(images_folder_dir+row[0])):\n",
" \n",
" if last_filename == \"\":\n",
" last_filename = row[0] \n",
" x_and_y=(row[5].replace('{\"name\":\"polygon\",\"all_points_x\":[', '').replace('\"all_points_y\":', '')[:-2].split('],[')) \n",
" x_list = x_and_y[0].split(',')\n",
" y_list = x_and_y[1].split(',')\n",
" list_index=0\n",
" poly_list=[]\n",
" #build list of poly points\n",
" for x_pos in x_list:\n",
" #print(x_pos)\n",
" y_pos = int(y_list[list_index])\n",
" #print(y_pos)\n",
" x_pos = int(x_pos)\n",
" poly_list.append(x_pos)\n",
" poly_list.append(y_pos)\n",
" list_index+=1\n",
" #get poly type\n",
" type_reduce = (row[6]).replace('{\"type\":{\"', '').replace('\":true}}', '').replace('\"}', '').replace('{\"class\":', '').replace('\"', '').replace('{', '').replace('}', '')\n",
" list_of_types.append(type_reduce)\n",
" #get dimention of target image\n",
" image_found = Image.open(images_folder_dir+row[0])\n",
" width, height = image_found.size\n",
" list_of_polygons.append(poly_list)\n",
" total_polygon = int(row[3])\n",
" this_polygon = int(row[4])+1\n",
" if total_polygon == this_polygon:\n",
" back = Image.new('RGB', (width,height), (0,0,0))\n",
" poly = Image.new('RGB', (width,height))\n",
" pdraw = ImageDraw.Draw(poly)\n",
" colour_counter = 0\n",
" for each_poly in list_of_polygons:\n",
" #set colour\n",
" colour=(codes.index(list_of_types[colour_counter]))\n",
" pdraw.polygon(each_poly,fill=(colour,colour,colour))\n",
" colour_counter =+1 \n",
" back.paste(poly)\n",
" back.paste(poly)\n",
" back.save(directory+\"labels/\"+(os.path.splitext(os.path.basename(row[0]))[0])+\".png\")\n",
" list_of_polygons=[]\n",
" list_of_types=[]\n",
" last_filename = row[0]\n",
" #if (len(row[5])== 2):\n",
" #deals with images with classes, calls them Void 0,0,0 \n",
" if (len(row[5])== 2):\n",
" image_found = Image.open(images_folder_dir+row[0])\n",
" #print(image_found)\n",
" width, height = image_found.size\n",
" img = Image.new('RGB', (width,height), (0,0,0))\n",
" img.save(directory+\"labels/\"+(os.path.splitext(os.path.basename(row[0]))[0])+\".png\")\n",
" \n",
"\n",
" count +=1\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment