本帖最后由 Mr Guo 于 2022-8-8 00:27 编辑
挑战树莓派|基于行空板的延时摄影装置
图 1 延时摄影运行界面
项目起源 上一次旺仔爸爸制作了行空板的开箱测评,受到了广大网友的喜爱,也有不少网友想看一下行空板具体能够做哪些事情,与树莓派相比较有哪些比较擅长的方面。本期分享旺仔爸爸就基于行空板制作一款延时摄影装置。希望能够抛砖引玉,为大家带来一些启发
下面我们先通过视频来看一下是行空板是如何完成延时摄影的。
视频展示 图 2 延时摄影装置演示
首先,我们了解一下什么是延时摄影,延时摄影通常应用在拍摄城市风光、自然风景、天文现象、城市生活、建筑制造、生物演变等题材上。延时摄影也叫缩时摄影,它的原理很简单,类似于定格动画,把多张拍摄间隔时间相同的图片串联起来,合成一个动态的视频,以明显变化的影像展现景物低速变化的过程。譬如花蕾的开放约需 3 天 3 夜,即 72 小时。可以设置每半小时拍摄一张照片,以顺序记录开花动作的细微变化,共计拍摄 144 张照片 , 再将这些照片串联合成视频,按正常频率放映(每秒 24 帧),在 6 秒钟之内 , 展现 3 天 3 夜的开花过程。
了解了延时摄影的原理,现在我们就可以开始制作延时摄影装置了。主要用到的器材有行空板和 USB 摄像头。具体实现方案是由行空板控制摄像头每隔一段时间拍摄一张照片,直到拍摄结束,最后将照片合成视频
方案确定后,我们设计如下思维导图
编程思路 图 3 延时摄影装置程序设计思维导图
准备工作 准备编程环境 上一次的介绍中我们知道行空板支持五种编程方式:Mind+编程、Jupyter编程、Thonny编程、SSH远程访问、VScode编程,本次我们继续使用Mind+编程环境为例进行讲解,如果你还没有安装环境可以先在官方网站提供的百度网盘连接下载对应版本的编程环境
图 4 mind+下载
软件下载并安装完成后,我们需要将行空板与电脑连接,官方文档中介绍的连接方式有很多种,我们采用USB数据线的方式进行测试
行空板连接电脑后,在Mind+软件右上角选择“Python模式”, 点击左下角的 “终端”按钮打开终端。随后,在软件的上方会出现“连接远程终端”的菜单,我们选择行空板的IP地址进行连接
创建文件
行空板与电脑连接成功,点击下图中的“+”号可以创建Python程序,或者点击“三横线”可以创建新文件夹
图 6 在mind+软件中新建文件
根据自己的需求创建一个新的Python程序并为文件取一个名字,双击鼠标即可打开新文件进行程序设计了。
程序编写
导入库 行空板中内置了各种第三方库,本次我们需要使用 OpenCV 库来完成图像采集,使用 pinpong 库控制行空板的屏幕和按键,除此之外还会用到 time 库、 os 库等。以上库的导入方式如下:
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
mso-font-kerning:0pt">import</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"> cv2,time,os<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
mso-font-kerning:0pt">from</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"> pinpong.board </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">import</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> Board<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
mso-font-kerning:0pt">from</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"> pinpong.extension.unihiker </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">import</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> *<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
mso-font-kerning:0pt">from</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"> unihiker </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">import</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> GUI</span></p> 复制代码
下面我们根据思维导图来编写程序,首先设置一个初始化后的界面
设置界面 本次延时摄影装置的界面如下图所示,其中需要用到“文字draw_text”、“按钮add_button”、以及灯带按键A/B按下的指令“ wait_a_click() ”
其中有两个比较关键的时间数据:拍摄间隔和拍摄时长,我们采用“ input ”的方式获取这两个时间数据,放主界面函数运行结束后需要将这两个时间数据传递给拍摄函数,程序如下,其中使用 button 按钮的作用仅仅是为了让文字有边框,增加美观程度
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
mso-font-kerning:0pt">def</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"> window():<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> img_time = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">int</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">input</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">请输入拍摄间隔(单位秒)</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">))<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> video_time = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">int</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">input</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">请输入拍摄时长(单位分钟)</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">))<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> logo=u_gui.draw_text(text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">延时摄影装置</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">,x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">40</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,y=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">0</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,font_size=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">20</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, color=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"#33CCFF"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> u_gui.add_button(x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">60</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, y=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">70</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, w=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">100</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, h=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">30</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">拍摄间隔</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">(</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">秒</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">)"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, origin=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'center'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> u_gui.add_button(x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">60</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, y=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">110</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, w=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">100</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, h=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">30</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">拍摄时长</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">(</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">分</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">)"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, origin=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'center'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> <o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> sec=u_gui.draw_text(text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">str</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(img_time)+</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">秒</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">,x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">160</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,y=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">60</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,font_size=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">12</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, color=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"#FF0000"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> mins=u_gui.draw_text(text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">str</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(video_time)+</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">分</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">,x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">160</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,y=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">100</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,font_size=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">12</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, color=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"#CC33CC"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> u_gui.add_button(x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">120</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, y=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">160</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, w=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">100</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, h=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">30</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">按</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">A</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">键开始</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, origin=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'center'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> u_gui.wait_a_click()<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; text-indent: 31.8pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">take_photo(img_time,video_time)</span></p> 复制代码
拍摄照片 主界面设置完成,当我们按下行空板侧面的 A 键后即可触发拍摄程序。
这里我们需要掌握 OpenCV 库采集图形 的基本指令
调用摄像头的方法: cv2.VideoCapture(0) (这里的数字编号代表 USB 摄像头的序号,比如电脑自带的摄像头编号为 0 ,外接的摄像头编号为 1 )
l 新建拍摄窗口的方法: cv2.na medWindow('Video Cam', cv2.WINDOW_NORMAL),括号中第一项为窗口名称,第二项为窗口尺寸
l 在窗口展示图像的方法:cv2.imshow('Video Cam',frame)括号中第一项为窗口名称,第二项为图像数据
l 图像读取的方法:imread()
l 图形存储的方法:imwrite(path, frame)括号中第一项为保存路径,第二项为图像数据
l 释放内存:release()
l 关闭窗口:destroyAllWindows()
掌握了 OpenCV 库的基本使用方法之后,这里我们还需要学习一种间隔时间采集图像的方法,假设我们设置间隔拍摄的时间变量为“ img_time ” , 首次开始拍摄的时间记录在变量“ last_start_time ”中,然后不断以 time.time() 的方式获取当前时间(单位是秒),如果当前时间减去开始拍摄时的时间超过间隔拍摄的时间“ img_time ”,那么就控制摄像头采集一张图像并保存下来,并将开始拍摄时间变量“ last_start_time ”重新赋值。如此往复即可实现相同间隔时间拍摄。
那么如何才能让拍摄结束呢?这里我们用拍摄时长“video_time”除以间隔时间“img_time”即可得到应采集图像的数量(注意时间单位要统一),当数量达到即终止拍摄。程序如下:
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
mso-font-kerning:0pt">def</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"> take_photo(img_time,video_time):<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> cap = cv2.VideoCapture(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">0</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> cv2.namedWindow(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'Video Cam'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">, cv2.WINDOW_NORMAL) </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">#</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
0pt">创建窗口</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;
mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
0pt">"Video Cam"</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> src_path = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'/root/faM/img/'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> delete(src_path)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> last_start_time = time.time()<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> i=</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#098658;mso-font-kerning:0pt">0</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> num = video_time*</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">60</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">//img_time<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> <o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">while</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt">(cap.isOpened() </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">and</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> ((cv2.waitKey(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">1</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">) & </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#3030C0;mso-font-kerning:0pt">0xFF</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> )!= </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">ord</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'q'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">))):<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> ret,frame = cap.read()<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">if</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> time.time() - last_start_time >= img_time: <o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> path = src_path+</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"{}.jpg"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">.</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">format</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(i)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> cv2.imwrite(path, frame)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> i+=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">1</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> last_start_time = time.time()<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">if</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> ret == </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">True</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> cv2.imshow(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'Video Cam'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,frame)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">else</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">break</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">if</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> i>num:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">break</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> cap.release()<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> cv2.destroyAllWindows()<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; text-indent: 31.8pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">makevideo(src_path)</span></p> 复制代码
程序运行后采集的图像如下图
合成视频
图像采集完成后,接下来就可以将采集到的图像合成视频了
合成视频同样需要用到 OpenCV 库中视频操作的基本方法
l 设置视频写入器:cv2.VideoWriter_fourcc(*'mp4v')(括号内为mp4格式)
l 创建视频写入对象:cv2.VideoWriter(sav_path,fourcc,2,size) 括号第一个参数是保存路径,第二个参数是编码器,第三个参数是帧数,第四个参数是图像尺寸)
l 图像写入视频:videowrite.write(img_array)(通常会将图像放入数组,再以循环的方式将所有图像数据写入视频)
<i><p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
mso-font-kerning:0pt">def</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"> makevideo(src_path):<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt"># 1.</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">每张图像大小</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> size = (</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">320</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">240</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">print</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">每张图片的大小为</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">({},{})"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">.</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">format</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(size[</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">0</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">],size[</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">1</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">]))<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt"># 2.</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">设置源路径与保存路径</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> sav_path = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'/root/faM/video.mp4'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt"># 3.</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">获取图片总的个数</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> all_files = os.listdir(src_path)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> index = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">len</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(all_files)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">print</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">图片总数为</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">:"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> + </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">str</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(index) + </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">张</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt"># 4.</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">设置视频写入器</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> fourcc = cv2.VideoWriter_fourcc(*</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'mp4v'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">#MP4</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
0pt">格式</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;
mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt">#</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">完成写入对象的创建,第一个参数是合成之后的视频的名称,第二个参数是可以使用的编码器,第三个参数是帧率即每秒钟展示多少张图片,第四个参数是图片大小信息</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> videowrite = cv2.VideoWriter(sav_path,fourcc,</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">2</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,size)</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">#2</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
0pt">是每秒的帧数,</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;
mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
0pt">size</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:
Consolas;mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;
mso-font-kerning:0pt">是图片尺寸</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt"># 5.</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">临时存放图片的数组</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> img_array=[]<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt"># 6.</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">读取所有</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">jpg</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
0pt">格式的图片</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;
mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
0pt"> (</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:
Consolas;mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;
mso-font-kerning:0pt">这里图片命名是</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt">0-index.jpg example: 0.jpg 1.jpg ...)</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">for</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt"> filename </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">in</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> [src_path +</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'/'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">+ r</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'{}.jpg'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">.</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">format</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(i) </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">for</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> i </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">in</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">range</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">0</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,index)]:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> img = cv2.imread(filename)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">if</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> img </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">is</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">None</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">print</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(filename + </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">" is error!"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">continue</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> img_array.append(img)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">print</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">"sz"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;mso-font-kerning:0pt">+</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">str</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">len</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(img_array)))<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:green;mso-font-kerning:0pt"># 7.</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">合成视频</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">for</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt"> i </span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;color:blue;mso-font-kerning:0pt">in</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">range</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#098658;mso-font-kerning:0pt">0</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt">,index):<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> img_array[i] = cv2.resize(img_array[i],(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">320</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#098658;mso-font-kerning:0pt">240</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">))<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> videowrite.write(img_array[i])<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">print</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
mso-font-kerning:0pt">第</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:#A31515;mso-font-kerning:0pt">{}</span><span style="font-size:14.5pt;
font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">张图片合成成功</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">.</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">format</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(i))</span></p></i> 复制代码
程序运行后合成视频如下
图 24 合成视频
删除文件 在进行下一次的延时摄影时,需要将之前的图像数据删除,删除的方法如下
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
mso-font-kerning:0pt">def</span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
mso-font-kerning:0pt"> delete(src_path):<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> all_files = os.listdir(src_path)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> index = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">len</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(all_files)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">for</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt"> i </span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;color:blue;mso-font-kerning:0pt">in</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
color:blue;mso-font-kerning:0pt">range</span><span lang="EN-US" style="font-size:
14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
宋体;mso-font-kerning:0pt">(index):<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left; margin-bottom: 0.0001pt; line-height: 19.5pt; background: rgb(255, 255, 254);"><span lang="EN-US" style="font-size:14.5pt;font-family:
Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
0pt"> os.remove(src_path+</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"{}.jpg"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">.</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
mso-bidi-font-family:宋体;color:blue;mso-font-kerning:0pt">format</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(i))</span></p> 复制代码
完整程序 <div style="background-color: rgb(255, 255, 254); line-height: 26px;"><div style="color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre; text-align: left;"><span style="color: #0000ff;">import</span> cv2,time,os</div><div style="color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre; text-align: left;"><span style="color: #0000ff;">from</span> pinpong.board <span style="color: #0000ff;">import</span> Board</div><div style="color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre; text-align: left;"><span style="color: #0000ff;">from</span> pinpong.extension.unihiker <span style="color: #0000ff;">import</span> *</div><div style="color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre; text-align: left;"><span style="color: #0000ff;">from</span> unihiker <span style="color: #0000ff;">import</span> GUI</div><div style="text-align: left;"><font color="#000000" face="Consolas, Courier New, monospace"><span style="font-size: 19px; white-space: pre;">
</span></font></div><div style="text-align: left;">
</div><div style="text-align: left;">
</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"><span style="color: #0000ff;">def</span> window():</div><div style="text-align: left;">
</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> img_time = <span style="color: #0000ff;">int</span>(<span style="color: #0000ff;">input</span>(<span style="color: #a31515;">"请输入拍摄间隔(单位秒)"</span>))</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> video_time = <span style="color: #0000ff;">int</span>(<span style="color: #0000ff;">input</span>(<span style="color: #a31515;">"请输入拍摄时长(单位分钟)"</span>))</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> logo=u_gui.draw_text(text=<span style="color: #a31515;">"延时摄影装置"</span>,x=<span style="color: #098658;">40</span>,y=<span style="color: #098658;">0</span>,font_size=<span style="color: #098658;">20</span>, color=<span style="color: #a31515;">"#33CCFF"</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> u_gui.add_button(x=<span style="color: #098658;">60</span>, y=<span style="color: #098658;">70</span>, w=<span style="color: #098658;">100</span>, h=<span style="color: #098658;">30</span>, text=<span style="color: #a31515;">"拍摄间隔(秒)"</span>, origin=<span style="color: #a31515;">'center'</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> u_gui.add_button(x=<span style="color: #098658;">60</span>, y=<span style="color: #098658;">110</span>, w=<span style="color: #098658;">100</span>, h=<span style="color: #098658;">30</span>, text=<span style="color: #a31515;">"拍摄时长(分)"</span>, origin=<span style="color: #a31515;">'center'</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> </div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> sec=u_gui.draw_text(text=<span style="color: #0000ff;">str</span>(img_time)+<span style="color: #a31515;">"秒"</span>,x=<span style="color: #098658;">160</span>,y=<span style="color: #098658;">60</span>,font_size=<span style="color: #098658;">12</span>, color=<span style="color: #a31515;">"#FF0000"</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> mins=u_gui.draw_text(text=<span style="color: #0000ff;">str</span>(video_time)+<span style="color: #a31515;">"分"</span>,x=<span style="color: #098658;">160</span>,y=<span style="color: #098658;">100</span>,font_size=<span style="color: #098658;">12</span>, color=<span style="color: #a31515;">"#CC33CC"</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> u_gui.add_button(x=<span style="color: #098658;">120</span>, y=<span style="color: #098658;">160</span>, w=<span style="color: #098658;">100</span>, h=<span style="color: #098658;">30</span>, text=<span style="color: #a31515;">"按A键开始"</span>, origin=<span style="color: #a31515;">'center'</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> u_gui.wait_a_click()</div><div style="text-align: left;">
</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> take_photo(img_time,video_time)</div><div style="text-align: left;">
</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"><span style="color: #0000ff;">def</span> delete(src_path):</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> all_files = os.listdir(src_path)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> index = <span style="color: #0000ff;">len</span>(all_files)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> <span style="color: #0000ff;">range</span>(index):</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> os.remove(src_path+<span style="color: #a31515;">"{}.jpg"</span>.<span style="color: #0000ff;">format</span>(i))</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"><span style="color: #0000ff;">def</span> take_photo(img_time,video_time):</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> cap = cv2.VideoCapture(<span style="color: #098658;">0</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> cv2.namedWindow(<span style="color: #a31515;">'Video Cam'</span>, cv2.WINDOW_NORMAL) <span style="color: #008000;">#创建窗口"Video Cam"</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> src_path = <span style="color: #a31515;">'/root/faM/img/'</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> delete(src_path)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> last_start_time = time.time()</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> i=<span style="color: #098658;">0</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> num = video_time*<span style="color: #098658;">60</span>//img_time</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;">#cap.isOpened() and ((cv2.waitKey(1) & 0xFF )!= ord('q'))</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">while</span>(cap.isOpened() <span style="color: #0000ff;">and</span> ((cv2.waitKey(<span style="color: #098658;">1</span>) & <span style="color: #3030c0;">0xFF</span> )!= <span style="color: #0000ff;">ord</span>(<span style="color: #a31515;">'q'</span>))):</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> ret,frame = cap.read()</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">if</span> time.time() - last_start_time >= img_time: </div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> path = src_path+<span style="color: #a31515;">"{}.jpg"</span>.<span style="color: #0000ff;">format</span>(i)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> cv2.imwrite(path, frame)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> i+=<span style="color: #098658;">1</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> last_start_time = time.time()</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">if</span> ret == <span style="color: #0000ff;">True</span>:</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> cv2.imshow(<span style="color: #a31515;">'Video Cam'</span>,frame)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">else</span>:</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">break</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">if</span> i>num:</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">break</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> cap.release()</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> cv2.destroyAllWindows()</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> makevideo(src_path)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"><span style="color: #0000ff;">def</span> makevideo(src_path):</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;"># 1.每张图像大小</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> size = (<span style="color: #098658;">320</span>,<span style="color: #098658;">240</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">print</span>(<span style="color: #a31515;">"每张图片的大小为({},{})"</span>.<span style="color: #0000ff;">format</span>(size[<span style="color: #098658;">0</span>],size[<span style="color: #098658;">1</span>]))</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;"># 2.设置源路径与保存路径</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> sav_path = <span style="color: #a31515;">'/root/faM/video.mp4'</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;"># 3.获取图片总的个数</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> all_files = os.listdir(src_path)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> index = <span style="color: #0000ff;">len</span>(all_files)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">print</span>(<span style="color: #a31515;">"图片总数为:"</span> + <span style="color: #0000ff;">str</span>(index) + <span style="color: #a31515;">"张"</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;"># 4.设置视频写入器</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> fourcc = cv2.VideoWriter_fourcc(*<span style="color: #a31515;">'mp4v'</span>)<span style="color: #008000;">#MP4格式</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;">#完成写入对象的创建,第一个参数是合成之后的视频的名称,第二个参数是可以使用的编码器,第三个参数是帧率即每秒钟展示多少张图片,第四个参数是图片大小信息</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> videowrite = cv2.VideoWriter(sav_path,fourcc,<span style="color: #098658;">2</span>,size)<span style="color: #008000;">#2是每秒的帧数,size是图片尺寸</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;"># 5.临时存放图片的数组</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> img_array=[]</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;"># 6.读取所有jpg格式的图片 (这里图片命名是0-index.jpg example: 0.jpg 1.jpg ...)</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">for</span> filename <span style="color: #0000ff;">in</span> [src_path +<span style="color: #a31515;">'/'</span>+ r<span style="color: #a31515;">'{}.jpg'</span>.<span style="color: #0000ff;">format</span>(i) <span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> <span style="color: #0000ff;">range</span>(<span style="color: #098658;">0</span>,index)]:</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> img = cv2.imread(filename)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">if</span> img <span style="color: #0000ff;">is</span> <span style="color: #0000ff;">None</span>:</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">print</span>(filename + <span style="color: #a31515;">" is error!"</span>)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">continue</span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> img_array.append(img)</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">print</span>(<span style="color: #a31515;">"sz"</span>+<span style="color: #0000ff;">str</span>(<span style="color: #0000ff;">len</span>(img_array)))</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #008000;"># 7.合成视频 </span></div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> <span style="color: #0000ff;">range</span>(<span style="color: #098658;">0</span>,index):</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> img_array[i] = cv2.resize(img_array[i],(<span style="color: #098658;">320</span>,<span style="color: #098658;">240</span>))</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> videowrite.write(img_array[i])</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> <span style="color: #0000ff;">print</span>(<span style="color: #a31515;">'第{}张图片合成成功'</span>.<span style="color: #0000ff;">format</span>(i))</div><div style="text-align: left;">
</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"><span style="color: #0000ff;">if</span> <span style="color: #0000ff;">__name__</span> == <span style="color: #a31515;">'__main__'</span>:</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> u_gui=GUI()</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> Board().begin()</div><div style="text-align: left; color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> window()</div>
<div style="color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 19px; white-space: pre;"> </div></div> 复制代码
import cv2,time,os
from pinpong.board import Board
from pinpong.extension.unihiker import *
from unihiker import GUI
def window():
img_time = int (input ("请输入拍摄间隔(单位秒)" ))
video_time = int (input ("请输入拍摄时长(单位分钟)" ))
logo=u_gui.draw_text(text="延时摄影装置" ,x=40 ,y=0 ,font_size=20 , color="#33CCFF" )
u_gui.add_button(x=60 , y=70 , w=100 , h=30 , text="拍摄间隔(秒)" , origin='center' )
u_gui.add_button(x=60 , y=110 , w=100 , h=30 , text="拍摄时长(分)" , origin='center' )
sec=u_gui.draw_text(text=str (img_time)+"秒" ,x=160 ,y=60 ,font_size=12 , color="#FF0000" )
mins=u_gui.draw_text(text=str (video_time)+"分" ,x=160 ,y=100 ,font_size=12 , color="#CC33CC" )
u_gui.add_button(x=120 , y=160 , w=100 , h=30 , text="按A键开始" , origin='center' )
u_gui.wait_a_click()
take_photo(img_time,video_time)
def delete(src_path):
all_files = os.listdir(src_path)
index = len (all_files)
for i in range (index):
os.remove(src_path+"{}.jpg" .format (i))
def take_photo(img_time,video_time):
cap = cv2.VideoCapture(0 )
cv2.namedWindow('Video Cam' , cv2.WINDOW_NORMAL) #创建窗口"Video Cam"
src_path = '/root/faM/img/'
delete(src_path)
last_start_time = time.time()
i=0
num = video_time*60 //img_time
#cap.isOpened() and ((cv2.waitKey(1) & 0xFF )!= ord('q'))
while (cap.isOpened() and ((cv2.waitKey(1 ) & 0xFF )!= ord ('q' ))):
ret,frame = cap.read()
if time.time() - last_start_time >= img_time:
path = src_path+"{}.jpg" .format (i)
cv2.imwrite(path, frame)
i+=1
last_start_time = time.time()
if ret == True :
cv2.imshow('Video Cam' ,frame)
else :
break
if i>num:
break
cap.release()
cv2.destroyAllWindows()
makevideo(src_path)
def makevideo(src_path):
# 1.每张图像大小
size = (320 ,240 )
print ("每张图片的大小为({},{})" .format (size[0 ],size[1 ]))
# 2.设置源路径与保存路径
sav_path = '/root/faM/video.mp4'
# 3.获取图片总的个数
all_files = os.listdir(src_path)
index = len (all_files)
print ("图片总数为:" + str (index) + "张" )
# 4.设置视频写入器
fourcc = cv2.VideoWriter_fourcc(*'mp4v' )#MP4格式
#完成写入对象的创建,第一个参数是合成之后的视频的名称,第二个参数是可以使用的编码器,第三个参数是帧率即每秒钟展示多少张图片,第四个参数是图片大小信息
videowrite = cv2.VideoWriter(sav_path,fourcc,2 ,size)#2是每秒的帧数,size是图片尺寸
# 5.临时存放图片的数组
img_array=[]
# 6.读取所有jpg格式的图片 (这里图片命名是0-index.jpg example: 0.jpg 1.jpg ...)
for filename in [src_path +'/' + r'{}.jpg' .format (i) for i in range (0 ,index)]:
img = cv2.imread(filename)
if img is None :
print (filename + " is error!" )
continue
img_array.append(img)
print ("sz" +str (len (img_array)))
# 7.合成视频
for i in range (0 ,index):
img_array = cv2.resize(img_array,(320 ,240 ))
videowrite.write(img_array)
print ('第{}张图片合成成功' .format (i))
if __name__ == '__main__' :
u_gui=GUI()
Board().begin()
window()
总结 至此,我们延时摄影装置的程序就全部设计完成了,在本次的案例中我们主要使用了OpenCV库的一些基础用法完成了延时摄影的任务,回头审视整个过程,由于行空板出厂自带了OpenCV等第三方库,相比与树莓派省去了很多配置的麻烦,这样可以专注于程序设计本身,大大提高了效率。 并且行空板板载了按键和屏幕,在实现人机交互方面也便捷了不少。假如再增加一些特殊的环境感知传感器的话还可以做更加丰富有趣的项目,比如检测植物生长过程的环境数据,再结合摄像头拍摄延时摄影的数据。
接下来旺仔爸爸还会分享一些类似的实用案例,一起期待吧