OpenCV行人检测
时间: 2024-06-13 22:05:49 浏览: 75
以下是使用OpenCV进行行人检测的步骤:
1.导入必要的库和模型文件
```python
import cv2
# 加载HOG+SVM模型
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
```
2.读取图像或视频流
```python
# 读取图像
image = cv2.imread('image.jpg')
# 读取视频流
cap = cv2.VideoCapture('video.mp4')
```
3.对图像或视频流进行行人检测
```python
# 对图像进行行人检测
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05)
# 对视频流进行行人检测
while True:
ret, frame = cap.read()
if not ret:
break
(rects, weights) = hog.detectMultiScale(frame, winStride=(4, 4), padding=(8, 8), scale=1.05)
# 在视频流中绘制检测框
for (x, y, w, h) in rects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
```
4.显示结果
```python
# 在图像中绘制检测框
for (x, y, w, h) in rects:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
# 显示结果
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文
相关推荐
















