Exemplo n.º 1
0
 def __init__(self, video_name):
     self.vid = PreProcess()
     self.vid.read_video(video_name)
     self.flow = OptFlow()
     self.vid.setVideoDimension(100)
     self.index = 0
     self.binary_mags = []
Exemplo n.º 2
0
    def getViolentFlow(self):
        vid = PreProcess()
        vid.read_video(self.video_name)
        flow = OptFlow()
        vid.setVideoDimension(100)
        index = 0
        temp_flows = []
        for each_frame_index in range(3,vid.total_frames - vid.FRAME_GAP - 5,vid.FRAME_GAP):

            PREV_F = vid.getFrameFromIndex(each_frame_index)
            CURRENT_F = vid.getFrameFromIndex(each_frame_index + vid.MOVEMENT_INTERVAL)
            NEXT_F = vid.getFrameFromIndex(each_frame_index + (2 * vid.MOVEMENT_INTERVAL))

            PREV_F = vid.resize_frame(PREV_F)
            CURRENT_F = vid.resize_frame(CURRENT_F)
            NEXT_F = vid.resize_frame(NEXT_F)

            (vx1,vy1,w1) = flow.sorFlow(PREV_F,CURRENT_F)
            (vx2,vy2,w2) = flow.sorFlow(CURRENT_F,NEXT_F)

            m1 = flow.getFlowMagnitude(vx1,vy1)
            index = index + 1
            m2 = flow.getFlowMagnitude(vx2,vy2)
            # all_zeros1 = not np.any(m1)
            # all_zeros2 = not np.any(m2)
            # if (all_zeros1 and all_zeros2):
            #     continue
            # index = index + 1


            change_mag = abs(m2-m1)
            binary_mag = np.ones(change_mag.shape,dtype=np.float64)
            threshold = np.mean(change_mag , dtype=np.float64)
            temp_flows.append(np.where(change_mag < threshold,0,binary_mag))

        flow_video = np.zeros(change_mag.shape,dtype=np.float64)
        for each_flow in temp_flows:
            flow_video = flow_video + each_flow

        flow_video = flow_video / index


        #original
        # self.height = flow_video.shape[0]
        # self.width = flow_video.shape[1]
        # self.B_height = int(math.floor(float(self.height - 11)/4))
        # self.B_width = int(math.floor(float(self.width - 11)/4))

        # testing
        self.height = flow_video.shape[0]
        self.width = flow_video.shape[1]
        self.B_height = int(math.floor(float(self.height - 10)/5))
        self.B_width = int(math.floor(float(self.width - 10)/5))

        # print self.height , self.width , self.B_height , self.B_width

        return flow_video
