-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
128 lines (89 loc) · 2.82 KB
/
testing.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
# testing.py of EPQ Project FINAL NAME HERE
# Copyright James Robinson 2021-2022
# All rights reserved
#
# James Robinson (8116) - The Burgate School and Sixth Form (58815)
import numpy as np
import pickle
import cv2
from time import time
import matplotlib.pyplot as plt
WIDTH = 1920
HEIGHT = 1080
NORMALISE = False
cap = cv2.VideoCapture(1)
cap.set(3, WIDTH)
cap.set(3, HEIGHT)
pf = open("models/model_train2.pickle", 'rb')
model = pickle.load(pf)
print(model.summary())
def pre_proc(img, normalise=False):
# center = img.shape[0] / 2, img.shape[1] / 2
# x = center[1] - 420 / 2
# y = center[0] - 420 / 2
#
# img = img[int(y):int(y + 420), int(x):int(x + 420)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if normalise:
img = cv2.equalizeHist(img)
img = img / 255
return img
# Func to be removed
def bar_chart(pred=None):
print("REMOVE DEPRECIATED FUNCTION IN TESTING.py\n")
if pred is None:
pred = []
# ========== Plotting ==========
plt.ion()
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
x = [*range(10)]
y = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
plt.title("Predictions from Netowrk", fontsize=18)
plt.xlabel("Number", fontsize=18)
plt.ylabel("Confidence", fontsize=18)
# ========== Main bit ==========
st = time()
t = time()
p2 = {}
re_img = None
re_img_original = None
while True:
success, img_original = cap.read()
# Properly crop image...
center = img_original.shape[0] / 2, img_original.shape[1] / 2
x, y = center[1] - 1080 / 2, center[0] - 1080 / 2
img_original = img_original[int(y):int(y + 1080), int(x):int(x + 1080)]
img = np.asarray(img_original)
# img = cv2.resize(img, (420, 420))
img = cv2.resize(img, (28, 28))
img = pre_proc(img)
re_img = cv2.resize(img, (420, 420), interpolation=cv2.INTER_NEAREST)
re_img_original = cv2.resize(img_original, (420, 420), interpolation=cv2.INTER_NEAREST)
cv2.imshow("Pre-Processed Image (CNN Input)", re_img)
cv2.imshow("Webcam Input", re_img_original)
#
# cv2.imwrite("img.png", re_img)
# cv2.imwrite("img_original.png", re_img_original)
# cv2.imshow("Combined", cv2.hconcat([re_img, re_img_original]))
img = img.reshape(1, 28, 28, 1)
pred = model.predict(img)
# bar_chart(pred=pred[0])
class_index = int(np.argmax(pred, axis=1))
for index, i in enumerate(pred[0]):
p2[index] = int(i * 10000) / 100
print(" " * 150, end="\r")
# ========= Choices ==========
# print(" " * 150, end="\r")
print(" " * 5, end="\r")
# print(class_index, "-", f"{p2[class_index]}% - {1 / (time() - t)}c/s -", p2, end="\r")
print(class_index, end="\r")
# # Plotting...
# y = pred[0]
#
# ax.bar(x, y)
# fig.canvas.draw()
# fig.canvas.flush_events()
t = time()
if cv2.waitKey(1) & 0xFF == ord("q"):
break