简体   繁体   中英

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

I have written simple python code

    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

When I remove cv2.imshow() line then code works fine. and it prints 0 to 9 all digits. It means cam is connected and opencv even reading images from camera but the issue is with displaying images

Could any one find similar problem before or have any solution? please share thanks

Your problem is not cv2.imshow() . your problem is cam = cv2.VideoCapture(0). opencv has a problem finding the camera within raspberry pi! the imshow() does work and noramly would give you the problem. 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:
    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!

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

Note: you will need to use picamera to use raspbery pi camera!

opencv does not take it's pride handeling a camera, other packages do.

Note: in rasbery pi you need to add () before your print such as print (i) sometimes it can create you an error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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