0

Im having troubles with saving Images from a Basler camera. I wanted to write a code so that I can press the key "s" and save a picture. It seems like the code gets hung up on cam.RetrieveResult(2000). Any Idea how I can fix this problem ?

#Code inspiration from: https://github.com/basler/pypylon/blob/master/samples/save_image.py

from pypylon import pylon
import platform
import cv2

count = 0
#path_root = 'C:\\Users'
img = pylon.PylonImage()
tlf = pylon.TlFactory.GetInstance()

cam = pylon.InstantCamera(tlf.CreateFirstDevice())
cam.Open()
cam.StartGrabbing()
 
while True:
    with cam.RetrieveResult(2000) as result:

        # Calling AttachGrabResultBuffer creates another reference to the
        # grab result buffer. This prevents the buffer's reuse for grabbing.
        img.AttachGrabResultBuffer(result)
        key = cv2.waitKey() & 0xFF
        if platform.system() == 'Windows' and key == ord('s'): # press s to safe:
            filename = "saved_pypylon_img_%d.png" % count
            img.Save(pylon.ImageFileFormat_Png, filename)
            print('png image saved')
            count += 1
        if key == ord('q'):   
            break
            
        
        # In order to make it possible to reuse the grab result for grabbing
        # again, we have to release the image (effectively emptying the
        # image object).
        img.Release()
cam.StopGrabbing()
cam.Close()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • make sure you could actually open the camera device. -- how can it "hang" on that RetrieveResult when it's got a timeout of 2000? is that 2000 millisecs, or 2000 seconds? – Christoph Rackwitz Jul 12 '22 at 15:53

1 Answers1

0

Your code looks OK and works for the camera on my desk. This might be some misconfiguration. For instance, if your camera's Trigger Mode is On then you'll always expire not providing pulses to cam's inputs.

  • go to pylon Viewer and reset camera to default settings (User Set Control > User Set Selector: Default User Set > User Set Load: Execute)

  • put camera in Continuous Shot to make sure no errors are popping up in Messages pane and camera runs smoothly

  • close camera & close pylon Viewer & try your sample again