Advertisement
Twinsen85

create_name_list.py

Apr 9th, 2018
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. import os
  2. from PIL import Image
  3. import translate_xml
  4. import tensorflow as tf
  5. import cv2
  6.  
  7. imagepath = './datasets/VOCdevkit/VOC2007/JPEGImages'
  8. # imagepath = './ssd_model/VOCdevkit/VOC2007/JPEGImages'
  9. xmlpath = imagepath.replace('JPEGImages', 'Annotations')
  10. PATH_TO_LABELS = './pb/pascal_label_map.pbtxt'
  11.  
  12. files = os.listdir(imagepath)
  13. mode_to_depth = {'L': 0, 'P': 1, 'RGB': 3, 'RGBA': 4, 'CMYK': 4, 'I;16': 2, 'I;16L': 2, 'I;16B': 2}
  14.  
  15. cascPath = "haarcascade_frontalface_default.xml"
  16. faceCascade = cv2.CascadeClassifier(cascPath)
  17.  
  18. with tf.gfile.GFile(PATH_TO_LABELS, 'r') as fid:
  19.     label_map_string = fid.read()
  20.  
  21.  
  22. def run():
  23.     fo = open('./datasets/VOCdevkit/VOC2007/ImageSets/Main/train.txt', 'w+')
  24.     # fo = open('./ssd_model/VOCdevkit/VOC2007/ImageSets/Main/train.txt', 'w+')
  25.  
  26.     remove_files = list()
  27.     write_files = list()
  28.     remove_files.clear()
  29.     write_files.clear()
  30.     for file in files:
  31.         if not os.path.isdir(file):
  32.             imagefile = imagepath + '/' + file
  33.             try:
  34.                 faceimage = cv2.imread(imagefile)
  35.                 gray = cv2.cvtColor(faceimage, cv2.COLOR_BGR2GRAY)
  36.                 faces = faceCascade.detectMultiScale(
  37.                     gray,
  38.                     scaleFactor=1.05,
  39.                     minNeighbors=1,
  40.                     minSize=(30, 30),
  41.                     flags=cv2.IMREAD_GRAYSCALE
  42.                 )
  43.                 if not len(faces):
  44.                     raise Exception("No Face Detected !")
  45.  
  46.                 with Image.open(imagefile) as image:
  47.                     (width, height) = image.size
  48.                     # xmlpath = imagepath.replace('JPEGImages', 'Annotations')
  49.                     image.close()
  50.                     dicts = translate_xml.xml_file_to_dicts(xmlpath + '/' + file.replace('.jpg', '.xml'))
  51.                     if '.jpg' not in dicts['annotation']['filename']:
  52.                         file = file.replace('.jpg', '')
  53.  
  54.                     if file == dicts['annotation']['filename']:
  55.                         if not str(width) == dicts['annotation']['size']['width'] \
  56.                                 or not str(height) == dicts['annotation']['size']['height'] \
  57.                                 or not '3' == dicts["annotation"]['size']['depth']:
  58.                             raise Exception('object size Error')
  59.                         if 'object' in dicts['annotation']:
  60.                             for i in range(len(dicts['annotation']['object'])):
  61.                                 if int(dicts['annotation']['object'][i]['bndbox']['xmax']) >= width \
  62.                                         or int(dicts['annotation']['object'][i]['bndbox']['ymax']) >= height \
  63.                                         or int(dicts['annotation']['object'][i]['bndbox']['xmax']) == 0 \
  64.                                         or int(dicts['annotation']['object'][i]['bndbox']['ymax']) == 0:
  65.                                     raise Exception('object width or height Error')
  66.                                 if 'bndbox' not in dicts['annotation']['object'][i]:
  67.                                     raise Exception('bndbox Error')
  68.                                 if 'name' not in dicts['annotation']['object'][i]:
  69.                                     raise Exception('name Error')
  70.                                 if dicts['annotation']['object'][i]['name'] not in label_map_string:
  71.                                     raise Exception('name Error')
  72.                         else:
  73.                             raise Exception('none objects Error')
  74.                         if '.jpg' in file:
  75.                             file = file.replace('.jpg', '')
  76.                         file = '\n' + file
  77.                         write_files.append(file)
  78.                     else:
  79.                         raise Exception('xml file Error')
  80.  
  81.             except Exception as ex:
  82.                 remove_files.append(file)
  83.                 print(str(ex))
  84.  
  85.     write_files.sort()
  86.     fo.writelines(write_files)
  87.     fo.close()
  88.  
  89.     if len(remove_files) > 0:
  90.         for remove_file in remove_files:
  91.             try:
  92.                 remove_filename = remove_file
  93.                 if '.jpg' not in remove_file:
  94.                     remove_filename = remove_file + '.jpg'
  95.                 os.remove(imagepath + '/' + remove_filename)
  96.                 os.remove(xmlpath + '/' + remove_filename.replace('.jpg', '.xml'))
  97.             except Exception as ex:
  98.                 print(str(ex))
  99.  
  100.  
  101. if __name__ == '__main__':
  102.     run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement