#python #opencv #deep-learning #computer-vision #background-subtraction
#python #opencv #глубокое обучение #компьютерное зрение #фон-вычитание
Вопрос:
Я работаю над проектом для вмешательства в камеру. У меня есть код для обнаружения несанкционированного доступа для статической камеры. Вмешательство означает блокировку или расфокусировку камеры. Я хочу изменить его для движущейся камеры или вращающейся камеры, чтобы он брал все кадры из фона, а затем сравнивал их с новыми кадрами, используя тот же метод вычитания фона.
Я пытался, но не смог понять, как использовать список кадров для сравнения захваченного кадра с другими.
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) amp; 0xff== ord('q'): #To close the camera press q
break
else : break
cap.release()
cv2.destroyAllWindows()
Комментарии:
1. В чем именно ваша проблема и ваш вопрос ?
2. У меня есть код для обнаружения несанкционированного доступа для статической камеры. Вмешательство означает блокировку или расфокусировку камеры. Я хочу изменить его для движущейся камеры или вращающейся камеры, чтобы он брал все кадры из фона, а затем сравнивал их с новыми кадрами, используя тот же метод вычитания фона.
Ответ №1:
На мой взгляд, я не думаю, что вычитание фона является хорошим решением вашей проблемы, ваши методы в основном используются для обнаружения движущегося объекта путем вычитания фона. Этот метод чаще всего используется в статической камере, но для движущейся камеры также может потребоваться учитывать изменение интенсивности или изменение текстуры.