Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def crop_outdoorRGB(image_path, image_size, margin, gpu_memory_fraction):
- # 1、获取数据
- dataset = list_all_files(image_path)
- with tf.Graph().as_default():
- gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction)
- sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
- with sess.as_default():
- pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
- minsize = 20 # minimum size of face
- threshold = [ 0.6, 0.7, 0.7 ] # three steps's threshold
- factor = 0.709 # scale factor
- failedface_filename = 'D:/Desktop/outdoorfailedfacergb_filename.txt'
- with open(failedface_filename, "w") as text_file:
- nrof_images_total = 0
- nrof_successfully_aligned = 0
- for image_path in dataset:
- nrof_images_total += 1
- try:
- img = misc.imread(image_path)
- except (IOError, ValueError, IndexError) as e:
- errorMessage = '{}: {}'.format(image_path, e)
- print(errorMessage)
- else:
- if img.ndim<2: # 数据的维度<2 ,说明都不是一张图片
- print('Unable to align "%s"' % image_path)
- continue
- if img.ndim == 2: # 数据维度 = 2 ,灰度图,复制3次转化成rgb 3通道图片
- img = function.to_rgb(img)
- img = img[:,:,0:3]
- # # 直方图均衡化
- # img = hil(img)
- # 检测
- # 人脸检测 bounding_boxes:表示边界框 形状为[n,5] 5对应x1,y1,x2,y2,score
- # _:人脸关键点坐标 形状为 [n,10]
- bounding_boxes, landmark = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
- nrof_faces = bounding_boxes.shape[0]
- # bbox = bounding_boxes.tolist()
- # bbox = [y for x in bbox for y in x]
- # landmark = landmark.tolist()
- # landmark = [y for x in landmark for y in x]
- # dict_save[image_path[len(DATAPATH):].split('.')[0]] = (bbox, landmark)\
- # if (nrof_faces != 0) else (None,None,None)
- if nrof_faces>0: # 检测出的框>0,说明检测到了 如果图片中只有一个人的话,nrof_faces = 1
- # [n,4] 人脸框
- nrof_successfully_aligned += 1
- det = bounding_boxes[:,0:4]
- # 保存所有人脸框
- det_arr = []
- img_size = np.asarray(img.shape)[0:2]
- if nrof_faces>1:
- # '''
- # 如果检测出来很多张脸,但是要选一个最大的,采用的方法如下:
- # 用人脸框大小-偏移平方的2倍,得到的结果那个大就选哪个
- # 即人脸框越大越好,偏移量越小越好
- # '''
- # # det [n,0:4]是矩阵
- bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1]) # (x2-x1)*(y2-y1) 人脸框大小
- img_center = img_size / 2 # 原图片中心
- offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
- offset_dist_squared = np.sum(np.power(offsets,2.0),0)
- index = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
- det_arr.append(det[index,:])
- bounding_boxes_using = bounding_boxes[index,:]
- landmark_using = landmark[index,:]
- else:
- bounding_boxes_using = bounding_boxes
- landmark_using = landmark
- bbox = bounding_boxes_using.tolist()
- # bbox = [y for x in bbox for y in x]
- landmark = landmark_using.tolist()
- # landmark = [y for x in landmark for y in x]
- dict_save[image_path[len(DATAPATH):].split('.')[0]] = (bbox, landmark)
- else: # 没有检测到
- dict_save[image_path[len(DATAPATH):].split('.')[0]] = (None,None,None)
- print('Unable to align "%s"' % image_path)
- text_file.write('%s\n' % (image_path))
- path_save = os.path.join('D:/Desktop/Outdoor', "outdoorrgbdetect.txt")
- with open(path_save,'w') as f:
- f.write(str(dict_save))
- f.write('\n')
- print('Total number of images: %d' % nrof_images_total)
- print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement