2268浏览
查看: 2268|回复: 1

[教程] 挑战树莓派|基于行空板的延时摄影装置

[复制链接]
本帖最后由 Mr Guo 于 2022-8-8 00:27 编辑

挑战树莓派|基于行空板的延时摄影装置
IMG_20220801_002508.jpg
图 1 延时摄影运行界面

项目起源
上一次旺仔爸爸制作了行空板的开箱测评,受到了广大网友的喜爱,也有不少网友想看一下行空板具体能够做哪些事情,与树莓派相比较有哪些比较擅长的方面。本期分享旺仔爸爸就基于行空板制作一款延时摄影装置。希望能够抛砖引玉,为大家带来一些启发

下面我们先通过视频来看一下是行空板是如何完成延时摄影的。
视频展示
图 2 延时摄影装置演示

方案介绍

首先,我们了解一下什么是延时摄影,延时摄影通常应用在拍摄城市风光、自然风景、天文现象、城市生活、建筑制造、生物演变等题材上。延时摄影也叫缩时摄影,它的原理很简单,类似于定格动画,把多张拍摄间隔时间相同的图片串联起来,合成一个动态的视频,以明显变化的影像展现景物低速变化的过程。譬如花蕾的开放约需33夜,即72小时。可以设置每半小时拍摄一张照片,以顺序记录开花动作的细微变化,共计拍摄144张照片,再将这些照片串联合成视频,按正常频率放映(每秒24帧),在6秒钟之内,展现33夜的开花过程。
了解了延时摄影的原理,现在我们就可以开始制作延时摄影装置了。主要用到的器材有行空板和USB摄像头。具体实现方案是由行空板控制摄像头每隔一段时间拍摄一张照片,直到拍摄结束,最后将照片合成视频

程序设计
方案确定后,我们设计如下思维导图
编程思路
2022-07-31_182307.png
图 3 延时摄影装置程序设计思维导图


准备工作
准备编程环境
上一次的介绍中我们知道行空板支持五种编程方式:Mind+编程、Jupyter编程、Thonny编程、SSH远程访问、VScode编程,本次我们继续使用Mind+编程环境为例进行讲解,如果你还没有安装环境可以先在官方网站提供的百度网盘连接下载对应版本的编程环境

2022-04-22_010116.jpg

图 4 mind+下载

软件下载并安装完成后,我们需要将行空板与电脑连接,官方文档中介绍的连接方式有很多种,我们采用USB数据线的方式进行测试
行空板连接电脑后,在Mind+软件右上角选择“Python模式”,点击左下角的“终端”按钮打开终端。随后,在软件的上方会出现“连接远程终端”的菜单,我们选择行空板的IP地址进行连接
2022-07-31_183452.jpg

创建文件
行空板与电脑连接成功,点击下图中的“+”号可以创建Python程序,或者点击“三横线”可以创建新文件夹
2022-07-31_183655.png
图 6 在mind+软件中新建文件
根据自己的需求创建一个新的Python程序并为文件取一个名字,双击鼠标即可打开新文件进行程序设计了。


程序编写
导入库
行空板中内置了各种第三方库,本次我们需要使用OpenCV库来完成图像采集,使用pinpong库控制行空板的屏幕和按键,除此之外还会用到time库、os库等。以上库的导入方式如下:
  1. <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:
  2. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
  3. mso-font-kerning:0pt">import</span><span lang="EN-US" style="font-size:14.5pt;
  4. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  5. mso-font-kerning:0pt"> cv2,time,os<o:p></o:p></span></p>
  6. <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:
  7. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
  8. mso-font-kerning:0pt">from</span><span lang="EN-US" style="font-size:14.5pt;
  9. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  10. mso-font-kerning:0pt"> pinpong.board </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  11. 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:
  12. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> Board<o:p></o:p></span></p>
  13. <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:
  14. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
  15. mso-font-kerning:0pt">from</span><span lang="EN-US" style="font-size:14.5pt;
  16. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  17. mso-font-kerning:0pt"> pinpong.extension.unihiker </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  18. 宋体;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:
  19. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> *<o:p></o:p></span></p>
  20. <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:
  21. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
  22. mso-font-kerning:0pt">from</span><span lang="EN-US" style="font-size:14.5pt;
  23. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  24. mso-font-kerning:0pt"> unihiker </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  25. 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:
  26. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> GUI</span></p>
复制代码

