-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
124 lines (85 loc) · 3.34 KB
/
test.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
# import numpy as np
# from keras.preprocessing import image
# from keras.models import load_model
# from PIL import Image
# from keras import models
# from keras.preprocessing.image import ImageDataGenerator
# import cv2
# model = load_model('model.h5')
# size = 200
# test_image =image.load_img('/home/itm-it1018/Downloads/webcam-model-master/data_set/test-data/Manikantha .138.jpg', target_size=(size,size))
# # print(test_image)
# test_image = image.img_to_array(test_image)
# test_image = np.expand_dims(test_image, axis=0)
# result = model.predict(test_image)
# train_datagen = ImageDataGenerator(
# rescale=1./255,
# rotation_range=40,
# width_shift_range=0.2,
# height_shift_range=0.2,
# shear_range=0.2,
# zoom_range=0.2,
# horizontal_flip=True)
# validation_datagen = ImageDataGenerator(rescale=1.255)
# train_generator = train_datagen.flow_from_directory('data_set/train',target_size=(size,size),batch_size=32, class_mode='categorical')
# validation_generator = validation_datagen.flow_from_directory('data_set/test', target_size=(size,size), batch_size=32, class_mode='categorical')
# for k,v in train_generator.class_indices.items():
# if v==int(result[0][0]):
# print(k)
# print(int(result[0][0]))
# print(train_generator.class_indices)
import numpy as np
from keras.preprocessing import image
from keras.models import load_model
from PIL import Image
from keras import models
from keras.preprocessing.image import ImageDataGenerator
import cv2
model = load_model('model.h5')
size = 128
video = cv2.VideoCapture(0)
# while True:
_, frame = video.read()
#print("frame",frame)
#Convert the captured frame into RGB
im = Image.fromarray(frame, 'RGB')
#Resizing into 128x128 because we trained the model with this image size.
im = im.resize((size,size))
img_array = np.array(im)
#print("frame123",img_array)
#Our keras model used a 4D tensor, (images x height x width x channel)
#So changing dimension 128x128x3 into 1x128x128x3
img_array = np.expand_dims(img_array, axis=0)
#Calling the predict method on model to predict 'me' on the image
# prediction = int(model.predict(img_array)[0][0])
prediction = model.predict(img_array)
# print(prediction)
#if prediction is 0, which means I am missing on the image, then show the frame in gray color.
# if prediction == 1:
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow("Capturing", frame)
key=cv2.waitKey(225)
# # test_image = image.load_img('data_set/test/sample5.jpg', target_size = (size,size))
# test_image = image.img_to_array(test_image)
# test_image = np.expand_dims(test_image, axis = 0)
# result = model.predict(test_image)
# #training_set.class_indices
from keras.preprocessing.image import ImageDataGenerator
train_data_dir = 'data_set1/train'
validation_data_dir = 'data_set1/test'
datagen = ImageDataGenerator(rescale=1./255)
batch_size = 32
# test_datagen = ImageDataGenerator(rescale = 1./255)
train_generator = datagen.flow_from_directory(
train_data_dir,
target_size=(size,size),
batch_size=batch_size,
class_mode='categorical')
# validation_generator = datagen.flow_from_directory(
# validation_data_dir,
# target_size=(size,size),
# batch_size=batch_size,
# class_mode='categorical')
for k,v in train_generator.class_indices.items():
if v==int(prediction[0][0]):
print(k)