鱼C论坛

 找回密码
 立即注册
查看: 27|回复: 1

[☆☆★★★] Python | Day 14:基于深度学习的人脸打码自动化方案(视频)(★★★)

[复制链接]
发表于 12 小时前 | 显示全部楼层 |阅读模式
购买主题 本主题需向作者支付 20 鱼币 才能浏览
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 12 小时前 | 显示全部楼层
下面是对人脸打码自动化方案的优化解答,包含进度条添加和漏打问题解决方案:

### 问题1:添加视频处理进度条
使用 `tqdm` 库实时显示处理进度,修改 `mosaic_faces` 函数:

  1. import cv2
  2. from mtcnn import MTCNN
  3. from tqdm import tqdm  # 新增进度条库

  4. def mosaic_area(img, x, y, w, h, block_size=10):
  5.     """修正版:正确替换原图像区域"""
  6.     # 调整越界坐标
  7.     y1, y2 = max(0, y), min(img.shape[0], y + h)
  8.     x1, x2 = max(0, x), min(img.shape[1], x + w)
  9.    
  10.     # 截取有效区域
  11.     roi = img[y1:y2, x1:x2]
  12.     if roi.size == 0:  # 空区域跳过
  13.         return
  14.    
  15.     # 马赛克处理
  16.     small = cv2.resize(roi, (8, 8), interpolation=cv2.INTER_LINEAR)
  17.     mosaic = cv2.resize(small, (x2 - x1, y2 - y1), interpolation=cv2.INTER_NEAREST)
  18.     img[y1:y2, x1:x2] = mosaic  # 关键:替换原图区域

  19. def mosaic_faces(input_path, output_path, block_size=10, conf_threshold=0.8):
  20.     detector = MTCNN()
  21.     cap = cv2.VideoCapture(input_path)
  22.    
  23.     # 获取视频信息
  24.     total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))  # 总帧数
  25.     fps = int(cap.get(cv2.CAP_PROP_FPS))
  26.     width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  27.     height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  28.    
  29.     # 初始化进度条
  30.     pbar = tqdm(total=total_frames, desc="Processing Video")
  31.    
  32.     # 创建输出视频
  33.     fourcc = cv2.VideoWriter_fourcc(*'mp4v')
  34.     out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))

  35.     while True:
  36.         ret, frame = cap.read()
  37.         if not ret:
  38.             break
  39.             
  40.         # 人脸检测与打码
  41.         results = detector.detect_faces(frame)
  42.         for res in results:
  43.             if res['confidence'] < conf_threshold:  # 置信度过滤
  44.                 continue
  45.             x, y, w, h = res['box']
  46.             mosaic_area(frame, x, y, w, h, block_size)
  47.         
  48.         out.write(frame)
  49.         pbar.update(1)  # 更新进度
  50.    
  51.     cap.release()
  52.     out.release()
  53.     pbar.close()  # 关闭进度条
复制代码


### 问题2:解决马赛克"漏打"问题
通过以下两种方法提升检测准确率:
1. **调整置信度阈值**  
   - 降低 `conf_threshold` 值(默认0.8)检测更多人脸
   - 示例:`mosaic_faces(..., conf_threshold=0.6)`

2. **多尺度检测增强**  
   初始化检测器时启用多尺度检测:
  
  1. detector = MTCNN(steps_threshold=[0.5, 0.6, 0.7])  # 降低P/R/O网络阈值
复制代码


### 使用说明
1. 安装依赖库:
   pip install opencv-python tensorflow mtcnn tqdm

2. 执行主程序:
  
  1. if __name__ == "__main__":
  2.        mosaic_faces("input.mp4", "output.mp4",
  3.                    block_size=15,    # 马赛克块大小
  4.                    conf_threshold=0.7)  # 人脸检测置信度
复制代码


> 注意事项:
> 1. 进度条长度 = 视频总帧数
> 2. 降低 `conf_threshold` 可能增加误检
> 3. 复杂场景建议结合 min_face_size 参数调整:
>    detector = MTCNN(min_face_size=40)

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 16:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表