翼度科技»论坛 编程开发 python 查看内容

Python实现人脸识别,对视频跟踪打码,拒绝少儿不宜!

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
事情是这样的,昨天去表弟家,用了下他的电脑,不小心点到了他硬盘里隐藏的秘密,本来我只需要用几分钟电脑的,害得我硬是在电脑旁坐了几个小时~
还好他爸妈不在家,不然表弟又要被毒打一顿!
为了防止表弟的秘密被发现,从而被赏赐一顿男女混合双打,于是我用Python把他所有的视频都给打上了万恶的马赛克。

我想,表弟肯定会感谢我的!

准备工作

话不多少,我们直接开始操作!
首先需要一些素材,大家可以自己准备,也可以直接在文章最后面的名片扫码领取。

这个是要用的工具

代码实战

使用的模块
  1. import cv2
  2. import face_recognition
  3. import matplotlib.pyplot as plt
  4. # %matplotlib inline # 在 jupyter 中使用的时候,去掉注释
  5. import ffmpy3
  6. import subprocess
  7. import os
  8. from PIL import Image
复制代码
 
将视频转为音频
  1. def video2mp3(file_name):
  2.     outfile_name = file_name.split('.')[0] + '.mp3'
  3.     cmd = 'ffmpeg -i ' + file_name + ' -f mp3 ' + outfile_name
  4.     print(cmd)
  5.     subprocess.call(cmd, shell=True)
复制代码
 
视频添加音频
  1. def video_add_mp3(file_name, mp3_file):
  2.     outfile_name = file_name.split('.')[0] + '-f.mp4'
  3.     subprocess.call('ffmpeg -i ' + file_name
  4.                     + ' -i ' + mp3_file + ' -strict -2 -f mp4 '
  5.                     + outfile_name, shell=True)
复制代码
 
主要代码
  1. def mask_video(input_video, output_video, mask_path='mask.jpg'):
  2.     # 打码图片
  3.     # 完整源码、视频讲解
  4.     # Python学习交流群:708525271
  5.     # 直接加它领取
  6.     mask = cv2.imread(mask_path)
  7.     # 读取视频
  8.     cap = cv2.VideoCapture(input_video)
  9.     # 读取视频参数,fps、width、heigth
  10.     CV_CAP_PROP_FPS = 5
  11.     CV_CAP_PROP_FRAME_WIDTH = 3
  12.     CV_CAP_PROP_FRAME_HEIGHT = 4
  13.     v_fps = cap.get(CV_CAP_PROP_FPS)
  14.     v_width = cap.get(CV_CAP_PROP_FRAME_WIDTH)
  15.     v_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT)
  16.     # 设置写视频参数,格式为 mp4
  17.     size = (int(v_width), int(v_height))
  18.     fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
  19.     out = cv2.VideoWriter(output_video, fourcc, v_fps, size)
  20.     # 已知人脸
  21.     known_image = face_recognition.load_image_file("tmr.jpg")
  22.     biden_encoding = face_recognition.face_encodings(known_image)[0]
  23.     # 读取视频
  24.     cap = cv2.VideoCapture(input_video)
  25.     while (cap.isOpened()):
  26.         ret, frame = cap.read()
  27.         if ret:
  28.             # 检测人脸
  29.             face_locations = face_recognition.face_locations(frame)
  30.             # print(face_locations)
  31.             # 检测每一个人脸
  32.             for (top_right_y, top_right_x, left_bottom_y, left_bottom_x) in face_locations:
  33.                 unknown_image = frame[top_right_y - 50:left_bottom_y + 50, left_bottom_x - 50:top_right_x + 50]
  34.                 print(face_recognition.face_encodings(unknown_image))
  35.                 if face_recognition.face_encodings(unknown_image) != []:
  36.                     unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
  37.                     # 对比结果
  38.                     results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
  39.                     # 是仝卓,就将打码贴图。
  40.                     if results[0] == True:
  41.                         mask = cv2.resize(mask, (top_right_x - left_bottom_x, left_bottom_y - top_right_y))
  42.                         frame[top_right_y:left_bottom_y, left_bottom_x:top_right_x] = mask
  43.             # 写入视频
  44.             out.write(frame)
  45.         else:
  46.             break
复制代码
 
将音频保存为cut.mp3
  1. video2mp3(file_name='cut.mp4')
复制代码
 
处理视频,自动打码,输出视频为output.mp4
  1. mask_video(input_video='cut.mp4', output_video='output.mp4')
复制代码
 
  1. 为 output.mp4 处理好的视频添加声音
复制代码
  1. video_add_mp3(file_name='output.mp4', mp3_file='cut.mp3')
  2. # 我录制了视频讲解,跟源码一起打包好放在这个Q裙了:708525271
  3. # 直接加它领取
复制代码
 
效果展示

不愿透露姓名的唐马儒先生

最后

兄弟们,今天的分享就到这里结束了,咱们下次见!
点关注,不迷路,每天分享有用的Python知识!

来源:https://www.cnblogs.com/hahaa/p/17181982.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具