Advertisement
dan-masek

Untitled

Jan 5th, 2023
1,233
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. #! /usr/bin/python3.7
  2.  
  3. from datetime import datetime
  4. from functools import partial
  5. import queue
  6. import time
  7.  
  8. from vimba import *
  9. import cv2
  10.  
  11.  
  12. def setup_camera(cam):
  13.     cam.set_pixel_format(PixelFormat.BayerRG8)
  14.     cam.ExposureTimeAbs.set(10000)
  15.     cam.BalanceWhiteAuto.set('Off')
  16.     cam.Gain.set(0)
  17.     cam.AcquisitionMode.set('Continuous')
  18.     cam.GainAuto.set('Off')
  19.     # NB: Following adjusted for my Manta G-033C
  20.     cam.Height.set(492)
  21.     cam.Width.set(656)
  22.  
  23. # Called periodically as frames are received by Vimba's capture thread
  24. # NB: This is invoked in a different thread than the rest of the code!
  25. def frame_handler(frame_queue, cam, frame):
  26.     img = frame.as_numpy_ndarray()
  27.     img_rgb = cv2.cvtColor(img, cv2.COLOR_BAYER_RG2RGB)
  28.     try:
  29.         # Try to put the frame in the queue...
  30.         frame_queue.put_nowait(img_rgb)
  31.     except queue.Full:
  32.         # If that fials (queue is full), just drop the frame
  33.         # NB: You may want to handle this better...
  34.         print('Dropped Frame')
  35.     cam.queue_frame(frame)
  36.    
  37. def do_something(img, count):
  38.     filename = 'data/IMG_' + str(count) + '.jpg'
  39.     cv2.putText(img, str(datetime.now()), (20, 40)
  40.         , cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255)
  41.         , 2, cv2.LINE_AA)
  42.     cv2.imwrite(filename, img)
  43.  
  44. def run_processing(cam):
  45.     try:
  46.         # Create a queue to use for communication between Vimba's capture thread
  47.         # and the main thread, limit capacity to 10 entries
  48.         frame_queue = queue.Queue(maxsize=10)
  49.         # Start asynchronous capture, using frame_handler
  50.         # Bind the first parameter of frame handler to our frame_queue
  51.         cam.start_streaming(handler=partial(frame_handler,frame_queue)
  52.             , buffer_count=10)
  53.  
  54.         start = time.time()
  55.         frame_count = 0
  56.         while True:
  57.             if frame_queue.qsize() > 0:
  58.                 # If there's something in the queue, try to fetch it and process
  59.                 try:
  60.                     frame = frame_queue.get_nowait()
  61.                     frame_count += 1
  62.                     cv2.imshow('Live feed', frame)
  63.                     do_something(frame, frame_count)
  64.                 except queue.Empty:
  65.                     pass
  66.                
  67.             key = cv2.waitKey(1)
  68.             if (key == ord('q')) or (frame_count >= 100):
  69.                 cv2.destroyAllWindows()
  70.                 break
  71.        
  72.         fps = int((frame_count + 1)/(time.time() - start))
  73.         print('FPS:', fps)
  74.     finally:
  75.         # Stop the asynchronous capture
  76.         cam.stop_streaming()
  77.  
  78. #@profile
  79. def main():
  80.     with Vimba.get_instance() as vimba:
  81.         with vimba.get_all_cameras()[0] as cam:
  82.             setup_camera(cam)
  83.             run_processing(cam)
  84.  
  85. if __name__ == "__main__":
  86.     main()
  87.  
Advertisement
Comments
  • smitshah_19
    1 year
    Comment was deleted
  • smitshah_19
    1 year
    # text 0.15 KB | 0 0
    1. Is it possible that we add a key before do something function and when we press that key, it should start saving the images using do something fuction?
Add Comment
Please, Sign In to add comment
Advertisement