-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvmodel_player.py
72 lines (50 loc) · 1.95 KB
/
convmodel_player.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
# -*- coding: utf-8 -*-
# Sample code to use string producer.
import numpy as np
import tensorflow as tf
# --------------------------------------------------
#
# MODEL
#
# --------------------------------------------------
n_input = 80 * 140
with tf.variable_scope('ConvNet', reuse=False):
x = tf.placeholder(tf.float32, [None, 128, 128, 1])
o1 = tf.layers.conv2d(inputs=x, filters=32, kernel_size=3, activation=tf.nn.relu)
o2 = tf.layers.max_pooling2d(inputs=o1, pool_size=2, strides=2)
o3 = tf.layers.conv2d(inputs=o2, filters=64, kernel_size=3, activation=tf.nn.relu)
o4 = tf.layers.max_pooling2d(inputs=o3, pool_size=2, strides=2)
h = tf.layers.dense(inputs=tf.reshape(o4, [1, 30 * 30 * 64]), units=10, activation=tf.nn.relu)
y = tf.layers.dense(inputs=h, units=3, activation=tf.nn.softmax)
# --------------------------------------------------
#
# PLAY
#
# --------------------------------------------------
import cv2
cap = cv2.VideoCapture(0)
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, "./tmp/model10.ckpt")
print("Model restored.")
while True:
ret, img = cap.read() # 720x1280x3 <-- print(img.shape);
resized = cv2.resize(img, (128, 128), interpolation=cv2.INTER_AREA)
# cropped = resized[0:180, 70:250]
# resized64 = cv2.resize(cropped, (128, 128), interpolation=cv2.INTER_AREA)
gray = np.asarray(cv2.cvtColor(resized, 7))
cv2.imshow('Capture', gray)
frame = gray.reshape(-1, 128, 128, 1)
output = sess.run(y, feed_dict={x: frame})
if round(int(output[0][0])) == 1:
print("manzana")
if round(output[0][1]) == 1:
print("plátano")
if round(output[0][2]) == 1:
print("telefono")
ch = 0xFF & cv2.waitKey(1)
if ch == 27:
break
cv2.destroyAllWindows()