opencv通过摄像头行人检测python
时间: 2023-06-14 16:08:00 浏览: 109
要使用OpenCV进行摄像头行人检测,您可以按照以下步骤进行操作:
1. 导入OpenCV库:
```python
import cv2
```
2. 加载已经预训练好的行人检测器模型:
```python
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
```
3. 打开摄像头并进行实时检测:
```python
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# 行人检测
boxes, weights = hog.detectMultiScale(frame, winStride=(8, 8), padding=(16, 16), scale=1.05)
# 绘制检测结果
for box in boxes:
x, y, w, h = box
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.imshow('pedestrian detection', frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
```
在上述代码中,我们使用 `cv2.VideoCapture()` 打开了计算机上的默认摄像头,并使用 `hog.detectMultiScale()` 方法检测行人区域。然后,我们使用 `cv2.rectangle()` 方法绘制了行人检测框,并使用 `cv2.imshow()` 方法将结果显示在窗口中。
请注意,上述代码中的 `winStride`、`padding` 和 `scale` 参数可以根据您的需求进行调整。
阅读全文
相关推荐

















