码迷,mamicode.com
首页 > 编程语言 > 详细

Python_科学计算库

时间:2018-05-17 23:25:36      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:extern   sar   int   bsp   +=   nbsp   sha   ISE   hid   

 

文字识别

技术分享图片
# -*- 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_科学计算库

标签:extern   sar   int   bsp   +=   nbsp   sha   ISE   hid   

原文地址:https://www.cnblogs.com/hellangels333/p/9053682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!