docs/notebooks/blurring_faces.ipynb
Click the Open in Colab button to run the cookbook on Google Colab.
In this cookbook we'll use a frame from a video of someone in a supermarket. We'll download this video via the supervision assets module. We'll then run inference on this frame using the hosted Roboflow API to fetch detections of faces utilizing an open source face detection model on Roboflow Universe. Finally, we'll use supervision to blur the detected faces.
Let's quickly install the supervision package with the assets module, as well as the roboflow inference_sdk with pip. We'll also install tqdm to show a progress bar, but this is optional in production code.
!pip3 install -q supervision inference tqdm "Pillow<12"
In order to blur a face in a frame, we'll need a frame with a face in it. Let's download a video, and grab a frame in the middle of the video. I played around a little, and found that the 800th frame is great frame for us to test, since the customer is facing the camera. In this code, we're also using tqdm to display a progress bar of our script.
import supervision as sv
video = sv.download_assets(sv.VideoAssets.GROCERY_STORE)
# Seek directly to frame 800 using the start parameter (O(1) seek)
frame = next(sv.get_video_frames_generator(video, start=800))
sv.plot_image(frame)
Now that we've got our image we'll need a good face detecting model. For this task, there are already an impressive amount of open source models available on Roboflow Universe. After a little digging, this face detection model has over 1300 images. Some models, including this one, require a Roboflow API key. You can create a free account here. From there, you can find the key under Settings > Workspaces > Roboflow API. Let's give it a try.
import os
from inference_sdk import InferenceHTTPClient
try:
from google.colab import userdata
ROBOFLOW_API_KEY = userdata.get("ROBOFLOW_API_KEY") or ""
except ImportError:
ROBOFLOW_API_KEY = os.environ.get("ROBOFLOW_API_KEY", "")
assert ROBOFLOW_API_KEY, "Set ROBOFLOW_API_KEY in Colab secrets or as env var"
client = InferenceHTTPClient(
api_url="https://detect.roboflow.com",
api_key=ROBOFLOW_API_KEY
)
results = client.infer(frame, model_id="face-detection-mik1i/18")
print(f"Detected {len(results['predictions'])} face(s)")
print(results)
Now that we're detecting faces, bluring them is easy with supervision. Let's pass our results into a Detections object and annotate the frame with a BlurAnnotator.
blur = sv.BlurAnnotator(kernel_size=100)
detections = sv.Detections.from_inference(results)
annotated_frame = blur.annotate(scene=frame.copy(), detections=detections)
sv.plot_image(annotated_frame)
With supervision, inference, and Roboflow Universe we were able to blur faces in minutes with an open source model. There are many other impressive use cases out there, so feel free to share in your own cookbooks. Happy building!