Skip to content

Commit 09d97a3

Browse files
author
李雪洋
committed
提交
1 parent 60184de commit 09d97a3

1,008 files changed

Lines changed: 219 additions & 24 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
65.3 KB
Binary file not shown.

neural-networks-and-deep-learning/src/mytest/network.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def sigmoid(z):
162162
return 1.0 / (1.0 + np.exp(-z))
163163

164164

165+
#计算S函数的导数
165166
def sigmoid_prime(z):
166167
"""Derivative of the sigmoid function."""
167168
return sigmoid(z) * (1 - sigmoid(z))

neural-networks-and-deep-learning/src/mytest/test.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,30 @@ def feedforward(a):
5555
return a
5656

5757

58-
# card = [4, 1, 3, 0, 2, 6, 1, 9, 9, 2, 1, 2, 2, 7, 5, 7, 1, 3]
59-
# n = 0
60-
# pkllist = []
61-
# for i in range(1, 37):
62-
# srcImg = cv2.imread(f'./tmp/{i}.bmp', 0)
63-
# gray = cv2.equalizeHist(srcImg)
64-
# cv2.imshow('equalizeHist', gray)
65-
# cv2.waitKey(0)
66-
#
67-
# array = np.array(gray) # array is a numpy array
68-
# ary = []
69-
# for x in array:
70-
# for y in x:
71-
# aa = [float(Decimal((255 - y) / 255).quantize(Decimal('0.00000000')))]
72-
# ary.append(aa)
73-
# if n == len(card):
74-
# n = 0
75-
# print(card[n])
76-
# pkllist.append((ary, card[n]))
77-
# n += 1
78-
# fw = open('./data.pkl', 'wb+')
79-
# pickle.dump(pkllist, fw)
58+
card = [4, 1, 3, 0, 2, 6, 1, 9, 9, 2, 1, 2, 2, 7, 5, 7, 1, 3]
59+
n = 0
60+
pkllist = []
61+
for i in range(1, 37):
62+
srcImg = cv2.imread(f'./tmp/{i}.bmp', 0)
63+
gray = cv2.equalizeHist(srcImg)
64+
cv2.imshow('equalizeHist', gray)
65+
cv2.waitKey(0)
66+
67+
array = np.array(gray) # array is a numpy array
68+
print(array)
69+
cv2.waitKey(0)
70+
ary = []
71+
for x in array:
72+
for y in x:
73+
aa = [float(Decimal((255 - y) / 255).quantize(Decimal('0.00000000')))]
74+
ary.append(aa)
75+
if n == len(card):
76+
n = 0
77+
print(card[n])
78+
pkllist.append((ary, card[n]))
79+
n += 1
80+
fw = open('./data.pkl', 'wb+')
81+
pickle.dump(pkllist, fw)
8082

8183
data = pickle.load(open('./data.pkl', 'rb'))
8284
for x, y in data:

neural-networks-and-deep-learning/src/test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010
import mnist_loader
1111

1212
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
13-
net = network.Network([784, 30, 10])
14-
net.SGD(training_data, 30, 10, 3.0, test_data=test_data)
13+
# net = network.Network([784, 30, 10])
14+
# net.SGD(training_data, 30, 10, 3.0, test_data=test_data)
15+
for x, y in training_data:
16+
print(x)
17+
print('---------------')
18+
print(y)
19+
break

opencv/cbss/01.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# @Time : 2017/11/8 0008 下午 19:18
5+
# @Author : 李雪洋
6+
# @File : 01.py
7+
# @Software: PyCharm
8+
import cv2
9+
import numpy as np
10+
import os
11+
12+
img_save_path = 'E:\\pythonDemo\\opencv\\cbss\\spit\\'
13+
14+
15+
# 按照指定图像大小调整尺寸
16+
def resize_image(image, height, width):
17+
top, bottom, left, right = (0, 0, 0, 0)
18+
19+
# 获取图像尺寸
20+
h, w = image.shape
21+
22+
# 对于长宽不相等的图片,找到最长的一边
23+
longest_edge = max(h, w)
24+
25+
# 计算短边需要增加多上像素宽度使其与长边等长
26+
if h < longest_edge:
27+
dh = longest_edge - h
28+
top = dh // 2
29+
bottom = dh - top
30+
elif w < longest_edge:
31+
dw = longest_edge - w
32+
left = dw // 2
33+
right = dw - left
34+
else:
35+
pass
36+
37+
# RGB颜色
38+
BLACK = [0, 0, 0]
39+
40+
# 给图像增加边界,是图片长、宽等长,cv2.BORDER_CONSTANT指定边界颜色由value指定
41+
constant = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK)
42+
43+
# 调整图像大小并返回
44+
return cv2.resize(constant, (height, width))
45+
46+
47+
def saveSpitImg(boxList, dst):
48+
for box in boxList:
49+
img_org2 = dst.copy()
50+
img_plate = img_org2[box[2]:box[3], box[0]:box[1]]
51+
# img_plate = cv2.resize(img_plate, (30, 30), interpolation=cv2.INTER_CUBIC)
52+
img_plate = resize_image(img_plate, 30, 30)
53+
l = os.listdir(img_save_path)
54+
size = len(l)
55+
size += 1
56+
cv2.imwrite(img_save_path + str(size) + '.bmp', img_plate)
57+
58+
59+
for i in range(1, 5001):
60+
print(i)
61+
srcImg = cv2.imread(f"E:\\pythonDemo\\opencv\\cbss\\img\\{i}.bmp")
62+
h, w, _ = srcImg.shape
63+
dstImg = cv2.resize(srcImg, (w * 10, h * 10), interpolation=cv2.INTER_CUBIC)
64+
# cv2.imshow('dstImg', dstImg)
65+
# cv2.waitKey(0)
66+
67+
gray = cv2.cvtColor(dstImg, cv2.COLOR_BGR2GRAY)
68+
69+
ret1, th1 = cv2.threshold(gray, 82, 255, cv2.THRESH_BINARY_INV)
70+
# cv2.imshow('th1', th1)
71+
# cv2.waitKey(0)
72+
73+
kernel = np.ones((11, 11), np.uint8)
74+
dilation = cv2.dilate(th1, kernel, iterations=1)
75+
76+
# cv2.imshow('dilation', dilation)
77+
# cv2.waitKey(0)
78+
79+
kernel = np.ones((11, 11), np.uint8)
80+
dilation2 = cv2.dilate(dilation, kernel, iterations=1)
81+
82+
# cv2.imshow('dilation2', dilation2)
83+
# cv2.waitKey(0)
84+
85+
(_, contours, _) = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
86+
87+
boxList = []
88+
for j in contours:
89+
# 计算该轮廓的面积
90+
area = cv2.contourArea(j)
91+
#
92+
# 面积小的都筛选掉
93+
if area < 1300:
94+
continue
95+
96+
x, y, width, height = cv2.boundingRect(j)
97+
box = [x, y, width, height]
98+
box = [box[0], box[0] + box[2], box[1], box[1] + box[3]]
99+
boxList.append(box)
100+
# cv2.rectangle(dstImg, (box[0], box[2]), (box[1], box[3]), (0, 255, 0), 1)
101+
saveSpitImg(boxList, th1)

opencv/cbss/download.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# @Time : 2017/11/8 0008 下午 19:18
5+
# @Author : 李雪洋
6+
# @File : download.py
7+
# @Software: PyCharm
8+
9+
# https://hq.cbss.10010.com/image?mode=validate&width=60&height=20&random=0.5718313927110062
10+
import requests
11+
12+
def download(n):
13+
14+
url = 'http://hq.cbss.10010.com/image?mode=validate&width=60&height=20&random=0.5718313927110062'
15+
res = requests.get(url, stream=True)
16+
with open('E:\\pythonDemo\\opencv\\cbss\\img\\' + str(n) + '.bmp', 'wb') as f:
17+
for chunk in res.iter_content(chunk_size=1024):
18+
if chunk: # filter out keep-alive new chunks
19+
f.write(chunk)
20+
f.flush()
21+
f.close()
22+
print(n)
23+
24+
for i in range(1, 5001):
25+
download(i)

opencv/cbss/makePkl.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# @Time : 2017/11/9 0009 下午 15:15
5+
# @Author : 李雪洋
6+
# @File : makePkl.py
7+
# @Software: PyCharm
8+
9+
import pickle
10+
from decimal import Decimal
11+
import cv2
12+
import numpy as np
13+
import os
14+
15+
dir_name = ['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']
16+
17+
base_path = 'E:\\pythonDemo\\opencv\\cbss\\trainDemo\\'
18+
19+
pkllist = []
20+
for dirName in dir_name:
21+
rootdir = base_path + dirName
22+
fileList = os.listdir(rootdir) # 列出文件夹下所有的目录与文件
23+
24+
realNp = np.zeros((len(dir_name), 1))
25+
for n in range(len(dir_name)):
26+
if dir_name[n] == dirName:
27+
realNp[n] = 1
28+
else:
29+
realNp[n] = 0
30+
31+
for i in range(0, len(fileList)):
32+
path = os.path.join(rootdir, fileList[i])
33+
if os.path.isfile(path):
34+
#print(path)
35+
img = cv2.imread(path, 0)
36+
# cv2.imshow('img', img)
37+
# cv2.waitKey(0)
38+
array = np.array(img) # array is a numpy array
39+
print(array)
40+
ary = np.zeros((900, 1))
41+
num = 0
42+
for x in array:
43+
for y in x:
44+
ary[num] = [float(Decimal(y / 255).quantize(Decimal('0.00000000')))]
45+
num += 1
46+
pkllist.append((ary, realNp))
47+
48+
fw = open('./data.pkl', 'wb+')
49+
pickle.dump(pkllist, fw)

opencv/cbss/readPkl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# @Time : 2017/11/9 0009 下午 16:49
5+
# @Author : 李雪洋
6+
# @File : readPkl.py
7+
# @Software: PyCharm
8+
import pickle
9+
from numpy import *
10+
11+
data = pickle.load(open('./data.pkl', 'rb'))
12+
print(len(data))

opencv/cbss/trainDemo/A/147.bmp

1.99 KB
Binary file not shown.

opencv/cbss/trainDemo/A/159.bmp

1.99 KB
Binary file not shown.

0 commit comments

Comments
 (0)