• Python实现视频自动打码,不用担心透露隐私了


    准备工作

    环境咱们还是使用 Python3.8 和 pycharm2021 即可

    实现原理

    1. 将视频分为音频和画面
    2. 画面中出现人脸和目标比对,相应人脸进行打码
    3. 处理后的视频添加声音

    模块

    手动安装一下 cv2 模块 ,pip install opencv-python 安装
    安装遇到报错,可以私信我

    素材工具

    我们需要安装一下 ffmpeg 音视频转码工具

    请添加图片描述

    所有的素材.源码.点击此处即可领取

    请添加图片描述

    代码解析

    导入模块

    import cv2  
    import face_recognition  # 人脸识别库  99.7%    cmake  dlib  face_recognition
    import subprocess
    
    • 1
    • 2
    • 3

    视频转为音频

    def video2mp3(file_name):
        """
        :param file_name: 视频文件路径
        :return:
        """
        outfile_name = file_name.split('.')[0] + '.mp3'
        cmd = 'ffmpeg -i ' + file_name + ' -f mp3 ' + outfile_name
        print(cmd)
        subprocess.call(cmd, shell=False)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    打码

    def mask_video(input_video, output_video, mask_path='mask.jpg'):
        """
        :param input_video: 需打码的视频
        :param output_video: 打码后的视频
        :param mask_path: 打码图片
        :return:
        """
        # 读取图片
        mask = cv2.imread(mask_path)
        # 读取视频
        cap = cv2.VideoCapture(input_video)
        # 视频  fps  width  height
        v_fps = cap.get(5)
        v_width = cap.get(3)
        v_height = cap.get(4)
    
        # 设置写入视频参数  格式MP4
        # 画面大小
        size = (int(v_width), int(v_height))
        fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
    
        # 输出视频
        out = cv2.VideoWriter(output_video, fourcc, v_fps, size)
    
        # 已知人脸
        known_image = face_recognition.load_image_file('tmr.jpg')
        biden_encoding = face_recognition.face_encodings(known_image)[0]
    
        cap = cv2.VideoCapture(input_video)
    
        while (cap.isOpened()):
            ret, frame = cap.read()
            if ret:
                # 检测人脸
                # 人脸区域
                face_locations = face_recognition.face_locations(frame)
    
                for (top_right_y, top_right_x, left_bottom_y, left_bottom_x) in face_locations:
                    print((top_right_y, top_right_x, left_bottom_y, left_bottom_x))
                    unknown_image = frame[top_right_y - 50:left_bottom_y + 50, left_bottom_x - 50:top_right_x + 50]
                    if face_recognition.face_encodings(unknown_image) != []:
                        unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
    
                        # 对比人脸
                        results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
                        # [True]
                        # 贴图
                        if results == [True]:
                            mask = cv2.resize(mask, (top_right_x - left_bottom_x, left_bottom_y - top_right_y))
                            frame[top_right_y:left_bottom_y, left_bottom_x:top_right_x] = mask
                out.write(frame)
    
    
            else:
                break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    音频添加到画面

    def video_add_mp3(file_name, mp3_file):
        """
        :param file_name: 视频画面文件
        :param mp3_file:  视频音频文件
        :return:
        """
        outfile_name = file_name.split('.')[0] + '-f.mp4'
        subprocess.call('ffmpeg -i ' + file_name + ' -i ' + mp3_file + ' -strict -2 -f mp4 ' + outfile_name, shell=False)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    请添加图片描述

    完整代码

    源码.素材.资料.点击领取即可

    在这里插入图片描述

    好啦,今天的分享到这里就结束了 ~

    如果需要更多视频学习的可以在b站搜索 :Python小圆 / ka-爆浆麻薯团子

    对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
    觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

    请添加图片描述

  • 相关阅读:
    <Linux系统复习>信号
    Ajax用法
    Flutter 3.3 正式发布啦
    nodejs清空文件内容
    作为业主,你在享受物业管理与服务中有哪些困扰与需求?
    请查收.NET MAUI 的最新学习资源
    vue3 和vue2 的比较
    【JavaEE】_HTTP响应
    Javascript命令模式
    转载--关闭onenote2013 /中点击超链接(指向本地文件夹)后出现的安全声明 / Microsoft onenote2021 安全声明关闭
  • 原文地址:https://blog.csdn.net/aliYz/article/details/127541002