[英]python opencv people detection
我正在嘗試使用opencv和python構建人員檢測功能,但是我需要一些幫助來理解一些事情。 圖像來自粘貼在窗戶上的手機。 這是代碼:
# USAGE
# python detect.py --images images
from __future__ import print_function
from glob import glob
from imutils.object_detection import non_max_suppression
import argparse
import cv2
import numpy as np
import os
# parse arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())
# strip last / if present
arg_images = args["images"].rstrip("/")
# check folder exists
if not os.path.isdir(arg_images):
print(arg_images + " is not a folder, terminate")
quit()
# load images names
imagePaths = sorted(glob(arg_images + "/*.jpg"))
# check folder is not empty
if len(imagePaths) == 0:
print(arg_images + " is empty, terminate")
quit()
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
for imagePath in imagePaths:
# INTER_NEAREST - a nearest-neighbor interpolation
# INTER_LINEAR - a bilinear interpolation (used by default)
# INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
# INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
# INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
image = cv2.imread(imagePath)
image = cv2.resize(image, (min(800, image.shape[1]), min(600, image.shape[0])), interpolation = cv2.INTER_LINEAR)
#image = cv2.resize(image, (min(1200, image.shape[1]), min(900, image.shape[0])), interpolation = cv2.INTER_LINEAR)
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(0, 0), scale=1.01)
foldername = imagePath[0:imagePath.rfind("/")]
filename = imagePath[imagePath.rfind("/") + 1:]
if len(rects) == 0:
print("- " + filename)
else:
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
for (xA, yA, xB, yB) in pick:
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
print("+ " + filename)
cv2.imwrite(foldername + "/detected/" + filename, image)
當我在這些圖像( test.zip )上運行此功能時,根據圖像大小,我會得到非常不同的結果:
我可以假設這是因為detectMultiScale可以在較小的檢測窗口中工作並且可以更改步幅,但是不能更改大小嗎?
另外,如果您查看IMG_20180329_061603.jpg,則始終會檢測到誤報,但我不明白為什么。 夜景圖片(對我而言)看起來都一樣,但是有太多的綠框。
歡迎對此提供任何幫助。 如果您需要澄清,請詢問..謝謝
您看到的原因是用於人員檢測的HOG描述符具有64x128的窗口。 因此,您想優化scale
參數和圖像尺寸,以獲得大部分真實的正片。 您可能主要是關心一個人靠近自行車,所以遠(又小)的人並不重要。 我將收集一組“訓練”圖像以找到最佳參數值。 同樣,您也忽略了detectMultiScale()
返回的權重,同樣可以直觀地看到真陽性和假陽性的值,並找到最佳閾值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.