본문 바로가기
Python/OpenCV

[OpenCV] HOG(Histogram of Oriented Gradients) 영상 테스트

by hotelshoe 2022. 3. 3.
반응형

이전 포스팅에서 다루었던 HOG 알고리즘을 영상에 적용시켜 테스트를 진행해 보겠습니다. 마찬가지로 자세한 파라미터나 알고리즘에 대한 설명은 하단의 링크를 참조하면 되겠습니다.

https://docs.opencv.org/4.x/d5/d33/structcv_1_1HOGDescriptor.html

 

OpenCV: cv::HOGDescriptor Struct Reference

Implementation of HOG (Histogram of Oriented Gradients) descriptor and object detector. More... #include  HOGDescriptor ()  Creates the HOG descriptor and detector with default parameters. More...    HOGDescriptor (Size _winSize, Size _blockSize, Size

docs.opencv.org


소스 코드

import cv2

vedio_path = '/vedio.mp4' #-- 본인 환경에 맞게 변경할 것

#-- HOG 알고리즘 불러오기
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

#-- 비디오 or 캠 불러오기
cap = cv2.VideoCapture(vedio_path) #-- 캠 사용 시 vedio_path 대신 0 사용

while True:
    ret, frame = cap.read()

    if not ret:
        break

    #-- 객체 검출
    detected, _ = hog.detectMultiScale(frame)
    
    #-- 검출 객체 박스 표시
    for (x, y, w, h) in detected:
        cv2.rectangle(frame, (x, y, w, h), (0, 255, 0), 3)
        cv2.putText(frame, 'Person',(x, y - 5), cv2.FONT_HERSHEY_DUPLEX, 1, (0,255,0), 1)

    cv2.imshow('HOG_vedio_test', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

테스트

출력 예시

 

출력 예시

이미지에서 진행했던 테스트와 마찬가지로 만족할 만큼의 성능을 보여주지는 못한다.

반응형