forked from Martico09/Intruder-Detection-System
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
232 lines (163 loc) · 6.03 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python
# coding: utf-8
# In[9]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
from sklearn.utils import shuffle
from sklearn.metrics import confusion_matrix,classification_report
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras
import logging
logging.getLogger('tensorflow').disabled = True
from keras.layers import Dropout, Dense, Flatten, BatchNormalization, Conv2D, MaxPooling2D, GlobalAveragePooling2D
from tensorflow.keras.optimizers import SGD
from keras import layers
from keras.models import Sequential
from keras.callbacks import EarlyStopping
from keras.applications.inception_v3 import InceptionV3
from keras.applications.xception import Xception
from keras.applications.mobilenet import MobileNet
from keras.applications.vgg16 import VGG16
import cv2
from tqdm import tqdm
import os
import gc
from playsound import playsound
# In[2]:
images = []
labels = []
main_directory = 'data2'
for animal in tqdm(os.listdir(main_directory)):
for i in range(len(os.listdir(main_directory + '/' + animal))):
img = cv2.imread(main_directory + '/' + animal + '/' + os.listdir(main_directory + '/' + animal)[i])
resized_img = cv2.resize(img,(224,224))
resized_img = resized_img / 255
images.append(resized_img)
labels.append(animal)
images = np.array(images,dtype = 'float32')
# In[3]:
le = preprocessing.LabelEncoder()
le.fit(labels)
class_names = le.classes_
labels = le.transform(labels)
labels = np.array(labels, dtype = 'uint8')
labels = np.resize(labels, (len(labels),1))
# In[4]:
x_train_images, x_test_images, y_train_labels, y_test_labels = train_test_split(images, labels,
test_size=0.25,
stratify = labels,
random_state=9,
shuffle=True)
# In[5]:
from tensorflow.keras.utils import to_categorical
y_cat_train=to_categorical(y_train_labels,2)
y_cat_test=to_categorical(y_test_labels,2)
classes_info = {}
classes = sorted(os.listdir(main_directory))
for name in classes:
classes_info[name] = len(os.listdir(main_directory + f'/{name}'))
print(classes_info)
# In[7]:
from keras.models import Sequential
from tensorflow.keras import layers
model = Sequential([#layers.Dropout(0.2),
layers.Conv2D(6, kernel_size=5, strides=1, activation='tanh', input_shape=x_train_images[0].shape, padding='same'),
layers.AveragePooling2D(),
layers.Conv2D(16, kernel_size=5, strides=1, activation='relu', padding='valid'),
layers.AveragePooling2D(),
#layers.Conv2D(120, kernel_size=5, strides=1, activation='relu', padding='valid'),
#layers.AveragePooling2D(),
layers.Conv2D(120, kernel_size=5, strides=1, activation='relu', padding='valid'),
layers.AveragePooling2D(),
layers.Conv2D(16, kernel_size=5, strides=1, activation='relu', padding='valid'),
layers.Flatten(),
layers.Dense(84, activation='relu'),
#layers.Dense(168, activation='relu'),
#layers.Dense(84, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"] )
model.summary()
# In[11]:
model.fit(x_train_images,y_train_labels, epochs=5, verbose=2)
# In[108]:
#model.save('1Final Model')
# In[2]:
model=tf.keras.models.load_model('Final Model')
# In[12]:
model.evaluate(x_test_images,y_test_labels)
# In[7]:
q = 80
import matplotlib.pyplot as plt
plt.imshow(x_test_images[q] , cmap = "gray")
result = model.predict(x_test_images)
if result[q]>=0.5:
print('intruder')
else:
print('allowed')
# In[16]:
##realtime one
t=1
import cv2
face_classifier=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") #Note the change
video_capture = cv2.VideoCapture(0)
timer = 0
unknown = 0
wait_time = 100
while True:
# Capture frame-by-frame
ret, frames = video_capture.read()
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
#minSize=(30, 30),
#flags=cv2.CASCADE_SCALE_IMAGE
)
if len(faces) == 0:
timer = 0
unknown = 0
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (224, 224))
color_channeled_image = cv2.cvtColor(face_resize, cv2.COLOR_GRAY2BGR)
array=np.array(color_channeled_image)
array=array/255
img = array.reshape(1,224,224,3)
result=model.predict(img)
name='name'
for j in range(100):
for i in range(result.shape[1]):
if result[0][i]<0.75:
name='Known'
else:
name='Unknown'
timer +=1
if name == 'Unknown':
unknown +=1
t=1
value = str(((unknown)/wait_time)*100)+" %"
cv2.putText(img=frames, text=value, org=(50,50), fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=1, color=(255, 0, 0),thickness=1)
cv2.putText(img=frames, text=name, org=(x, y-20), fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=1, color=(255, 0, 0),thickness=1)
print(result)
print(f"timer = {timer},unknown = {unknown}")
if timer>wait_time:
timer = 0
if unknown>=wait_time//2:
playsound('beep.wav')
t=0
# Display the resulting frame
cv2.imshow('Video', frames)
if (cv2.waitKey(1) & 0xFF == ord('q')) or t==0:
break
video_capture.release()
cv2.destroyAllWindows()
# In[ ]: