-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo_Functions.py
More file actions
383 lines (303 loc) · 11.3 KB
/
Video_Functions.py
File metadata and controls
383 lines (303 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#--------------------------------------------------------------------
# Author: Dan Duncan
# Date created: 4/20/2017
#
# Note: Some of the ideas here are borrowed or inspired
# from the PyImageSearch blog
#
# The imutils.video.* library also contains classes similar
# to these, although implementations may differ.
#
#--------------------------------------------------------------------
import cv2 as cv
import numpy as np
from imutils.video import FPS
import imutils
from threading import Thread
from Queue import Queue
import time
#--------------------------------------------------------------------
# Create class for mulithreaded streaming of video from a file
class FileVideoStream:
def __init__(self, path, queueSize=256):
# initialize the file video stream along with the boolean
# used to indicate if the thread should be stopped or not
self.stream = cv.VideoCapture(path)
self.stopped = False
# initialize the queue used to store frames read from
# the video file
self.Q = Queue(maxsize=queueSize)
def start(self):
# start a thread to read frames from the file video stream
t = Thread(target=self.update, args=())
t.setDaemon(True) # This causes thread to kill itself automatically
#t.daemon = True # This causes thread to kill itself automatically
t.start()
return self
def update(self):
# keep looping infinitely
while True:
# if the thread indicator variable is set, stop the
# thread
if self.stopped:
return
# otherwise, ensure the queue has room in it
if not self.Q.full():
# read the next frame from the file
(grabbed, frame) = self.stream.read()
# if the `grabbed` boolean is `False`, then we have
# reached the end of the video file
if not grabbed:
self.stop()
return
# add the frame to the queue
self.Q.put(frame)
def read(self):
# return next frame in the queue
if self.Q.qsize() > 0:
return self.Q.get()
else:
return False
def more(self):
# return True if there are still frames in the queue
# Or if new frames will be loaded into the queue
return (self.stopped == False) or (self.Q.qsize() > 0)
#return self.Q.qsize() > 0
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
self.stream.release()
#--------------------------------------------------------------------
# Create class for multithreaded webcam streaming
class WebcamVideoStream:
def __init__(self, src=0):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv.VideoCapture(src)
(self.grabbed, self.frame) = self.stream.read()
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
# Initialize thread
self.t = Thread(target=self.update, args=())
self.t.setDaemon(True)
def start(self):
# start the thread to read frames from the video stream
self.t.start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
# otherwise, read the next frame from the stream
(self.grabbed, self.frame) = self.stream.read()
def read(self):
# return the frame most recently read
return self.frame
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
# --------------------------------------------------------------------
# START SCRIPT
# Select modules to run
module0 = False # Video file playback
module1 = False # Video file playback w/ FPS tracking
module2 = False # Multithreaded video file playback
module3 = False # Webcam playback
module4 = False # Multithreaded webcam playback
module5 = True # Save webcam video to file
inputFile = "input/dachie.mov"
# Video playback in single-threaded mode
# Slow playback is likely due to decoding bottleneck
if module0 == True:
# iPhone video resolution is 1920 x 1080
# 3x downsize = 640 x 360
cap = cv.VideoCapture(inputFile)
# Create a window of fixed size
#cv.namedWindow('frame',cv.WINDOW_NORMAL)
#cv.resizeWindow('frame',640,360)
while(cap.isOpened()):
ret, frame = cap.read()
#gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Resize frame to fit on screen
# Original frame was 1920 x 1080 (iPhone 6+)
# Note that np.shape returns (height,width)
# But opencv takes arguments as (width,height)
# For zooming, use interpolation = linear or cubic
frame_small = cv.resize(frame, (360, 640), interpolation=cv.INTER_AREA)
cv.imshow('frame',frame_small)
if cv.waitKey(1) & 0xFF == ord('q'):
print("Q pressed. Quitting.")
break
#print(frame.shape)
cap.release()
cv.destroyAllWindows()
cv.waitKey(1)
# Module 1: Single-Threaded with FPS counting and text added
# Achieves framerate of 7.59 fps
if module1:
stream = cv.VideoCapture(inputFile)
fps = FPS().start() # Object that counts the frame rate
# Loop over frames from video file stream
while True:
# Grab next frame
grabbed, frame = stream.read()
# if the frame not grabbed, we have reached end of stream
if not grabbed:
break
# resize the frame and convert it to grayscale (while still
# retaining 3 channels)
frame = imutils.resize(frame, width=450)
frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
frame = np.dstack([frame, frame, frame])
# Display text on frame (for benchmarking vs threaded method)
cv.putText(frame, "Slow Method", (10, 30),
cv.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# Show the frame and update the FPS counter
cv.imshow("Frame", frame)
fps.update()
if cv.waitKey(1) & 0xFF == ord('q'):
print("Q pressed. Quitting.")
break
fps.stop()
print("Elapsed time: {:.2f}".format(fps.elapsed()))
print("Approx. FPS: {:.2f}".format(fps.fps()))
stream.release()
cv.destroyAllWindows()
cv.waitKey(1)
# Module 2: Multi-threaded video streaming
# Improves to > 12 fps if a large enough buffer is used
# Bottleneck for speed is reading and decoding frames
# May get further improvement by having many concurrent threads read frames
if module2:
fvs = FileVideoStream(inputFile,queueSize=512).start()
time.sleep(3.0) # Wait for fvs to initialize
fps = FPS().start() # start the FPS timer
# loop over frames from the video file stream
while True:
if fvs.more() == False:
print("No more frames")
break
# grab the frame from the threaded video file stream, resize
# it, and convert it to grayscale (while still retaining 3
# channels)
frame = fvs.read()
if frame is False:
#print("Display exceeded frame read speed")
continue
# If new frame was loaded, display it
frame = imutils.resize(frame, width=450)
frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
frame = np.dstack([frame, frame, frame])
# display the size of the queue on the frame
cv.putText(frame, "Queue Size: {}".format(fvs.Q.qsize()),
(10, 30), cv.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# show the frame and update the FPS counter
cv.imshow("Frame", frame)
fps.update()
if cv.waitKey(1) & 0xFF == ord('q'):
print("Q pressed. Quitting.")
fvs.stop()
break
# Stop the timer and display FPS information
fps.stop()
print("Elapsed time: {:.2f}".format(fps.elapsed()))
print("Approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
cv.destroyAllWindows()
cv.waitKey(1)
#fvs.stop()
# Single-threaded webcam playback
if module3:
# Argument 0 accesses webcam
cap = cv.VideoCapture(0)
fps = FPS().start() # start the FPS timer
while(cap.isOpened()):
ret, frame = cap.read()
# Check if error occurred reading frame
if ret == False:
# Error occurred
continue
# Resize frame to fit on screen
# Webcame frame.shape = (720,1280,3)
# Note that np.shape returns (height,width)
# But opencv takes arguments as (width,height)
# For zooming, use interpolation = linear or cubic
#frame = cv.resize(frame, (360, 640), interpolation=cv.INTER_AREA)
cv.imshow('frame',frame)
fps.update()
if cv.waitKey(1) & 0xFF == ord('q'):
print("Q pressed. Quitting.")
break
# Stop the timer and display FPS information
fps.stop()
print("Elapsed time: {:.2f}".format(fps.elapsed()))
print("Approx. FPS: {:.2f}".format(fps.fps()))
#print(frame.shape)
cap.release()
cv.destroyAllWindows()
cv.waitKey(1)
# Multi-threaded webcam playback
# Achieves framerate of 14 fps
if module4:
wvs = WebcamVideoStream().start()
fps = FPS().start()
while True:
# Grab most recent frame
frame = wvs.read()
cv.imshow('frame', frame)
fps.update()
if cv.waitKey(1) & 0xFF == ord('q'):
print("Q pressed. Quitting.")
break
# Display FPS information
fps.stop()
print("Elapsed time: {:.2f}".format(fps.elapsed()))
print("Approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
cv.destroyAllWindows()
cv.waitKey(1)
wvs.stop()
# Save webcam video to file
# Uses multithreaded webcam playback
# Video writing in OpenCV is platform-dependent. Running this code
# on another platform may require trial-and-error with different codecs
# and file extensions.
# For Mac OSX, MJPG + .avi works
if module5:
# Define the codec and create VideoWriter object
# Popular codec options: H264, MJPG, XVID, DIVX
# List of all codecs: http://www.fourcc.org/codecs.php
fourcc = cv.VideoWriter_fourcc(*'MJPG')
# File extensions options: .avi, .mp4, .mov
out = cv.VideoWriter('output/output.avi', fourcc, 10.0, (1280, 720))
# Create webcam input stream
wvs = WebcamVideoStream().start()
fps = FPS().start()
while True:
# Grab most recent frame
frame = wvs.read()
# Display frame to screen
cv.imshow('frame', frame)
fps.update()
# Write mirror-imaged frame to output stream
frameOut = cv.flip(frame,0) # Zero means flip horizontally
out.write(frame)
if cv.waitKey(1) & 0xFF == ord('q'):
print("Q pressed. Quitting.")
break
# Display FPS information
fps.stop()
print("Elapsed time: {:.2f}".format(fps.elapsed()))
print("Approx. FPS: {:.2f}".format(fps.fps()))
# Write video file
out.release() # Finalize write to file
print("Video file written.")
# Clean up
cv.destroyAllWindows()
cv.waitKey(1)
wvs.stop()
# Finish
print("All done!")