practice_cv2(批量加入Mask,视频帧中加入logo)

import cv2
import ffmpeg
import cv2
import numpy
import numpy as np
import glob

video_f = 'D:/CCTV/CCTV.mp4'  # 视频文件名
output_f = 'D:/CCTV/logoafter.mp4'  # 输出视频文件名
video = cv2.VideoCapture(video_f)

# 获取视频宽度
frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))#480
# 获取视频高度
frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))#852
start_x, start_y, w, h = 740, 420, 100, 28  # logo位置
logo_f = 'shrink.png'
logo = cv2.imdecode(np.fromfile(logo_f, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
alpha = logo[..., 3]  #取出索引号为3的通道
alpha[alpha>0]=1   #透明度不为0的地方,置为1
alpha=np.expand_dims(alpha, axis=-1)  #广播机制,扩充维度,axis=0 [1,28,100], axis=1 [28,1,100] ,  axis=2 [28,100,1] , axis=-1 [28,100,1]
alpha=np.expand_dims(alpha, axis=0)   #(1, 28, 100, 1)

logo = cv2.imread(logo_f)
logo = cv2.resize(logo, (w, h))
logo=np.expand_dims(logo,axis=0)
print("logo:",logo.shape)


fps = video.get(cv2.CAP_PROP_FPS) #视频帧速率
sum_fps=video.get(cv2.CAP_PROP_FRAME_COUNT)  #总帧数
print("总帧数:",sum_fps)#1214


ret, frame = video.read()
img_h, img_w = frame.shape[:2]

batch_size=300
listframes = []
idx=0

def save_to_list(batch_size,idx,listframes):
    while True:
        ret, frame = video.read() #读出来是一帧一帧的

        idx += 1
        if frame is None:
            break
        if idx >0 and idx <=batch_size:
            listframes.append(frame)
            if idx==300:
                new_Matrix = numpy.stack(listframes)
                print(new_Matrix.shape)  # (300, 480, 852, 3)
                frame_mask = np.ones_like(new_Matrix)  # 返回一个用1填充的形状和类型一样的数组,全黑。构造了一个与视频帧形状大小一样的4维数组
                frame_mask[:, start_y:start_y + h, start_x:start_x + w, :] = 1 - alpha  # 将放置logo区域全部弄成0
                new_Matrix *= frame_mask  # 保留 非logo区域的原值 ,  空出logo大小的矩形框区域给logo
                new_Matrix[:, start_y:start_y + h, start_x:start_x + w, :] += logo * alpha

                idx = 0
                listframes = []

                for i in range(300):
                    cv2.imshow('f', new_Matrix[i, :, :, :])
                    cv2.waitKey(1)


save_to_list(batch_size,idx,listframes)







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,首先需要在代码引入SE注意力模块所需的库和函数: ```python import tensorflow as tf def se_block(input_feature, ratio=8): """Squeeze-and-excitation block""" channel_axis = 1 if tf.keras.backend.image_data_format() == "channels_first" else -1 channel = input_feature.shape[channel_axis] se_feature = tf.keras.layers.GlobalAveragePooling2D()(input_feature) se_feature = tf.keras.layers.Reshape((1, 1, channel))(se_feature) se_feature = tf.keras.layers.Dense(channel // ratio, activation='relu', kernel_initializer='he_normal', use_bias=True)(se_feature) se_feature = tf.keras.layers.Dense(channel, activation='sigmoid', kernel_initializer='he_normal', use_bias=True)(se_feature) if tf.keras.backend.image_data_format() == 'channels_first': se_feature = tf.keras.layers.Permute((3, 1, 2))(se_feature) se_tensor = tf.keras.layers.multiply([input_feature, se_feature]) return se_tensor ``` 然后在代码的残差块添加SE注意力模块: ```python def residual_block(inputs, filters, strides=(1, 1), use_se=True): shortcut = inputs # first block x = tf.keras.layers.Conv2D(filters=filters, kernel_size=(3, 3), strides=strides, padding='same', kernel_initializer='he_normal')(inputs) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation('relu')(x) # second block x = tf.keras.layers.Conv2D(filters=filters, kernel_size=(3, 3), strides=(1, 1), padding='same', kernel_initializer='he_normal')(x) x = tf.keras.layers.BatchNormalization()(x) # SE block if use_se: x = se_block(x) # shortcut connection if strides != (1, 1) or inputs.shape[-1] != filters: shortcut = tf.keras.layers.Conv2D(filters=filters, kernel_size=(1, 1), strides=strides, padding='same', kernel_initializer='he_normal')(inputs) shortcut = tf.keras.layers.BatchNormalization()(shortcut) x = tf.keras.layers.Add()([x, shortcut]) x = tf.keras.layers.Activation('relu')(x) return x ``` 这样就在代码添加了SE注意力模块。注意,这里的实现方式是在残差块嵌入SE注意力模块,而不是在整个模型添加。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值