Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "Befor you start you need to set up a couple things.\n",
  8. "Make a new folder, within that folder you need 3 things:\n",
  9. "1 All of your images in a folder called \"images\"\n",
  10. "2 Your VGG VIA anotations exported as a csv\n",
  11. "3 A \"codes.txt\" document with your anotation classes\n",
  12. "the codes.txt should look like this\n",
  13. "Void\n",
  14. "cat\n",
  15. "dog\n",
  16. "\n",
  17. "The code below maps the codes.txt anotations to raster RGB values ect Void = 0,0,0 cat = 1,1,1\n",
  18. "\n",
  19. "Once you have run this code you should be able to use it as an input to the Camvid notebook.\n",
  20. "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",
  21. "\n"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 2,
  27. "metadata": {},
  28. "outputs": [],
  29. "source": [
  30. "from PIL import Image, ImageDraw\n",
  31. "import csv\n",
  32. "from tkinter import Tk\n",
  33. "from tkinter.filedialog import askopenfilename\n",
  34. "import os\n",
  35. "from pathlib import Path"
  36. ]
  37. },
  38. {
  39. "cell_type": "code",
  40. "execution_count": 4,
  41. "metadata": {
  42. "scrolled": true
  43. },
  44. "outputs": [],
  45. "source": [
  46. "#open a dialog to find the VGG2 csv file\n",
  47. "#if you have data in VGG1 format open it and VGG2 and save it back out\n",
  48. "Tk().withdraw()\n",
  49. "VIA_csv = askopenfilename()\n",
  50. "#print(VIA_csv)\n",
  51. "directory = os.path.dirname(VIA_csv)+\"/\"\n",
  52. "#print(directory)"
  53. ]
  54. },
  55. {
  56. "cell_type": "code",
  57. "execution_count": 11,
  58. "metadata": {},
  59. "outputs": [
  60. {
  61. "name": "stdout",
  62. "output_type": "stream",
  63. "text": [
  64. "['Void', 'square', 'turkey', 'valley', 'roaded', 'ditch', 'shed', 'tree', 'outcrop', 'tank', 'yard', 'golf', 'crop', 'salt', '']\n"
  65. ]
  66. }
  67. ],
  68. "source": [
  69. "#load in codes file for converting poly types into colour codes\n",
  70. "with open(directory+'codes.txt') as f:\n",
  71. " codes = f.readlines()\n",
  72. " codes = [s.strip('\\n') for s in codes]\n",
  73. " print(codes)"
  74. ]
  75. },
  76. {
  77. "cell_type": "code",
  78. "execution_count": 12,
  79. "metadata": {},
  80. "outputs": [
  81. {
  82. "name": "stdout",
  83. "output_type": "stream",
  84. "text": [
  85. "Creation of the directory %s failed\n"
  86. ]
  87. }
  88. ],
  89. "source": [
  90. "#make labels folder\n",
  91. "try:\n",
  92. " os.mkdir(directory+\"labels/\")\n",
  93. "except OSError:\n",
  94. " print (\"Creation of the directory %s failed\")"
  95. ]
  96. },
  97. {
  98. "cell_type": "code",
  99. "execution_count": 15,
  100. "metadata": {},
  101. "outputs": [],
  102. "source": [
  103. "#loads the VIA v2 CSV file\n",
  104. "with open(VIA_csv) as csvfile:\n",
  105. " readCSV = csv.reader(csvfile, delimiter=',')\n",
  106. "\n",
  107. " count = 0\n",
  108. " list_of_types=[]\n",
  109. " list_of_polygons=[]\n",
  110. " last_filename=\"\"\n",
  111. " images_folder_dir = (directory+\"images/\")\n",
  112. " #print(images_folder_dir)\n",
  113. " for row in readCSV:\n",
  114. " #below skips the first row empty rows and rows with missing images\n",
  115. " #print(image_dir)\n",
  116. " if (count != 0) and (len(row[5])!= 2) and (os.path.isfile(images_folder_dir+row[0])):\n",
  117. " \n",
  118. " if last_filename == \"\":\n",
  119. " last_filename = row[0] \n",
  120. " x_and_y=(row[5].replace('{\"name\":\"polygon\",\"all_points_x\":[', '').replace('\"all_points_y\":', '')[:-2].split('],[')) \n",
  121. " x_list = x_and_y[0].split(',')\n",
  122. " y_list = x_and_y[1].split(',')\n",
  123. " list_index=0\n",
  124. " poly_list=[]\n",
  125. " #build list of poly points\n",
  126. " for x_pos in x_list:\n",
  127. " #print(x_pos)\n",
  128. " y_pos = int(y_list[list_index])\n",
  129. " #print(y_pos)\n",
  130. " x_pos = int(x_pos)\n",
  131. " poly_list.append(x_pos)\n",
  132. " poly_list.append(y_pos)\n",
  133. " list_index+=1\n",
  134. " #get poly type\n",
  135. " type_reduce = (row[6]).replace('{\"type\":{\"', '').replace('\":true}}', '').replace('\"}', '').replace('{\"class\":', '').replace('\"', '').replace('{', '').replace('}', '')\n",
  136. " list_of_types.append(type_reduce)\n",
  137. " #get dimention of target image\n",
  138. " image_found = Image.open(images_folder_dir+row[0])\n",
  139. " width, height = image_found.size\n",
  140. " list_of_polygons.append(poly_list)\n",
  141. " total_polygon = int(row[3])\n",
  142. " this_polygon = int(row[4])+1\n",
  143. " if total_polygon == this_polygon:\n",
  144. " back = Image.new('RGB', (width,height), (0,0,0))\n",
  145. " poly = Image.new('RGB', (width,height))\n",
  146. " pdraw = ImageDraw.Draw(poly)\n",
  147. " colour_counter = 0\n",
  148. " for each_poly in list_of_polygons:\n",
  149. " #set colour\n",
  150. " colour=(codes.index(list_of_types[colour_counter]))\n",
  151. " pdraw.polygon(each_poly,fill=(colour,colour,colour))\n",
  152. " colour_counter =+1 \n",
  153. " back.paste(poly)\n",
  154. " back.paste(poly)\n",
  155. " back.save(directory+\"labels/\"+(os.path.splitext(os.path.basename(row[0]))[0])+\".png\")\n",
  156. " list_of_polygons=[]\n",
  157. " list_of_types=[]\n",
  158. " last_filename = row[0]\n",
  159. " #if (len(row[5])== 2):\n",
  160. " #deals with images with classes, calls them Void 0,0,0 \n",
  161. " if (len(row[5])== 2):\n",
  162. " image_found = Image.open(images_folder_dir+row[0])\n",
  163. " #print(image_found)\n",
  164. " width, height = image_found.size\n",
  165. " img = Image.new('RGB', (width,height), (0,0,0))\n",
  166. " img.save(directory+\"labels/\"+(os.path.splitext(os.path.basename(row[0]))[0])+\".png\")\n",
  167. " \n",
  168. "\n",
  169. " count +=1\n"
  170. ]
  171. },
  172. {
  173. "cell_type": "code",
  174. "execution_count": null,
  175. "metadata": {},
  176. "outputs": [],
  177. "source": []
  178. },
  179. {
  180. "cell_type": "code",
  181. "execution_count": null,
  182. "metadata": {},
  183. "outputs": [],
  184. "source": []
  185. },
  186. {
  187. "cell_type": "code",
  188. "execution_count": null,
  189. "metadata": {},
  190. "outputs": [],
  191. "source": []
  192. },
  193. {
  194. "cell_type": "code",
  195. "execution_count": null,
  196. "metadata": {},
  197. "outputs": [],
  198. "source": []
  199. }
  200. ],
  201. "metadata": {
  202. "kernelspec": {
  203. "display_name": "Python 3",
  204. "language": "python",
  205. "name": "python3"
  206. },
  207. "language_info": {
  208. "codemirror_mode": {
  209. "name": "ipython",
  210. "version": 3
  211. },
  212. "file_extension": ".py",
  213. "mimetype": "text/x-python",
  214. "name": "python",
  215. "nbconvert_exporter": "python",
  216. "pygments_lexer": "ipython3",
  217. "version": "3.6.9"
  218. }
  219. },
  220. "nbformat": 4,
  221. "nbformat_minor": 2
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement