Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 09/28/19 00:28:12
  4.  
  5. @author: Satoshi Murashige
  6. """
  7.  
  8. import time
  9. import numpy as np
  10. import cv2
  11. from video_stream import VideoStream
  12.  
  13. video = VideoStream('m3v1mp4.mp4')
  14. video.start()
  15.  
  16. time.sleep(1.0)
  17.  
  18. while video.more():
  19.  
  20. frame = video.read()
  21.  
  22. cv2.imshow('frame', frame)
  23.  
  24. key = cv2.waitKey(1)
  25. if key == ord('q'):
  26. break
  27.  
  28. video.release()
  29.  
  30. # -*- coding: utf-8 -*-
  31. """
  32. Created on 09/28/19 00:20:10
  33.  
  34. @author: Satoshi Murashige
  35. """
  36.  
  37. import cv2
  38. from threading import Thread
  39. from queue import Queue
  40.  
  41. class VideoStream:
  42. def __init__(self, video, queue_size=1024):
  43. self.stream = cv2.VideoCapture(video)
  44. self.stopped = False
  45.  
  46. self.queue = Queue(maxsize=queue_size)
  47.  
  48. def start(self):
  49. t = Thread(target=self.update, args=())
  50. t.daemon = True
  51. t.start()
  52. return self
  53.  
  54. def update(self):
  55. while True:
  56. # If the thread indicator var is set, stop the thread
  57. if self.stopped:
  58. return
  59.  
  60. # Ensure the queue has room in it
  61. if not self.queue.full():
  62. # Read the next frame
  63. ok, frame = self.stream.read()
  64.  
  65. # If `ok` is `False`, the we have reached the end of stream
  66. if not ok:
  67. self.release()
  68. return
  69.  
  70. # Add the frame to the queue
  71. self.queue.put(frame)
  72.  
  73. def read(self):
  74. # Return next frame in the queue
  75. return self.queue.get()
  76.  
  77. def more(self):
  78. return self.queue.qsize() > 0
  79.  
  80. def release(self):
  81. # Indicate that the thread should be stopped
  82. self.stopped = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement