Skip to content

Instantly share code, notes, and snippets.

@giljr
Created October 23, 2022 22:41
Show Gist options
  • Save giljr/4bc08bc7e37eb3bb7a6ef9a41ed0eaff to your computer and use it in GitHub Desktop.
Save giljr/4bc08bc7e37eb3bb7a6ef9a41ed0eaff to your computer and use it in GitHub Desktop.
# https://stackoverflow.com/questions/65827830/disabledfunctionerror-cv2-imshow-is-disabled-in-colab-because-it-causes-jupy
from google.colab.patches import cv2_imshow
from PIL import Image
import face_recognition
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("office.jpg")
new_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Find all the faces in the image using the default HOG-based model.
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
# See also: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)
for face_location in face_locations:
# Print the location of each face in this image
top, right, bottom, left = face_location
# You can access the actual face itself like this:
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
face_image = cv2.GaussianBlur(face_image, (99, 99), 30)
new_img[top:bottom, left:right] = face_image
# Display the resulting image
cv2_imshow(new_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment