如何通过pi上的边缘检测保存视频记录

时间:2019-01-28 20:16:13

标签: python opencv video raspberry-pi edge-detection

我目前正在尝试使用树莓派相机在树莓派上运行我的代码。但是,当我运行它时,文件会保存,但是它要么不允许我在vlc上查看,要么播放,但仅会出现静态-取决于我使用的编解码器。

我尝试了多种编解码器,例如XVID,MJPG,MPEG,H264,只有MJPG允许播放,但它是静态播放的。录制时,我可以看到相机在哪里检测到整个房间的边缘。但是,它没有显示出记录的方式。我试图将.avi转换为.mp4,这没有帮助。我将文件上传到youtube,并且播放方式相同。我还运行了没有边缘检测的其他代码,这似乎总是可以正常工作,并且每次都能完美播放。除了边缘检测代码外,我还将在下面进行介绍。

{% macro crop_text(text, len) %}
  {% if text | length < len %}
    {{ text }}
  {% else %}
    {% set break = false %}
    {% set croppedParagraph = '' %}
    {% set paragraph = text | split(' ') %}
    {% set totalLength = 0 %}
    {% for word in paragraph  if not break %}
        {% if totalLength >= len %}
            {% set break = true %}
        {% else %}
            {% set totalLength = totalLength + 1 +  word | length %}
            {% set croppedParagraph = croppedParagraph ~ ' ' ~  word %}
        {% endif %}
    {% endfor %}
    {{ croppedParagraph ~ ' ...' }}
  {% endif %}
{% endmacro %}

我希望通过边缘检测可以看到视频的播放,但是没有。

1 个答案:

答案 0 :(得分:0)

根据OpenCV文档(链接:https://docs.opencv.org/4.0.0/dd/d9e/classcv_1_1VideoWriter.html#ac3478f6257454209fa99249cc03a5c59):

仅在Windows上支持cv2.VideoWriter的标志isColor。在其他平台上,编码器始终期望使用彩色帧(即channels == 3)。在上面的代码中,当您尝试在RGB frame上应用Canny Edge Detection时,它将返回灰度frame图像。因此,您将无法看到播放。

在将单通道frame输出传递到VideoWriter之前,先尝试将其转换为三通道帧:

frame = cv2.Canny(frame,100,200)       # Single channel. Shape = (640, 480)
frame = np.expand_dims(frame, axis=-1) # Single channel. Shape = (640, 480, 1)
frame = np.concatenate((frame, frame, frame), axis=2) # 3 channel. Shape = (640, 480, 3)

out.write(frame)