try to implement cv2.findContours for person detection

假装没事ソ 提交于 2021-01-29 07:32:49

问题


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:

  1. Use saliency to detect the standout parts of the video
  2. Apply K-Means Clustering to cluster the objects into individual clusters.
  3. 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).
  4. Crop the objects
  5. Send the cropped objects to Yolo
  6. 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:

  1. Convert frames to black and white
  2. Use pedestrian_cascade.detectMultiScale() on the grey frames.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!