题目

#从网上下载或自己手机录制一段视频(>30秒),第0-5秒显示一句话的字幕,第6-15秒显示另一句话的字幕。
#第20秒开始从屏幕中心出现一个光点,发出眩光,逐渐扩大覆盖的整个屏幕(类似太阳),最后光点缩小复原,整个过程10秒。

先给出框架

data文件夹(文末有链接)

.mp4文件可以选取自己的视频,图片是你要生成的眩光效果,相应的要改utils。

gen_glareRGB.py

##gen_glareRGB.py##
#获取眩光图片的像素分布,是调用的函数,下次放在modules模块里面会更好
import cv2def GlaredgeRgb():#这里是我们需要获取的眩光半径343个像素,是取了视频高度的一半radius_rgb = [0]*343glare_data = cv2.imread('../data/glare.jpg')glare_data = cv2.cvtColor(glare_data,cv2.COLOR_BGR2RGB)row,col,cha = glare_data.shape#选择中心的那一行chose_row=int(row/2)#从第300列开始,获取像素值chose_col = 300for i in range(len(radius_rgb)):radius_rgb[i] = tuple(glare_data[chose_row,i+chose_col])return radius_rgb[266:343]  #343-77=266,是由于在这之内的像素全是白色,只取非白色点if __name__ == '__main__':a = GlaredgeRgb()print(a)count = 0for i in range(len(a)):#找非白色点有多少个if a[i][0] != 255:print(i)count +=1print(count) #77print(count/len(a))  #0.2244,计算非白色点的比率

SubtitlesVideo1.py

加上字幕,生成第一种眩光,时间花费较短,20s左右,效果中下。

##SubtitlesVideo1.py##
#从网上下载或自己手机录制一段视频(>30秒),第0-5秒显示一句话的字幕,第6-15秒显示另一句话的字幕。
#第20秒开始从屏幕中心出现一个光点,发出眩光,逐渐扩大覆盖的整个屏幕(类似太阳),最后光点缩小复原,整个过程10秒。
#这个光照实现的不够好,是比较生硬的感觉
import datetime
import time# 方法一:datetime.datetime.now() 时间和日期的结合 eg: 2021-10-15 14:19:27.875779
#——————————————————————————————————————————————————————————————————————#
start_dt = datetime.datetime.now()
print("start_datetime:", start_dt)
time.sleep(2)
for i in range(10000):i += 1
#——————————————————————————————————————————————————————————————————————#import cv2
from utils.gen_glareRGB import GlaredgeRgbdef glare_circle(img = '../data/glare.jpg',center = (30,30),radius=3):edge_i = 0glare_edge =  GlaredgeRgb()cent_rate = 0.78cv2.circle(img,center,int(radius*cent_rate),(255,255,255),-1)for i in range(int(radius*cent_rate)+1,radius+1):if edge_i >= len(glare_edge):edge_i = len(glare_edge)  - edge_i%len(glare_edge) -1cv2.circle(img,center,i,tuple([int(x) for x in glare_edge[edge_i]]),0)edge_i += 1return 0#original_video
org_video = "../data/LawerCrush.mp4"
#video_subtitled
sub_video = "../data/LawerCrush_subtitles1.mp4"
#read video
Video = cv2.VideoCapture(org_video)
#Gets the video frame rate
Fps_video = Video.get(cv2.CAP_PROP_FPS)
#Sets the encoding format for writing video
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
#Get video width
frame_width = int(Video.get(cv2.CAP_PROP_FRAME_WIDTH))
#Get video Height
frame_height = int(Video.get(cv2.CAP_PROP_FRAME_HEIGHT))
#Save the video after the subtitles
videoWriter = cv2.VideoWriter(sub_video, fourcc, Fps_video, (frame_width, frame_height))##Add subtitles
glare_time = int(Fps_video*5)-1 #vanish overflow
glare_count = 0
frame_id = 0
w_index = 0
putword = ['He is a down-and-out lawyer','God gave him another surprise','  ']
cc_x = int(frame_width/2)
cc_y = int(frame_height/2)
while (Video.isOpened()):ret, frame = Video.read()if ret == True:frame_id += 1time_s = int(frame_id/Fps_video)if(time_s<6):w_index = 0elif(time_s<16):w_index = 1else:w_index =2if 20<time_s <= 25 :glare_count += 1elif 25< time_s <=30:glare_count -= 1#fullglare_circle(frame,(cc_x,cc_y),int((cc_x/glare_time)*glare_count))#Text coordinatesword_x = 450word_y = int(frame_height)-18cv2.putText(frame, '%s' %putword[w_index], (word_x, word_y), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,255,255), 2)#****写入视频videoWriter.write(frame)else:videoWriter.release()break#——————————————————————————————————————————————————————————————————————#
end_dt = datetime.datetime.now()
print("end_datetime:", end_dt)
print("time cost:", (end_dt - start_dt).seconds, "s")
#——————————————————————————————————————————————————————————————————————#

