Advertisement
kfirbarzilay

final data code

Apr 8th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.21 KB | None | 0 0
  1. """mask """
  2.  
  3. # import the necessary packages
  4. import cv2
  5. import os, os.path
  6. import numpy as np
  7. #debug info OpenCV version
  8. print ("OpenCV version: " + cv2.__version__)
  9.  
  10. #image path and valid extensions
  11. imageDir = "/home/kfir/data" #specify your path here
  12. image_path_list = []
  13. valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] #specify your valid extensions here
  14. valid_image_extensions = [item.lower() for item in valid_image_extensions]#sorting endings
  15.  
  16. #create a list of all the images in directory and
  17. #append images with a vaild extention to image_path_list
  18. for file in os.listdir(imageDir):
  19.     extension = os.path.splitext(file)[1]
  20.     if extension.lower() not in valid_image_extensions:
  21.         continue
  22.     image_path_list.append(os.path.join(imageDir, file))
  23. image_path_list = sorted(image_path_list)
  24.    
  25. #loop through image_path_list to open each image
  26. for imagePath in image_path_list:
  27.     image = cv2.imread(imagePath)
  28.     # blur the frame and convert it to HSV color space
  29.     blurred = cv2.GaussianBlur(image, (5, 5), 0)    
  30.     hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
  31.    
  32.     #define the lower and upper boundaries in the HSV coordiantes.
  33.     lower = np.array([90,85,0])
  34.     upper = np.array([120,255,255])
  35.    
  36.     # define a filtering kernel
  37.     kernel = np.ones((5,5), np.uint8)
  38.    
  39.     #construct a mask for the desired color range, then perform a series of
  40.     #erosions and dilations (or opening and closing) to remove any small noise left    
  41.     mask = cv2.inRange(hsv, lower, upper)
  42. #    mask = cv2.erode(mask, None, iterations = 2) #erosion
  43. #    mask = cv2.dilate(mask, None, iterations = 2)   #dilation
  44.     mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)#opening
  45.     mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)#closing
  46.     res = cv2.bitwise_and(image,image, mask = mask)
  47.          
  48.     # find contours in the mask and initialize the current
  49.     # (x, y) center of the objects
  50.     cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
  51.     cv2.CHAIN_APPROX_SIMPLE)[-2]
  52.     center = None
  53.    
  54.     #only proceed if at least one contour was found
  55.     if len(cnts) > 0:
  56.         for i in range(len(cnts)):
  57.             x,y,w,h = cv2.boundingRect(cnts[i])#retrieves a bounding rectangle pixel locations
  58.             M = cv2.moments(cnts[i])#the contour moments
  59.             center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))#x and Y center of mass
  60.        
  61.             # only proceed if the rectangle meets a minimum size
  62.             # only look in the bottom third part of the image, overlook the car
  63.             if (w > 15 and h > 25) and (w < 60 and h < 80):
  64.                 if center[1] > 480 and center[1] < 600:
  65. #                    print("printing %s",imagePath)
  66.                     # draw the box and centroid on the image
  67.                     cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
  68.                     #annotate the bounding box to a new file
  69. #                    up_x = x
  70. #                    up_y = y
  71. #                    down_x = x + w
  72. #                    down_y = y + h
  73. #                    with open(imagePath +".txt","a+")  as dataFile:
  74. #                        dataFile.write(" ".join([" cone",str(up_x),str(up_y),str(down_x),str(down_y)]))
  75.                
  76. #    show the resulted image (and optionally mask and res)
  77.     cv2.imshow('image',image)
  78.     cv2.imshow('mask',mask)
  79.     cv2.imshow('res',res)
  80.     #0 = wait indefinitely
  81.     #exit when escape key is pressed
  82.     key = cv2.waitKey(0)
  83.     if key == 27: # escape
  84.         break
  85.  
  86.     # close any open windows
  87.     cv2.destroyAllWindows()
  88.  
  89. xml_files = []
  90. #get teh text files
  91. def get_txt_files(path):
  92.     for file in os.listdir(path):
  93.         extension = os.path.splitext(file)[1]
  94.         if extension == ".txt":
  95.             xml_files.append(os.path.join(path, file))
  96.         else:
  97.             continue
  98.  
  99. get_txt_files(imageDir)
  100. xml_files = sorted(xml_files)  
  101. #the xml format
  102. def create_xml(text,name):
  103.     xmls = ['<annotation>\n', '\t<folder>VOC2007</folder>\n', '\t<filename>000026.jpg</filename>\n', '\t<source>\n', '\t\t<database>The VOC2007 Database</database>\n', '\t\t<annotation>PASCAL VOC2007</annotation>\n', '\t\t<image>flickr</image>\n', '\t\t<flickrid>192073981</flickrid>\n', '\t</source>\n', '\t<owner>\n', '\t\t<flickrid>tobeng</flickrid>\n', '\t\t<name>kfir_yotam</name>\n', '\t</owner>\n', '\t<size>\n', '\t\t<width>1280</width>\n', '\t\t<height>720</height>\n', '\t\t<depth>3</depth>\n', '\t</size>\n', '\t<segmented>0</segmented>\n', '\t<object>\n', '\t\t<name>car</name>\n', '\t\t<pose>unspecified</pose>\n', '\t\t<truncated>0</truncated>\n', '\t\t<difficult>0</difficult>\n', '\t\t<bndbox>\n', '\t\t\t<xmin>90</xmin>\n', '\t\t\t<ymin>125</ymin>\n', '\t\t\t<xmax>337</xmax>\n', '\t\t\t<ymax>212</ymax>\n', '\t\t</bndbox>\n', '\t</object>\n', '</annotation>\n']
  104.     annotation_data = text.read()
  105.     file_name = name
  106.     object_name = "cone"
  107.     x_min = annotation_data.split(' ')[2]
  108.     y_min = annotation_data.split(' ')[3]
  109.     x_max = annotation_data.split(' ')[4]
  110.     y_max = annotation_data.split(' ')[5]
  111.     xmls[2]= '\t<filename>'+file_name+'</filename>\n'
  112.     xmls[20]= '\t\t<name>'+object_name+'</name>\n'
  113.     xmls[25]= '\t\t\t<xmin>'+x_min+'</xmin>\n'
  114.     xmls[26]= '\t\t\t<ymin>'+y_min+'</ymin>\n'
  115.     xmls[27]= '\t\t\t<xmax>'+x_max+'</xmax>\n'
  116.     xmls[28]= '\t\t\t<ymax>'+y_max+'</ymax>\n'
  117.     return xmls
  118.  
  119. #write a list of XML to file
  120. def write_to_file(xmls,file_name):
  121.     f = open(file_name,'w')
  122.     for line in xmls:
  123.         f.write(line)
  124.     f.close()
  125.  
  126. def convert(file_name, text):
  127.     xmls = create_xml(text,name)
  128.     write_to_file(xmls,file_name.split('.')[0]+'.xml')
  129. #loop through .txt files and create .xml
  130. for j in range(len(xml_files)):
  131.     text = open(xml_files[j],'r')
  132.     name = os.path.basename(xml_files[j])
  133.     name = os.path.splitext(name)[0]
  134.     convert(name, text)
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. """ lexicograph """
  145. import os
  146. import shutil
  147.  
  148. itemPath = []
  149. dirPath = []
  150. #Directory where your crawler saves the images
  151. rootDir = "/home/kfir/data"
  152. #Directory where you want to organize your images
  153. destDir = "/home/kfir/data/adapt"
  154.  
  155. #find all the jpg files and save the path
  156. def openFolder(rootDir):
  157.     path = []
  158.     global itemPath
  159.     global dirPath
  160.     for listItem in os.listdir(rootDir):
  161.         path = os.path.join(rootDir,listItem)
  162.         if os.path.isdir(path):
  163.             dirPath.append(path)
  164.             openFolder(path)
  165.         if os.path.isfile(path) and path[-3:]=="jpg":
  166.             itemPath.append(path)
  167.  
  168.  
  169. if __name__ == "__main__":
  170.     openFolder(rootDir)
  171.     files=itemPath
  172.     #Give each image a unique and consistent name
  173.     counter = 0
  174.     progress = 0
  175.     length = len(files)
  176.     #print every 5%
  177.     delta = int(length/20)
  178.     for item in files:
  179.         shutil.move(item,destDir+"/"+str(counter).zfill(6)+".jpg")
  180.         counter += 1
  181.  
  182.  
  183. """ create training and test sets """
  184. # split the data into "train", "test", "validation", "val"
  185. # creates .txt files with the corresponding image serial number
  186. import cv2
  187. import os, os.path
  188. import random
  189. import sys
  190.  
  191. im_path = "/home/kfir/data"
  192. xml_path =  "/home/kfir/data/Annotations"
  193. #appending xml files
  194. xml_files = []
  195. for file in os.listdir(xml_path):
  196.         extension = os.path.splitext(file)[1]
  197.         if extension == ".xml":
  198.             xml_files.append(os.path.join(xml_path, file))
  199.         else:
  200.             continue
  201. images = []
  202. for file in os.listdir(im_path):
  203.         extension = os.path.splitext(file)[1]
  204.         if extension == ".jpg":
  205.             images.append(os.path.join(im_path, file))
  206.         else:
  207.             continue
  208.  
  209. #leaving only the file name without the extension
  210. for j in range(len(xml_files)):
  211.     xml_files[j] = os.path.basename(xml_files[j])
  212.     xml_files[j] = os.path.splitext(xml_files[j])[0]
  213.  
  214. for j in range(len(images)):
  215.     images[j] = os.path.basename(images[j])
  216.     images[j] = os.path.splitext(images[j])[0]
  217.  
  218. xml_files = sorted(xml_files)
  219. images = sorted(images)
  220.  
  221. #i = -1
  222. #j = -1
  223. #counter=0
  224. #while i+1 < len(xml_files):
  225. #    j += 1
  226. #    i += 1
  227. #    while xml_files[i] != images[j]:
  228. ##        os.remove(im_path+"/"+images[j]+".jpg")
  229. #        print(images[j])
  230. #        counter+=1
  231. #        j += 1
  232. #print("counter",counter)
  233.    
  234.  
  235.    
  236. random.shuffle(xml_files)#randomizing the images
  237. # splits the data (make sure test_data and train_data don't overlap)
  238. train_data = xml_files[:round(0.8*len(xml_files))]
  239. test_data = xml_files[len(train_data):]
  240. if train_data[len(train_data)-1] == test_data[0]:
  241.     test_data = xml_files[len(train_data)+1:]
  242. #sort the file names
  243. train_data = sorted(train_data)
  244. test_data = sorted(test_data)
  245. #create the train and test txt files
  246. train = open("train.txt","w+")
  247. trainval = open("trainval.txt","w+")
  248. test = open("test.txt","w+")
  249. cone_train = open("cone_train.txt","w+")
  250. cone_test = open("cone_test.txt","w+")
  251. for i in range(len(train_data)):
  252.     train.write("%s\n" % train_data[i])
  253.     trainval.write("%s\n" % train_data[i])
  254.     cone_train.write("%s 1\n" % train_data[i])
  255. train.close()
  256. cone_train.close()
  257. trainval.close()
  258.  
  259. for i in range(len(test_data)):
  260.     test.write("%s\n" % test_data[i])
  261.     cone_test.write("%s 1\n" % test_data[i])
  262. test.close()
  263. cone_test.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement