简体   繁体   English

cv2.imshow() 函数重启 python shell 而不显示任何错误程序停止

[英]cv2.imshow() function restart python shell without showing any error program stops

I have written simple python code我写了简单的python代码

    import cv2
    cam = cv2.VideoCapture(0)
    try: 
      for i in range(10):
         print i
         _,f=cam.read()
         cv2.imshow('f',f)
         k=cv2.waitKey(1)

    catch:
      print 'error'

this code only display 0 and quit working further python shell restarts I am working with Respberry PI with python 2.7.9 and Opencv 2.4.9.1此代码仅显示 0 并退出进一步工作 python shell 重新启动我正在使用带有 python 2.7.9 和 Opencv 2.4.9.1 的 Respberry PI

When I remove cv2.imshow() line then code works fine.当我删除 cv2.imshow() 行时,代码工作正常。 and it prints 0 to 9 all digits.它打印 0 到 9 的所有数字。 It means cam is connected and opencv even reading images from camera but the issue is with displaying images这意味着 cam 已连接,opencv 甚至可以从相机读取图像,但问题在于显示图像

Could any one find similar problem before or have any solution?有没有人以前发现过类似的问题或有任何解决方案? please share thanks请分享谢谢

Your problem is not cv2.imshow() .你的问题不是cv2.imshow() your problem is cam = cv2.VideoCapture(0).你的问题是cam = cv2.VideoCapture(0). opencv has a problem finding the camera within raspberry pi! opencv 在树莓派中找到摄像头有问题! the imshow() does work and noramly would give you the problem. imshow()确实有效,通常会给您带来问题。 To be honest i am suprised your try/catch work, this is not how you write a try/catch in python, the proper way is :老实说,我很惊讶你的 try/catch 工作,这不是你在 python 中编写 try/catch 的方式,正确的方法是:

try:
    peace of the code that might throw an exception
except Exception, e:
        print e

Note: your code did not work for me before i changed the try/catch!注意:在我更改 try/catch 之前,您的代码对我不起作用!

now Back to the problem, since the image can not show it will tell you your image length/width is 0. to solve this is a litel bit long, however someone allready created a working code for that here现在回到问题,因为图像无法显示它会告诉你你的图像长度/宽度是 0。解决这个问题有点长,但是有人已经在这里创建了一个工作代码

Note: you will need to use picamera to use raspbery pi camera!注意:您需要使用picamera才能使用 raspbery pi 相机!

opencv does not take it's pride handeling a camera, other packages do. opencv 并不以处理相机为荣,其他软件包则如此。

Note: in rasbery pi you need to add () before your print such as print (i) sometimes it can create you an error注意:在 rasbery pi 中,您需要在打印之前添加 (),例如print (i)有时它会给您带来错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM