Skip to content

Commit 8767b3e

Browse files
authored
convolution neural network example, for training
1 parent b9c79bb commit 8767b3e

6 files changed

Lines changed: 527 additions & 0 deletions

File tree

cnn_example/IMG_3451.HEIC

778 KB
Binary file not shown.

cnn_example/character_cutting.py

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import cv2
2+
import numpy as np
3+
import keras
4+
from keras.models import Sequential, load_model
5+
6+
7+
# 水平方向投影
8+
def hProject(binary):
9+
h, w = binary.shape
10+
11+
# 水平投影
12+
hprojection = np.zeros(binary.shape, dtype=np.uint8)
13+
14+
# 创建h长度都为0的数组
15+
h_h = [0]*h
16+
for j in range(h):
17+
for i in range(w):
18+
if binary[j,i] == 0:
19+
h_h[j] += 1
20+
# 画出投影图
21+
for j in range(h):
22+
for i in range(h_h[j]):
23+
hprojection[j,i] = 255
24+
25+
# cv2.imshow('hpro', hprojection)
26+
27+
return h_h
28+
29+
# 垂直反向投影
30+
def vProject(binary):
31+
h, w = binary.shape
32+
# 垂直投影
33+
vprojection = np.zeros(binary.shape, dtype=np.uint8)
34+
35+
# 创建 w 长度都为0的数组
36+
w_w = [0]*w
37+
for i in range(w):
38+
for j in range(h):
39+
if binary[j, i ] == 0:
40+
w_w[i] += 1
41+
42+
for i in range(w):
43+
for j in range(w_w[i]):
44+
vprojection[j,i] = 255
45+
46+
# cv2.imshow('vpro', vprojection)
47+
48+
return w_w
49+
50+
51+
def load_model_cnn(model, all_imagea):
52+
predictt = []
53+
for imgf in all_imagea:
54+
# img = cv2.imread(f"{i}", 0)
55+
img = cv2.resize(imgf, (28, 28))
56+
img = 255 - img
57+
img = img.astype("float32")
58+
img_4 = img - np.amin(img)
59+
img_5 = 255 * img_4 / (np.amax(img_4))
60+
x_test_img = np.reshape(img_5, (1, 28, 28))
61+
x_Test4D = x_test_img.reshape(x_test_img.shape[0], 28, 28, 1).astype('float32')
62+
x_Test4D_normalize = (x_Test4D / np.amax(x_test_img))
63+
prediction = model.predict_classes(x_Test4D_normalize)
64+
predictt.append(prediction)
65+
# .append(prediction)
66+
return predictt
67+
68+
69+
def load_model_cnn_unit(model, image):
70+
img = cv2.resize(image, (28, 28))
71+
img = 255 - img
72+
img = img.astype("float32")
73+
img_4 = img - np.amin(img)
74+
img_5 = 255 * img_4 / (np.amax(img_4))
75+
x_test_img = np.reshape(img_5, (1, 28, 28))
76+
x_Test4D = x_test_img.reshape(x_test_img.shape[0], 28, 28, 1).astype('float32')
77+
x_Test4D_normalize = (x_Test4D / np.amax(x_test_img))
78+
prediction = model.predict_classes(x_Test4D_normalize)
79+
return prediction
80+
81+
def trackChaned(x):
82+
pass
83+
84+
if __name__ == '__main__':
85+
all_image = []
86+
video = cv2.VideoCapture(1)
87+
model = load_model('my_model.h5')
88+
model.load_weights('my_model_weights.h5')
89+
90+
# video.set(cv2.CAP_PROP_AUTOFOCUS, 0)
91+
# frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
92+
# frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
93+
# avi = cv2.VideoWriter_fourcc(*'MP4V')
94+
# out = cv2.VideoWriter("test123.mov", avi, 25, (1200, 600))
95+
lower_blue = np.array([78, 43, 46])
96+
upper_blue = np.array([110, 255, 255])
97+
98+
cv2.namedWindow('Mask')
99+
cv2.createTrackbar("Min", "Mask", 0, 255, trackChaned)
100+
if not video.isOpened():
101+
print("Could not open video")
102+
sys.exit()
103+
ok, frame = video.read()
104+
if not ok:
105+
print('Cannot read video file')
106+
sys.exit()
107+
108+
tmp = None
109+
# cv2.namedWindow("Tracking", cv2.WINDOW_NORMAL)
110+
while True:
111+
all_imagea = []
112+
# Read a new frame
113+
ok, frame = video.read()
114+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
115+
huh = cv2.getTrackbarPos("Min", "Mask")
116+
ret, th = cv2.threshold(gray, huh, 255, 0)
117+
cv2.imshow('originla', frame)
118+
cv2.imshow('Mask', th)
119+
120+
# Exit if ESC pressed
121+
k = cv2.waitKey(100) & 0xff
122+
if k == 27:
123+
cv2.destroyAllWindows()
124+
cv2.waitKey(1)
125+
break
126+
if k == ord('p'):
127+
print("press the key == p")
128+
# framee = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
129+
# ret, th = cv2.threshold(framee, 127, 255, 0)
130+
h, w = gray.shape
131+
h_h = hProject(th)
132+
133+
start = 0
134+
h_start, h_end = [], []
135+
position = []
136+
137+
# 根据水平投影获取垂直分割
138+
for i in range(len(h_h)):
139+
if h_h[i] > 0 and start == 0:
140+
h_start.append(i)
141+
start = 1
142+
if h_h[i] == 0 and start == 1:
143+
h_end.append(i)
144+
start = 0
145+
146+
for i in range(len(h_start)):
147+
cropImg = th[h_start[i]:h_end[i], 0:w]
148+
if i == 0:
149+
pass
150+
# cv2.imshow('cropimg', cropImg)
151+
# cv2.imwrite('words_cropimg.jpg', cropImg)
152+
w_w = vProject(cropImg)
153+
154+
wstart , wend, w_start, w_end = 0, 0, 0, 0
155+
for j in range(len(w_w)):
156+
if w_w[j] > 0 and wstart == 0:
157+
w_start = j
158+
wstart = 1
159+
wend = 0
160+
if w_w[j] ==0 and wstart == 1:
161+
w_end = j
162+
wstart = 0
163+
wend = 1
164+
165+
# 当确认了起点和终点之后保存坐标
166+
if wend == 1:
167+
position.append([w_start, h_start[i], w_end, h_end[i]])
168+
wend = 0
169+
170+
# 确定分割位置
171+
for i, p in enumerate(position):
172+
height = abs(p[1] - p[3])
173+
weidgh = abs(p[0] - p[2])
174+
y = height if height > weidgh else weidgh
175+
x = height if height > weidgh else weidgh
176+
# print(p)
177+
# int((y - height) / 2)
178+
center_point = (int((p[0] + p[2]) / 2), int((p[1] + p[3]) / 2))
179+
print(f"center point {center_point}")
180+
# print(int((center_point[0] - y / 2)), int((center_point[0] + y / 2)))
181+
# print(int((center_point[0] - y / 2)), int((center_point[1] + x / 2)))
182+
imgg = th[int((center_point[1] - y / 2)):int((center_point[1] + y / 2)),
183+
int((center_point[0] - x / 2)):int((center_point[0] + x / 2))]
184+
# print(int((y - height) / 2), int(y - (y - height) / 2))
185+
# imgg = th[int((p[0] - height) / 2): int(p[0] - (p[0] - height) / 2),
186+
# int((p[1] - weidgh) / 2): int(p[1] - (p[1] - weidgh) / 2)]
187+
imgg = cv2.resize(imgg, (28, 28))
188+
# cv2.imwrite(f"{i}.jpg", imgg)
189+
cv2.imshow(f"{i}", imgg)
190+
# all_image.append(imgg)
191+
predic = load_model_cnn_unit(model, imgg)
192+
print(predic[0])
193+
cv2.putText(frame, f"{predic[0]}", (center_point[0], center_point[1] - 50),
194+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA)
195+
cv2.rectangle(frame, (int((center_point[0] - x / 2)), int((center_point[1] - y / 2))),
196+
(int((center_point[0] + x / 2)), int((center_point[1] + y / 2))), (0, 0, 255), 2)
197+
# print(load_model_cnn(model, all_image))
198+
# cv2.imshow("th", th)
199+
cv2.imshow("test", frame)
200+
201+
video.release()
202+
cv2.destroyAllWindows()
203+

