Skip to content

Commit bec9e82

Browse files
committed
行人检测
1 parent ba9bba5 commit bec9e82

8 files changed

Lines changed: 157 additions & 2 deletions

File tree

ch10-图像上的算术运算/长方形2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ def get_euler_distance(pt1, pt2):
3939

4040
cv2.imshow('src', img22)
4141
cv2.imshow('warp', warp)
42-
cv2.imwrite('crop0.jpg',warp)
42+
# cv2.imwrite('crop0.jpg',warp)
4343
cv2.waitKey(0)

ch51-对象检测-使用Haar分类器进行面部检测/行人检测/Pedestrian_Detection1.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,63 @@
66

77
"""
88
Pedestrian_Detection1.py:
9+
10+
http://www.pyimagesearch.com/2015/11/09/pedestrian-detection-opencv/
11+
12+
运行
13+
python Pedestrian_Detection1.py --images images
914
"""
1015

16+
# import the necessary packages
17+
from __future__ import print_function
18+
from imutils.object_detection import non_max_suppression
19+
from imutils import paths
20+
import numpy as np
21+
import argparse
22+
import imutils
23+
import cv2
24+
25+
# construct the argument parse and parse the arguments
26+
ap = argparse.ArgumentParser()
27+
ap.add_argument("-i", "--images", required=True, help="path to images directory")
28+
args = vars(ap.parse_args())
29+
30+
# initialize the HOG descriptor/person detector
31+
hog = cv2.HOGDescriptor()
32+
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
33+
34+
# loop over the image paths
35+
for imagePath in paths.list_images(args["images"]):
36+
# load the image and resize it to (1) reduce detection time
37+
# and (2) improve detection accuracy
38+
image = cv2.imread(imagePath)
39+
image = imutils.resize(image, width=min(400, image.shape[1]))
40+
orig = image.copy()
41+
42+
# detect people in the image
43+
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
44+
padding=(8, 8), scale=1.05)
45+
46+
# draw the original bounding boxes
47+
for (x, y, w, h) in rects:
48+
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
49+
50+
# apply non-maxima suppression to the bounding boxes using a
51+
# fairly large overlap threshold to try to maintain overlapping
52+
# boxes that are still people
53+
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
54+
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
55+
56+
# draw the final bounding boxes
57+
for (xA, yA, xB, yB) in pick:
58+
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
59+
60+
# show some information on the number of bounding boxes
61+
filename = imagePath[imagePath.rfind("/") + 1:]
62+
print("[INFO] {}: {} original boxes, {} after suppression".format(
63+
filename, len(rects), len(pick)))
64+
65+
# show the output images
66+
cv2.imshow("Before NMS", orig)
67+
cv2.imshow("After NMS", image)
68+
cv2.waitKey(0)

ch51-对象检测-使用Haar分类器进行面部检测/行人检测/Pedestrian_Detection_video.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,99 @@
66

77
"""
88
Pedestrian_Detection_video.py:
9+
10+
视频网站
11+
https://v.qq.com/x/page/t0501y6jtfi.html
12+
913
"""
1014

15+
# import the necessary packages
16+
from __future__ import print_function
17+
from imutils.object_detection import non_max_suppression
18+
from imutils import paths
19+
import numpy as np
20+
import argparse
21+
import imutils
22+
import cv2
23+
import time
24+
25+
# construct the argument parse and parse the arguments
26+
# ap = argparse.ArgumentParser()
27+
# ap.add_argument("-i", "--images", required=True, help="path to images directory")
28+
# args = vars(ap.parse_args())
29+
30+
# initialize the HOG descriptor/person detector
31+
hog = cv2.HOGDescriptor()
32+
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
33+
34+
#
35+
cap = cv2.VideoCapture('videos/礼让斑马线!齐齐哈尔城市文明的伤!.mp4')
36+
37+
fps = cap.get(cv2.CAP_PROP_FPS) # 25.0
38+
print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
39+
num_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
40+
print('共有', num_frames, '帧') # 共有 2499.0 帧
41+
42+
frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
43+
frame_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
44+
print('高:', frame_height, '宽:', frame_width) # 高: 480.0 宽: 640.0
45+
# exit(0)
46+
47+
48+
# 跳过多少帧
49+
skips = 20
50+
51+
# loop over the image paths
52+
# for imagePath in paths.list_images(args["images"]):
53+
while cap.isOpened():
54+
55+
# load the image and resize it to (1) reduce detection time
56+
# and (2) improve detection accuracy
57+
# image = cv2.imread(imagePath)
58+
59+
ret, frame = cap.read()
60+
image = frame
61+
62+
#
63+
current = cap.get(cv2.CAP_PROP_POS_FRAMES)
64+
if current % skips != 0:
65+
continue
66+
67+
image = imutils.resize(image, width=min(400, image.shape[1]))
68+
orig = image.copy()
69+
70+
# detect people in the image
71+
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
72+
padding=(8, 8), scale=1.05)
73+
74+
# draw the original bounding boxes
75+
for (x, y, w, h) in rects:
76+
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
77+
78+
# apply non-maxima suppression to the bounding boxes using a
79+
# fairly large overlap threshold to try to maintain overlapping
80+
# boxes that are still people
81+
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
82+
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
83+
84+
# draw the final bounding boxes
85+
for (xA, yA, xB, yB) in pick:
86+
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
87+
88+
# show some information on the number of bounding boxes
89+
# filename = imagePath[imagePath.rfind("/") + 1:]
90+
# print("[INFO] {}: {} original boxes, {} after suppression".format(
91+
print("[INFO] {} original boxes, {} after suppression".format(len(rects), len(pick)))
92+
93+
# show the output images
94+
cv2.imshow("Before NMS", orig)
95+
cv2.imshow("After NMS", image)
96+
cv2.moveWindow("After NMS", y=0, x=400)
97+
98+
key = cv2.waitKey(delay=1)
99+
if key == ord("q"):
100+
break
101+
102+
# When everything done, release the capture
103+
cap.release()
104+
cv2.destroyAllWindows()
431 KB
Loading
271 KB
Loading
232 KB
Loading

官方samples/peopledetect.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22

33
'''
4+
人类检测
5+
python peopledetect.py ../data/basketball1.png ../data/basketball2.png ../data/dnn/rgb.jpg
6+
47
example to detect upright people in images using HOG features
58
69
Usage:
@@ -64,7 +67,7 @@ def draw_detections(img, rects, thickness = 1):
6467
draw_detections(img, found)
6568
draw_detections(img, found_filtered, 3)
6669
print('%d (%d) found' % (len(found_filtered), len(found)))
67-
cv2.imshow('img', img)
70+
cv2.imshow(fn, img)
6871
ch = cv2.waitKey()
6972
if ch == 27:
7073
break

0 commit comments

Comments
 (0)