From 640e80c8ae78b5c05382f30067381dc585ad196a Mon Sep 17 00:00:00 2001 From: wegamekinglc Date: Wed, 20 May 2020 11:59:57 +0800 Subject: [PATCH] FEATURE: codes change to greatly enhance the performance --- dnn/darknet.py | 8 ++++---- dnn/ocr.py | 7 +++++-- dnn/text.py | 9 ++++++--- helper/image.py | 4 ++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/dnn/darknet.py b/dnn/darknet.py index c56f549..b36836e 100644 --- a/dnn/darknet.py +++ b/dnn/darknet.py @@ -19,8 +19,9 @@ def sample(probs): return len(probs)-1 def c_array(ctype, values): - arr = (ctype*len(values))() - arr[:] = values + arr = (ctype * len(values))() + values = values.astype(ctype) + memmove(arr, values.ctypes.data, values.nbytes) return arr class BOX(Structure): @@ -115,8 +116,7 @@ class METADATA(Structure): import numpy as np import cv2 def array_to_image(image): - boxed_image = np.array(image) - boxed_image = np.array(cv2.split(boxed_image)) + boxed_image = np.array(cv2.split(image)) c = boxed_image.shape[0] h = boxed_image.shape[1] w = boxed_image.shape[2] diff --git a/dnn/ocr.py b/dnn/ocr.py index da3bd59..38ab1a2 100644 --- a/dnn/ocr.py +++ b/dnn/ocr.py @@ -7,6 +7,8 @@ ## add opencv dnn for relu and stride ## add ocr prob for every char """ +from ctypes import memmove + import cv2 import os import time @@ -84,8 +86,9 @@ def predict_darknet(image): res=predict_image(ocrNet,im) outW = int(np.ceil(w/4)-3) nchars = len(charactersPred) - out = [ res[i] for i in range(outW*nchars)] - out = np.array(out).reshape((nchars,outW)) + out = np.zeros(outW * nchars, dtype=res._type_) + memmove(out.ctypes.data, res, out.nbytes) + out = out.reshape((nchars, outW)) out = out.transpose((1,0)) out = softmax(out) diff --git a/dnn/text.py b/dnn/text.py index cdb4d24..9e8271e 100644 --- a/dnn/text.py +++ b/dnn/text.py @@ -4,6 +4,8 @@ text detect @author: chineseocr """ +from ctypes import memmove + import cv2 import numpy as np from config import textPath,anchors,GPU @@ -26,9 +28,10 @@ def detect_box(image,scale=600,maxScale=900): scale=16 iw = int(np.ceil(im.w/scale)) ih = int(np.ceil(im.h/scale)) - h,w = image.shape[:2] - out = [ res[i] for i in range(40*ih*iw)] - out=np.array(out).reshape((1,40,ih,iw)) + h,w = image.shape[:2] + out = np.zeros(40 * ih * iw, dtype=res._type_) + memmove(out.ctypes.data, res, out.nbytes) + out = out.reshape((1, 40, ih, iw)) else: inputBlob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(w,h),swapRB=False ,crop=False); outputName = textNet.getUnconnectedOutLayersNames() diff --git a/helper/image.py b/helper/image.py index 422cee1..a1e8bc8 100644 --- a/helper/image.py +++ b/helper/image.py @@ -128,10 +128,10 @@ def box_to_center(box): xmin,ymin,xmax,ymax = box w = xmax-xmin h = ymax-ymin - return [round(xmin,4),round(ymin,4),round(w,4),round(h,4)] + return [xmin, ymin, w, h] newBoxes = [ box_to_center(box) for box in boxes] - newscores = [ round(float(x),6) for x in scores] + newscores = [ float(x) for x in scores] index = cv2.dnn.NMSBoxes(newBoxes, newscores, score_threshold=score_threshold, nms_threshold=nms_threshold) if len(index)>0: index = index.reshape((-1,))