在Raspberry上的python中使用opencv进行分段错误 [英] Segmentation fault with opencv, in python on Raspberry

查看:158
本文介绍了在Raspberry上的python中使用opencv进行分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个非常简单的程序,该程序使用python中的opencv从Raspberry pi摄像机捕获视频.我正在使用Raspbian作为OS. 我已经用opencv 2.4.5版制作了一些程序,现在我已经安装了opencv 2.4.9. 我以前在opencv的早期版本上运行的所有程序现在都无法正常运行,我认为我发现了这些程序给我带来错误的地方. 只是尝试启动以下代码:

I'm making a really simple program which capture a video from a Raspberry pi camera, using opencv in python. I'm using Raspbian as OS. I've already made a few programs with the version 2.4.5 of opencv and now i've installed opencv 2.4.9. All the programs that i used to run on the previous version of opencv are not working now, and i think i found the point in which the programs gives me errors. Just trying to launch the following code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
resAcquisitionWidth = 160
resAcquisitionHeight = 120
cap.set(3, resAcquisitionWidth);
cap.set(4, resAcquisitionHeight);
cv2.namedWindow('frame')  
i = 0
while(True):
    print(i)
    i = i + 1
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

我收到错误

分段错误

我发现,如果我运行相同的代码,但没有尝试调整分辨率(因此,在第7-8行没有cap.set()命令),则一切正常.所以应该与此有关. 我已经看过其他有关类似错误的帖子,而所有这些帖子似乎都是出于其他原因. 任何人都知道利沙酮可能是什么吗?

I found out that if i run the same code, but without trying to adjust the resolution (so without the cap.set() commands on the lines 7-8) everything works fine. So it should be something related with that. I've already seen other posts about similar errors, and all of those seem to come for other reasons. Anybody know what the resasone could be ?

推荐答案

问题可能是 y0u 4re n0t c0d1ng s4f3ly :

cap = cv2.VideoCapture(0)
if not cap:
    print "!!! Failed VideoCapture: unable to open device 0"
    sys.exit(1)

您对正在发生的事情的描述可以看作是调用cap.set()cap null 的证据,从而导致崩溃.当VideoCapture()无法打开该设备时,就会发生这种情况.

You description of what's going on can be seen as evidence that cap is null when cap.set() is called, hence the crash. This happens when VideoCapture() is unable to open that device.

这是什么意思?

  • 该相机不是设备0(尝试其他数字);
  • 相机可能未安装(驱动程序问题)或未正确连接到设备;
  • OpenCV不支持该相机.
  • The camera is not device 0 (try other numbers);
  • The camera might not be installed (driver issue) or connected properly to your device;
  • The camera is not supported by OpenCV.

但是,在与OP(问问题的人)交换了一些消息之后,很明显,崩溃的可能原因是相机不支持指定的分辨率.这就是为什么检查API并注意函数返回如此重要的原因.这确实是 n0t c0d1ng s4f3ly 的另一种情况.

However, after exchanging a few messages with the OP (person that asked the question), it became clear that the probable cause of the crash is the camera not supporting the specified resolution. That's why is so important to check the API and be aware of the return of the functions. This really seems to be just another case of n0t c0d1ng s4f3ly.

根据文档set()根据操作的成功/失败返回true/false:

According to the docs, set() returns true/false depending on the success/failure of the operation:

Python :cv.SetCaptureProperty(capture,property_id,value)→检索

Python: cv.SetCaptureProperty(capture, property_id, value) → retval

确保测试这些调用的返回,并且如果set()失败,不要让程序继续执行.

Make sure to test the return of these calls, and do not let the execution of the program continue if set() fails.

这篇关于在Raspberry上的python中使用opencv进行分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