下面我们根据思维导图来编写程序,首先设置一个初始化后的界面
设置界面
本次延时摄影装置的界面如下图所示,其中需要用到“文字draw_text”、“按钮add_button”、以及灯带按键A/B按下的指令“wait_a_click()
2022-08-01_002338.png
其中有两个比较关键的时间数据:拍摄间隔和拍摄时长,我们采用“input”的方式获取这两个时间数据,放主界面函数运行结束后需要将这两个时间数据传递给拍摄函数,程序如下,其中使用button按钮的作用仅仅是为了让文字有边框,增加美观程度
  1. <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:
  2. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
  3. mso-font-kerning:0pt">def</span><span lang="EN-US" style="font-size:14.5pt;
  4. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  5. mso-font-kerning:0pt"> window():<o:p></o:p></span></p>
  6. <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:
  7. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  8. 0pt"><o:p> </o:p></span></p>
  9. <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:
  10. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  11. 0pt">    img_time = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  12. 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:
  13. 宋体;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:宋体;
  14. 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:
  15. 宋体;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:宋体;
  16. mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  17. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  18. mso-font-kerning:0pt">请输入拍摄间隔(单位秒)</span><span lang="EN-US" style="font-size:
  19. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  20. 宋体;color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  21. mso-bidi-font-family:宋体;mso-font-kerning:0pt">))<o:p></o:p></span></p>
  22. <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:
  23. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  24. 0pt">    video_time = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  25. 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:
  26. 宋体;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:宋体;
  27. 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:
  28. 宋体;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:宋体;
  29. mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  30. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  31. mso-font-kerning:0pt">请输入拍摄时长(单位分钟)</span><span lang="EN-US" style="font-size:
  32. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  33. 宋体;color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  34. mso-bidi-font-family:宋体;mso-font-kerning:0pt">))<o:p></o:p></span></p>
  35. <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:
  36. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  37. 0pt">    logo=u_gui.draw_text(text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  38. mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  39. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  40. mso-font-kerning:0pt">延时摄影装置</span><span lang="EN-US" style="font-size:14.5pt;
  41. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  42. color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  43. 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:宋体;
  44. 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:
  45. 宋体;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:宋体;
  46. 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:
  47. 宋体;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:
  48. 宋体;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:
  49. 宋体;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:
  50. 宋体;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:
  51. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  52. <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:
  53. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  54. 0pt">    u_gui.add_button(x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  55. 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:
  56. 宋体;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:
  57. 宋体;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:
  58. 宋体;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:
  59. 宋体;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:
  60. 宋体;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:
  61. 宋体;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:
  62. 宋体;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:
  63. 宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  64. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  65. mso-font-kerning:0pt">拍摄间隔</span><span lang="EN-US" style="font-size:14.5pt;
  66. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  67. color:#A31515;mso-font-kerning:0pt">(</span><span style="font-size:14.5pt;
  68. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  69. 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:
  70. 宋体;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:
  71. 宋体;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:
  72. 宋体;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:
  73. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  74. <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:
  75. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  76. 0pt">    u_gui.add_button(x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  77. 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:
  78. 宋体;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:
  79. 宋体;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:
  80. 宋体;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:
  81. 宋体;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:
  82. 宋体;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:
  83. 宋体;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:
  84. 宋体;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:
  85. 宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  86. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  87. mso-font-kerning:0pt">拍摄时长</span><span lang="EN-US" style="font-size:14.5pt;
  88. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  89. color:#A31515;mso-font-kerning:0pt">(</span><span style="font-size:14.5pt;
  90. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  91. 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:
  92. 宋体;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:
  93. 宋体;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:
  94. 宋体;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:
  95. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  96. <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:
  97. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  98. 0pt">    <o:p></o:p></span></p>
  99. <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:
  100. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  101. 0pt">    sec=u_gui.draw_text(text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  102. 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:
  103. 宋体;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:
  104. 宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  105. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  106. mso-font-kerning:0pt">秒</span><span lang="EN-US" style="font-size:14.5pt;
  107. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  108. color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  109. 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:宋体;
  110. 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:
  111. 宋体;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:宋体;
  112. 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:
  113. 宋体;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:
  114. 宋体;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:
  115. 宋体;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:
  116. 宋体;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:
  117. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  118. <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:
  119. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  120. 0pt">    mins=u_gui.draw_text(text=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  121. 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:
  122. 宋体;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:
  123. 宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  124. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  125. mso-font-kerning:0pt">分</span><span lang="EN-US" style="font-size:14.5pt;
  126. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  127. color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  128. 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:宋体;
  129. 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:
  130. 宋体;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:宋体;
  131. 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:
  132. 宋体;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:
  133. 宋体;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:
  134. 宋体;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:
  135. 宋体;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:
  136. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  137. <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:
  138. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  139. 0pt">    u_gui.add_button(x=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  140. 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:
  141. 宋体;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:
  142. 宋体;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:
  143. 宋体;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:
  144. 宋体;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:
  145. 宋体;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:
  146. 宋体;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:
  147. 宋体;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:
  148. 宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  149. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  150. mso-font-kerning:0pt">按</span><span lang="EN-US" style="font-size:14.5pt;
  151. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  152. color:#A31515;mso-font-kerning:0pt">A</span><span style="font-size:14.5pt;
  153. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  154. 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:
  155. 宋体;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:
  156. 宋体;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:
  157. 宋体;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:
  158. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  159. <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:
  160. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  161. 0pt">    u_gui.wait_a_click()<o:p></o:p></span></p>
  162. <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:
  163. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  164. 0pt"><o:p> </o:p></span></p>
  165. <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:宋体;
  166. 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.namedWindow('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”即可得到应采集图像的数量(注意时间单位要统一),当数量达到即终止拍摄。程序如下:
  1. <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:
  2. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
  3. mso-font-kerning:0pt">def</span><span lang="EN-US" style="font-size:14.5pt;
  4. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  5. mso-font-kerning:0pt"> take_photo(img_time,video_time):<o:p></o:p></span></p>
  6. <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:
  7. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  8. 0pt">    cap = cv2.VideoCapture(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  9. 宋体;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:
  10. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  11. <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:
  12. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  13. 0pt">    cv2.namedWindow(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  14. 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:
  15. 宋体;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:
  16. 宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:0pt">#</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  17. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
  18. 0pt">创建窗口</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;
  19. mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
  20. 0pt">"Video Cam"</span><span lang="EN-US" style="font-size:14.5pt;
  21. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  22. mso-font-kerning:0pt"><o:p></o:p></span></p>
  23. <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:
  24. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  25. 0pt">    src_path = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  26. 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:
  27. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  28. <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:
  29. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  30. 0pt">    delete(src_path)<o:p></o:p></span></p>
  31. <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:
  32. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  33. 0pt">    last_start_time = time.time()<o:p></o:p></span></p>
  34. <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:
  35. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  36. 0pt">    i=</span><span lang="EN-US" style="font-size:14.5pt;
  37. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  38. color:#098658;mso-font-kerning:0pt">0</span><span lang="EN-US" style="font-size:
  39. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  40. 宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  41. <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:
  42. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  43. 0pt">    num = video_time*</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  44. 宋体;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:
  45. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">//img_time<o:p></o:p></span></p>
  46. <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:
  47. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  48. 0pt">  <o:p></o:p></span></p>
  49. <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:
  50. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  51. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  52. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  53. color:blue;mso-font-kerning:0pt">while</span><span lang="EN-US" style="font-size:
  54. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  55. 宋体;mso-font-kerning:0pt">(cap.isOpened() </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  56. 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:
  57. 宋体;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:
  58. 宋体;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:
  59. 宋体;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:
  60. 宋体;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:
  61. 宋体;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:
  62. 宋体;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:
  63. 宋体;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:宋体;
  64. 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:
  65. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">))):<o:p></o:p></span></p>
  66. <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:
  67. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  68. 0pt">        ret,frame = cap.read()<o:p></o:p></span></p>
  69. <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:
  70. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  71. 0pt">        </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  72. 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:
  73. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> time.time() - last_start_time >= img_time:  <o:p></o:p></span></p>
  74. <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:
  75. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  76. 0pt">            path = src_path+</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  77. 宋体;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:
  78. 宋体;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:宋体;
  79. 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:
  80. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(i)<o:p></o:p></span></p>
  81. <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:
  82. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  83. 0pt">            cv2.imwrite(path, frame)<o:p></o:p></span></p>
  84. <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:
  85. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  86. 0pt">            i+=</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  87. 宋体;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:
  88. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  89. <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:
  90. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  91. 0pt">            last_start_time = time.time()<o:p></o:p></span></p>
  92. <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:
  93. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  94. 0pt">        </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  95. 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:
  96. 宋体;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:
  97. 宋体;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:
  98. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">:<o:p></o:p></span></p>
  99. <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:
  100. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  101. 0pt">            cv2.imshow(</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  102. 宋体;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:
  103. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,frame)<o:p></o:p></span></p>
  104. <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:
  105. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  106. 0pt">        </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  107. 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:
  108. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">:<o:p></o:p></span></p>
  109. <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:
  110. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  111. 0pt">            </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  112. 宋体;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:
  113. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  114. <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:
  115. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  116. 0pt">        </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  117. 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:
  118. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"> i>num:<o:p></o:p></span></p>
  119. <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:
  120. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  121. 0pt">            </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  122. 宋体;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:
  123. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  124. <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:
  125. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  126. 0pt">    cap.release()<o:p></o:p></span></p>
  127. <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:
  128. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  129. 0pt">    cv2.destroyAllWindows()<o:p></o:p></span></p>
  130. <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:宋体;
  131. mso-bidi-font-family:宋体;mso-font-kerning:0pt">makevideo(src_path)</span></p>
