JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
pphumanseg.py
Go to the documentation of this file.
1 # This file is part of OpenCV Zoo project.
2 # It is subject to the license terms in the LICENSE file found in the same directory.
3 #
4 # Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
5 # Third party copyrights are property of their respective owners.
6 
7 import numpy as np
8 import cv2 as cv
9 
10 class PPHumanSeg:
11  def __init__(self, modelPath, backendId=0, targetId=0):
12  self._modelPath = modelPath
13  self._backendId = backendId
14  self._targetId = targetId
15 
16  self._model = cv.dnn.readNet(self._modelPath)
17  self._model.setPreferableBackend(self._backendId)
18  self._model.setPreferableTarget(self._targetId)
19 
20  self._inputNames = ''
21  self._outputNames = ['save_infer_model/scale_0.tmp_1']
22  self._inputSize = [192, 192]
23  self._mean = np.array([0.5, 0.5, 0.5])[np.newaxis, np.newaxis, :]
24  self._std = np.array([0.5, 0.5, 0.5])[np.newaxis, np.newaxis, :]
25 
26  @property
27  def name(self):
28  return self.__class__.__name__
29 
30  def setBackend(self, backend_id):
31  self._backendId = backend_id
32  self._model.setPreferableBackend(self._backendId)
33 
34  def setTarget(self, target_id):
35  self._targetId = target_id
36  self._model.setPreferableTarget(self._targetId)
37 
38  def _preprocess(self, image):
39  image = image.astype(np.float32, copy=False) / 255.0
40  image -= self._mean
41  image /= self._std
42  return cv.dnn.blobFromImage(image)
43 
44  def infer(self, image):
45  assert image.shape[0] == self._inputSize[1], '{} (height of input image) != {} (preset height)'.format(image.shape[0], self._inputSize[1])
46  assert image.shape[1] == self._inputSize[0], '{} (width of input image) != {} (preset width)'.format(image.shape[1], self._inputSize[0])
47 
48  # Preprocess
49  inputBlob = self._preprocess(image)
50 
51  # Forward
52  self._model.setInput(inputBlob, self._inputNames)
53  outputBlob = self._model.forward(self._outputNames)
54 
55  # Postprocess
56  results = self._postprocess(outputBlob)
57 
58  return results
59 
60  def _postprocess(self, outputBlob):
61  result = np.argmax(outputBlob[0], axis=1).astype(np.uint8)
62  return result
63 
pphumanseg.PPHumanSeg
Definition: pphumanseg.py:10
pphumanseg.PPHumanSeg.infer
def infer(self, image)
Definition: pphumanseg.py:44
pphumanseg.PPHumanSeg._outputNames
_outputNames
Definition: pphumanseg.py:21
pphumanseg.PPHumanSeg.name
def name(self)
Definition: pphumanseg.py:27
pphumanseg.PPHumanSeg._inputSize
_inputSize
Definition: pphumanseg.py:22
pphumanseg.PPHumanSeg.__init__
def __init__(self, modelPath, backendId=0, targetId=0)
Definition: pphumanseg.py:11
pphumanseg.PPHumanSeg._mean
_mean
Definition: pphumanseg.py:23
pphumanseg.PPHumanSeg._modelPath
_modelPath
Definition: pphumanseg.py:12
pphumanseg.PPHumanSeg._model
_model
Definition: pphumanseg.py:16
pphumanseg.PPHumanSeg.setTarget
def setTarget(self, target_id)
Definition: pphumanseg.py:34
pphumanseg.PPHumanSeg._std
_std
Definition: pphumanseg.py:24
pphumanseg.PPHumanSeg._preprocess
def _preprocess(self, image)
Definition: pphumanseg.py:38
pphumanseg.PPHumanSeg._backendId
_backendId
Definition: pphumanseg.py:13
pphumanseg.PPHumanSeg._targetId
_targetId
Definition: pphumanseg.py:14
pphumanseg.PPHumanSeg.setBackend
def setBackend(self, backend_id)
Definition: pphumanseg.py:30
pphumanseg.PPHumanSeg._postprocess
def _postprocess(self, outputBlob)
Definition: pphumanseg.py:60
pphumanseg.PPHumanSeg._inputNames
_inputNames
Definition: pphumanseg.py:20