# libraries that will be needed
import numpy as np # numpy
import cv2 # opencv
import imutils # allows video editing
import random
from imutils.object_detection import non_max_suppression
from imutils import paths
import imutils
import cv2
#default HOG
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# function to trak people
def tracker(cap):
while True:
ret, img = cap.read()
# if video stopped playing, quit
if ret == False:
break
# resize window
img = imutils.resize(img, width = 400)
# convert to graysclae and equalize
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
# detect people
rects, weights = hog.detectMultiScale(gray, winStride = (8, 8), padding = (8, 8), scale = 1.25)
# store detected people in array
rects = np.array([[x, y, x+w, y+h] for(x, y, w, h) in rects])
# find largest possible rectangel to avoid detection
# of same person several times
biggest = non_max_suppression(rects, probs = None, overlapThresh = 0.65)
# draw largest rectangle
for (xA, yA, xB, yB) in biggest:
# create random color
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
cv2.rectangle(img, (xA, yA), (xB, yB), color, 2)
# show image
cv2.imshow('Image', img)
k = cv2.waitKey(30) & 0xFF
if k == 27:
break
# run video
cap = cv2.VideoCapture('NYC.mp4')
tracker(cap)
# release frame and destroy windows
cap.release()
cv2.destroyAllWindows
OpenCV를 사용하여 여러 사람을 한 번에 추적하려고합니다. 사람이 감지되면 그 주위에 직사각형을 그립니다. 나는 사람이 발견 된 후 동일한 색상 상자를 유지하면서 각 사람마다 무작위/다른 색상 상자를 갖는 데 어려움을 겪고 있습니다.OpenCV (Python) 비디오에서 그림 그리기
현재 사람이 감지되고 상자가 그려집니다. 다음 프레임에서는 여전히 색이있는 상자가 그려지지만 원래 색을 유지하려고합니다.
내 코드와 추적 기능을 향상시키는 팁/트릭을 열어 본다.
나는 hog.detectMultiScale에서 반환 "의 구형"값에 따라이 작업을 수행 할 수 있을까? 또한, 임의의 색상의 위치를 변경하면 유익합니까? – abashaw80