You need to sign in or sign up before continuing.
infer.py 9.1 KB
Newer Older
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
# coding: utf8
# copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import ast
import time
import json
import argparse

import numpy as np
import cv2

import paddle.fluid as fluid

from PIL import Image
from PIL import ImageDraw

import argparse


def parse_args():
    parser = argparse.ArgumentParser('mask detection.')
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    parser.add_argument(
        '--models_dir', type=str, default='', help='path of models.')
    parser.add_argument(
        '--img_paths', type=str, default='', help='path of images')
    parser.add_argument(
        '--video_path', type=str, default='', help='path of video.')
    parser.add_argument(
        '--use_camera',
        type=bool,
        default=False,
        help='switch detect video or camera, default:video.')
    parser.add_argument(
        '--open_imshow',
        type=bool,
        default=False,
        help='visualize results in real time.')
    parser.add_argument(
        '--use_gpu',
        type=bool,
        default=False,
        help='switch cpu/gpu, default:cpu.')
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 105 106 107
    args = parser.parse_args()
    return args


class FaceResult:
    def __init__(self, rect_data, rect_info):
        self.rect_info = rect_info
        self.rect_data = rect_data
        self.class_id = -1
        self.score = 0.0


def VisualizeResult(im, faces):
    LABELS = ['NO_MASK', 'MASK']
    COLORS = [(0, 0, 255), (0, 255, 0)]
    for face in faces:
        label = LABELS[face.class_id]
        color = COLORS[face.class_id]
        left, right, top, bottom = [int(item) for item in face.rect_info]
        label_position = (left, top)
        cv2.putText(im, label, label_position, cv2.FONT_HERSHEY_SIMPLEX, 1,
                    color, 2, cv2.LINE_AA)
        cv2.rectangle(im, (left, top), (right, bottom), color, 3)
    return im


def LoadModel(model_dir, use_gpu=False):
    config = fluid.core.AnalysisConfig(model_dir + '/__model__',
                                       model_dir + '/__params__')
    if use_gpu:
        config.enable_use_gpu(100, 0)
        config.switch_ir_optim(True)
    else:
        config.disable_gpu()
    config.disable_glog_info()
    config.switch_specify_input_names(True)
    config.enable_memory_optim()
    return fluid.core.create_paddle_predictor(config)


class MaskClassifier:
    def __init__(self, model_dir, mean, scale, use_gpu=False):
        self.mean = np.array(mean).reshape((3, 1, 1))
        self.scale = np.array(scale).reshape((3, 1, 1))
        self.predictor = LoadModel(model_dir, use_gpu)
        self.EVAL_SIZE = (128, 128)

    def Preprocess(self, faces):
        h, w = self.EVAL_SIZE[1], self.EVAL_SIZE[0]
        inputs = []
        for face in faces:
108 109 110 111 112
            im = cv2.resize(
                face.rect_data, (128, 128),
                fx=0,
                fy=0,
                interpolation=cv2.INTER_CUBIC)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            # HWC -> CHW
            im = im.swapaxes(1, 2)
            im = im.swapaxes(0, 1)
            # Convert to float
            im = im[:, :, :].astype('float32') / 256.0
            # im  = (im - mean) * scale
            im = im - self.mean
            im = im * self.scale
            im = im[np.newaxis, :, :, :]
            inputs.append(im)
        return inputs

    def Postprocess(self, output_data, faces):
        argmx = np.argmax(output_data, axis=1)
        for idx in range(len(faces)):
            faces[idx].class_id = argmx[idx]
            faces[idx].score = output_data[idx][argmx[idx]]
        return faces

    def Predict(self, faces):
        inputs = self.Preprocess(faces)
        if len(inputs) != 0:
            input_data = np.concatenate(inputs)
            im_tensor = fluid.core.PaddleTensor(
                input_data.copy().astype('float32'))
C
channingss 已提交
138
            output_data = self.predictor.run([im_tensor])[0]
139 140 141 142 143 144 145 146 147 148 149 150 151
            output_data = output_data.as_ndarray()
            self.Postprocess(output_data, faces)


class FaceDetector:
    def __init__(self, model_dir, mean, scale, use_gpu=False, threshold=0.7):
        self.mean = np.array(mean).reshape((3, 1, 1))
        self.scale = np.array(scale).reshape((3, 1, 1))
        self.threshold = threshold
        self.predictor = LoadModel(model_dir, use_gpu)

    def Preprocess(self, image, shrink):
        h, w = int(image.shape[1] * shrink), int(image.shape[0] * shrink)
