Advertisement
dan-masek

Untitled

May 10th, 2018
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.51 KB | None | 0 0
  1. import logging
  2. import logging.handlers
  3. import os
  4.  
  5. from Queue import Queue
  6. from threading import Thread, Event
  7. from timeit import default_timer as timer
  8.  
  9. import cv2
  10. import numpy as np
  11.  
  12. # ============================================================================
  13.  
  14. QUEUE_MAXSIZE = 8
  15.  
  16. # ============================================================================
  17.  
  18. def init_logging(log_to_console = True):
  19.     import sys
  20.     main_logger = logging.getLogger()
  21.  
  22.     LOG_FILENAME = 'debug.log'
  23.  
  24.     # Check if log exists and should therefore be rolled
  25.     needRoll = os.path.isfile(LOG_FILENAME)
  26.  
  27.     formatter = logging.Formatter(
  28.         fmt='%(asctime)s.%(msecs)03d %(levelname)-8s <%(threadName)s> [%(name)s] %(message)s'
  29.         , datefmt='%Y-%m-%d %H:%M:%S')
  30.  
  31.     handler_file = logging.handlers.RotatingFileHandler(LOG_FILENAME
  32.         , maxBytes = 2**24
  33.         , backupCount = 10)
  34.     handler_file.setFormatter(formatter)
  35.     main_logger.addHandler(handler_file)
  36.  
  37.     if not sys.executable.endswith("pythonw.exe") and log_to_console:
  38.         handler_stream = logging.StreamHandler(sys.stdout)
  39.         handler_stream.setFormatter(formatter)
  40.         main_logger.addHandler(handler_stream)
  41.  
  42.     main_logger.setLevel(logging.DEBUG)
  43.  
  44.     if needRoll:
  45.         # Roll over on application start
  46.         handler_file.doRollover()
  47.  
  48. # ============================================================================
  49.  
  50. def load_alpha(filename, q_alpha_fg, q_alpha_bg, stop_event):
  51.     logger = logging.getLogger('load_alpha')
  52.     video = cv2.VideoCapture(filename)
  53.     if not video.isOpened():
  54.         logger.error("Unable to open input video '%s'.", filename)
  55.         exit(-1)
  56.  
  57.     alpha = [] # Cache
  58.     alpha_inv = [] # Cache
  59.  
  60.     frame_count = 0
  61.     total_time = 0
  62.     while not stop_event.is_set():
  63.         t0 = timer()
  64.         res, alpha_raw = video.read()
  65.         if not res:
  66.             break # End of video
  67.  
  68.         if frame_count == 0: # Pre-allocate
  69.             for i in range(q_alpha_fg.maxsize + 2):
  70.                 alpha.append(np.zeros_like(alpha_raw, np.float32))
  71.             for i in range(q_alpha_bg.maxsize + 2):
  72.                 alpha_inv.append(np.zeros_like(alpha_raw, np.float32))
  73.  
  74.         k = frame_count % (q_alpha_fg.maxsize + 2)
  75.         k_i = frame_count % (q_alpha_bg.maxsize + 2)
  76.  
  77.         np.multiply(alpha_raw, np.float32(1/255.0), alpha[k])
  78.         cv2.subtract((1.0, 1.0, 1.0, 0.0), alpha[k], alpha_inv[k_i])
  79.         q_alpha_fg.put(alpha[k])
  80.         q_alpha_bg.put(alpha_inv[k_i])
  81.         t1 = timer()
  82.         logger.debug("Processed frame %d in %0.4f ms", frame_count, (t1-t0)*1000)
  83.         frame_count += 1
  84.         total_time += (t1-t0)
  85.  
  86.     q_alpha_fg.put(None) # Signal end
  87.     q_alpha_bg.put(None) # Signal end
  88.     if (frame_count > 0):
  89.         logger.debug("Done (average iteration %0.4f ms).", (total_time / frame_count) * 1000)
  90.     else:
  91.         logger.debug("Done")
  92.  
  93. # ============================================================================
  94.  
  95. def weigh_frames(filename, q_alpha, q_output, logname='weigh_frames'):
  96.     logger = logging.getLogger(logname)
  97.     video = cv2.VideoCapture(filename)
  98.     if not video.isOpened():
  99.         logger.error("Unable to open input video '%s'.", filename)
  100.         exit(-1)
  101.  
  102.     frame_weighed = [] # Cache
  103.  
  104.     frame_count = 0
  105.     total_time = 0
  106.     while True:
  107.         t0 = timer()
  108.         res, frame_raw = video.read()
  109.         alpha = q_alpha.get()
  110.         if (not res) or (alpha is None):
  111.             break # End of video
  112.  
  113.         if frame_count == 0: # Pre-allocate
  114.             for i in range(q_output.maxsize + 2):
  115.                 frame_weighed.append(np.zeros_like(frame_raw, np.float32))
  116.  
  117.         k = frame_count % (q_output.maxsize + 2)
  118.  
  119.         cv2.multiply(frame_raw, alpha, frame_weighed[k], dtype=cv2.CV_32FC3)
  120.  
  121.         q_output.put(frame_weighed[k])
  122.  
  123.         q_alpha.task_done()
  124.         t1 = timer()
  125.         logger.debug("Processed frame %d in %0.4f ms", frame_count, (t1-t0)*1000)
  126.         frame_count += 1
  127.         total_time += (t1-t0)
  128.  
  129.     q_output.put(None) # Signal end
  130.     if (frame_count > 0):
  131.         logger.debug("Done (average iteration %0.4f ms).", (total_time / frame_count) * 1000)
  132.     else:
  133.         logger.debug("Done")
  134.  
  135. # ============================================================================
  136.  
  137. def blend_frames(q_fq, q_bg, q_output):
  138.     logger = logging.getLogger('blend_frames')
  139.    
  140.     result = [] # Cache
  141.    
  142.     frame_count = 0
  143.     total_time = 0
  144.     while True:
  145.         t0 = timer()
  146.         foreground = q_fq.get()
  147.         background = q_bg.get()
  148.  
  149.         if (foreground is None) or (background is None):
  150.             break # End of video
  151.            
  152.         if frame_count == 0: # Pre-allocate
  153.             for i in range(q_output.maxsize + 2):
  154.                 result.append(np.zeros_like(foreground, np.uint8))
  155.                
  156.         k = frame_count % (q_output.maxsize + 2)
  157.  
  158.         cv2.add(foreground, background, result[k], dtype=cv2.CV_8UC3)
  159.  
  160.         q_output.put(result[k])
  161.  
  162.         q_fq.task_done()
  163.         q_bg.task_done()
  164.         t1 = timer()
  165.         logger.debug("Processed frame %d in %0.4f ms", frame_count, (t1-t0)*1000)
  166.         frame_count += 1
  167.         total_time += (t1-t0)
  168.  
  169.     q_output.put(None) # Signal end
  170.     if (frame_count > 0):
  171.         logger.debug("Done (average iteration %0.4f ms).", (total_time / frame_count) * 1000)
  172.     else:
  173.         logger.debug("Done")
  174.  
  175. # ============================================================================
  176.  
  177. init_logging(False)
  178. logger = logging.getLogger('main')
  179.  
  180. q_alpha_fg = Queue(maxsize=QUEUE_MAXSIZE)
  181. q_alpha_bg = Queue(maxsize=QUEUE_MAXSIZE)
  182. q_weighed_fg = Queue(maxsize=QUEUE_MAXSIZE)
  183. q_weighed_bg = Queue(maxsize=QUEUE_MAXSIZE)
  184. q_result = Queue(maxsize=QUEUE_MAXSIZE)
  185. stop_event = Event()
  186.  
  187. FOREGROUND_FILENAME = 'circle.mp4'
  188. BACKGROUND_FILENAME = 'video.mp4'
  189. ALPHA_FILENAME = 'circle_alpha.mp4'
  190.  
  191. try:
  192.  
  193.     workers = []
  194.  
  195.     worker = Thread(target=load_alpha, args=(ALPHA_FILENAME, q_alpha_fg, q_alpha_bg, stop_event))
  196.     workers.append(worker)
  197.  
  198.     worker = Thread(target=weigh_frames, args=(FOREGROUND_FILENAME, q_alpha_fg, q_weighed_fg, 'weigh_fg'))
  199.     workers.append(worker)
  200.  
  201.     worker = Thread(target=weigh_frames, args=(BACKGROUND_FILENAME, q_alpha_bg, q_weighed_bg, 'weigh_bg'))
  202.     workers.append(worker)
  203.  
  204.     worker = Thread(target=blend_frames, args=(q_weighed_fg, q_weighed_bg, q_result))
  205.     workers.append(worker)
  206.  
  207.     # Start all threads
  208.     for worker in workers:
  209.         worker.start()
  210.  
  211.     frame_count = 0
  212.     total_time = 0
  213.     while True:
  214.         t0 = timer()
  215.  
  216.         frame = q_result.get()
  217.         if frame is None:
  218.             break # End of video
  219.  
  220.         if not q_result.empty():
  221.             logger.debug("Skip frame, more waiting...")
  222.         else:
  223.             cv2.imshow('My Image', frame)
  224.             if cv2.waitKey(1) == ord('q'):
  225.                 logger.debug("Exit requested, setting stop event.")
  226.                 stop_event.set()
  227.  
  228.         q_result.task_done()
  229.  
  230.         t1 = timer()
  231.         logger.debug("Processed frame %d in %0.4f ms", frame_count, (t1-t0)*1000)
  232.         frame_count += 1
  233.         total_time += (t1-t0)
  234.  
  235.     if (frame_count > 0):
  236.         logger.debug("Done (average iteration %0.4f ms).", (total_time / frame_count) * 1000)
  237.     else:
  238.         logger.debug("Done")
  239.  
  240.     for worker in workers:
  241.         worker.join()
  242.         logger.debug("Worker joined.")
  243.  
  244.     logger.debug("Done")
  245.  
  246. except KeyboardInterrupt:
  247.     stop_event.set()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement