0

I need to take a single snapshot from webcam. I choice SimpleCV for this task.

Now i try to get a single image and show it:

from SimpleCV import Camera


cam = Camera()
img = cam.getImage()
img.show()

But i see only black image. I think camera is not ready at this moment, because if I call time.sleep(10) before cam.getImage() all works good.

What the right way for this? Thank you!

ssbb
  • 1,965
  • 2
  • 14
  • 26

4 Answers4

1

Have you installed PIL? I had similar problems, but on installing PIL everything works fine. Hope this helps.

You can download PIL from Pythonware

1

I ran into this same issue and came up with the following work-around. Basically it grabs an image, tests the middle pixel to see if it's black (0,0,0). If it is, then it waits 1 second and tries again.

import time
from SimpleCV import Camera
cam = Camera()
r = g = b = 0
while r + g + b < 0.01:
    img = cam.getImage()
    r, g, b = img[img.width/2, img.height/2]
    print("r: {} g: {} b: {}".format(r,g,b))
    time.sleep(1)
img.save(file_name)
brousch
  • 1,064
  • 5
  • 10
  • This works for me and it sort of makes sense- the camera needs to be started up first. Do you have any idea why the simpleCV package would display a black canvas when trying to load an image? – FredFury Jun 06 '15 at 07:58
0

Your problem may be that when the script ends the camera object doesn't seem to release the webcam for other programs. When you wait to run the script Windows frees it up (maybe?) so it will work again. If you use it will the old script still thinks it owns the webcam, it shows up as black. Anyway the solution here seems to work. Add "del cam" to the end of your script which will make the camera object go away and let you use the camera again.

Community
  • 1
  • 1
samt1903
  • 169
  • 1
  • 2
  • 11
0

try this
from SimpleCV import Camera cam = Camera(0) while True: img = cam.getImage() img.show()