Exemplo n.º 3
0
    def __init__(self):
        json_file = open('model_100.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        self.model = model_from_json(loaded_model_json)
        self.model.load_weights("model_100.h5")
        print 'loaded model from disk'

        self.vid = PreProcess()
        self.vid.setVideoDimension(100)
        self.flow = OptFlow()
        self.height = 0
        self.width = 0
        self.B_height = 0
        self.B_width = 0
        self.index = 0
        self.temp_flows = []
        self.bins = np.arange(0.0, 1.05, 0.05, dtype=np.float64)
Exemplo n.º 4
0
def getViolentFlow(video_name):
    vid = PreProcess()
    vid.read_video(video_name)
    flow = OptFlow()
    vid.setVideoDimension(100)
    index = 0
    temp_flows = []
    for each_frame_index in range(3,vid.total_frames - vid.FRAME_GAP - 5,vid.FRAME_GAP):

        PREV_F = vid.getFrameFromIndex(each_frame_index)
        CURRENT_F = vid.getFrameFromIndex(each_frame_index + vid.MOVEMENT_INTERVAL)
        NEXT_F = vid.getFrameFromIndex(each_frame_index + (2 * vid.MOVEMENT_INTERVAL))

        PREV_F = vid.resize_frame(PREV_F)
        CURRENT_F = vid.resize_frame(CURRENT_F)
        NEXT_F = vid.resize_frame(NEXT_F)

        (vx1,vy1,w1) = flow.sorFlow(PREV_F,CURRENT_F)
        (vx2,vy2,w2) = flow.sorFlow(CURRENT_F,NEXT_F)

        m1 = flow.getFlowMagnitude(vx1,vy1)
        index = index + 1
        m2 = flow.getFlowMagnitude(vx2,vy2)

    #    print m1
    #    print m2

        change_mag = abs(m2-m1)
        binary_mag = np.ones(change_mag.shape,dtype=np.float64)
        threshold = np.mean(change_mag , dtype=np.float64)
        temp_flows.append(np.where(change_mag < threshold,0,binary_mag))

    flow_video = np.zeros(change_mag.shape,dtype=np.float64)
    for each_flow in temp_flows:
        flow_video = flow_video + each_flow

    flow_video = flow_video / index
    return flow_video
Exemplo n.º 5
0
import cv2
import numpy as np
from VideoProcess import PreProcess
from OpticalFlow import OptFlow
import math

vid = PreProcess()
vid.read_video('vio_1.avi')
flow = OptFlow()
vid.setVideoDimension(100)
index = 0
all_hists1 = []
all_hists2 = []


def binData(X, bins, mag):
    hist = [0.0] * 9
    inds = np.digitize(X, bins)
    for i in range(X.size):
        hist[inds[i][0] - 1] = hist[inds[i][0] - 1] + mag[i][0]
    return hist


for each_frame_index in range(3, vid.total_frames - vid.FRAME_GAP - 5,
                              vid.FRAME_GAP):

    PREV_F = vid.getFrameFromIndex(each_frame_index)
    CURRENT_F = vid.getFrameFromIndex(each_frame_index + vid.MOVEMENT_INTERVAL)
    NEXT_F = vid.getFrameFromIndex(each_frame_index +
                                   (2 * vid.MOVEMENT_INTERVAL))
Exemplo n.º 6
0
class ContinousSurv2:
    def __init__(self):
        json_file = open('model_100.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        self.model = model_from_json(loaded_model_json)
        self.model.load_weights("model_100.h5")
        print 'loaded model from disk'

        self.vid = PreProcess()
        self.vid.setVideoDimension(100)
        self.flow = OptFlow()
        self.height = 0
        self.width = 0
        self.B_height = 0
        self.B_width = 0
        self.index = 0
        self.temp_flows = []
        self.bins = np.arange(0.0, 1.05, 0.05, dtype=np.float64)

    def setVideoName(self, video_name):
        self.vid.read_video(video_name)

    def histc(self, X, bins):
        map_to_bins = np.digitize(X, bins)
        r = np.zeros(bins.shape, dtype=np.float64)
        for i in map_to_bins:
            r[i - 1] += 1
        return r

    def getBlockHist(self, flow_video):
        flow_vec = np.reshape(flow_video,
                              (flow_video.shape[0] * flow_video.shape[1], 1))
        count_of_bins = self.histc(flow_vec, self.bins)
        return count_of_bins / np.sum(count_of_bins)

    def getFrameHist(self, flow_video_size):
        flow_video = np.zeros(flow_video_size, dtype=np.float64)
        for each_flow in self.temp_flows:
            flow_video = flow_video + each_flow
        flow_video = flow_video / self.index
        self.index = 0
        self.temp_flows = []
        self.height = flow_video.shape[0]
        self.width = flow_video.shape[1]
        self.B_height = int(math.floor((self.height - 11) / 4))
        self.B_width = int(math.floor((self.width - 11) / 4))
        frame_hist = []
        for y in range(6, self.height - self.B_height - 4, self.B_height):
            for x in range(6, self.width - self.B_width - 4, self.B_width):
                block_hist = self.getBlockHist(flow_video[y:y + self.B_height,
                                                          x:x + self.B_width])
                frame_hist = np.append(frame_hist, block_hist, axis=0)
        return frame_hist

    def doSurveillanceFromVideo(self):
        video_time_start = time.time()
        FPS = round(self.vid.getFPS())
        print 'FPS is : ' + str(FPS)
        while True:
            frames = self.vid.getFramesFromVideoSource()
            PREV_F = frames[0]
            CURRENT_F = frames[1]
            NEXT_F = frames[2]

            frame_number = frames[3]

            PREV_F = self.vid.resize_frame(PREV_F)
            CURRENT_F = self.vid.resize_frame(CURRENT_F)
            NEXT_F = self.vid.resize_frame(NEXT_F)

            (vx1, vy1, w1) = self.flow.sorFlow(PREV_F, CURRENT_F)
            (vx2, vy2, w2) = self.flow.sorFlow(CURRENT_F, NEXT_F)

            m1 = self.flow.getFlowMagnitude(vx1, vy1)
            self.index = self.index + 1
            m2 = self.flow.getFlowMagnitude(vx2, vy2)

            change_mag = abs(m2 - m1)
            binary_mag = np.ones(change_mag.shape, dtype=np.float64)
            threshold = np.mean(change_mag, dtype=np.float64)
            self.temp_flows.append(
                np.where(change_mag < threshold, 0, binary_mag))

            if self.index >= int(FPS / 3):
                vif = self.getFrameHist(CURRENT_F.shape)
                X_frame = np.empty((0, 336))
                vif = np.reshape(vif, (-1, vif.shape[0]))
                X_frame = np.vstack((X_frame, vif))
                pred = self.model.predict(X_frame)
                pred = round(pred[0][0])
                if pred == 1:
                    time_violence = float(frame_number) / self.vid.fps
                    print 'violent  ---   ' + str(
                        int(time_violence)
                    ) + ' seconds , processing time : ' + str(time.time() -
                                                              video_time_start)

    def doSurveillanceFromCamera(self):
        start_time = time.time()
        self.vid.useCamera()
        FPS = round(self.vid.getFPS())
        print 'FPS is :' + str(FPS)
        while True:
            frames = self.vid.getFramesFromCameraSource()
            PREV_F = frames[0]
            CURRENT_F = frames[1]
            NEXT_F = frames[2]

            time_now = frames[3]

            PREV_F = self.vid.resize_frame(PREV_F)
            CURRENT_F = self.vid.resize_frame(CURRENT_F)
            NEXT_F = self.vid.resize_frame(NEXT_F)

            (vx1, vy1, w1) = self.flow.sorFlow(PREV_F, CURRENT_F)
            (vx2, vy2, w2) = self.flow.sorFlow(CURRENT_F, NEXT_F)

            m1 = self.flow.getFlowMagnitude(vx1, vy1)
            self.index = self.index + 1
            m2 = self.flow.getFlowMagnitude(vx2, vy2)

            change_mag = abs(m2 - m1)
            binary_mag = np.ones(change_mag.shape, dtype=np.float64)
            threshold = np.mean(change_mag, dtype=np.float64)
            self.temp_flows.append(
                np.where(change_mag < threshold, 0, binary_mag))

            if self.index >= int(FPS / 3):
                vif = self.getFrameHist(CURRENT_F.shape)
                X_frame = np.empty((0, 336))
                vif = np.reshape(vif, (-1, vif.shape[0]))
                X_frame = np.vstack((X_frame, vif))
                pred = self.model.predict(X_frame)
                print pred, time_now - start_time
                pred = round(pred[0][0])
                if pred == 1:
                    print 'violent  ---   ' + str(time_now - start_time)
Exemplo n.º 7
0
import cv2
from VideoProcess import PreProcess
from OpticalFlow import OptFlow

vid = PreProcess()
flow = OptFlow()
vid.useCamera()
vid.setVideoDimension(300)
while True:
    frame1 = vid.getFramesFromSource()
    frame2 = vid.getFramesFromSource()
    (vx, vy, w) = flow.sorFlow(frame1, frame2)

    cv2.imshow('camera', frame2)
    cv2.imshow('vy', vy)
    cv2.imshow('vx', vx)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
Exemplo n.º 8
0
class OvifCalc:
    def __init__(self, video_name):
        self.vid = PreProcess()
        self.vid.read_video(video_name)
        self.flow = OptFlow()
        self.vid.setVideoDimension(100)
        self.index = 0
        self.binary_mags = []

    def binData(self, X, bins, mag):
        hist = [0.0] * 9
        inds = np.digitize(X, bins)
        for i in range(X.size):
            hist[inds[i][0] - 1] = hist[inds[i][0] - 1] + mag[i][0]
        return hist

    def getBinaryMag(self, hist1, hist2):
        hist1 = np.array(hist1)
        hist2 = np.array(hist2)
        change_mag = abs(hist1 - hist2)
        threshold = np.mean(change_mag, dtype=np.float64)
        binary_mag = np.ones(change_mag.shape, dtype=np.float64)
        binary_mag = np.where(change_mag < threshold, 0, binary_mag)
        return binary_mag

    def getOvifFeature(self):
        for each_frame_index in range(
                3, self.vid.total_frames - self.vid.FRAME_GAP - 5,
                self.vid.FRAME_GAP):

            PREV_F = self.vid.getFrameFromIndex(each_frame_index)
            CURRENT_F = self.vid.getFrameFromIndex(each_frame_index +
                                                   self.vid.MOVEMENT_INTERVAL)
            NEXT_F = self.vid.getFrameFromIndex(each_frame_index +
                                                (2 *
                                                 self.vid.MOVEMENT_INTERVAL))

            PREV_F = self.vid.resize_frame(PREV_F)
            CURRENT_F = self.vid.resize_frame(CURRENT_F)
            NEXT_F = self.vid.resize_frame(NEXT_F)

            (vx1, vy1, w1) = self.flow.sorFlow(PREV_F, CURRENT_F)
            (vx2, vy2, w2) = self.flow.sorFlow(CURRENT_F, NEXT_F)

            m1 = self.flow.getFlowMagnitude(vx1, vy1)
            self.index = self.index + 1
            m2 = self.flow.getFlowMagnitude(vx2, vy2)

            all_zeros1 = not np.any(vx1)
            all_zeros2 = not np.any(vx2)
            all_hists1 = []
            all_hists2 = []
            if not (all_zeros1 or all_zeros2):
                phi1 = np.arctan(vy1 / vx1)
                phi2 = np.arctan(vy2 / vx2)

                data_block_size = (m1.shape[0] * m1.shape[1]) / (4 * 4)

                m1 = np.reshape(m1, (m1.shape[0] * m1.shape[1], 1))
                m2 = np.reshape(m2, (m2.shape[0] * m2.shape[1], 1))
                phi1 = np.reshape(phi1, (phi1.shape[0] * phi1.shape[1], 1))
                phi2 = np.reshape(phi2, (phi2.shape[0] * phi2.shape[1], 1))

                hist_bins = np.arange(9.) * (2 * np.pi) / 9
                hist_bins = np.degrees(hist_bins)
                phi1 = np.rad2deg(phi1)
                phi2 = np.rad2deg(phi2)
                phi1 = np.where(phi1 < 0, 360 + phi1, phi1)
                phi2 = np.where(phi2 < 0, 360 + phi2, phi2)

                for k in range(0, (4 * 4)):
                    data_to_be_binned1 = []
                    data_to_be_binned2 = []
                    if k != (4 * 4) - 1:
                        data_to_be_binned1 = m1[data_block_size *
                                                k:data_block_size * (k + 1)]
                        temp_phi1 = phi1[data_block_size * k:data_block_size *
                                         (k + 1)]
                        data_to_be_binned2 = m2[data_block_size *
                                                k:data_block_size * (k + 1)]
                        temp_phi2 = phi2[data_block_size * k:data_block_size *
                                         (k + 1)]
                    else:
                        data_to_be_binned1 = m1[data_block_size * k:]
                        temp_phi1 = phi1[data_block_size * k:]
                        data_to_be_binned2 = m2[data_block_size * k:]
                        temp_phi2 = phi2[data_block_size * k:]

                    hist1 = self.binData(temp_phi1, hist_bins,
                                         data_to_be_binned1)
                    hist2 = self.binData(temp_phi2, hist_bins,
                                         data_to_be_binned2)

                    all_hists1 = all_hists1 + hist1
                    all_hists2 = all_hists2 + hist2

                binary_mag = self.getBinaryMag(all_hists1, all_hists2)
                self.binary_mags.append(binary_mag)
            else:
                binary_mag = np.zeros(((4 * 4) * (9), 1), dtype=np.float64)
                self.binary_mags.append(binary_mag)
        return self.getAverageBinary()

    def getAverageBinary(self):
        ovif_video = np.zeros(self.binary_mags[0].shape, dtype=np.float64)
        for each_ovif in self.binary_mags:
            ovif_video = ovif_video + each_ovif

        return ovif_video / self.index

    def writeFeatureToFile(self, filename):
        np.savetxt(filename, self.getOvifFeature(), delimiter=',')