diff --git a/.gitignore b/.gitignore index 40a6d8d..13225f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode -*.pyc \ No newline at end of file +*.pyc +.DS_Store \ No newline at end of file diff --git a/src/auto_blur_image.py b/src/auto_blur_image.py index 89c2d08..9e110f3 100644 --- a/src/auto_blur_image.py +++ b/src/auto_blur_image.py @@ -6,7 +6,7 @@ from DetectorAPI import Detector -def blurBoxes(image, boxes): +def blurBoxes(image, boxes, blur_strength, extend_selection): """ Argument: image -- the image that will be edited as a matrix @@ -21,11 +21,18 @@ def blurBoxes(image, boxes): x1, y1 = box["x1"], box["y1"] x2, y2 = box["x2"], box["y2"] + height, width, _ = image.shape + + x1 = max(0, x1 - extend_selection) + x2 = min(width, x2 + extend_selection) + y1 = max(0, y1 - extend_selection) + y2 = min(height, y2 + extend_selection) + # crop the image due to the current box sub = image[y1:y2, x1:x2] # apply GaussianBlur on cropped area - blur = cv2.blur(sub, (25, 25)) + blur = cv2.blur(sub, (blur_strength, blur_strength)) # paste blurred image on the original image image[y1:y2, x1:x2] = blur @@ -37,6 +44,8 @@ def main(args): # assign model path and threshold model_path = args.model_path threshold = args.threshold + blur_strength = args.blur_strength + extend_selection = args.extend_selection # create detection object detector = Detector(model_path=model_path, name="detection") @@ -48,7 +57,7 @@ def main(args): faces = detector.detect_objects(image, threshold=threshold) # apply blurring - image = blurBoxes(image, faces) + image = blurBoxes(image, faces, blur_strength, extend_selection) # show image cv2.imshow('blurred', image) @@ -89,6 +98,16 @@ def main(args): help='Face detection confidence', default=0.7, type=float) + parser.add_argument('-s', + '--blur_strength', + help='Blur strength, default 25', + default=25, + type=int) + parser.add_argument('-e', + '--extend_selection', + help='Extend the selected area by x amount of pixels', + default=0, + type=int) args = parser.parse_args() print(args) # if input image path is invalid then stop diff --git a/src/auto_blur_video.py b/src/auto_blur_video.py index 7cab8d5..1fed64f 100644 --- a/src/auto_blur_video.py +++ b/src/auto_blur_video.py @@ -6,7 +6,7 @@ from DetectorAPI import Detector -def blurBoxes(image, boxes): +def blurBoxes(image, boxes, blur_strength, extend_selection): """ Argument: image -- the image that will be edited as a matrix @@ -21,11 +21,18 @@ def blurBoxes(image, boxes): x1, y1 = box["x1"], box["y1"] x2, y2 = box["x2"], box["y2"] + height, width, _ = image.shape + + x1 = max(0, x1 - extend_selection) + x2 = min(width, x2 + extend_selection) + y1 = max(0, y1 - extend_selection) + y2 = min(height, y2 + extend_selection) + # crop the image due to the current box sub = image[y1:y2, x1:x2] # apply GaussianBlur on cropped area - blur = cv2.blur(sub, (25, 25)) + blur = cv2.blur(sub, (blur_strength, blur_strength)) # paste blurred image on the original image image[y1:y2, x1:x2] = blur @@ -37,6 +44,8 @@ def main(args): # assign model path and threshold model_path = args.model_path threshold = args.threshold + blur_strength = args.blur_strength + extend_selection = args.extend_selection # create detection object detector = Detector(model_path=model_path, name="detection") @@ -46,12 +55,14 @@ def main(args): # video width = capture.get(3) # video height = capture.get(4) - # video fps = capture.get(5) + fps = capture.get(5) if args.output_video: - fourcc = cv2.VideoWriter_fourcc(*'mp4v') - output = cv2.VideoWriter(args.output_video, fourcc, - 20.0, (int(capture.get(3)), int(capture.get(4)))) + fourcc = cv2.VideoWriter_fourcc(*"mp4v") + output = cv2.VideoWriter( + args.output_video, fourcc, fps, (int( + capture.get(3)), int(capture.get(4))) + ) frame_counter = 0 while True: @@ -70,12 +81,12 @@ def main(args): faces = detector.detect_objects(frame, threshold=threshold) # apply blurring - frame = blurBoxes(frame, faces) + frame = blurBoxes(frame, faces, blur_strength, extend_selection) # show image cv2.imshow('blurred', frame) - # if image will be saved then save it + # if image will be saved then save it if args.output_video: output.write(frame) print('Blurred video has been saved successfully at', @@ -110,6 +121,16 @@ def main(args): help='Face detection confidence', default=0.7, type=float) + parser.add_argument('-s', + '--blur_strength', + help='Blur strength, default 25', + default=25, + type=int) + parser.add_argument('-e', + '--extend_selection', + help='Extend the selected area by x amount of pixels', + default=0, + type=int) args = parser.parse_args() # if input image path is invalid then stop