Python_科学计算库

Posted hellangels333

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python_科学计算库相关的知识,希望对你有一定的参考价值。

 

文字识别

技术分享图片
# -*- coding: utf-8 -*-
"""
简介:用样本训练数据,再识别
"""

import cv2
import numpy as np
from PIL import Image  #Python Image Lib
import skimage.feature as feature2d
import sklearn.neighbors as nhb
from sklearn.externals import joblib  #对训练模型保存或读取
#cvhog=cv2.HOGDescriptor()

#预处理图片
def imgPrepare(filename):
    img=cv2.imread(filename,0)
    img=np.uint8(img/img.ptp()*255)
    img=np.where(img>128,255,img)
    img=np.where(img<=128,0,img)
    img=np.bitwise_not(img)
    return img

#横切    
def splitchar(img,axis=1):
    idxrowb=np.all(img<64,axis=axis)
    idxrowi=np.uint8(idxrowb).ravel()
    dy=idxrowi[1:]-idxrowi[:-1]
    #print(dy)
    rowb=np.argwhere(dy==255).ravel()
    rowe=np.argwhere(dy==1).ravel()
    #print(rowb,rowe)
    if axis==1:
        imglines=[img[b:e+1,:] for b,e in zip(rowb,rowe)]
    else:
        imglines=[img[:,b:e+1] for b,e in zip(rowb,rowe)]
  
    return imglines

#切块
def splitBox(img):
    idxrowb=np.all(img<64,axis=1)
    idxrowi=np.uint8(idxrowb).ravel()
    dy=idxrowi[1:]-idxrowi[:-1]
    #print(dy)
    rowb=np.argwhere(dy==255).ravel()
    rowe=np.argwhere(dy==1).ravel()
    b=0
    e=-1
    if len(rowe)>0:
        e=rowe[-1]+1
    if len(rowb)>0:
        b=rowb[0]
    return img[b:e,:]

#把图片整成一样大小
def myResize(img,size=(48,48)):
    h,w=img.shape
    bw=max(h,w)
    bh=bw
    bimg=np.zeros((bh,bw),np.uint8)
    if bw==w:
        dh=(bh-h)//2
        bimg[dh:dh+h,:]=img[:,:]
    else:
        dw=(bw-w)//2
        bimg[:,dw:dw+w]=img[:,:]
        
    bimg=cv2.resize(bimg,size)
    return bimg

#获取hog向量   图片转为向量
def getHog(img,cell=(16,16),block=(3,3)):
    vec=feature2d.hog(img,12,cell,block,L2)
    return vec

#训练的主方法
gimg=imgPrepare(e:/sx.jpg)
lines=splitchar(gimg,axis=1)
chars=[]
for line in lines:
    charlist=splitchar(line,axis=0)
    cchars=[  myResize(splitBox(c))  for c in charlist]
    chars.append(cchars)
chars=np.asarray(chars)
X=[]
Y=[]
y=0
for linech in chars:
    
    for ch in linech:
        chhog=getHog(ch)
        X.append(chhog)
        Y.append(y)
    
    y+=1

KNC=nhb.KNeighborsClassifier(algorithm=ball_tree,n_neighbors=3)
KNC.fit(X,Y)

joblib.dump(KNC,knc.knn)



# 识别的主方法
def predict(img):
     knc=nhb.KNeighborsClassifier(algorithm=ball_tree,n_neighbors=3)
     knc=joblib.load(knc.knn)
     lines=splitchar(img,axis=1)
     chars=[]
     for line in lines:
         charlist=splitchar(line,axis=0)
         cchars=[  myResize(splitBox(c))  for c in charlist]
         chars.append(cchars)
    
     chars=np.asarray(chars)
    
     Y=[]
     for linech in chars:
        x=[]
        for ch in linech:
            chhog=getHog(ch)
            x.append(chhog)
            
        y=knc.predict(x)
        print(y)
        Y.append(y)
    
     return Y
     
     
文字识别

 

以上是关于Python_科学计算库的主要内容,如果未能解决你的问题,请参考以下文章

『python』科学计算专项_科学绘图库matplotlib学习

『Python』Numpy学习指南第十章_高端科学计算库scipy入门(系列完结)

『python』科学计算专项_科学绘图库matplotlib学习(下)

python的科学计算库都有哪些

python能做啥科学计算

python 基础知识汇总