Python小项目之字符图片的识别

今天给大家讲解的是如何利用Python实现对字符图片的识别。

整体代码:

from PIL import Image

import hashlib

import time

import os

import math

class VectorCompare: # 向量比较类

def magnitude(self,concordance): # 计算向量长度

    total = 0

    for word,count in concordance.items():

        total += count ** 2

    return math.sqrt(total)



def relation(self,concordance1, concordance2):#求两个向量的关系值?协。

    relevance = 0

    topvalue = 0

    for word, count in concordance1.items():

        if word in concordance2:

            topvalue += count * concordance2[word]

    return topvalue / (self.magnitude(concordance1) * self.magnitude(concordance2))

def buildvector(im): # 创建向量

d1 = {}



count = 0

for i in im.getdata():

    d1[count] = i

    count += 1 #计数值



return d1

#返回类型是一个字典

v = VectorCompare()

#图标集

iconset = [‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘0’,‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’,‘i’,‘j’,‘k’,‘l’,‘m’,‘n’,‘o’,‘p’,‘q’,‘r’,‘s’,‘t’,‘u’,‘v’,‘w’,‘x’,‘y’,‘z’]

#图片集

imageset = []

for letter in iconset:

for img in os.listdir('./iconset/%s/'%(letter)):

    temp = []

    if img != "Thumbs.db" and img != ".DS_Store": # 找到目录inconset下的样例图片

        temp.append(buildvector(Image.open("./iconset/%s/%s"%(letter,img)))) #将图片转化为向量存给temp

    imageset.append({letter:temp})  # 将图片集合加入 字母和样例的集合

im = Image.open(“captcha.gif”) #打开我们要检测的图片

im2 = Image.new(“P”,im.size,255) #使用p模式打开,色彩255

im.convert(“P”) # 将im的模式转换位P

temp = {} #暂存空间

for x in range(im.size[1]): # 宽度

for y in range(im.size[0]): # 高度

    pix = im.getpixel((y,x)) #获取像素存到字典中

    temp[pix] = pix # ?

    if pix == 220 or pix == 227: # these are the numbers to get

        im2.putpixel((y,x),0) # im2导入

inletter = False

foundletter=False

start = 0

end = 0

letters = []

for y in range(im2.size[0]): # slice across

for x in range(im2.size[1]): # slice down

    pix = im2.getpixel((y,x))

    if pix != 255:

        inletter = True

        break



if foundletter == False and inletter == True:

    foundletter = True

    start = y



if foundletter == True and inletter == False:

    foundletter = False

    end = y

    letters.append((start,end))





inletter=False

count = 0

for letter in letters:

im3 = im2.crop(( letter[0] , 0, letter[1],im2.size[1] ))



guess = []



for image in imageset:

    for x,y in image.items():

        if len(y) != 0:

            guess.append( ( v.relation(y[0],buildvector(im3)),x) )



guess.sort(reverse=True)

print (guess[0])

count += 1

以上就是具体实现步骤,欢迎采纳。

文章部分内容源于网络,联系侵删*

已标记关键词 清除标记
相关推荐