跳至内容

使用Ultralytics YOLOv8 🚀 进行物体模糊处理

什么是物体模糊?

物体模糊 Ultralytics YOLOv8是指对图像或视频中的特定检测对象应用模糊效果。这可以利用YOLOv8 模型的功能来识别和处理给定场景中的物体。

物体模糊的优势?

  • 隐私保护:物体模糊功能可以隐藏图像或视频中的敏感信息或个人身份信息,是保护隐私的有效工具。
  • 选择性聚焦:YOLOv8 允许选择性模糊,使用户能够锁定特定对象,确保隐私和保留相关视觉信息之间的平衡。
  • 实时处理:YOLOv8它的高效性可实现实时对象模糊,因此适用于需要在动态环境中即时增强隐私的应用。

使用YOLOv8 对物体进行模糊处理示例

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors
import cv2

model = YOLO("yolov8n.pt")
names = model.names

cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Blur ratio
blur_ratio = 50

# Video writer
video_writer = cv2.VideoWriter("object_blurring_output.avi",
                               cv2.VideoWriter_fourcc(*'mp4v'),
                               fps, (w, h))

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break

    results = model.predict(im0, show=False)
    boxes = results[0].boxes.xyxy.cpu().tolist()
    clss = results[0].boxes.cls.cpu().tolist()
    annotator = Annotator(im0, line_width=2, example=names)

    if boxes is not None:
        for box, cls in zip(boxes, clss):
            annotator.box_label(box, color=colors(int(cls), True), label=names[int(cls)])

            obj = im0[int(box[1]):int(box[3]), int(box[0]):int(box[2])]
            blur_obj = cv2.blur(obj, (blur_ratio, blur_ratio))

            im0[int(box[1]):int(box[3]), int(box[0]):int(box[2])] = blur_obj

    cv2.imshow("ultralytics", im0)
    video_writer.write(im0)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
video_writer.release()
cv2.destroyAllWindows()

论据 model.predict

名称 类型 默认值 说明
source str 'ultralytics/assets' 图像或视频的源目录
conf float 0.25 检测对象置信度阈值
iou float 0.7 NMS 的 "相交大于结合"(IoU)阈值
imgsz int or tuple 640 图像尺寸标量或(高,宽)列表,即(640,480)
half bool False 使用半精度 (FP16)
device None or str None 设备上运行,如 cuda device=0/1/2/3 或 device=cpu
max_det int 300 每幅图像的最大检测次数
vid_stride bool False 视频帧速率跨度
stream_buffer bool False 缓冲所有流媒体帧(真)或返回最新帧(假)
visualize bool False 可视化模型特征
augment bool False 对预测源进行图像增强
agnostic_nms bool False 不分等级的 NMS
classes list[int] None 按类别筛选结果,即 classes=0,或 classes=[0,2,3]
retina_masks bool False 使用高分辨率分割掩膜
embed list[int] None 返回给定层的特征向量/嵌入值


创建于 2024-01-09,更新于 2024-01-15
作者:glenn-jocher(2)、AyushExel(1)、RizwanMunawar(1)

评论