|
| 1 | +# author: Asmaa Mirkhan ~ 2019 |
| 2 | + |
| 3 | +import os |
| 4 | +import argparse |
| 5 | +import cv2 as cv |
| 6 | +from DetectorAPI import DetectorAPI |
| 7 | + |
| 8 | +def blurBoxes(image, boxes): |
| 9 | + """ |
| 10 | + Argument: |
| 11 | + image -- the image that will be edited as a matrix |
| 12 | + boxes -- list of boxes that will be blurred, each box must be int the format (x_top_left, y_top_left, x_bottom_right, y_bottom_right) |
| 13 | +
|
| 14 | + Returns: |
| 15 | + image -- the blurred image as a matrix |
| 16 | + """ |
| 17 | + |
| 18 | + for box in boxes: |
| 19 | + # unpack each box |
| 20 | + x1, y1, x2, y2 = [d for d in box] |
| 21 | + |
| 22 | + # crop the image due to the current box |
| 23 | + sub = image[y1:y2, x1:x2] |
| 24 | + |
| 25 | + # apply GaussianBlur on cropped area |
| 26 | + blur = cv.blur(sub, (25, 25)) |
| 27 | + |
| 28 | + # paste blurred image on the original image |
| 29 | + image[y1:y2, x1:x2] = blur |
| 30 | + |
| 31 | + return image |
| 32 | + |
| 33 | + |
| 34 | +def main(args): |
| 35 | + # assign model path and threshold |
| 36 | + model_path = args.model_path |
| 37 | + threshold = args.threshold |
| 38 | + |
| 39 | + # create detection object |
| 40 | + odapi = DetectorAPI(path_to_ckpt=model_path) |
| 41 | + |
| 42 | + # open video |
| 43 | + capture = cv.VideoCapture(args.input_video) |
| 44 | + |
| 45 | + # video width = capture.get(3) |
| 46 | + # video height = capture.get(4) |
| 47 | + # video fps = capture.get(5) |
| 48 | + |
| 49 | + if args.output_video: |
| 50 | + fourcc = cv.VideoWriter_fourcc(*'mp4v') |
| 51 | + output = cv.VideoWriter(args.output_video, fourcc, |
| 52 | + 20.0, (int(capture.get(3)), int(capture.get(4)))) |
| 53 | + |
| 54 | + frame_counter = 0 |
| 55 | + while True: |
| 56 | + # read frame by frame |
| 57 | + r, frame = capture.read() |
| 58 | + frame_counter += 1 |
| 59 | + |
| 60 | + # the end of the video? |
| 61 | + if frame is None: |
| 62 | + break |
| 63 | + |
| 64 | + key = cv.waitKey(1) |
| 65 | + if key & 0xFF == ord('q'): |
| 66 | + break |
| 67 | + # real face detection |
| 68 | + boxes, scores, classes, num = odapi.processFrame(frame) |
| 69 | + |
| 70 | + # filter boxes due to threshold |
| 71 | + # boxes are in (x_top_left, y_top_left, x_bottom_right, y_bottom_right) format |
| 72 | + boxes = [boxes[i] for i in range(0, num) if scores[i] > threshold] |
| 73 | + |
| 74 | + # apply blurring |
| 75 | + frame = blurBoxes(frame, boxes) |
| 76 | + |
| 77 | + # show image |
| 78 | + cv.imshow('blurred', frame) |
| 79 | + |
| 80 | + # if image will be saved then save it |
| 81 | + if args.output_video: |
| 82 | + output.write(frame) |
| 83 | + print('Blurred video has been saved successfully at', |
| 84 | + args.output_video, 'path') |
| 85 | + |
| 86 | + # when any key has been pressed then close window and stop the program |
| 87 | + |
| 88 | + cv.destroyAllWindows() |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + # creating argument parser |
| 93 | + parser = argparse.ArgumentParser(description='Image blurring parameters') |
| 94 | + |
| 95 | + # adding arguments |
| 96 | + parser.add_argument('-i', |
| 97 | + '--input_video', |
| 98 | + help='Path to your video', |
| 99 | + type=str, |
| 100 | + required=True) |
| 101 | + parser.add_argument('-m', |
| 102 | + '--model_path', |
| 103 | + help='Path to .pb model', |
| 104 | + type=str, |
| 105 | + required=True) |
| 106 | + parser.add_argument('-o', |
| 107 | + '--output_video', |
| 108 | + help='Output file path', |
| 109 | + type=str) |
| 110 | + parser.add_argument('-t', |
| 111 | + '--threshold', |
| 112 | + help='Face detection confidence', |
| 113 | + default=0.7, |
| 114 | + type=float) |
| 115 | + args = parser.parse_args() |
| 116 | + print(args) |
| 117 | + # if input image path is invalid then stop |
| 118 | + assert os.path.isfile(args.input_video), 'Invalid input file' |
| 119 | + |
| 120 | + # if output directory is invalid then stop |
| 121 | + if args.output_video: |
| 122 | + assert os.path.isdir(os.path.dirname( |
| 123 | + args.output_video)), 'No such directory' |
| 124 | + |
| 125 | + main(args) |
0 commit comments