Ai
1 Star 0 Fork 1

Leo/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
detect_people_hog.py 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
import cv2
OPENCV_MAJOR_VERSION = int(cv2.__version__.split('.')[0])
OPENCV_MINOR_VERSION = int(cv2.__version__.split('.')[1])
def is_inside(i, o):
ix, iy, iw, ih = i
ox, oy, ow, oh = o
return ix > ox and ix + iw < ox + ow and \
iy > oy and iy + ih < oy + oh
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
img = cv2.imread('../images/haying.jpg')
if OPENCV_MAJOR_VERSION >= 5 or \
(OPENCV_MAJOR_VERSION == 4 and OPENCV_MINOR_VERSION >= 6):
# OpenCV 4.6 or a later version is being used.
found_rects, found_weights = hog.detectMultiScale(
img, winStride=(4, 4), scale=1.02, groupThreshold=1.9)
else:
# OpenCV 4.5 or an earlier version is being used.
# The groupThreshold parameter used to be named finalThreshold.
found_rects, found_weights = hog.detectMultiScale(
img, winStride=(4, 4), scale=1.02, finalThreshold=1.9)
found_rects_filtered = []
found_weights_filtered = []
for ri, r in enumerate(found_rects):
for qi, q in enumerate(found_rects):
if ri != qi and is_inside(r, q):
break
else:
found_rects_filtered.append(r)
found_weights_filtered.append(found_weights[ri])
for ri, r in enumerate(found_rects_filtered):
x, y, w, h = r
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)
text = '%.2f' % found_weights_filtered[ri]
cv2.putText(img, text, (x, y - 20),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
cv2.imshow('Women in Hayfield Detected', img)
cv2.imwrite('./women_in_hayfield_detected.png', img)
cv2.waitKey(0)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/akwkevin/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition.git
git@gitee.com:akwkevin/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition.git
akwkevin
Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition
Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition
master

搜索帮助