SubtitlesVideo2.py

加上字幕,生成第二种眩光,时间花费较长,30min左右,效果很好。

##SubtitlesVideo2.py##
#从网上下载或自己手机录制一段视频(>30秒),第0-5秒显示一句话的字幕,第6-15秒显示另一句话的字幕。
#第20秒开始从屏幕中心出现一个光点,发出眩光,逐渐扩大覆盖的整个屏幕(类似太阳),最后光点缩小复原,整个过程10秒。
#这个光照实现比较自然import cv2
import math
import numpy as np
import datetime
import time# 方法一:datetime.datetime.now() 时间和日期的结合 eg: 2021-10-15 14:19:27.875779
#——————————————————————————————————————————————————————————————————————#
start_dt = datetime.datetime.now()
print("start_datetime:", start_dt)
time.sleep(2)
for i in range(10000):i += 1
#——————————————————————————————————————————————————————————————————————##original_video
org_video = "../data/LawerCrush.mp4"
#video_subtitled
sub_video = "../data/LawerCrush_subtitles2.mp4"
#read video
Video = cv2.VideoCapture(org_video)
#Gets the video frame rate
Fps_video = Video.get(cv2.CAP_PROP_FPS)
#Sets the encoding format for writing video
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
#Get video width
frame_width = int(Video.get(cv2.CAP_PROP_FRAME_WIDTH))
#Get video Height
frame_height = int(Video.get(cv2.CAP_PROP_FRAME_HEIGHT))
#Save the video after the subtitles
videoWriter = cv2.VideoWriter(sub_video, fourcc, Fps_video, (frame_width, frame_height))##Add glare
strength = 222
centerx = frame_width/2
centery = frame_height/2
#full
radius = max(centerx,centery)
glare_time = int(Fps_video*5)-1  #5s一个阶段
glare_count = 0def glaring(frame,r):if r == 0:return 0for i in range(frame_height):for j in range(frame_width):distance  = math.pow((centery-i),2) + math.pow((centerx-j),2)#获取原始图像B = frame[i, j][0]G = frame[i, j][1]R = frame[i, j][2]if (distance<r*r):#按照距离大小计算增强的光照值result = (int)(strength * (1.0 -math. sqrt(distance) / radius))B = frame[i, j][0] + resultG = frame[i, j][1] + resultR = frame[i, j][2] + result#判断边界防止越界B = min(255, max(0, B))G = min(255, max(0, G))R = min(255, max(0, R))frame[i, j] = np.uint8((B, G, R))else:frame[i, j] = np.uint8((B, G, R))#______________________________________________###Add subtitlesframe_id = 0
w_index = 0
putword = ['He is a down-and-out lawyer','God gave him another surprise','  ']
#______________________________________________#
while (Video.isOpened()):ret, frame = Video.read()if ret == True:frame_id += 1print(frame_id)time_s = int(frame_id/Fps_video)if(time_s<6):w_index = 0elif(time_s<16):w_index = 1else:w_index =2if time_s<=20:glare_count = 0elif 20<time_s <= 25 :print('therebig{}'.format(frame_id))glare_count +=1elif 25< time_s <=30:glare_count -=1print('theresmalll{}'.format(frame_id))if frame_id%1 == 0:glaring(frame,(radius/glare_time )*glare_count)#Text coordinatesword_x = 450word_y = int(frame_height)-18cv2.putText(frame, '%s' %putword[w_index], (word_x, word_y), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,255,255), 2)#****写入视频videoWriter.write(frame)else:videoWriter.release()break#——————————————————————————————————————————————————————————————————————#
end_dt = datetime.datetime.now()
print("end_datetime:", end_dt)
print("time cost:", (end_dt - start_dt).seconds, "s")
#——————————————————————————————————————————————————————————————————————#

运行结果

运行SubtitlesVideo1.py之后,在data文件夹会多出来一个视频

查看视频完成要求情况:



运行SubtitlesVideo2.py之后,在data文件夹又会多出来一个视频

查看视频完成要求情况(主要是眩光有变化):

第三种美丽的眩光,并且耗时短,是由W同学的好想法(见代码)做到的

