-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
64 lines (45 loc) · 1.46 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
import cv2
import numpy as np
from keras.models import load_model
import argparse
from PIL import Image
import imutils
def mean_squared_loss(x1,x2):
difference=x1-x2
a,b,c,d,e=difference.shape
n_samples=a*b*c*d*e
sq_difference=difference**2
Sum=sq_difference.sum()
distance=np.sqrt(Sum)
mean_distance=distance/n_samples
return mean_distance
model=load_model("saved_model.h5")
cap = cv2.VideoCapture("test_video.mp4")
print(cap.isOpened())
while cap.isOpened():
imagedump=[]
ret,frame=cap.read()
for i in range(10):
ret,frame=cap.read()
image = imutils.resize(frame,width=1000,height=1200)
frame=cv2.resize(frame, (227,227), interpolation = cv2.INTER_AREA)
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)
output=model.predict(imagedump)
loss=mean_squared_loss(imagedump,output)
if frame.any()==None:
print("none")
if cv2.waitKey(10) & 0xFF==ord('q'):
break
if loss>0.00068:
print('Abnormal Event Detected')
cv2.putText(image,"Abnormal Event",(220,100),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255),4)
cv2.imshow("video",image)
cap.release()
cv2.destroyAllWindows()