When you find yourself deploying laptop imaginative and prescient options in areas the place individuals are current, you might choose to blur folks to keep up the privateness of individuals in a picture or video. To take action, you should utilize an individual detection API to detect folks, after which use the supervision
Python package deal to blur all areas the place individuals are current. This may be carried out in a number of strains of code.
On this information, we’re going to stroll by way of how one can blue folks in photographs and movies. The method we present for photographs and movies might be barely completely different. To blur folks in photographs, we’ll use the Roboflow Hosted API and supervision. To blur folks in movies, we’ll use the Roboflow Video Inference API, which is purpose-built for video processing, and supervision.
Right here is an instance of a video by which folks have been blurred:
With out additional ado, let’s get began!
Blur Folks API: Photographs
To blur folks in a picture, we have to know the pixel coordinates that correspond with every particular person. For that, we will use an individual detection laptop imaginative and prescient mannequin. On this information, we’re going to use the Folks Detection mannequin hosted on Roboflow Universe. This mannequin can be utilized out of the field, with none coaching required.
We are able to question this mannequin utilizing a hosted API.
Let’s blur all the folks on this picture:
First, we have to set up the Roboflow and supervision Python packages, which we’ll use to name the folks detection API and to blur folks, respectively:
pip set up roboflow supervision
Subsequent, create a brand new Python file and add the next code:
from roboflow import Roboflow
import supervision as sv
import cv2 rf = Roboflow(api_key="API_KEY")
mission = rf.workspace().mission("people-detection-o4rdr")
mannequin = mission.model(7).mannequin outcomes = mannequin.predict("folks.jpg", confidence=40, overlap=30).json() predictions = sv.Detections.from_roboflow(outcomes) picture = cv2.imread("folks.jpg") # filter out predictions that are not class id == 0, which maps to the category "particular person"
folks = predictions[predictions.class_id == 0] blur_annotator = sv.BlurAnnotator() annotated_frame = blur_annotator.annotate(
scene=picture.copy(),
detections=folks
) sv.plot_image(annotated_frame)
Above, exchange API_KEY
together with your Roboflow API key. Discover ways to retrieve your Roboflow API key. Then, run the code. The picture you might be processing will seem on display, with all folks blurred:
We efficiently blurred the folks within the picture.
Blur Folks API: Movies
To blur folks in movies, we will use the hosted Roboflow Video Inference API. This API is optimized for working inference on movies. We are able to ship a video to the API and specify that we wish to use a pre-trained folks detection mannequin. Then, we will retrieve predictions for every body. We are able to use these predictions to blur folks in a video.
Create a brand new Python file and add the next code:
import json import os
from roboflow import Roboflow
import numpy as np
import supervision as sv
import cv2 PROJECT_NAME = "people-detection-o4rdr"
VIDEO_FILE = "folks.mp4" rf = Roboflow(api_key="API_KEY")
mission = rf.workspace().mission(PROJECT_NAME)
mannequin = mission.model(7).mannequin job_id, signed_url, expire_time = mannequin.predict_video(
VIDEO_FILE,
fps=5,
prediction_type="batch-video",
) outcomes = mannequin.poll_until_video_results(job_id) with open("outcomes.json", "w") as f:
json.dump(outcomes, f) frame_offset = outcomes["frame_offset"]
model_results = outcomes["people-detection-o4rdr"] def callback(scene: np.ndarray, index: int) -> np.ndarray: if index in frame_offset: detections = sv.Detections.from_roboflow(model_results[frame_offset.index(index)]) else: nearest = min(frame_offset, key=lambda x: abs(x - index)) detections = sv.Detections.from_roboflow( model_results[frame_offset.index(nearest)]) blur_annotator = sv.BlurAnnotator() annotated_image = blur_annotator.annotate( scene=scene, detections=detections) return annotated_image sv.process_video( source_path=VIDEO_FILE, target_path="output.mp4", callback=callback,
)
Above, exchange API_KEY
together with your Roboflow API key. Discover ways to retrieve your Roboflow API key. We advocate setting fps
to five as a place to begin. Which means inference might be run 5 occasions each second. The upper the FPS, the dearer inference might be.
Once you run the code above, your video might be despatched for processing. The period of time it takes for inference to run in your video relies on what number of frames are in your video. The “poll_until_video_results” perform will ballot the video API each 60 seconds till a result’s obtainable. We are going to then save that end result to a file.
You will notice the next output as processing begins:
loading Roboflow workspace...
loading Roboflow mission...
Checking for video inference outcomes for job 15be7ab5-232a-4e02-aa47-864ff8a2581b each 60s
The video inference API returns the pixel coordinates of individuals in every body. We then use the supervision
Python package deal to blur the areas the place individuals are current.
When the script above has completed working, the outcomes might be saved to a file known as “output.mp4”. Right here is an instance of the output of the video blur code above:
We efficiently blurred folks within the video.
Conclusion
When you find yourself recording video that comprises folks and individuals are not related to your laptop imaginative and prescient mission, blurring areas the place individuals are current is advisable. Blurring folks lets you protect the privateness of individuals in body.
On this information, we walked by way of how one can blur folks in photographs and movies utilizing the Roboflow picture and video processing APIs, and the supervision Python package deal. We used the Individual Detection mannequin hosted on Roboflow Universe to detect folks in photographs and movies, then supervision to blur areas the place folks had been current within the visible media.