Advertisement
Guest User

Untitled

a guest
Dec 20th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. from pypylon import pylon
  2. import cv2, numpy as np
  3. from concurrent.futures import ThreadPoolExecutor
  4.  
  5.  
  6.  
  7. def save_image(self, img,name):
  8. np.save(name, img, allow_pickle=False)
  9.  
  10.  
  11. image_counter = 0
  12. serial_number = '23437639'
  13. info = None
  14. max_threads = 5
  15. pool = ThreadPoolExecutor(max_threads)
  16.  
  17.  
  18. for i in pylon.TlFactory.GetInstance().EnumerateDevices():
  19. if i.GetSerialNumber() == serial_number:
  20. info = i
  21. break
  22. else:
  23. print('Camera with {} serial number not found '.format(serial_number))
  24. if info is not None:
  25. camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateDevice(info))
  26. camera.Open()
  27. camera.ReverseY.SetValue(True)
  28.  
  29. converter = pylon.ImageFormatConverter()
  30. converter.OutputPixelFormat = pylon.PixelType_BGR8packed
  31. converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
  32.  
  33. camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
  34.  
  35.  
  36. print('starting to grab images')
  37. while camera.IsGrabbing():
  38. with ThreadPoolExecutor(max_workers=5) as executor:
  39. grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
  40. if grabResult.GrabSucceeded():
  41. img = converter.Convert(grabResult)
  42. img = img.GetArray()
  43. h, w, ch = img.shape
  44. image_counter += 1
  45. filename = f"./unprocessed_images/image-{image_counter}"
  46. print(f'\r save image = {image_counter}',end='',flush=True)
  47. executor.submit(save_image,filename,img,allow_picke=False)
  48. #np.save(filename,img, allow_pickle=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement