Error while trying to save webcam picture with OpenCV

后端 未结 2 1532
时光说笑
时光说笑 2020-12-21 21:03
import cv

capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
cv.SaveImage(\"test.JPG\", img)

Hi, I just want to save a picture from my we

相关标签:
2条回答
  • 2020-12-21 21:39

    Save yourself a trip to the emergency room and use SimpleCV. It's a Pythonic wrapper for OpenCV's Python bindings and a few more tools (it uses Numpy, Scipy and PIL):

    from SimpleCV import *
    
    camera = Camera()
    image = camera.getImage()
    
    image.save('test.JPG')
    
    0 讨论(0)
  • 2020-12-21 21:50

    I see this mistake over and over and over and over again: the CaptureFromCAM() call is failing, which means that QueryFrame() is failing as a consequence and returning NULL as image, causing SaveImage() to fail as well.

    Two things that you need to take into consideration here:

    1) your webcam might not be index 0 (try -1, or 1) 2) learn to code safely! Always check the return of the functions that are being called. This practice will save you a lot of time in the future:

     capture = cv.CaptureFromCAM(0)
     if not capture:
         // deal with error, return, print a msg or something else.
    
     img = cv.QueryFrame(capture)
     if not img:
         // deal with error again, return, print a msg or something else entirely.
    
     cv.SaveImage("test.JPG", img)
    
    0 讨论(0)
提交回复
热议问题