最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)視頻自動(dòng)打碼的示例代碼

 更新時(shí)間:2022年04月08日 09:36:10   作者:嗨學(xué)編程  
我們?cè)谟^看視頻的時(shí)候,有時(shí)候會(huì)出現(xiàn)一些奇怪的馬賽克,影響我們的觀影體驗(yàn),那么這些馬賽克是如何精確的加上去的呢?本文就來為大家詳細(xì)講講

序言

我們?cè)谟^看視頻的時(shí)候,有時(shí)候會(huì)出現(xiàn)一些奇怪的馬賽克,影響我們的觀影體驗(yàn),那么這些馬賽克是如何精確的加上去的呢?

本次我們就來用Python實(shí)現(xiàn)對(duì)視頻自動(dòng)打碼!

準(zhǔn)備工作

環(huán)境咱們還是使用 Python3.8 和 pycharm2021 即可

實(shí)現(xiàn)原理

將視頻分為音頻和畫面;

畫面中出現(xiàn)人臉和目標(biāo)比對(duì),相應(yīng)人臉進(jìn)行打碼;

處理后的視頻添加聲音;

模塊

手動(dòng)安裝一下 cv2 模塊 ,pip install opencv-python 安裝

素材工具

我們需要安裝一下 ffmpeg 音視頻轉(zhuǎn)碼工具

代碼解析

導(dǎo)入需要使用的模塊

import cv2  
import face_recognition  # 人臉識(shí)別庫  99.7%    cmake  dlib  face_recognition
import subprocess

將視頻轉(zhuǎn)為音頻

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)

打碼

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)

    # 設(shè)置寫入視頻參數(shù)  格式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:
            # 檢測(cè)人臉
            # 人臉區(qū)域
            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]

                    # 對(duì)比人臉
                    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

音頻添加到畫面

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)

完整代碼

import cv2 
import face_recognition  # 人臉識(shí)別庫  99.7%    cmake  dlib  face_recognition
import subprocess

def video2mp3(file_name):

    outfile_name = file_name.split('.')[0] + '.mp3'
    cmd = 'ffmpeg -i ' + file_name + ' -f mp3 ' + outfile_name
    print(cmd)
    subprocess.call(cmd, shell=False)


def mask_video(input_video, output_video, mask_path='mask.jpg'):

    # 讀取圖片
    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)

    # 設(shè)置寫入視頻參數(shù)  格式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:
            # 檢測(cè)人臉
            # 人臉區(qū)域
            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]

                    # 對(duì)比人臉
                    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


def video_add_mp3(file_name, mp3_file):

    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)


if __name__ == '__main__':
    # 1.
    video2mp3('cut.mp4')
    # 2.
    mask_video(input_video='cut.mp4',output_video='output.mp4')
    # 3.
    video_add_mp3(file_name='output.mp4',mp3_file='cut.mp3')

兄弟們,快去試試吧!

到此這篇關(guān)于Python實(shí)現(xiàn)視頻自動(dòng)打碼的示例代碼的文章就介紹到這了,更多相關(guān)Python視頻打碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python字符串常用方法及文件簡(jiǎn)單讀寫的操作方法

    python字符串常用方法及文件簡(jiǎn)單讀寫的操作方法

    字符串(sting)是 Python 中最常用的數(shù)據(jù)類型。我們可以使用引號(hào)('或")來創(chuàng)建字符。本文給大家介紹python字符串常用方法及文件簡(jiǎn)單讀寫的操作方法,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-03-03
  • python中os庫的具體使用

    python中os庫的具體使用

    本文介紹了Python中os庫的一些常見功能,包括獲取和改變工作目錄、列出目錄內(nèi)容、創(chuàng)建和刪除目錄等,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • 使用Python對(duì)MySQL數(shù)據(jù)操作

    使用Python對(duì)MySQL數(shù)據(jù)操作

    本文介紹Python3使用PyMySQL連接數(shù)據(jù)庫,并實(shí)現(xiàn)簡(jiǎn)單的增刪改查。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • 布同 Python中文問題解決方法(總結(jié)了多位前人經(jīng)驗(yàn),初學(xué)者必看)

    布同 Python中文問題解決方法(總結(jié)了多位前人經(jīng)驗(yàn),初學(xué)者必看)

    首先談?wù)勎沂窃趺从龅絇ython中文輸入問題的。我寫了一個(gè)小工具,用來查詢Python的庫函數(shù)。
    2011-03-03
  • 對(duì)python中執(zhí)行DOS命令的3種方法總結(jié)

    對(duì)python中執(zhí)行DOS命令的3種方法總結(jié)

    今天小編就為大家分享一篇對(duì)python中執(zhí)行DOS命令的3種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助一起。一起跟隨小編過來看看吧
    2018-05-05
  • Windows上使用Python增加或刪除權(quán)限的方法

    Windows上使用Python增加或刪除權(quán)限的方法

    下面小編就為大家分享一篇Windows上使用Python增加或刪除權(quán)限的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Pytorch實(shí)戰(zhàn)之?dāng)?shù)據(jù)加載和處理詳解

    Pytorch實(shí)戰(zhàn)之?dāng)?shù)據(jù)加載和處理詳解

    Pytorch提供了許多工具來簡(jiǎn)化和希望數(shù)據(jù)加載,使代碼更具可讀性,本文將通過一些簡(jiǎn)單示例為大家具體講講,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • python-docx如何刪除所有bookmarks

    python-docx如何刪除所有bookmarks

    在Python-docx庫中,雖然沒有直接刪除書簽的功能,但可以通過操作XML元素,遍歷文檔結(jié)構(gòu)并刪除指定元素來實(shí)現(xiàn)刪除所有書簽的目的,首先要明白書簽在XML文件中的位置,然后利用Python-docx提供的element元素遍歷并刪除特定的書簽元素
    2024-09-09
  • python實(shí)現(xiàn)AES加密與解密

    python實(shí)現(xiàn)AES加密與解密

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)AES加密與解密,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • django使用圖片延時(shí)加載引起后臺(tái)404錯(cuò)誤

    django使用圖片延時(shí)加載引起后臺(tái)404錯(cuò)誤

    本文給大家介紹的是作者在Django中使用圖片的延時(shí)加載技術(shù)后引起后臺(tái)404錯(cuò)誤的問題以及解決思路和方法,有需要的小伙伴可以參考下
    2017-04-04

最新評(píng)論

洱源县| 彭州市| 荥阳市| 章丘市| 天门市| 项城市| 东台市| 定安县| 澄城县| 临湘市| 盐津县| 周口市| 兖州市| 依兰县| 金秀| 北宁市| 滦南县| 仁怀市| 商丘市| 黑河市| 冕宁县| 阜宁县| 柞水县| 无锡市| 华蓥市| 晋城| 囊谦县| 南汇区| 兴仁县| 南宫市| 颍上县| 余庆县| 特克斯县| 清河县| 保靖县| 拜城县| 苍梧县| 定边县| 嘉荫县| 丰城市| 安远县|