├── README.md └── camera_tampering_detection.py /README.md: -------------------------------------------------------------------------------- 1 | # Camera Tampering Detection using Python 2 | Camera Tampering Detection using Python 3 | 4 | Modify the path to video filename in the camera_tampering_detection.py file 5 | 6 | ## Then run camera_tampering_detection.py using: 7 | python camera_tampering_detection.py 8 | -------------------------------------------------------------------------------- /camera_tampering_detection.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | # In[5]: 5 | 6 | 7 | import numpy as np 8 | import cv2 9 | cap = cv2.VideoCapture('path to the video file') 10 | fgbg = cv2.createBackgroundSubtractorMOG2() 11 | ret, frame = cap.read() 12 | fgmask = fgbg.apply(frame) 13 | kernel = np.ones((5,5), np.uint8) 14 | while(True): 15 | ret, frame = cap.read() 16 | if(frame is None): 17 | print("End of frame") 18 | break; 19 | else: 20 | a = 0 21 | bounding_rect = [] 22 | fgmask = fgbg.apply(frame) 23 | fgmask= cv2.erode(fgmask, kernel, iterations=5) 24 | fgmask = cv2.dilate(fgmask, kernel, iterations = 5) 25 | cv2.imshow('frame',frame) 26 | _,contours,_ = cv2.findContours(fgmask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 27 | for i in range(0,len(contours)): 28 | bounding_rect.append(cv2.boundingRect(contours[i])) 29 | for i in range(0,len(contours)): 30 | if bounding_rect[i][2] >=40 or bounding_rect[i][3] >= 40: 31 | a = a+(bounding_rect[i][2])*bounding_rect[i][3] 32 | if(a >=int(frame.shape[0])*int(frame.shape[1])/3): 33 | cv2.putText(frame,"TAMPERING DETECTED",(5,30),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,255),2) 34 | cv2.imshow('frame',frame) 35 | 36 | k = cv2.waitKey(30) & 0xff 37 | if k == 27: 38 | break 39 | cap.release() 40 | cv2.destroyAllWindows() 41 | 42 | --------------------------------------------------------------------------------