Last active
July 31, 2016 16:41
-
-
Save stanchiang/b4e4890160a054a9c1d65f9152172600 to your computer and use it in GitHub Desktop.
blurring_nonhumans in video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import the necessary packages | |
from __future__ import print_function | |
from imutils.video import VideoStream | |
from imutils.object_detection import non_max_suppression | |
from imutils import paths | |
import numpy as np | |
import argparse | |
import imutils | |
import cv2 | |
# INPUT | |
# take in the video | |
vc = cv2.VideoCapture('clip_personwalking.mp4') | |
# default start with true | |
success = True | |
# OUTPUT | |
# construct the argument parse and parse the arguments | |
ap = argparse.ArgumentParser() | |
ap.add_argument("-o", "--output", required=True, | |
help="path to output video file") | |
ap.add_argument("-f", "--fps", type=int, default=20, | |
help="FPS of output video") | |
ap.add_argument("-c", "--codec", type=str, default="avc1", | |
help="codec of output video") | |
args = vars(ap.parse_args()) | |
# initialize the FourCC, video writer, dimensions of the frame, and | |
fourcc = cv2.VideoWriter_fourcc('a', 'v', 'c', '1') | |
writer = None | |
(h, w) = (None, None) | |
# initialize the HOG descriptor/person detector | |
hog = cv2.HOGDescriptor() | |
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) | |
while success: | |
success, image = vc.read() | |
if success: | |
image = imutils.resize(image, width=1920) | |
# copy a blurred version of the image to a new image | |
blurred = cv2.medianBlur(image, 7).copy() | |
if writer is None: | |
(h, w) = image.shape[:2] | |
writer = cv2.VideoWriter(args["output"], fourcc, args["fps"], | |
(w, h), True) | |
# detect people in the image | |
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), | |
padding=(8, 8), scale=1.05) | |
# apply non-maxima suppression to the bounding boxes using a | |
# fairly large overlap threshold to try to maintain overlapping | |
# boxes that are still people | |
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) | |
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) | |
# our hacky way of masking blurry image with hi res objects | |
# identify important the coordinates of objects we will avoid blurring | |
# grab the the hi-fi verion and place it in the same spot as the blurred version | |
for (xA, yA, xB, yB) in pick: | |
cv2.rectangle(blurred, (xA, yA), (xB, yB), (0, 255, 0), 2) | |
blurred[yA:yB, xA:xB] = image[yA:yB, xA:xB] | |
# construct the final output image | |
h=1080 | |
w=1920 | |
output = np.zeros((h, w, 3), dtype="uint8") | |
output[0:h, 0:w] = blurred | |
# write the output image to file | |
writer.write(output) | |
# if the `q` key was pressed, break from the loop | |
key = cv2.waitKey(1) & 0xFF | |
if key == ord("q"): | |
break | |
writer.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment