您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python如何實(shí)現(xiàn)人臉識(shí)別并進(jìn)行視頻跟蹤打碼的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Python如何實(shí)現(xiàn)人臉識(shí)別并進(jìn)行視頻跟蹤打碼文章都會(huì)有所收獲,下面我們一起來看看吧。
首先需要一些素材,大家可以自己準(zhǔn)備
這個(gè)是要用的工具
使用的模塊
import cv2 import face_recognition import matplotlib.pyplot as plt # %matplotlib inline # 在 jupyter 中使用的時(shí)候,去掉注釋 import ffmpy3 import subprocess import os from PIL import Image
將視頻轉(zhuǎn)為音頻
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=True)
視頻添加音頻
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=True)
主要代碼
def mask_video(input_video, output_video, mask_path='mask.jpg'): # 打碼圖片 # 完整源碼、視頻講解 # Python學(xué)習(xí)交流群:708525271 # 直接加它領(lǐng)取 mask = cv2.imread(mask_path) # 讀取視頻 cap = cv2.VideoCapture(input_video) # 讀取視頻參數(shù),fps、width、heigth CV_CAP_PROP_FPS = 5 CV_CAP_PROP_FRAME_WIDTH = 3 CV_CAP_PROP_FRAME_HEIGHT = 4 v_fps = cap.get(CV_CAP_PROP_FPS) v_width = cap.get(CV_CAP_PROP_FRAME_WIDTH) v_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT) # 設(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: # 檢測人臉 face_locations = face_recognition.face_locations(frame) # print(face_locations) # 檢測每一個(gè)人臉 for (top_right_y, top_right_x, left_bottom_y, left_bottom_x) in face_locations: unknown_image = frame[top_right_y - 50:left_bottom_y + 50, left_bottom_x - 50:top_right_x + 50] print(face_recognition.face_encodings(unknown_image)) if face_recognition.face_encodings(unknown_image) != []: unknown_encoding = face_recognition.face_encodings(unknown_image)[0] # 對(duì)比結(jié)果 results = face_recognition.compare_faces([biden_encoding], unknown_encoding) # 是仝卓,就將打碼貼圖。 if results[0] == 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
將音頻保存為cut.mp3
video2mp3(file_name='cut.mp4')
處理視頻,自動(dòng)打碼,輸出視頻為output.mp4
mask_video(input_video='cut.mp4', output_video='output.mp4')
為 output.mp4 處理好的視頻添加聲音
video_add_mp3(file_name='output.mp4', mp3_file='cut.mp3')
關(guān)于“Python如何實(shí)現(xiàn)人臉識(shí)別并進(jìn)行視頻跟蹤打碼”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Python如何實(shí)現(xiàn)人臉識(shí)別并進(jìn)行視頻跟蹤打碼”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。