JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
youtureid.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(modelPath)
17 self._model.setPreferableBackend(self._backendId)
18 self._model.setPreferableTarget(self._targetId)
19
20 self._input_size = (128, 256) # fixed
21 self._output_dim = 768
22 self._mean = (0.485, 0.456, 0.406)
23 self._std = (0.229, 0.224, 0.225)
24
25 @property
26 def name(self):
27 return self.__class__.__name__
28
29 def setBackend(self, backend_id):
30 self._backendId = backend_id
31 self._model.setPreferableBackend(self._backendId)
32
33 def setTarget(self, target_id):
34 self._targetId = target_id
35 self._model.setPreferableTarget(self._targetId)
36
37 def _preprocess(self, image):
38 image = image[:, :, ::-1]
39 image = (image / 255.0 - self._mean) / self._std
40 return cv.dnn.blobFromImage(image.astype(np.float32))
41 # return cv.dnn.blobFromImage(image, scalefactor=(1.0/255.0), size=self._input_size, mean=self._mean) / self._std
42
43 def infer(self, image):
44 # Preprocess
45 inputBlob = self._preprocess(image)
46
47 # Forward
48 self._model.setInput(inputBlob)
49 features = self._model.forward()
50 return np.reshape(features, (features.shape[0], features.shape[1]))
51
52 def query(self, query_img_list, gallery_img_list, topK=5):
53 query_features_list = []
54 for q in query_img_list:
55 query_features_list.append(self.infer(q))
56 query_features = np.concatenate(query_features_list, axis=0)
57 query_norm = np.linalg.norm(query_features, ord=2, axis=1, keepdims=True)
58 query_arr = query_features / (query_norm + np.finfo(np.float32).eps)
59
60 gallery_features_list = []
61 for g in gallery_img_list:
62 gallery_features_list.append(self.infer(g))
63 gallery_features = np.concatenate(gallery_features_list, axis=0)
64 gallery_norm = np.linalg.norm(gallery_features, ord=2, axis=1, keepdims=True)
65 gallery_arr = gallery_features / (gallery_norm + np.finfo(np.float32).eps)
66
67 dist = np.matmul(query_arr, gallery_arr.T)
68 idx = np.argsort(-dist, axis=1)
69 return [i[0:topK] for i in idx]
70
query(self, query_img_list, gallery_img_list, topK=5)
Definition youtureid.py:52
_preprocess(self, image)
Definition youtureid.py:37
infer(self, image)
Definition youtureid.py:43
setTarget(self, target_id)
Definition youtureid.py:33
setBackend(self, backend_id)
Definition youtureid.py:29
__init__(self, modelPath, backendId=0, targetId=0)
Definition youtureid.py:11