Python3.6.6 OpenCV 将视频中人物标记或者打马赛克或加图片并保存为不同格式

1、轻松识别视频人物并做出标记

需安装face_recongnition与dlib,过程有点困难,还请网上查找方法

import face_recognition
import cv2
#镜像源 -i https://pypi.mirrors.ustc.edu.cn/simple 
# 加载视频
video_file = 'E:\\videos\\1.mp4'
video_capture = cv2.VideoCapture(video_file)

width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(video_capture.get(cv2.CAP_PROP_FPS))
frame_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
 
# 设置视频格式
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# 调用VideoWrite()函数
size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
video_writer = cv2.VideoWriter('output1.avi', fourcc, fps, size)

count = 0 
# 通过循环读取视频的每一帧
while True and count < 200:
    ret, frame = video_capture.read()
    # 如果正确读取帧,ret为True
    if not ret:
        break
    
    # 将帧转换为灰度图像,因为人脸识别对颜色不敏感
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # 使用face_recognition库的API进行人脸定位
    face_locations = face_recognition.face_locations(gray_frame)
    
    # 遍历所有找到的人脸
    for top, right, bottom, left in face_locations:
        # 画出人脸框
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
    
    # 显示帧
    #cv2.imshow('Video', frame)
    
    if not video_writer is False:
            video_writer.write(frame)
    
    count = count + 1
    
    # 按'q'退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 
# 释放视频捕获对象
video_capture.release()
# 关闭所有OpenCV窗口
cv2.destroyAllWindows()

实现效果

2、实现视频人物加图,代码如下

import cv2
import numpy as np
import face_recognition
from PIL import Image 

# 加载视频
cap = cv2.VideoCapture('E:\\videos\\1.mp4')
 
# 图片加密马赛克
def apply_mosaic(frame, mosaic_image, x, y, w, h):
    #print(mosaic_image.shape)
    mosaic_image = cv2.resize(mosaic_image, (w, h))
    #print(mosaic_image.shape)
    #cv2.imwrite('1.png',mosaic_image)
    #roi = frame[y:y+h, x:x+w]
    image_np = np.array(mosaic_image)
    #print(mosaic_image.shape)
    #print(frame.shape)
    #frame[y:y+h, x:x+w] = image_np#cv2.addWeighted(mosaic_image, 0, roi, 1, 0)
    for i in range(h):
        for j in range(w):
            #if(y+i<frame_height and x+j<frame_width):
                frame[x+i, y+j] = image_np[i, j]
# 加载图片
mosaic_image = cv2.imread('masaike.png')
 
# 读取视频的宽度和高度
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
 
# 设置马赛克的位置和大小
x, y, w, h = 50, 50, 100, 100
 
# 写入视频
#out = cv2.VideoWriter('output_video.avi', cv2.VideoWriter_fourcc(*'XVID'), 20.0, (frame_width, frame_height))

width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
 
# 设置视频格式
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# 调用VideoWrite()函数
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
video_writer = cv2.VideoWriter('output2.avi', fourcc, fps, size)

count = 0 
 
while cap.isOpened() and count < 250:
    ret, frame = cap.read()
    if ret and count > 50:

        # 将帧转换为灰度图像,因为人脸识别对颜色不敏感
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
        # 使用face_recognition库的API进行人脸定位
        face_locations = face_recognition.face_locations(gray_frame)
    
        # 遍历所有找到的人脸
        for top, right, bottom, left in face_locations:
            # 画出人脸框
            #cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
        
            # 应用马赛克
            apply_mosaic(frame, mosaic_image, top, left, abs(top-bottom), abs(right-left))
        
        # 输出帧
        video_writer.write(frame)
        
        count = count + 1
        # 显示帧
        #cv2.imshow('Video', frame)
        
        # 按 'q' 退出循环
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    elif count >= 250:
        break
    
    count = count + 1
 
# 释放资源
cap.release()
video_writer.release()
cv2.destroyAllWindows()

实现效果如下

至此完成,谢谢阅读 

相关推荐

  1. python视频按帧转化图片保存

    2024-07-14 06:16:01       31 阅读
  2. 使用Python和MoviePy库进行视频人脸追踪和马赛克

    2024-07-14 06:16:01       54 阅读
  3. C# 使用ffmpeg图片保存mp4视频

    2024-07-14 06:16:01       39 阅读
  4. OpenCV识别视频物体运动截取保存

    2024-07-14 06:16:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 06:16:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 06:16:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 06:16:01       58 阅读
  4. Python语言-面向对象

    2024-07-14 06:16:01       69 阅读

热门阅读

  1. helm系列之-构建自己的Helm Chart

    2024-07-14 06:16:01       22 阅读
  2. (算法)硬币问题

    2024-07-14 06:16:01       24 阅读
  3. 【代码复现】STAEformer

    2024-07-14 06:16:01       21 阅读
  4. python中的pickle模块和json模块

    2024-07-14 06:16:01       23 阅读
  5. ClickHouse实战第二章-ClickHouse 的安装调试

    2024-07-14 06:16:01       25 阅读
  6. Spring事件监听机制详解

    2024-07-14 06:16:01       22 阅读
  7. 案例:分库分表与SELECT * 发生的线上问题

    2024-07-14 06:16:01       24 阅读
  8. TypeScript的类型谓词与控制流分析

    2024-07-14 06:16:01       26 阅读
  9. ThreadLocal详解

    2024-07-14 06:16:01       22 阅读
  10. 小程序如何刷新当前页面

    2024-07-14 06:16:01       25 阅读
  11. qt 根据名称获取按钮,并添加点击事件

    2024-07-14 06:16:01       19 阅读