e2_main_acc.py


import cv2
import os
from tqdm import tqdm
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import multiprocessing# 切割图像帧
def get_each_frame(video_path):# 读取视频文件video = cv2.VideoCapture(video_path)_, frame = video.read()fps = video.get(cv2.CAP_PROP_FPS)frames = video.get(cv2.CAP_PROP_FRAME_COUNT)frames = int(frames)print('开始切割图像帧....')print('*' + 50 * '-' + '*')for i in tqdm(range(1, frames + 1)):address = './per_picture/' + str(i) + '.jpg'cv2.imwrite(address, frame)_, frame = video.read()return frames, int(fps)# 合成图像帧为视频
def merge_image_to_video(folder_name, per_h, per_w, fps, n):fourcc = cv2.VideoWriter_fourcc(*'mp4v')img_size = (per_w, per_h)video = cv2.VideoWriter("output.mp4", fourcc, fps, img_size)print('开始合成图像帧....')print('*' + 50 * '-' + '*')for f1 in tqdm(range(1, n + 1)):f1 = str(f1) + '.jpg'filename = os.path.join(folder_name, f1)frame = cv2.imread(filename)frame = cv2.resize(frame, img_size, interpolation=cv2.INTER_CUBIC)video.write(frame)video.release()# 添加字幕
def add_text(s, e, t, h, w):# 字幕参数设置pos = (w // 3, h - 100)t_color = (0, 255, 255)t_size = 60print('开始添加字幕....')print('*' + 50 * '-' + '*')for i in tqdm(range(s, e + 1)):path = './per_picture/' + str(i) + '.jpg'img = cv2.imread(path)img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))# 创建一个可以在给定图像上绘图的对象draw = ImageDraw.Draw(img)# 字体的格式font = ImageFont.truetype("simsun.ttc", t_size, encoding="utf-8")# 绘制文本draw.text(pos, t, t_color, font=font)# 转换回OpenCV格式img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)cv2.imwrite(path, img)# 添加曝光点
def add_exposure(s, e, h, w, mode):if mode == 'up':r = 0.01v = (h ** 2 + w ** 2) ** (1 / 2) * 2 / (e - s + 1)print('增加逐渐增大的曝光点......')elif mode == 'down':r = (h ** 2 + w ** 2) ** (1 / 2) * 2v = -(h ** 2 + w ** 2) ** (1 / 2) * 2 / (e - s + 1)print('增加逐渐减小的曝光点......')print('*' + 50 * '-' + '*')for i in tqdm(range(s, e + 1)):r += vpath = './per_picture/' + str(i) + '.jpg'img = cv2.imread(path)# 曝光点部分m1 = np.zeros((h, w, 3))I = np.arange(h)[:, None]J = np.arange(w)i_r = ((I - h // 2) ** 2 + (J - w // 2) ** 2) ** (1 / 2)p1 = i_r < r# 逻辑矩阵扣出曝光点区域q = 255 * (1 - i_r / r)# 渐变光强q = np.repeat(q, 3, axis=1)# 复制光强到B、G、R三个通道q = q.reshape((h, w, 3))m1[p1] = q[p1]img = img + m1# 曝光点区域加上渐变的光强cv2.imwrite(path, img)if __name__ == '__main__':# 切割图像帧n, fps = get_each_frame('./test.mp4')# 获取每张图像长和宽image = cv2.imread('./per_picture/1.jpg')h, w, _ = image.shape# 第0-5秒 显示字幕:"你为什么离开?"text1 = multiprocessing.Process(target=add_text, args=(fps * 0 + 1, fps * 5, "你为什么离开?", h, w))# 第6-15秒显示另一句话的字幕 '我不会停留在—个地方看风景,我的目标是星辰大海'text2 = multiprocessing.Process(target=add_text, args=(fps * 6 + 1, fps * 15, '我不会停留在—个地方看风景,我的目标是星辰大海', h, w))# 增加逐渐变大的曝光点 20-25秒exposure1 = multiprocessing.Process(target=add_exposure, args=(fps * 20 + 1, fps * 25, h, w, 'up'))# 增加逐渐减小的曝光点 25-30秒exposure2 = multiprocessing.Process(target=add_exposure, args=(fps * 25 + 1, fps * 30, h, w, 'down'))text1.start()text2.start()exposure1.start()exposure2.start()text1.join()text2.join()exposure1.join()exposure2.join()# 合成图像帧为视频流merge_image_to_video('per_picture', h, w, fps=fps, n=n)

e2_main.py