152 153
        im = cv2.resize(
            image, (h, w), fx=0, fy=0, interpolation=cv2.INTER_CUBIC)
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        # HWC -> CHW
        im = im.swapaxes(1, 2)
        im = im.swapaxes(0, 1)
        # Convert to float
        im = im[:, :, :].astype('float32')
        # im  = (im - mean) * scale
        im = im - self.mean
        im = im * self.scale
        im = im[np.newaxis, :, :, :]
        return im

    def Postprocess(self, output_data, ori_im, shrink):
        det_out = []
        h, w = ori_im.shape[0], ori_im.shape[1]
        for out in output_data:
            class_id = int(out[0])
            score = out[1]
            xmin = (out[2] * w)
            ymin = (out[3] * h)
            xmax = (out[4] * w)
            ymax = (out[5] * h)
            wd = xmax - xmin
            hd = ymax - ymin
            valid = (xmax >= xmin and xmin > 0 and ymax >= ymin and ymin > 0)
            if score > self.threshold and valid:
                roi_rect = ori_im[int(ymin):int(ymax), int(xmin):int(xmax)]
                det_out.append(FaceResult(roi_rect, [xmin, xmax, ymin, ymax]))
        return det_out

    def Predict(self, image, shrink):
        ori_im = image.copy()
        im = self.Preprocess(image, shrink)
        im_tensor = fluid.core.PaddleTensor(im.copy().astype('float32'))
        output_data = self.predictor.run([im_tensor])[0]
        output_data = output_data.as_ndarray()
        return self.Postprocess(output_data, ori_im, shrink)


def predict_images(args):
193 194 195 196 197 198 199 200 201 202 203 204
    detector = FaceDetector(
        model_dir=args.models_dir + '/pyramidbox_lite/',
        mean=[104.0, 177.0, 123.0],
        scale=[0.007843, 0.007843, 0.007843],
        use_gpu=args.use_gpu,
        threshold=0.7)

    classifier = MaskClassifier(
        model_dir=args.models_dir + '/mask_detector/',
        mean=[0.5, 0.5, 0.5],
        scale=[1.0, 1.0, 1.0],
        use_gpu=args.use_gpu)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    names = []
    image_paths = []
    for name in os.listdir(args.img_paths):
        if name.split('.')[-1] in ['jpg', 'png', 'jpeg']:
            names.append(name)
            image_paths.append(os.path.join(args.img_paths, name))
    images = [cv2.imread(path, cv2.IMREAD_COLOR) for path in image_paths]

    path = './result'
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
    for idx in range(len(images)):
        im = images[idx]
        det_out = detector.Predict(im, shrink=0.7)
        classifier.Predict(det_out)
        img = VisualizeResult(im, det_out)
        cv2.imwrite(os.path.join(path, names[idx] + '.result.jpg'), img)


def predict_video(args, im_shape=(1920, 1080), use_camera=False):
    if args.use_camera:
        capture = cv2.VideoCapture(0)
    else:
        capture = cv2.VideoCapture(args.video_path)
230 231 232 233 234 235 236 237 238 239 240 241
    detector = FaceDetector(
        model_dir=args.models_dir + '/pyramidbox_lite/',
        mean=[104.0, 177.0, 123.0],
        scale=[0.007843, 0.007843, 0.007843],
        use_gpu=args.use_gpu,
        threshold=0.7)

    classifier = MaskClassifier(
        model_dir=args.models_dir + '/mask_detector/',
        mean=[0.5, 0.5, 0.5],
        scale=[1.0, 1.0, 1.0],
        use_gpu=args.use_gpu)
242 243 244 245 246 247

    path = './result'
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
    fps = 30
248 249
    width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
250
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
251 252
    writer = cv2.VideoWriter(
        os.path.join(path, 'result.mp4'), fourcc, fps, (width, height))
253 254 255 256 257 258 259 260 261 262 263 264 265 266
    import time
    start_time = time.time()
    index = 0
    while (1):
        ret, frame = capture.read()
        if not ret:
            break
        print('detect frame:%d' % (index))
        index += 1
        det_out = detector.Predict(frame, shrink=0.5)
        classifier.Predict(det_out)
        end_pre = time.time()
        im = VisualizeResult(frame, det_out)
        writer.write(im)
267 268 269 270
        if args.open_imshow:
            cv2.imshow('Mask Detection', im)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
271
    end_time = time.time()
272
    print("Average prediction time per frame:", (end_time - start_time) / index)
273 274 275 276 277 278 279 280
    writer.release()


if __name__ == "__main__":
    args = parse_args()
    print(args.models_dir)
    if args.img_paths != '':
        predict_images(args)
281
    elif args.video_path != '' or args.use_camera:
282
        predict_video(args)