Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. def crop_outdoorRGB(image_path, image_size, margin, gpu_memory_fraction):
  2. # 1、获取数据
  3. dataset = list_all_files(image_path)
  4.  
  5. with tf.Graph().as_default():
  6. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction)
  7. sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
  8. with sess.as_default():
  9. pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
  10.  
  11. minsize = 20 # minimum size of face
  12. threshold = [ 0.6, 0.7, 0.7 ] # three steps's threshold
  13. factor = 0.709 # scale factor
  14. failedface_filename = 'D:/Desktop/outdoorfailedfacergb_filename.txt'
  15.  
  16. with open(failedface_filename, "w") as text_file:
  17. nrof_images_total = 0
  18. nrof_successfully_aligned = 0
  19.  
  20. for image_path in dataset:
  21. nrof_images_total += 1
  22. try:
  23. img = misc.imread(image_path)
  24. except (IOError, ValueError, IndexError) as e:
  25. errorMessage = '{}: {}'.format(image_path, e)
  26. print(errorMessage)
  27. else:
  28. if img.ndim<2: # 数据的维度<2 ,说明都不是一张图片
  29. print('Unable to align "%s"' % image_path)
  30. continue
  31. if img.ndim == 2: # 数据维度 = 2 ,灰度图,复制3次转化成rgb 3通道图片
  32. img = function.to_rgb(img)
  33. img = img[:,:,0:3]
  34.  
  35. # # 直方图均衡化
  36. # img = hil(img)
  37.  
  38. # 检测
  39. # 人脸检测 bounding_boxes:表示边界框 形状为[n,5] 5对应x1,y1,x2,y2,score
  40. # _:人脸关键点坐标 形状为 [n,10]
  41. bounding_boxes, landmark = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
  42. nrof_faces = bounding_boxes.shape[0]
  43. # bbox = bounding_boxes.tolist()
  44. # bbox = [y for x in bbox for y in x]
  45. # landmark = landmark.tolist()
  46. # landmark = [y for x in landmark for y in x]
  47. # dict_save[image_path[len(DATAPATH):].split('.')[0]] = (bbox, landmark)\
  48. # if (nrof_faces != 0) else (None,None,None)
  49. if nrof_faces>0: # 检测出的框>0,说明检测到了 如果图片中只有一个人的话,nrof_faces = 1
  50. # [n,4] 人脸框
  51. nrof_successfully_aligned += 1
  52. det = bounding_boxes[:,0:4]
  53. # 保存所有人脸框
  54. det_arr = []
  55. img_size = np.asarray(img.shape)[0:2]
  56. if nrof_faces>1:
  57. # '''
  58. # 如果检测出来很多张脸,但是要选一个最大的,采用的方法如下:
  59. # 用人脸框大小-偏移平方的2倍,得到的结果那个大就选哪个
  60. # 即人脸框越大越好,偏移量越小越好
  61. # '''
  62. # # det [n,0:4]是矩阵
  63. bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1]) # (x2-x1)*(y2-y1) 人脸框大小
  64. img_center = img_size / 2 # 原图片中心
  65. offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
  66. offset_dist_squared = np.sum(np.power(offsets,2.0),0)
  67. index = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
  68. det_arr.append(det[index,:])
  69. bounding_boxes_using = bounding_boxes[index,:]
  70. landmark_using = landmark[index,:]
  71. else:
  72. bounding_boxes_using = bounding_boxes
  73. landmark_using = landmark
  74.  
  75. bbox = bounding_boxes_using.tolist()
  76. # bbox = [y for x in bbox for y in x]
  77. landmark = landmark_using.tolist()
  78. # landmark = [y for x in landmark for y in x]
  79. dict_save[image_path[len(DATAPATH):].split('.')[0]] = (bbox, landmark)
  80.  
  81. else: # 没有检测到
  82. dict_save[image_path[len(DATAPATH):].split('.')[0]] = (None,None,None)
  83. print('Unable to align "%s"' % image_path)
  84. text_file.write('%s\n' % (image_path))
  85. path_save = os.path.join('D:/Desktop/Outdoor', "outdoorrgbdetect.txt")
  86. with open(path_save,'w') as f:
  87. f.write(str(dict_save))
  88. f.write('\n')
  89. print('Total number of images: %d' % nrof_images_total)
  90. print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement