Home › Fóruns › Fórum Detecção de Movimentos com Python e OpenCV › Erro ao executar o “bg mask” › Responder a: Erro ao executar o “bg mask”
21 de fevereiro de 2022 às 09:27 #33161
Moderador
import numpy as np import cv2 import sys from random import randint TEXT_COLOR = (randint(0, 255), randint(0,255), randint (0,255)) BORDER_COLOR = (randint(0, 255), randint(0,255), randint (0,255)) FONT = cv2.FONT_HERSHEY_SIMPLEX # ////////////// # VIDEO_SOURCE =”Video/Traffic_4.mp4″ # Correção: VIDEO_SOURCE = "videos/Traffic_4.mp4" #print (TEXT_COLOR) # ///////////////// # BGS_TYPES = ('GMG', 'MOG', 'MOG2', 'KNN', 'CNT') # Correção: # Neste vetor de BGS deve ser em aspas duplas e não simples, # também estava entre parenteses e o correto é entre colchetes BGS_TYPES = ["GMG", "MOG", "MOG2", "KNN", "CNT"] #print(BGS_TYPES[1]) def getKernel(KERNEL_TYPE): if KERNEL_TYPE == "dilation": kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) if KERNEL_TYPE == "opening": kernel = np.ones((3, 3), np.uint8) if KERNEL_TYPE == "closing": kernel = np.ones((3, 3), np.uint8) return kernel #print(getKernel('opening')) def getFiltrer(img, filter): if filter == 'closing': return cv2.morphologyEx(img, cv2.MORPH_CLOSE, getKernel("closing"), iterations=2) if filter == 'opening': return cv2.morphologyEx(img, cv2.MORPH_OPEN, getKernel("opening"), iterations=2) if filter == 'dilation': return cv2.dilate(img, getKernel("dilation"), iterations=2) if filter == 'combine': closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, getKernel("closing"), iterations=2) opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, getKernel("opening"), iterations=2) dilation = cv2.dilate(opening, getKernel("dilation"), iterations=2) return dilation def getBGSubtractor(BGS_TYPE): # Correção: # deve ser em aspas duplas e não simples, # if BGS_TYPE == 'GMG': if BGS_TYPE == "GMG": return cv2.bgsegm.createBackgroundSubtractorGMG(initializationFrames = 120, decisionThreshold=0.8) # Correção: # deve ser em aspas duplas e não simples, # if BGS_TYPE == 'MOG': if BGS_TYPE == "MOG": return cv2.bgsegm.createBackgroundSubtractorMOG(history=200, nmixtures=5, backgroundRatio=0.7, noiseSigma=0) # Correção: # deve ser em aspas duplas e não simples, # if BGS_TYPE == 'MOG2': if BGS_TYPE == "MOG2": return cv2.createBackgroundSubtractorMOG2(history=500, detectShadows=True, varThreshold=100) # Correção: # deve ser em aspas duplas e não simples, # if BGS_TYPE == 'KNN': if BGS_TYPE == "KNN": return cv2.createBackgroundSubtractorKNN(history=500, dist2Threshold=400, detectShadows=True) # Correção: # deve ser em aspas duplas e não simples, # if BGS_TYPE == 'CNT': if BGS_TYPE == "CNT": # Correção: # Precisa incluir o bgsem para acessar ao BG CNT # return cv2.createBackgroundSubtractorCNT(minPixelStability=15, useHistory=True, maxPixelStability=15*60, isParallel=True) return cv2.bgsegm.createBackgroundSubtractorCNT(minPixelStability=15, useHistory=True, maxPixelStability=15*60, isParallel=True) print("Detector inválido") sys.exit(1) cap = cv2.VideoCapture(VIDEO_SOURCE) bg_subtractor = getBGSubtractor(BGS_TYPES[4]) def main(): while cap.isOpened(): ok, frame = cap.read() #print('frame.shape') if not ok: print('Erro') break frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5) #print(frame.shape) bg_mask = bg_subtractor.apply(frame) fg_mask = getFiltrer(bg_mask, 'dilation') cv2.imshow('Frame', frame) cv2.imshow('BG mask', bg_mask) cv2.imshow('BG mask filtrer ', fg_mask) if cv2.waitKey(1) & 0xFF == ord('q'): break main()