import tensorflow as tf
from PIL import Image, ImageFilter
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
import numpy as np
def inside(r1, r2):
"""判断r1是否在r2里面"""
x1, y1, w1, h1 = r1
x2, y2, w2, h2 = r2
if (x1 >= x2) and (y1 >= y2) and (x1 + w1 <= x2 + w2) and (y1 + h1 <= y2 + h2):
return True
else:
return False
def wrap_digit(rect):
"""扩大矩形范围"""
x, y, w, h = rect
padding = 5
hcenter = x + w / 2
vcenter = y + h / 2
if (h > w):
w = h
x = hcenter - (w / 2)
else:
h = w
y = vcenter - (h / 2)
return (x - padding, y - padding, w + padding, h + padding)
def imageprepare(path):
"""
This function returns the pixel values.
The imput is a png file location.
"""
# file_name='./images/3.png'#导入自己的图片地址
file_name = path
#in terminal 'mogrify -format png *.jpg' convert jpg to png
im = Image.open(file_name).convert('L')
# im.save("./image/sample.png")
# plt.imshow(im)
# plt.show()
tv = list(im.getdata()) #get pixel values
#normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
tva = [ (255-x)*1.0/255.0 for x in tv]
# print(tva)
return tva
# 创建两个占位符,x为输入网络的图像,y_为输入网络的图像类别,None表示此张量的第一个维度可以是任何长度的
x_ = tf.placeholder("float", shape=[None, 784])
# y_ = tf.placeholder("float", shape=[None, 10])
# 权重初始化函数
def weight_variable(shape):
# 输出服从截尾正态分布的随机值
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
# 偏置初始化函数
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
# 创建卷积op
# x 是一个4维张量,shape为[batch,height,width,channels]
# 卷积核移动步长为1。填充类型为SAME,可以不丢弃任何像素点
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME")
# 创建池化op
# 采用最大池化,也就是取窗口中的最大值作为结果
# x 是一个4维张量,shape为[batch,height,width,channels]
# ksize表示pool窗口大小为2x2,也就是高2,宽2
# strides,表示在height和width维度上的步长都为2
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding="SAME")
# 第1层,卷积层
# 初始化W为[5,5,1,32]的张量,表示卷积核大小为5*5,第一层网络的输入和输出神经元个数分别为1和32
W_conv1 = weight_variable([5, 5, 1, 32])
# 初始化b为[32],即输出大小
b_conv1 = bias_variable([32])
# 把输入x(二维张量,shape为[batch, 784])变成4d的x_image,x_image的shape应该是[batch,28,28,1]
# -1表示自动推测这个维度的size
x_image = tf.reshape(x_, [-1, 28, 28, 1])
# 把x_image和权重进行卷积,加上偏置项,然后应用ReLU激活函数,最后进行max_pooling
# h_pool1的输出即为第一层网络输出,shape为[batch,14,14,1]
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
# 第2层,卷积层
# 卷积核大小依然是5*5,这层的输入和输出神经元个数为32和64
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = weight_variable([64])
# h_pool2即为第二层网络输出,shape为[batch,7,7,1]
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
# 第3层, 全连接层
# 这层是拥有1024个神经元的全连接层
# W的第1维size为7*7*64,7*7是h_pool2输出的size,64是第2层输出神经元个数
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
# 计算前需要把第2层的输出reshape成[batch, 7*7*64]的张量
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# Dropout层
# 为了减少过拟合,在输出层前加入dropout
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# 输出层
# 最后,添加一个softmax层
# 可以理解为另一个全连接层,只不过输出时使用softmax将网络输出值转换成了概率
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
#tf.nn.softmax函数默认(dim=-1)是对张量最后一维的shape=(p,)向量进行softmax计算,得到一个概率向量。
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# 数据保存器的初始化
saver = tf.train.Saver()
font = cv2.FONT_HERSHEY_SIMPLEX
path = "numbers.jpg"
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#高斯模糊
bw = cv2.GaussianBlur(bw, (7, 7), 0)
#简单阈值,选取一个全局阈值,然后就把整幅图像分成了非黑即白的二值图像了
ret, thbw = cv2.threshold(bw, 127, 255, cv2.THRESH_BINARY_INV)
#腐蚀图像
thbw = cv2.erode(thbw, np.ones((2, 2), np.uint8), iterations=2)
#cv2.findContours()函数返回两个值,一个是轮廓本身,还有一个是每条轮廓对应的属性。
image, cntrs, hier = cv2.findContours(thbw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
rectangles = []
for i,c in enumerate(cntrs):
#用一个最小的矩形,把找到的形状包起来
r = x, y, w, h = cv2.boundingRect(c)
# 轮廓面积
a = cv2.contourArea(c)
#img.shape 显示尺寸,img.shape[0] 图片宽度,img.shape[1] 图片高度,img.shape[2] 图片通道数
#img.size 显示总像素个数,img.max() 最大像素值,img.mean() 像素平均值
b = (img.shape[0] - 3) * (img.shape[1] - 3)
is_inside = False
for j,q in enumerate(rectangles):
if inside(r, q):
is_inside = True
break
if inside(q, r):
rectangles.remove(q)
pass
if not is_inside:
if not a == b:
rectangles.append(r)
with tf.Session() as sess:
# 初始化变量
sess.run(tf.initialize_all_variables())
#恢复模型 路径写自己的
saver.restore(sess, r'D:\python_work\other\tensorflow\model\train_faces.model')
accuracy = tf.argmax(y_conv, 1)
i = 0
for r in rectangles:
i = i + 1
x, y, w, h = wrap_digit(r)
# TypeError: integer argument expected, got float
x = int(x)
y = int(y)
w = int(w)
h = int(h)
if x < 0:
x = 0
if y < 0:
y = 0
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
roi = img[y:y + h, x:x + w]
roi = cv2.resize(roi, (28,28))
cv2.imwrite("test.png",roi)
result = imageprepare("test.png")
predint = accuracy.eval(feed_dict={x_: [result], keep_prob: 1.0}, session=sess)
# print('recognize result:',predint[0])
cv2.putText(img, "%d" % predint, (x, y - 1), font, 1, (0, 255, 0))
cv2.imshow("thbw", thbw)
cv2.imshow("contours", img)
cv2.imwrite("sample02.jpg", img)
cv2.waitKey()