复制代码

程序运行后采集的图像如下图
2022-08-01_012212.png
合成视频
图像采集完成后,接下来就可以将采集到的图像合成视频了
合成视频同样需要用到OpenCV库中视频操作的基本方法      
l  设置视频写入器:cv2.VideoWriter_fourcc(*'mp4v')(括号内为mp4格式)
l  创建视频写入对象:cv2.VideoWriter(sav_path,fourcc,2,size) 括号第一个参数是保存路径,第二个参数是编码器,第三个参数是帧数,第四个参数是图像尺寸)
l  图像写入视频:videowrite.write(img_array)(通常会将图像放入数组,再以循环的方式将所有图像数据写入视频)

  1. <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:
  2. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
  3. mso-font-kerning:0pt">def</span><span lang="EN-US" style="font-size:14.5pt;
  4. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  5. mso-font-kerning:0pt"> makevideo(src_path):<o:p></o:p></span></p>
  6. <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:
  7. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  8. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  9. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  10. color:green;mso-font-kerning:0pt"># 1.</span><span style="font-size:14.5pt;
  11. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  12. 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:
  13. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  14. <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:
  15. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  16. 0pt">    size = (</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  17. 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:
  18. 宋体;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:宋体;
  19. 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:
  20. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  21. <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:
  22. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  23. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  24. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  25. color:blue;mso-font-kerning:0pt">print</span><span lang="EN-US" style="font-size:
  26. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  27. 宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;
  28. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  29. color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;
  30. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  31. 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:
  32. 宋体;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:
  33. 宋体;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:宋体;
  34. 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:
  35. 宋体;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:宋体;
  36. 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:
  37. 宋体;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:
  38. 宋体;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:
  39. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">]))<o:p></o:p></span></p>
  40. <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:
  41. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  42. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  43. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  44. color:green;mso-font-kerning:0pt"># 2.</span><span style="font-size:14.5pt;
  45. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  46. 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:
  47. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  48. <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:
  49. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  50. 0pt">    sav_path = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  51. 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:
  52. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  53. <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:
  54. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  55. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  56. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  57. color:green;mso-font-kerning:0pt"># 3.</span><span style="font-size:14.5pt;
  58. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  59. 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:
  60. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  61. <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:
  62. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  63. 0pt">    all_files = os.listdir(src_path)<o:p></o:p></span></p>
  64. <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:
  65. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  66. 0pt">    index = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  67. 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:
  68. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(all_files)<o:p></o:p></span></p>
  69. <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:
  70. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  71. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  72. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  73. color:blue;mso-font-kerning:0pt">print</span><span lang="EN-US" style="font-size:
  74. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  75. 宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;
  76. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  77. color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;
  78. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  79. 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:
  80. 宋体;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:
  81. 宋体;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:
  82. 宋体;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:
  83. 宋体;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:
  84. 宋体;mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">"</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  85. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  86. mso-font-kerning:0pt">张</span><span lang="EN-US" style="font-size:14.5pt;
  87. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  88. color:#A31515;mso-font-kerning:0pt">"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  89. mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  90. <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:
  91. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  92. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  93. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  94. color:green;mso-font-kerning:0pt"># 4.</span><span style="font-size:14.5pt;
  95. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  96. 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:
  97. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  98. <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:
  99. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  100. 0pt">    fourcc = cv2.VideoWriter_fourcc(*</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  101. 宋体;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:
  102. 宋体;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:宋体;
  103. 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;
  104. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
  105. 0pt">格式</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;
  106. mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  107. <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:
  108. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  109. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  110. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  111. color:green;mso-font-kerning:0pt">#</span><span style="font-size:14.5pt;
  112. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  113. 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:
  114. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  115. <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:
  116. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  117. 0pt">    videowrite = cv2.VideoWriter(sav_path,fourcc,</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  118. 宋体;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:
  119. 宋体;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:宋体;
  120. 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;
  121. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
  122. 0pt">是每秒的帧数,</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;
  123. mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
  124. 0pt">size</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:
  125. Consolas;mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;
  126. mso-font-kerning:0pt">是图片尺寸</span><span lang="EN-US" style="font-size:14.5pt;
  127. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  128. mso-font-kerning:0pt"><o:p></o:p></span></p>
  129. <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:
  130. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  131. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  132. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  133. color:green;mso-font-kerning:0pt"># 5.</span><span style="font-size:14.5pt;
  134. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  135. 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:
  136. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  137. <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:
  138. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  139. 0pt">    img_array=[]<o:p></o:p></span></p>
  140. <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:
  141. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  142. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  143. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  144. color:green;mso-font-kerning:0pt"># 6.</span><span style="font-size:14.5pt;
  145. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  146. 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:
  147. 宋体;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;
  148. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
  149. 0pt">格式的图片</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;
  150. mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:green;mso-font-kerning:
  151. 0pt"> (</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:
  152. Consolas;mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:green;
  153. mso-font-kerning:0pt">这里图片命名是</span><span lang="EN-US" style="font-size:14.5pt;
  154. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  155. 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:
  156. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  157. <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:
  158. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  159. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  160. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  161. color:blue;mso-font-kerning:0pt">for</span><span lang="EN-US" style="font-size:
  162. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  163. 宋体;mso-font-kerning:0pt"> filename </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  164. 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:
  165. 宋体;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:
  166. 宋体;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:
  167. 宋体;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:
  168. 宋体;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:
  169. 宋体;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:宋体;
  170. 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:
  171. 宋体;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:
  172. 宋体;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:
  173. 宋体;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:
  174. 宋体;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:
  175. 宋体;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:宋体;
  176. 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:
  177. 宋体;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:宋体;
  178. 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:
  179. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">,index)]:<o:p></o:p></span></p>
  180. <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:
  181. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  182. 0pt">        img = cv2.imread(filename)<o:p></o:p></span></p>
  183. <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:
  184. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  185. 0pt">        </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  186. 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:
  187. 宋体;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:
  188. 宋体;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:
  189. 宋体;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:宋体;
  190. 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:
  191. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">:<o:p></o:p></span></p>
  192. <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:
  193. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  194. 0pt">            </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  195. 宋体;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:
  196. 宋体;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:
  197. 宋体;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:
  198. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">)<o:p></o:p></span></p>
  199. <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:
  200. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  201. 0pt">            </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  202. 宋体;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:
  203. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  204. <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:
  205. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  206. 0pt">        img_array.append(img)<o:p></o:p></span></p>
  207. <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:
  208. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  209. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  210. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  211. color:blue;mso-font-kerning:0pt">print</span><span lang="EN-US" style="font-size:
  212. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  213. 宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;
  214. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  215. color:#A31515;mso-font-kerning:0pt">"sz"</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  216. 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:宋体;
  217. 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:
  218. 宋体;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:宋体;
  219. 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:
  220. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(img_array)))<o:p></o:p></span></p>
  221. <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:
  222. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  223. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  224. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  225. color:green;mso-font-kerning:0pt"># 7.</span><span style="font-size:14.5pt;
  226. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  227. 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:
  228. 宋体;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:
  229. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt"><o:p></o:p></span></p>
  230. <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:
  231. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  232. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  233. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  234. color:blue;mso-font-kerning:0pt">for</span><span lang="EN-US" style="font-size:
  235. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  236. 宋体;mso-font-kerning:0pt"> i </span><span lang="EN-US" style="font-size:
  237. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  238. 宋体;color:blue;mso-font-kerning:0pt">in</span><span lang="EN-US" style="font-size:
  239. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  240. 宋体;mso-font-kerning:0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
  241. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  242. color:blue;mso-font-kerning:0pt">range</span><span lang="EN-US" style="font-size:
  243. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  244. 宋体;mso-font-kerning:0pt">(</span><span lang="EN-US" style="font-size:14.5pt;
  245. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  246. color:#098658;mso-font-kerning:0pt">0</span><span lang="EN-US" style="font-size:
  247. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  248. 宋体;mso-font-kerning:0pt">,index):<o:p></o:p></span></p>
  249. <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:
  250. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  251. 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:
  252. 宋体;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:
  253. 宋体;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:宋体;
  254. 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:
  255. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">))<o:p></o:p></span></p>
  256. <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:
  257. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  258. 0pt">        videowrite.write(img_array[i])<o:p></o:p></span></p>
  259. <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:
  260. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  261. 0pt">        </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  262. 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:
  263. 宋体;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:宋体;
  264. mso-bidi-font-family:宋体;color:#A31515;mso-font-kerning:0pt">'</span><span style="font-size:14.5pt;font-family:宋体;mso-ascii-font-family:Consolas;
  265. mso-hansi-font-family:Consolas;mso-bidi-font-family:宋体;color:#A31515;
  266. mso-font-kerning:0pt">第</span><span lang="EN-US" style="font-size:14.5pt;
  267. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  268. color:#A31515;mso-font-kerning:0pt">{}</span><span style="font-size:14.5pt;
  269. font-family:宋体;mso-ascii-font-family:Consolas;mso-hansi-font-family:Consolas;
  270. 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:
  271. 宋体;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:
  272. 宋体;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:宋体;
  273. 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:
  274. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(i))</span></p></i>
