Exemplo n.º 1
0
 def predict(self, image):
     image = resizeNormalize(image, 32)
     image = image.astype(np.float32)
     image = np.array([[image]])
     self.model.setInput(image)
     preds = self.model.forward()
     preds = preds.transpose(0, 2, 3, 1)
     preds = preds[0]
     preds = np.argmax(preds, axis=2).reshape((-1, ))
     raw = strLabelConverter(preds, self.alphabet)
     return raw
Exemplo n.º 2
0
 def predict(self,image):
     image = resizeNormalize(image,32)
     image = image.astype(np.float32)
     image = np.array([[image]])
     global graph
     with graph.as_default():
       preds       = self.model.predict(image)
     #preds = preds[0]
     preds = np.argmax(preds,axis=2).reshape((-1,))
     raw = strLabelConverter(preds,self.alphabet)
     return raw
Exemplo n.º 3
0
    def predict(self, image):
        image = resizeNormalize(image, 32)
        image = image.astype(np.float32)
        image = torch.from_numpy(image)
        if torch.cuda.is_available() and self.GPU:
            image = image.cuda()
        else:
            image = image.cpu()

        image = image.view(1, 1, *image.size())
        image = Variable(image)
        preds = self(image)
        _, preds = preds.max(2)
        preds = preds.transpose(1, 0).contiguous().view(-1)
        raw = strLabelConverter(preds, self.alphabet)
        return raw
Exemplo n.º 4
0
    def predict_batch(self, boxes, batch_size=1):
        """
        predict on batch
        """

        N = len(boxes)
        res = []
        imgW = 0
        batch = N // batch_size
        if batch * batch_size != N:
            batch += 1
        for i in range(batch):
            tmpBoxes = boxes[i * batch_size:(i + 1) * batch_size]
            imageBatch = []
            imgW = 0
            for box in tmpBoxes:
                img = box['img']
                image = resizeNormalize(img, 32)
                h, w = image.shape[:2]
                imgW = max(imgW, w)
                imageBatch.append(np.array([image]))

            imageArray = np.zeros((len(imageBatch), 1, 32, imgW),
                                  dtype=np.float32)
            n = len(imageArray)
            for j in range(n):
                _, h, w = imageBatch[j].shape
                imageArray[j][:, :, :w] = imageBatch[j]

            image = torch.from_numpy(imageArray)
            image = Variable(image)
            if torch.cuda.is_available() and self.GPU:
                image = image.cuda()
            else:
                image = image.cpu()

            preds = self(image)
            preds = preds.argmax(2)
            n = preds.shape[1]
            for j in range(n):
                res.append(strLabelConverter(preds[:, j], self.alphabet))

        for i in range(N):
            boxes[i]['text'] = res[i]
        return boxes
Exemplo n.º 5
0
    def predict_batch(self,boxes,batch_size=1):
        """
        predict on batch
        """

        N = len(boxes)
        res = []
        imgW = 0
        batch = N//batch_size
        if batch*batch_size!=N:
            batch+=1
        for i in range(batch):
            tmpBoxes = boxes[i*batch_size:(i+1)*batch_size]
            imageBatch =[]
            imgW = 0
            for box in tmpBoxes:
                img = box['img']
                image = resizeNormalize(img,32)
                h,w = image.shape[:2]
                imgW = max(imgW,w)
                imageBatch.append(np.array([image]))
                
            imageArray = np.zeros((len(imageBatch),1,32,imgW),dtype=np.float32)
            n = len(imageArray)
            for j in range(n):
                _,h,w = imageBatch[j].shape
                imageArray[j][:,:,:w] = imageBatch[j]
            
            global graph
            with graph.as_default():    
               preds       = self.model.predict(imageArray,batch_size=batch_size)
               
            preds = preds.argmax(axis=2)
            n = preds.shape[0]
            for j in range(n):
                res.append(strLabelConverter(preds[j,].tolist(),self.alphabet))

              
        for i in range(N):
            boxes[i]['text'] = res[i]
        return boxes
Exemplo n.º 6
0
# ---------------Params---------------

IMAGE_PATH = "./demo_images/test.jpg"
CRNN_API_URL = "http://text_recognition:8501/v1/models/crnn:predict"

# ---------------Alphabet---------------

alphabet = alphabetChinese
nclass = len(alphabet) + 1

# ---------------Process image---------------

image = cv2.imread(IMAGE_PATH)
image = Image.fromarray(image)
image = image.convert('L')
image = resizeNormalize(image, 32)
image = image.astype(np.float32)
image = np.array([image])

# ---------------Build post---------------

post_json = {"instances": [{"input_image": image.tolist()}]}

# ---------------Test---------------

t0 = time.time()
response = requests.post(CRNN_API_URL, data=json.dumps(post_json))
print("forward time : {}".format(time.time() - t0))
response.raise_for_status()
prediction = response.json()["predictions"]
print(prediction)