-
Notifications
You must be signed in to change notification settings - Fork 2
/
prediction.py
52 lines (40 loc) · 1.45 KB
/
prediction.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
import os
import cv2
import numpy as np
from keras.models import load_model
def get_im(path):
"""Prepare img."""
img = cv2.imread(path, 0)
# Reduce size
resized = cv2.resize(img, (128, 128))
# resized = resized.transpose()
return resized
def _load_from_csv(filename):
value_dict = {}
with open('{}'.format(filename), 'r') as csv_file:
for row in csv_file.readlines():
print(row)
data = row.split(';')
value_dict[data[0]] = int(data[1].replace('\n', ''))
return value_dict
def alert_box(tokenizer_categories, predictions):
"""Show alert box."""
alert = ['hand_ups']
index = np.argmax(predictions)
for word, value in tokenizer_categories.items():
print('{}: {}'.format(word, predictions[value]))
if value == index:
if word in alert and predictions[value] >= 0.9:
os.system(
'zenity --error --text="{} detected" '
'--title="Warning!"'.format(word))
tokenizer_categories = _load_from_csv('tokenizer_categories.csv')
os.system('streamer -f jpeg -s 1024 -o test.jpeg')
X_train = [get_im('test.jpeg')]
X_train = np.array(X_train, dtype=np.uint8)
X_train = X_train.reshape(X_train.shape[0], 128, 128, 1)
print(X_train.shape)
model = load_model('neuron_webcam.h5')
predictions = model.predict(X_train, batch_size=128, verbose=1)
alert_box(tokenizer_categories, predictions[0])
os.system('rm test.jpeg')