Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added aditional arguments to auto_blur_image and video #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
*.pyc
*.pyc
.DS_Store
25 changes: 22 additions & 3 deletions src/auto_blur_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
37 changes: 29 additions & 8 deletions src/auto_blur_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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:
Expand All @@ -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',
Expand Down Expand Up @@ -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
Expand Down