-1

I am working on a project for camera tampering. I have the code for tamper detection for a static camera. Tampering means blocking or defocussing the camera. I want to modify it for a moving camera or rotating camera so that it takes all the frames from the background and then compare it with new frames using the same background subtractor method.

I have tried but unable to figure out how to use the list of frames to compare the captured frame from other ones.

import numpy as np
import cv2
from playsound import playsound
import time

#cap = cv2.VideoCapture('http://192.168.43.1:8080/video')  #Opening of IP camera just enter the ip address
cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2()    #generating a foreground mask

frame_list = [] #creating the list of frame
##################################################################
while(True):
    ret, frame = cap.read()
    if ret == True:
        if frame not in frame_list:
            frame_list.append(frame)  #This list contain all the frames
###################################################################    
#ret, frame = cap.read()        #to get the initial frame 
#fgmask = fgbg.apply(frame)     #to save the initial frame
kernel = np.ones((5,5), np.uint8)  #creating a matrix of (5, 5) consisting of 1
while(True):
    ret, frame = cap.read()    #reading all the frames
    if ret == True:
        
        a = 0
        bounding_rect = []  # An empty list where will furthur input the contours
        
        fgmask = fgbg.apply(frame) #Applying the changes of the backgroud to the foreground mask
        fgmask= cv2.erode(fgmask, kernel, iterations=5) 
        fgmask = cv2.dilate(fgmask, kernel, iterations = 5)    #Erosion and Dilation is done to detect even the blur objects better
        
        cv2.imshow('frame',frame)  #Showing the frame.
        contours,_ = cv2.findContours(fgmask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #The mode RETR_TREE together with the CHAIN APPROX_SIMPLE returns only the endpoints required to draw contour
        
        for i in range(0,len(contours)):
            bounding_rect.append(cv2.boundingRect(contours[i]))  #cv2.bounding rectangle gives the coordinates of bounding rectangle and then we will input these coordinates to the list we made
        for i in range(0,len(contours)):
            if bounding_rect[i][2] >=40 or bounding_rect[i][3] >= 40: #setting the threshold for the width and height if the contour 
                a = a+(bounding_rect[i][2])*bounding_rect[i][3]     #updating the area of contour
            if(a >=int(frame.shape[0])*int(frame.shape[1])/3):    #It is basically the comparison of the change in area of the background, so if there is a certain change in area it will detect the tampering
                cv2.putText(frame,"TAMPERING DETECTED",(5,30),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,255),2)   
                #playsound('warning.mp3') #put the address of the warning tune in the playsound object and uncomment it 
            cv2.imshow('frame',frame)    #showing the final frame     
               
        if cv2.waitKey(30) & 0xff== ord('q'): #To close the camera press q
            break
        
    else : break
    
cap.release()
cv2.destroyAllWindows()
  • What *exactly* is your issue and your *question*? – desertnaut Sep 19 '20 at 22:52
  • I have the code for tamper detection for a static camera. Tampering means blocking or defocussing the camera. I want to modify it for a moving camera or rotating camera so that it takes all the frames from the background and then compare it with new frames using the same background subtractor method. – Aaditya khandelwal Sep 20 '20 at 02:20

1 Answers1

0

In my opinion, I don't think background subtraction is a good solution to your problem, your methods is mainly used to detect moving object by subtracting background. This method most often used in static camera but for moving camera, intensity variation or texture change may also need to take account.