From f2cb5aa2997d06db61321e30c229936f3644d738 Mon Sep 17 00:00:00 2001 From: wenlihaoyu Date: Mon, 27 May 2019 19:40:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7opencv=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=88=B04.0.0.21=EF=BC=8Copencv=E8=B0=83=E7=94=A8darknet?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=97=A0=E8=AF=86=E5=88=AB=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup-cpu.md | 2 +- setup.md | 2 +- text/opencv_dnn_detect.py | 46 ++++++++++++++++++++++++--------------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/setup-cpu.md b/setup-cpu.md index 9de7191..7124ce8 100644 --- a/setup-cpu.md +++ b/setup-cpu.md @@ -3,7 +3,7 @@ conda create -n chineseocr python=3.6 pip scipy numpy jupyter ipython ##运用co source activate chineseocr git submodule init && git submodule update cd darknet/ && make && cd .. -pip install easydict opencv-contrib-python==3.4.2.16 Cython h5py lmdb mahotas pandas requests bs4 matplotlib lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/ +pip install easydict opencv-contrib-python==4.0.0.21 Cython h5py lmdb mahotas pandas requests bs4 matplotlib lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install -U pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install web.py==0.40.dev0 pip install keras==2.1.5 tensorflow==1.8 diff --git a/setup.md b/setup.md index fb5bdbd..887b2be 100644 --- a/setup.md +++ b/setup.md @@ -2,7 +2,7 @@ conda create -n chineseocr python=3.6 pip scipy numpy jupyter ipython ##运用conda 创建python环境 source activate chineseocr git submodule init && git submodule update -pip install easydict opencv-contrib-python==3.4.2.16 Cython h5py lmdb mahotas pandas requests bs4 matplotlib lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/ +pip install easydict opencv-contrib-python==4.0.0.21 Cython h5py lmdb mahotas pandas requests bs4 matplotlib lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install -U pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install keras==2.1.5 tensorflow==1.8 tensorflow-gpu==1.8 pip install web.py==0.40.dev0 diff --git a/text/opencv_dnn_detect.py b/text/opencv_dnn_detect.py index f3310c1..0a9a144 100644 --- a/text/opencv_dnn_detect.py +++ b/text/opencv_dnn_detect.py @@ -24,24 +24,36 @@ if opencvFlag=='keras': else: angleNet = cv2.dnn.readNetFromTensorflow(AngleModelPb,AngleModelPbtxt)##dnn 文字方向检测 textNet = cv2.dnn.readNetFromDarknet(yoloCfg,yoloWeights)##文字定位 + def text_detect(img): - thresh=0 - h,w = img.shape[:2] - - inputBlob = cv2.dnn.blobFromImage(img, scalefactor=0.00390625, size=IMGSIZE,swapRB=True ,crop=False); - textNet.setInput(inputBlob) - pred = textNet.forward() - cx = pred[:,0]*w - cy = pred[:,1]*h - xmin = cx - pred[:,2]*w/2 - xmax = cx + pred[:,2]*w/2 - ymin = cy - pred[:,3]*h/2 - ymax = cy + pred[:,3]*h/2 - scores = pred[:,4] - indx = np.where(scores>thresh)[0] - scores = scores[indx] - boxes = np.array(list(zip(xmin[indx],ymin[indx],xmax[indx],ymax[indx]))) - return boxes,scores + thresh = 0 + img_height,img_width = img.shape[:2] + inputBlob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=IMGSIZE,swapRB=True ,crop=False); + textNet.setInput(inputBlob/255.0) + outputName = textNet.getUnconnectedOutLayersNames() + outputs = textNet.forward(outputName) + class_ids = [] + confidences = [] + boxes = [] + for output in outputs: + for detection in output: + scores = detection[5:] + class_id = np.argmax(scores) + confidence = scores[class_id] + if confidence > thresh: + center_x = int(detection[0] * img_width) + center_y = int(detection[1] * img_height) + width = int(detection[2] * img_width) + height = int(detection[3] * img_height) + left = int(center_x - width / 2) + top = int(center_y - height / 2) + if class_id==1: + class_ids.append(class_id) + confidences.append(float(confidence)) + boxes.append([left, top,left+width, top+height ]) + + return np.array(boxes),np.array(confidences) + def angle_detect_dnn(img,adjust=True): -- GitLab