Skip to content

Instantly share code, notes, and snippets.

@Thimira
Created May 8, 2020 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Thimira/c5491022ee7e64b6752584b98a6f6b7f to your computer and use it in GitHub Desktop.
Save Thimira/c5491022ee7e64b6752584b98a6f6b7f to your computer and use it in GitHub Desktop.
# Blur Your Face Automatically with OpenCV and Dlib
# See tutorial at https://www.youtube.com/watch?v=QKggnWdCTNY
import numpy as np
import cv2
import dlib
video_capture = cv2.VideoCapture(1)
detector = dlib.get_frontal_face_detector()
blurred = False
framed = False
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
if (ret):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
for rect in rects:
x = rect.left()
y = rect.top()
x1 = rect.right()
y1 = rect.bottom()
if blurred:
frame[y:y1, x:x1] = cv2.blur(frame[y:y1, x:x1], (25, 25))
if framed:
cv2.rectangle(frame, (x, y), (x1, y1), (0, 0, 255), 2)
# Display the resulting frame
cv2.imshow('Video Feed', frame)
ch = 0xFF & cv2.waitKey(1)
# press "b" to toggle blurring.
if ch == ord("b"):
blurred = not blurred
# press "f" to toggle the frame.
if ch == ord("f"):
framed = not framed
# press "q" to quit the program.
if ch == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment