From 55412467b9a208be239ac92ecd7ef2a2e9269956 Mon Sep 17 00:00:00 2001 From: Guanghua Yu <742925032@qq.com> Date: Wed, 8 Jul 2020 18:34:23 +0800 Subject: [PATCH] Change PIL.Image to cv2 in face_detection (#1035) --- configs/face_detection/blazeface_nas.yml | 9 --------- configs/face_detection/blazeface_nas_v2.yml | 11 +---------- tools/face_eval.py | 19 ++++++++++--------- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/configs/face_detection/blazeface_nas.yml b/configs/face_detection/blazeface_nas.yml index 8f7b42170..5e0f81b23 100644 --- a/configs/face_detection/blazeface_nas.yml +++ b/configs/face_detection/blazeface_nas.yml @@ -98,10 +98,6 @@ EvalReader: - !DecodeImage to_rgb: true - !NormalizeBox {} - - !ResizeImage - interp: 1 - target_size: 640 - use_cv2: false - !Permute {} - !NormalizeImage is_scale: false @@ -111,7 +107,6 @@ EvalReader: TestReader: inputs_def: - image_shape: [3,640,640] fields: ['image', 'im_id', 'im_shape'] dataset: !ImageFolder @@ -119,10 +114,6 @@ TestReader: sample_transforms: - !DecodeImage to_rgb: true - - !ResizeImage - interp: 1 - target_size: 640 - use_cv2: false - !Permute {} - !NormalizeImage is_scale: false diff --git a/configs/face_detection/blazeface_nas_v2.yml b/configs/face_detection/blazeface_nas_v2.yml index e1d604752..f13c6c37a 100644 --- a/configs/face_detection/blazeface_nas_v2.yml +++ b/configs/face_detection/blazeface_nas_v2.yml @@ -7,7 +7,7 @@ log_smooth_window: 20 log_iter: 20 metric: WIDERFACE save_dir: output -weights: output/blazeface_nas/model_final +weights: output/blazeface_nas_v2/model_final # 1(label_class) + 1(background) num_classes: 2 @@ -98,10 +98,6 @@ EvalReader: - !DecodeImage to_rgb: true - !NormalizeBox {} - - !ResizeImage - interp: 1 - target_size: 640 - use_cv2: false - !Permute {} - !NormalizeImage is_scale: false @@ -111,7 +107,6 @@ EvalReader: TestReader: inputs_def: - image_shape: [3,640,640] fields: ['image', 'im_id', 'im_shape'] dataset: !ImageFolder @@ -119,10 +114,6 @@ TestReader: sample_transforms: - !DecodeImage to_rgb: true - - !ResizeImage - interp: 1 - target_size: 640 - use_cv2: false - !Permute {} - !NormalizeImage is_scale: false diff --git a/tools/face_eval.py b/tools/face_eval.py index a32eb6a57..fb6517d01 100644 --- a/tools/face_eval.py +++ b/tools/face_eval.py @@ -25,7 +25,7 @@ if parent_path not in sys.path: import paddle.fluid as fluid import numpy as np -from PIL import Image +import cv2 from collections import OrderedDict import ppdet.utils.checkpoint as checkpoint @@ -81,9 +81,10 @@ def face_eval_run(exe, if eval_mode == 'fddb': image_path += '.jpg' assert os.path.exists(image_path) - image = Image.open(image_path).convert('RGB') + image = cv2.imread(image_path) + image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if multi_scale: - shrink, max_shrink = get_shrink(image.size[1], image.size[0]) + shrink, max_shrink = get_shrink(image.shape[0], image.shape[1]) det0 = detect_face(exe, compile_program, fetches, image, shrink) det1 = flip_test(exe, compile_program, fetches, image, shrink) [det2, det3] = multi_scale_test(exe, compile_program, fetches, @@ -106,10 +107,10 @@ def face_eval_run(exe, def detect_face(exe, compile_program, fetches, image, shrink): - image_shape = [3, image.size[1], image.size[0]] + image_shape = [3, image.shape[0], image.shape[1]] if shrink != 1: h, w = int(image_shape[1] * shrink), int(image_shape[2] * shrink) - image = image.resize((w, h), Image.ANTIALIAS) + image = cv2.resize(image, (w, h)) image_shape = [3, h, w] img = face_img_process(image) @@ -133,13 +134,13 @@ def detect_face(exe, compile_program, fetches, image, shrink): def flip_test(exe, compile_program, fetches, image, shrink): - img = image.transpose(Image.FLIP_LEFT_RIGHT) + img = cv2.flip(image, 1) det_f = detect_face(exe, compile_program, fetches, img, shrink) det_t = np.zeros(det_f.shape) - # image.size: [width, height] - det_t[:, 0] = image.size[0] - det_f[:, 2] + img_width = image.shape[1] + det_t[:, 0] = img_width - det_f[:, 2] det_t[:, 1] = det_f[:, 1] - det_t[:, 2] = image.size[0] - det_f[:, 0] + det_t[:, 2] = img_width - det_f[:, 0] det_t[:, 3] = det_f[:, 3] det_t[:, 4] = det_f[:, 4] return det_t -- GitLab