问题
I'm new to opencv and I'm trying to detect person through cv2.findContours with morphological transformation of the video. Here is the code snippet..
import numpy as np
import imutils
import cv2 as cv
import time
cap = cv.VideoCapture(0)
while(cap.isOpened()):
ret, frame = cap.read()
#frame = imutils.resize(frame, width=700,height=100)
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(gray, (21, 21), 0)
cv.accumulateWeighted(gray, avg, 0.5)
mask2 = cv.absdiff(gray, cv.convertScaleAbs(avg))
mask = cv.absdiff(gray, cv.convertScaleAbs(avg))
contours0, hierarchy = cv.findContours(mask2,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
for cnt in contours0:
.
.
.
The rest of the code has the logic of a contour passing a line and incrementing the count.
The problem I'm encountering is, cv.findContours detects every movement/change in the frame (including the person). What I want is cv.findContours to detect only person and not any other movement. I know that person detection can be achieved through harrcasacade but is there any way I can implement detection using cv2.findContours?
If not then is there a way I can still do morphological transformation and detect people because the project I'm working on requires filtering of noise and much of the background to detect the person and increment it's count on passing the line.
回答1:
I will show you two options to do this.
The method I mentioned in the comments which you can use with Yolo to detect humans:
- Use saliency to detect the standout parts of the video
- Apply K-Means Clustering to cluster the objects into individual clusters.
- Apply Background Subtraction and erosion or dilation (or both depends on the video but try them all and see which one does the best job).
- Crop the objects
- Send the cropped objects to Yolo
- If the class name is a pedestrian or human then draw the bounding boxes on them.
Using OpenCV's builtin pedestrian detection which is much more easier:
- Convert frames to black and white
- Use pedestrian_cascade.detectMultiScale() on the grey frames.
- Draw a bounding box over each pedestrian
Second method is much simpler but it depends what is expected of you for this project.
来源:https://stackoverflow.com/questions/63913571/try-to-implement-cv2-findcontours-for-person-detection