|
6 | 6 |
|
7 | 7 | """ |
8 | 8 | Pedestrian_Detection_video.py: |
| 9 | +
|
| 10 | +视频网站 |
| 11 | +https://v.qq.com/x/page/t0501y6jtfi.html |
| 12 | +
|
9 | 13 | """ |
10 | 14 |
|
| 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() |
0 commit comments