1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| import sys sys.path.append("./PaddleDetection")
import cv2 import paddle import numpy as np from PIL import Image, ImageDraw, ImageFont 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__': config_path = './PaddleDetection/configs/face_detection/blazeface_1000e.yml' cfg = load_config(config_path) weight_path = 'blazeface_1000e.pdparams'
cap = cv2.VideoCapture('/Xian.mp4') cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
if not cap.isOpened(): print('无法打开摄像头') exit()
cfg.weights = weight_path bbox_thre = 0.8 paddle.set_device('gpu')
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()
while True: ret, frame = cap.read()
if not ret: print("无法接受帧,请退出") break
im = frame
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]))
outs = trainer.model(data_dict)
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]
for box in bbox: x1, y1, w, h = box['bbox']
im = Image.fromarray(im) draw = ImageDraw.Draw(im)
font = ImageFont.truetype("SimHei.ttf", 15, encoding="utf-8") string = '概率:{}%'.format(round(box['score'] * 100, 2)) draw.text((int(x1), int(y1) - 15), string, (0, 255, 0), font=font) im = np.array(im).copy()
cv2.rectangle(im, (int(x1), int(y1)), (int(x1 + w), int(y1 + h)), (0, 255, 0), 2)
cv2.imshow('Camera', im)
if cv2.waitKey(1) & 0xFF == ord('q'): break
cap.release() cv2.destoryALLWindows()
|