示例#1
0
def check_for_anomalies(vc):

    imagedump = []

    for i in range(10):
        rval, frame = vc.read()
        frame = imresize(frame, (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)

    output = model.predict(imagedump)

    loss = mean_squared_loss(imagedump, output)

    if loss > threshold:
        result = 'Anomalies Detected'
    else:
        result = 'No Anomalies'

    return result
		#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')


frames = X_test.shape[2]
#Need to make number of frames divisible by 10

flag = 0  #Overall video flagq

frames = frames - frames % 10

X_test = X_test[:, :, :frames]
X_test = X_test.reshape(-1, 227, 227, 10)
X_test = np.expand_dims(X_test, axis=4)

for number, bunch in enumerate(X_test):
    n_bunch = np.expand_dims(bunch, axis=0)
    reconstructed_bunch = model.predict(n_bunch)

    loss = mean_squared_loss(n_bunch, reconstructed_bunch)

    if loss > threshold:
        print("Anomalous bunch of frames at bunch number {}".format(number))
        flag = 1

    else:
        print('Bunch Normal')

if flag == 1:
    print("Anomalous Events detected")

from keras.preprocessing.image import img_to_array, load_img
import numpy as np
import glob
import os