import cv2
import os
from tqdm import tqdm
import numpy as np
from PIL import Image, ImageDraw, ImageFont# 切割图像帧
def get_each_frame(video_path):# 创建视频流对象video = cv2.VideoCapture(video_path)# 获取视频帧率fps = video.get(cv2.CAP_PROP_FPS)# 获取总帧数frames = video.get(cv2.CAP_PROP_FRAME_COUNT)frames = int(frames)print('开始切割图像帧....')print('*' + 50 * '-' + '*')for i in tqdm(range(1, frames + 1)):# 存储图片帧的位置address = './per_picture/' + str(i) + '.jpg'_, frame = video.read()cv2.imwrite(address, frame)return frames, int(fps)# 合成图像帧为视频
def merge_image_to_video(folder_name, per_h, per_w, fps, n):# 设置视频流的格式fourcc = cv2.VideoWriter_fourcc(*'mp4v')# 封装图像帧格式为元组img_size = (per_w, per_h)# 传入 视频 的帧率 和图像帧大小video = cv2.VideoWriter("output.mp4", fourcc, fps, img_size)print('开始合成图像帧....')print('*' + 50 * '-' + '*')for f1 in tqdm(range(1, n + 1)):f1 = str(f1) + '.jpg'# 图像帧路径filename = os.path.join(folder_name, f1)# 获取图像frame = cv2.imread(filename)frame = cv2.resize(frame, img_size, interpolation=cv2.INTER_CUBIC)# 写入图像帧video.write(frame)video.release()# 添加字幕
def add_text(s, e, t, h, w):# 字幕参数设置pos = (w // 3, h - 100)t_color = (0, 255, 255)t_size = 60print('开始添加字幕....')print('*' + 50 * '-' + '*')for i in tqdm(range(s, e + 1)):path = './per_picture/' + str(i) + '.jpg'img = cv2.imread(path)img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))# 创建一个可以在给定图像上绘图的对象draw = ImageDraw.Draw(img)# 字体的格式font = ImageFont.truetype("simsun.ttc", t_size, encoding="utf-8")# 绘制文本draw.text(pos, t, t_color, font=font)# 转换回OpenCV格式img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)cv2.imwrite(path, img)# 添加曝光点
def add_exposure(s, e, h, w, mode):if mode == 'up':r = 0.01v = (h ** 2 + w ** 2) ** (1 / 2) * 2 / (e - s + 1)elif mode == 'down':r = (h ** 2 + w ** 2) ** (1 / 2) * 2v = -(h ** 2 + w ** 2) ** (1 / 2) * 2  / (e - s + 1)print('*' + 50 * '-' + '*')for i in tqdm(range(s, e + 1)):r += vpath = './per_picture/' + str(i) + '.jpg'img = cv2.imread(path)# 曝光点部分m1 = np.zeros((h, w, 3))I = np.arange(h)[:, None]J = np.arange(w)i_r = ((I - h // 2) ** 2 + (J - w // 2) ** 2) ** (1 / 2)p1 = i_r < rq = 255 * (1 - i_r / r)q = np.repeat(q, 3, axis=1)q = q.reshape((h, w, 3))m1[p1] = q[p1]img = img + m1cv2.imwrite(path, img)if __name__ == '__main__':# 切割图像帧n, fps = get_each_frame('./test.mp4')# 获取每张图像长和宽image = cv2.imread('./per_picture/1.jpg')h, w, _ = image.shape# 第0-5秒 显示字幕:"你为什么离开?"add_text(fps * 0 + 1, fps * 5, "你为什么离开?", h, w)# 第6-15秒显示另一句话的字幕 '我不会停留在—个地方看风景,我的目标是星辰大海'add_text(fps * 6 + 1, fps * 15, '我不会停留在—个地方看风景,我的目标是星辰大海', h, w)# 增加逐渐变大的曝光点 20-25秒print('增加逐渐增大的曝光点......')add_exposure(fps * 20 + 1, fps * 25, h, w, mode='up')print('增加逐渐减小的曝光点......')# 增加逐渐减小的曝光点 25-30秒add_exposure(fps * 25 + 1, fps * 30, h, w, mode='down')# 合成图像帧为视频流merge_image_to_video('per_picture', h, w, fps=fps, n=n)

资源(data)

链接:https://pan.baidu.com/s/1EVmuKGFaLhK5wQ-kH94nNQ
提取码:2933

opencv--字幕-眩光实验相关推荐

  1. 【计算机视觉OpenCV基础】实验四 尺寸测量

    实验四 尺寸测量 计算机视觉OpenCV基础实验合辑(实验1234+扩展) 资源下载地址: https://download.csdn.net/download/weixin_53403301 合辑: ...

  2. 视觉SLAM总结——视觉特征子综述

    视觉特征子综述 视觉特征子综述 第一部分:2D特征子 1. Harris 2. SIFT(Scale-invariant feature transform) 3. SURF(Speed up rob ...

  3. 【今日CV 计算机视觉论文速览 第124期】Tue, 4 Jun 2019

    今日CS.CV 计算机视觉论文速览 Tue, 4 Jun 2019 Totally 62 papers ?上期速览✈更多精彩请移步主页 Interesting: ?FE-GAN)于多尺度注意力机制的时 ...

  4. 32位微型计算机原理接口,32位微机原理与接口实验箱

    系统采用模块化.积木式设计,清晰明了,提高了灵活性和适应性 兼容性强,减少设备投资:"主控单元+微机接口通用实验箱+扩展模块"分体式结构设计,只需构买一种微机通用接口实验箱,通过更 ...

  5. eccv 2018 image caption generation论文导读

    全部论文下载连接:链接:https://pan.baidu.com/s/1Di0K1jN7FMVFGsKIAF_ltg 提取码:ifjj "Factual" or "Em ...

  6. stm32mp157和imx6ull比较,开发板选哪个好?一文看懂!

    从开发板学习角度来说,stm32mp157和imx6ull对比,无论是硬件性能还是学习资源,stm32mp157都是非常具备优势的. 一图了解华清远见STM32MP157开发板对比imx6ull开发板 ...

  7. ffmpeg工具的简单使用

    ====================== 分为三部分: 1.ffmpeg介绍,及简单使用 2.ffprobe介绍,及简单使用 3.ffplay介绍,及简单使用 ================== ...

  8. 一种基于神经网络的对话模型

    摘要 对话模型在自然语言理解和机器智能方面是一个非常重要的任务.尽管之前已经有一些方法,但是他们受限于特定的领域(比如:预定机票)而且需要手动指定规则.在这篇文章中,我们针对这个任务展现了一种简单的方 ...

  9. 嵌入式人工智能教学科研平台

    ZN-AI-BC02型 嵌入式人工智能教学科研平台 一.概述 ZN-AI-BC02型 嵌入式人工智能教学科研平台采用RK3399处理器,具有图像硬件加速器与千兆以太网等: 2.嵌入式人工智能教学科研平 ...

最新文章

  1. 【leetcode】654. Maximum Binary Tree
  2. Java 类不可被继承的几种方法
  3. dede自定义表单增加添加时间怎么弄
  4. BCB 串口控件的使用 TComm
  5. 最长重复子串和最长不重复子串求解
  6. springboot学习笔记1
  7. 警告:Establishing SSL connection without server’s identity verification is not recommended
  8. 计步算法 睡眠 心率 学习 PPG传感器(转))
  9. MATLAB学习笔记(注释超详细)
  10. 通过Windows防火墙禁止某程序(或软件)联网
  11. 硬核,这 3 款 IDE 插件让你的代码牢不可破
  12. 怎么点亮段码屏_段码LCD液晶屏参考程序
  13. 深圳赛意信息 怎么样_深圳自动瓶坯检查机怎么样
  14. Linux下的启动oracle服务 启动监听 开放端口操作
  15. codeforces 1367B - Even Array
  16. GIS基础知识-名词解释
  17. sqli-labs Less-38、39、40、41、42、43、44、45(sqli-labs闯关指南 38、39、40、41、42、43、44、45)—堆叠注入
  18. 卡迪夫大数据专业排名_大数据分析:英超大数据!布莱顿vs卡迪夫
  19. 基于PHP学生成绩查询系统设计与实现 开题报告
  20. React自定义组件使用onClick传参注意:onClick只是一个名字而已!

热门文章

  1. 2022沈阳早幼教展览会|沈阳学前教育展会|沈阳幼教产业展会
  2. Linux系统——Nginx反向代理与负载均衡
  3. sql中批量删除数据
  4. BZOJ P1966[Ahoi2005]VIRUS 病毒检测
  5. 【bzoj 1966】: [Ahoi2005]VIRUS 病毒检测
  6. 使用Hammer制作移动端轮播图
  7. 小实验:关于期望的乘法性质
  8. c语言三角形的面积编码,C语言求三角形面积代码知道
  9. Linux(Centos7)服务器中配置Mysql主从数据库,以及数据库的安装,防火墙操作
  10. netty自定义handler分别支持群聊和单聊