def test_SomePermittedUsers_DoorOpenOnce( self, MockRepository: mock.MagicMock, MockRecognizer: mock.MagicMock, MockAccessControlSystem: mock.MagicMock): mock_acs = MockAccessControlSystem.return_value mock_acs.has_access.return_value = True mock_recognizer = MockRecognizer.return_value mock_recognizer.extract.return_value = Vector('1') mock_recognizer.compare_vectors.return_value = 1 mock_repository = MockRepository.return_value mock_repository.get_users.return_value = [ User('1', Vector('1'), ''), User('2', Vector('2'), ''), User('3', Vector('3'), '') ] mock_repository.get_control_points.return_value = [ ControlPoint('1', '1') ] mock_repository.get_cameras.return_value = [ Camera('1', ControlPoint('1', '1')) ] authorizer = Authorizer(mock_repository, mock_recognizer, mock_acs) authorizer.authorize(None) mock_acs.open_door.assert_called_once()
def test_OneScoreIsNone_ContinueAuthorization( self, MockRepository: mock.MagicMock, MockRecognizer: mock.MagicMock, MockAccessControlSystem: mock.MagicMock): mock_acs = MockAccessControlSystem.return_value mock_acs.has_access.return_value = True mock_recognizer = MockRecognizer.return_value mock_recognizer.extract.return_value = Vector('1') mock_recognizer.compare_vectors.side_effect = [None, 1] mock_repository = MockRepository.return_value mock_repository.get_users.return_value = [ User('1', Vector('1'), ''), User('2', Vector('2'), '') ] mock_repository.get_control_points.return_value = [ ControlPoint('1', '1') ] mock_repository.get_cameras.return_value = [ Camera('1', ControlPoint('1', '1')) ] authorizer = Authorizer(mock_repository, mock_recognizer, mock_acs) authorizer.authorize(None) mock_acs.open_door.assert_called()
def getBoundingBoxes(self, image, threshold=0.8): bodies = self.model.detect([self.preprocessImage(image)], verbose=0)[0] boxes = [] for id, score, box in zip(bodies["class_ids"], bodies["scores"], bodies["rois"]): if id == 1 and score > threshold: boxes.append( BoundingBox(Vector(int(box[1]), int(box[0])), Vector(int(box[3]), int(box[2])))) return image, boxes
def getBoundingBoxesAndMasks(self, image, threshold=0.8): bodies = self.model.detect([self.preprocessImage(image)], verbose=0)[0] boxes = [] masks = [] for index, (id, score, box) in enumerate( zip(bodies["class_ids"], bodies["scores"], bodies["rois"])): if id == 1 and score > threshold: boxes.append( BoundingBox(Vector(int(box[1]), int(box[0])), Vector(int(box[3]), int(box[2])))) masks.append(bodies["masks"][:, :, index]) return (boxes, masks)
def extract(self, picture) -> Vector: try: if picture is None: return None headers = { 'Content-Type': "image/jpeg", 'Content-Length': str(len(picture)), } req = requests.Request('POST', self._url + "extract", headers=headers) prep = req.prepare() prep.body = picture s = requests.Session() response = s.send(prep) if response.status_code != 200: if response.json()['code'] not in ['BPE-003002']: logger = logging.getLogger(__name__) logger.exception(response.text) return None return Vector(response.content) except Exception as e: logger = logging.getLogger(__name__) logger.exception(e) return None
def getFaces(self, image): faces = self.model.detect_faces(image) if faces is not None: return [ Face(boundingBox=BoundingBox( origin=Vector(int(face["box"][0]), int(face["box"][1])), end=Vector(int(face["box"][0] + face["box"][2]), int(face["box"][1] + face["box"][3])), ), leftEye=Vector(int(face["keypoints"]["left_eye"][0]), int(face["keypoints"]["left_eye"][1])), rightEye=Vector(int(face["keypoints"]["right_eye"][0]), int(face["keypoints"]["right_eye"][1])), nose=Vector(int(face["keypoints"]["nose"][0]), int(face["keypoints"]["nose"][1]))) for face in faces ]
def getBoundingBoxes(self, image, threshold=0.5): blob = cv.dnn.blobFromImage(image, 1 / 255., self.inputDim, swapRB=False, crop=False) self.model.setInput(blob) outs = self.model.forward(self.getOutputLayers()) boxVectors = [] confidence = [] for out in outs: for detection in out: id = np.argmax(detection[5:]) score = detection[5:][id] if id == 0 and score > threshold: boxVector = detection[:4] * np.array([ image.shape[1], image.shape[0], image.shape[1], image.shape[0] ]) boxVectors.append([ int(boxVector[0] - (boxVector[2] / 2)), int(boxVector[1] - (boxVector[3] / 2)), int(boxVector[2]), int(boxVector[3]) ]) confidence.append(float(score)) boxes = [] if boxVectors: indexes = cv.dnn.NMSBoxes(boxVectors, confidence, threshold, self.nmsThreshold) boxes = [ BoundingBox( Vector(boxVector[0], boxVector[1]), Vector(boxVector[0] + boxVector[2], boxVector[1] + boxVector[3])) for boxVector in map(lambda i: boxVectors[i[0]], indexes) ] return image, boxes
def getVectorByName(self, name): query = {"name":name} results = self.get(query) vector = None for result in results: vector = Vector( result["name"], result["desc"], result["nodes"] ) break return vector
def __init__(self): FlexServ = win32com.client.Dispatch("FlexServer.FlexServerGlobal") token = FlexServ.AuthenticateUser("admin", "admin", False) self._FlexACS = FlexServ.GetObject(token, "FlexACSModule.FlexACS") self._FlexACS.ConnectAll(None, 0) self._FlexDB = FlexServ.GetObject(token, "FlexDB.FlexDBModule") self._path = "D:\\Октаграм\\client_temp\\" self._last_time_accessed = datetime.datetime.now() - datetime.timedelta(seconds=6) self._last_user_accessed = User('','', Vector('')) logger = logging.getLogger(__name__) logger.debug("FlexACS: {}".format(str(self._FlexACS))) logger.debug("FlexDB: {}".format(str(self._FlexDB)))
def get_unidentified_users(self) -> List[User]: return [ User( ''.join( random.choice(string.ascii_lowercase) for i in range(10)), ''.join( random.choice(string.ascii_lowercase) for i in range(10)), Vector('')), User( ''.join( random.choice(string.ascii_lowercase) for i in range(10)), ''.join( random.choice(string.ascii_lowercase) for i in range(10)), Vector('')), User( ''.join( random.choice(string.ascii_lowercase) for i in range(10)), ''.join( random.choice(string.ascii_lowercase) for i in range(10)), Vector('')) ]
def get_unidentified_users(self) -> List[User]: try: users = [User(user.strSID, user.strFirstName+' '+user.strLastName, Vector('')) for user in self._FlexDB.GetUsers("", False, "")] except Exception as e: logger = logging.getLogger(__name__) logger.exception(e) return None return users
def test_ScoreIsHigherThanThreshold_DoNoting( self, MockRepository: mock.MagicMock, MockRecognizer: mock.MagicMock, MockAccessControlSystem: mock.MagicMock): mock_acs = MockAccessControlSystem.return_value mock_acs.has_access.return_value = True mock_recognizer = MockRecognizer.return_value mock_recognizer.extract.return_value = Vector('1') mock_repository = MockRepository.return_value mock_repository.get_users.return_value = [User('1', Vector('1'), '')] mock_repository.get_control_points.return_value = [ ControlPoint('1', '1') ] mock_repository.get_cameras.return_value = [ Camera('1', ControlPoint('1', '1')) ] authorizer = Authorizer(mock_repository, mock_recognizer, mock_acs) mock_recognizer.compare_vectors.return_value = authorizer._threshold + 0.0000000001 authorizer.authorize(None) mock_acs.open_door.assert_called()
def getBoundingBoxes(self, image, threshold=0.7): with tf.Session(graph=self.graph) as sess: detectionClasses, numDetections, detectionBoxes, detectionScores = sess.run( [ self.detectionClassesTensor, self.numDetectionsTensor, self.detectionBoxesTensor, self.detectionScoresTensor ], feed_dict={ self.inputImage: np.expand_dims(self.preprocessImage(image), axis=0) }) boxes = [] for i in range(int(numDetections[0])): if detectionClasses[0][ i] == 1 and detectionScores[0][i] > threshold: boxes.append( BoundingBox( Vector(int(detectionBoxes[0][i][1] * image.shape[1]), int(detectionBoxes[0][i][0] * image.shape[0])), Vector(int(detectionBoxes[0][i][3] * image.shape[1]), int(detectionBoxes[0][i][2] * image.shape[0])))) return image, boxes
def test_NoUsers_DoNothing(self, MockRepository: mock.MagicMock, MockRecognizer: mock.MagicMock, MockAccessControlSystem: mock.MagicMock): mock_acs = MockAccessControlSystem.return_value mock_acs.has_access.return_value = True mock_recognizer = MockRecognizer.return_value mock_recognizer.extract.return_value = Vector('1') mock_recognizer.compare_vectors.return_value = 1 mock_repository = MockRepository.return_value mock_repository.get_users.return_value = [] mock_repository.get_control_points.return_value = [ ControlPoint('1', '1') ] mock_repository.get_cameras.return_value = [ Camera('1', ControlPoint('1', '1')) ] authorizer = Authorizer(mock_repository, mock_recognizer, mock_acs) authorizer.authorize(None) mock_acs.open_door.assert_not_called()
def getVectors(self): vectors = [ Vector(vector["name"], vector["desc"], vector["nodes"]) for vector in self.get(None) ] return vectors
def add_vector(self, vid, vector_array): self.vectors.append(Vector(vid, vector_array))
def extract(self, picture) -> Vector: return Vector('asrtastsrt')