Python实现人脸检测与剪裁
# encoding=utf8 ''' Python实现人脸检测与剪裁 ''' # 导入包 import paddlehub as hub import cv2,os # 定义方法 def cv2_crop(im, box): '''cv2实现图片裁剪''' return im.copy()[box[1]:box[3], box[0]:box[2], :] def detection(imagepath, output_dir): '''调用模型检测人脸并剪裁保存''' image = cv2.imread(imagepath) face_detector = hub.Module(name="pyramidbox_lite_mobile") result = face_detector.face_detection(images=[image],visualization=True,shrink=1,output_dir=output_dir) for item in result[0]['data']: box = [item['left'], item['top'], item['right'], item['bottom']] result = cv2_crop(image, box) orgimgname = 'org_' + str(item['left']) + '_' + str(item['top']) + '_' + str(item['right']) + '_' + str(item['bottom']) + '.jpg' minpathc = os.path.join(output_dir, orgimgname) cv2.imwrite(minpathc, result) # 保存裁后图片 print('处理完成: ', item) if __name__ == '__main__': # 待检测图片路径 imagepath = r'D:\Test\image\4.jpeg' # 检测与剪裁后保存路径 output_dir = r'D:\Test\image\ly' # 调用方法 detection(imagepath, output_dir) pass