JeVoisBase  1.18
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
crnn.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 CRNN:
11  def __init__(self, modelPath, charsetPath, backendId=0, targetId=0):
12  self._model_path = modelPath
13  self._charsetPath = charsetPath
14  self._backendId = backendId
15  self._targetId = targetId
16 
17  self._model = cv.dnn.readNet(self._model_path)
18  self._model.setPreferableBackend(self._backendId)
19  self._model.setPreferableTarget(self._targetId)
20 
21  self._charset = self._load_charset(self._charsetPath)
22  self._inputSize = [100, 32] # Fixed
23  self._targetVertices = np.array([
24  [0, self._inputSize[1] - 1],
25  [0, 0],
26  [self._inputSize[0] - 1, 0],
27  [self._inputSize[0] - 1, self._inputSize[1] - 1]
28  ], dtype=np.float32)
29 
30  @property
31  def name(self):
32  return self.__class__.__name__
33 
34  def _load_charset(self, charsetPath):
35  charset = ''
36  with open(charsetPath, 'r') as f:
37  for char in f:
38  char = char.strip()
39  charset += char
40  return charset
41 
42  def setBackend(self, backend_id):
43  self._backendId = backend_id
44  self._model.setPreferableBackend(self._backendId)
45 
46  def setTarget(self, target_id):
47  self._targetId = target_id
48  self._model.setPreferableTarget(self._targetId)
49 
50  def _preprocess(self, image, rbbox):
51  # Remove conf, reshape and ensure all is np.float32
52  vertices = rbbox.reshape((4, 2)).astype(np.float32)
53 
54  rotationMatrix = cv.getPerspectiveTransform(vertices, self._targetVertices)
55  cropped = cv.warpPerspective(image, rotationMatrix, self._inputSize)
56 
57  if 'CN' in self._model_path:
58  pass
59  else:
60  cropped = cv.cvtColor(cropped, cv.COLOR_BGR2GRAY)
61 
62  return cv.dnn.blobFromImage(cropped, size=self._inputSize, mean=127.5, scalefactor=1 / 127.5)
63 
64  def infer(self, image, rbbox):
65  # Preprocess
66  inputBlob = self._preprocess(image, rbbox)
67 
68  # Forward
69  self._model.setInput(inputBlob)
70  outputBlob = self._model.forward()
71 
72  # Postprocess
73  results = self._postprocess(outputBlob)
74 
75  return results
76 
77  def _postprocess(self, outputBlob):
78  '''Decode charaters from outputBlob
79  '''
80  text = ''
81  for i in range(outputBlob.shape[0]):
82  c = np.argmax(outputBlob[i][0])
83  if c != 0:
84  text += self._charset[c - 1]
85  else:
86  text += '-'
87 
88  # adjacent same letters as well as background text must be removed to get the final output
89  char_list = []
90  for i in range(len(text)):
91  if text[i] != '-' and (not (i > 0 and text[i] == text[i - 1])):
92  char_list.append(text[i])
93  return ''.join(char_list)
94 
crnn.CRNN._backendId
_backendId
Definition: crnn.py:14
crnn.CRNN._inputSize
_inputSize
Definition: crnn.py:22
crnn.CRNN._charsetPath
_charsetPath
Definition: crnn.py:13
crnn.CRNN._targetVertices
_targetVertices
Definition: crnn.py:23
crnn.CRNN._model
_model
Definition: crnn.py:17
crnn.CRNN._charset
_charset
Definition: crnn.py:21
crnn.CRNN.setTarget
def setTarget(self, target_id)
Definition: crnn.py:46
crnn.CRNN.__init__
def __init__(self, modelPath, charsetPath, backendId=0, targetId=0)
Definition: crnn.py:11
crnn.CRNN
Definition: crnn.py:10
join
std::string join(std::vector< std::string > const &strings, std::string const &delimiter)
crnn.CRNN.infer
def infer(self, image, rbbox)
Definition: crnn.py:64
crnn.CRNN.name
def name(self)
Definition: crnn.py:31
crnn.CRNN.setBackend
def setBackend(self, backend_id)
Definition: crnn.py:42
crnn.CRNN._preprocess
def _preprocess(self, image, rbbox)
Definition: crnn.py:50
crnn.CRNN._postprocess
def _postprocess(self, outputBlob)
Definition: crnn.py:77
crnn.CRNN._load_charset
def _load_charset(self, charsetPath)
Definition: crnn.py:34
crnn.CRNN._model_path
_model_path
Definition: crnn.py:12
crnn.CRNN._targetId
_targetId
Definition: crnn.py:15