-
Notifications
You must be signed in to change notification settings - Fork 1
/
试卷批改系统.py
133 lines (106 loc) · 4.4 KB
/
试卷批改系统.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import numpy as np
import tensorflow as tf
import cv2
from PIL import Image
image = cv2.imread('D:\opencv_camera\digits\\12.jpg')
lower=np.array([50,50,150])
upper=np.array([140,140,250])
mask = cv2.inRange(image, lower, upper)
output1 = cv2.bitwise_and(image, image, mask=mask)
output=cv2.cvtColor(output1,cv2.COLOR_BGR2GRAY)
th2 = cv2.adaptiveThreshold(output,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,0)
# cv2.imshow("images",th2)
# cv2.waitKey(0)
roi=[]
for i in range(8):
img_roi = output1[260:310,35+55*i:80+58*i]
roi.append(img_roi)
#用来分割图片
def picture(x):
shuzu = []
left=0
right=x.shape[1]
for i in range(x.shape[1]):
if sum(map(sum,x[1:50,i:i+1]))>0:
left=i
break
for j in range(left,x.shape[1]):
if sum(map(sum,x[1:50,j:j+1]))==0:
right=j+2
break
leftimg=x[1:50,left:right]
shuzu.append(leftimg)
rightimg=x[1:50,right:x.shape[1]-5]
shuzu.append(rightimg)
return shuzu
def normalizepic(pic):
im_arr = pic
im_nparr = []
for x in im_arr:
x=1-x/255
im_nparr.append(x)
im_nparr = np.array([im_nparr])
return im_nparr
# cv2.imshow("images",roi[0])
# cv2.waitKey(0)
img1=[]
for i in range(8):
output=cv2.cvtColor(roi[i],cv2.COLOR_BGR2GRAY)
th2 = cv2.adaptiveThreshold(output,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,0)
# cv2.imshow("images", th2)
# cv2.waitKey(0)
img_seq = picture(th2)
for j in range(len(img_seq)):
#cv2.imshow("images",img_seq[j])
# cv2.waitKey(0)
ret, thresh2 = cv2.threshold(img_seq[j], 0, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("images", thresh2)
cv2.waitKey(0)
res = cv2.resize(thresh2, (28, 28), interpolation=cv2.INTER_CUBIC)
img=normalizepic(res).reshape((1,784))
img = img.astype(np.float32)
img1.append(img)
########图片预处理##########
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initial,dtype=tf.float32,name='weight')
def bias_variable(shape):
initial=tf.constant(0.1,shape=shape)
return tf.Variable(initial,dtype=tf.float32,name='biases')
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
xs=tf.placeholder(tf.float32,[None,784])#输入是一个28*28的像素点的数据
keep_prob=tf.placeholder(tf.float32)
x_image=tf.reshape(xs,[-1,28,28,1])#xs的维度暂时不管,用-1表示,28,28表示xs的数据,1表示该数据是一个黑白照片,如果是彩色的,则写成3
#卷积层1
W_conv1=weight_variable([5,5,1,32])#抽取一个5*5像素,高度是32的点,每次抽出原图像的5*5的像素点,高度从1变成32
b_conv1=bias_variable([32])
h_conv1=tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)#输出 28*28*32的图像
h_pool1=max_pool_2x2(h_conv1)##输出14*14*32的图像,因为这个函数的步长是2*2,图像缩小一半。
#卷积层2
W_conv2=weight_variable([5,5,32,64])#随机生成一个5*5像素,高度是64的点,抽出原图像的5*5的像素点,高度从32变成64
b_conv2=bias_variable([64])
h_conv2=tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)#输出14*14*64的图像
h_pool2=max_pool_2x2(h_conv2)##输出7*7*64的图像,因为这个函数的步长是2*2,图像缩小一半。
#fully connected 1
W_fc1=weight_variable([7*7*64,1024])
b_fc1=bias_variable([1024])
h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64])#将输出的h_pool2的三维数据变成一维数据,平铺下来,(-1)代表的是有多少个例子
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)
#fully connected 2
W_fc2=weight_variable([1024,10])
b_fc2=bias_variable([10])
prediction=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)#输出层
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(sess, "my_net/save_net.ckpt")
result= tf.argmax(prediction, 1)
#result= result.eval(feed_dict={xs:img, keep_prob: 1.0}, session=sess)
for i in range(16):
result=sess.run(prediction, feed_dict={xs:img1[i],keep_prob: 1.0})
result1 =np.argmax(result,1)
print(result1)