cnn_example/load_cnn.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
from keras.models import Sequential, load_model
4+
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
5+
from keras.utils import np_utils, plot_model
6+
from keras.datasets import mnist
7+
from keras.preprocessing import image
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
import cv2
11+
# import pandas as pd
12+
13+
# Load Model
14+
model = load_model('my_model.h5')
15+
model.load_weights('my_model_weights.h5')
16+
Path = ["3.jpg", "6.jpg", "10.jpg"]
17+
18+
print(model.input_shape)
19+
print(model.summary())
20+
# img = image.load_img("0.jpg", 0)
21+
for i in Path:
22+
23+
img = cv2.imread(f"{i}", 0)
24+
img = cv2.resize(img, (28, 28))
25+
img = 255 - img
26+
img = img.astype("float32")
27+
img_4 = img - np.amin(img)
28+
img_5 = 255 * img_4 / (np.amax(img_4))
29+
x_test_img = np.reshape(img_5, (1, 28, 28))
30+
x_Test4D = x_test_img.reshape(x_test_img.shape[0], 28, 28, 1).astype('float32')
31+
x_Test4D_normalize = (x_Test4D / np.amax(x_test_img))
32+
prediction = model.predict_classes(x_Test4D_normalize)
33+
34+
print(prediction)

cnn_example/my_model.h5

2.81 MB
Binary file not shown.

cnn_example/my_model_weights.h5

968 KB
Binary file not shown.

0 commit comments

Comments
 (0)