From 824defab50841e33e931e523bde890eb5b64db28 Mon Sep 17 00:00:00 2001 From: "freshield.eth" Date: Tue, 3 Jan 2023 15:33:36 +0800 Subject: [PATCH] add python example for face detection part in English readme file (#7568) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add python example for face detection part 为了方便二次开发,这里添加了直接使用Python脚本的形式进行人脸检测的示例。 * change the demo image * add python example for face detection part in English readme file sync the same face detection python example from the Chinese readme file --- configs/face_detection/README_en.md | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/configs/face_detection/README_en.md b/configs/face_detection/README_en.md index bf798b3c0..f7295f32d 100644 --- a/configs/face_detection/README_en.md +++ b/configs/face_detection/README_en.md @@ -110,6 +110,59 @@ legend_name = 'Paddle-BlazeFace'; matlab -nodesktop -nosplash -nojvm -r "run wider_eval.m;quit;" ``` +### Use by Python Code +In order to support development, here is an example of using the Paddle Detection whl package to make predictions through Python code. +```python +import cv2 +import paddle +import numpy as np +from ppdet.core.workspace import load_config +from ppdet.engine import Trainer +from ppdet.metrics import get_infer_results +from ppdet.data.transform.operators import NormalizeImage, Permute + + +if __name__ == '__main__': + # prepare for the parameters + config_path = 'PaddleDetection/configs/face_detection/blazeface_1000e.yml' + cfg = load_config(config_path) + weight_path = 'PaddleDetection/output/blazeface_1000e.pdparams' + infer_img_path = 'PaddleDetection/demo/hrnet_demo.jpg' + cfg.weights = weight_path + bbox_thre = 0.8 + paddle.set_device('gpu') + # create the class object + trainer = Trainer(cfg, mode='test') + trainer.load_weights(cfg.weights) + trainer.model.eval() + normaler = NormalizeImage(mean=[123, 117, 104], std=[127.502231, 127.502231, 127.502231], is_scale=False) + permuter = Permute() + # read the image file + im = cv2.imread(infer_img_path) + im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) + # prepare for the data dict + data_dict = {'image': im} + data_dict = normaler(data_dict) + data_dict = permuter(data_dict) + h, w, c = im.shape + data_dict['im_id'] = paddle.Tensor(np.array([[0]])) + data_dict['im_shape'] = paddle.Tensor(np.array([[h, w]], dtype=np.float32)) + data_dict['scale_factor'] = paddle.Tensor(np.array([[1., 1.]], dtype=np.float32)) + data_dict['image'] = paddle.Tensor(data_dict['image'].reshape((1, c, h, w))) + data_dict['curr_iter'] = paddle.Tensor(np.array([0])) + # do the prediction + outs = trainer.model(data_dict) + # to do the postprocess to get the final bbox info + for key in ['im_shape', 'scale_factor', 'im_id']: + outs[key] = data_dict[key] + for key, value in outs.items(): + outs[key] = value.numpy() + clsid2catid, catid2name = {0: 'face'}, {0: 0} + batch_res = get_infer_results(outs, clsid2catid) + bbox = [sub_dict for sub_dict in batch_res['bbox'] if sub_dict['score'] > bbox_thre] + print(bbox) +``` + ## Citations -- GitLab