JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
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
7import numpy as np
8import cv2 as cv
9
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
setTarget(self, target_id)
Definition pphumanseg.py:34
setBackend(self, backend_id)
Definition pphumanseg.py:30
_preprocess(self, image)
Definition pphumanseg.py:38
__init__(self, modelPath, backendId=0, targetId=0)
Definition pphumanseg.py:11
infer(self, image)
Definition pphumanseg.py:44
_postprocess(self, outputBlob)
Definition pphumanseg.py:60