-
Notifications
You must be signed in to change notification settings - Fork 5
/
start_live_feed.py
84 lines (43 loc) · 1.27 KB
/
start_live_feed.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
'''
Running The Spatiotemporal autoencoder on live webcam field
run python3 start_live_feed.py 'path_to_model' to start the feed and processing
Author: Harsh Tiku
'''
import cv2
from model import load_model
import numpy as np
# from scipy.misc import imresize
from PIL import Image
from test import mean_squared_loss
from keras.models import load_model
import argparse
parser=argparse.ArgumentParser()
# parser.add_argument('modelpath',type=str)
parser.add_argument('model.py')
args=parser.parse_args()
modelpath=args.modelpath
vc=cv2.VideoCapture(0)
rval=True
print('Loading model')
model=load_model(modelpath)
print('Model loaded')
threshold=0.1
while True:
imagedump=[]
for i in range(10):
rval,frame=vc.read()
frame=Image.fromarray(frame).resize(size=(227,227,3))
#Convert the Image to Grayscale
gray=0.2989*frame[:,:,0]+0.5870*frame[:,:,1]+0.1140*frame[:,:,2]
gray=(gray-gray.mean())/gray.std()
gray=np.clip(gray,0,1)
imagedump.append(gray)
imagedump=np.array(imagedump)
imagedump.resize(227,227,10)
imagedump=np.expand_dims(imagedump,axis=0)
imagedump=np.expand_dims(imagedump,axis=4)
print('Processing data')
output=model.predict(imagedump)
loss=mean_squared_loss(imagedump,output)
if loss>threshold:
print('Anomalies Detected')