复制代码


程序运行后合成视频如下
2022-08-01_012236.png

图 24 合成视频
删除文件
在进行下一次的延时摄影时,需要将之前的图像数据删除,删除的方法如下
  1. <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:
  2. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;color:blue;
  3. mso-font-kerning:0pt">def</span><span lang="EN-US" style="font-size:14.5pt;
  4. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  5. mso-font-kerning:0pt"> delete(src_path):<o:p></o:p></span></p>
  6. <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:
  7. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  8. 0pt">    all_files = os.listdir(src_path)<o:p></o:p></span></p>
  9. <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:
  10. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  11. 0pt">    index = </span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;
  12. 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:
  13. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(all_files)<o:p></o:p></span></p>
  14. <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:
  15. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  16. 0pt">    </span><span lang="EN-US" style="font-size:14.5pt;
  17. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  18. color:blue;mso-font-kerning:0pt">for</span><span lang="EN-US" style="font-size:
  19. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  20. 宋体;mso-font-kerning:0pt"> i </span><span lang="EN-US" style="font-size:
  21. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  22. 宋体;color:blue;mso-font-kerning:0pt">in</span><span lang="EN-US" style="font-size:
  23. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  24. 宋体;mso-font-kerning:0pt"> </span><span lang="EN-US" style="font-size:14.5pt;
  25. font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;
  26. color:blue;mso-font-kerning:0pt">range</span><span lang="EN-US" style="font-size:
  27. 14.5pt;font-family:Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:
  28. 宋体;mso-font-kerning:0pt">(index):<o:p></o:p></span></p>
  29. <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:
  30. Consolas;mso-fareast-font-family:宋体;mso-bidi-font-family:宋体;mso-font-kerning:
  31. 0pt">        os.remove(src_path+</span><span lang="EN-US" style="font-size:14.5pt;font-family:Consolas;mso-fareast-font-family:
  32. 宋体;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:
  33. 宋体;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:宋体;
  34. 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:
  35. 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">(i))</span></p>
复制代码


完整程序
  1. <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;">
  2. </span></font></div><div style="text-align: left;">
  3. </div><div style="text-align: left;">
  4. </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;">
  5. </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;">
  6. </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;">
  7. </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;">
  8. </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>
  9. <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等第三方库,相比与树莓派省去了很多配置的麻烦,这样可以专注于程序设计本身,大大提高了效率。 并且行空板板载了按键和屏幕,在实现人机交互方面也便捷了不少。假如再增加一些特殊的环境感知传感器的话还可以做更加丰富有趣的项目,比如检测植物生长过程的环境数据,再结合摄像头拍摄延时摄影的数据。
接下来旺仔爸爸还会分享一些类似的实用案例,一起期待吧



yanshi.rar

1.33 KB, 下载次数: 2

code

俺是胡胡  初级技师

发表于 2022-8-4 07:21:24

厉害
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2022 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail