Advertisement
dan-masek

Untitled

Oct 30th, 2018
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. import threading, time
  2. import cv2
  3. import queue
  4.  
  5. capFile = cv2.VideoCapture("f:\\roadtrip\\Roadtrip_01_720p.mp4 ")
  6. input_buffer = queue.Queue(20)
  7.  
  8. fps = capFile.get(cv2.CAP_PROP_FPS)
  9. time_frame = 1 / fps
  10.  
  11. stopped = False
  12. finished = False
  13.  
  14. window_name = 'Video File'
  15.  
  16. def clickListener(event, x, y, flags, param):
  17.     global stopped
  18.     if event==cv2.EVENT_LBUTTONUP:
  19.         print("Stop/Resume video")
  20.         stopped = not stopped
  21.  
  22. def readFile():
  23.     global finished
  24.     while not finished:
  25.         ret, frame = capFile.read()
  26.         if not ret:
  27.             finished = True
  28.        
  29.         while not finished:
  30.             try:
  31.                 input_buffer.put(frame, timeout=1)
  32.                 break
  33.             except queue.Full:
  34.                 pass
  35.  
  36. def processingFile():
  37.     cv2.namedWindow(window_name)
  38.     cv2.setMouseCallback(window_name, clickListener)
  39.     global finished
  40.     while True:
  41.         if not stopped:
  42.             try:
  43.                 global frame
  44.                 frame = input_buffer.get(timeout=1)
  45.                 cv2.imshow(window_name, frame)
  46.                 #time.sleep(time_frame)
  47.             except queue.Empty:
  48.                 if finished:
  49.                     break
  50.         if (cv2.waitKey(1) & 0xFF) == ord('q'):
  51.             finished = True
  52.             break
  53.        
  54.  
  55. tReadFile = threading.Thread(target=readFile)
  56. tProcessingFile = threading.Thread(target=processingFile)
  57.  
  58. tReadFile.start()
  59. tProcessingFile.start()
  60.  
  61. tProcessingFile.join()
  62. with input_buffer.mutex:
  63.     input_buffer.queue.clear()
  64. tReadFile.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement