-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractAndDisplay.py
executable file
·71 lines (50 loc) · 1.66 KB
/
ExtractAndDisplay.py
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
#!/usr/bin/env python3
import threading
import cv2
import numpy as np
import base64
import queue
def extractFrames(fileName, outputBuffer, maxFramesToLoad=9999):
# Initialize frame count
count = 0
# open video file
vidcap = cv2.VideoCapture(fileName)
# read first image
success,image = vidcap.read()
print(f'Reading frame {count} {success}')
while success and count < maxFramesToLoad:
# get a jpg encoded frame
success, jpgImage = cv2.imencode('.jpg', image)
#encode the frame as base 64 to make debugging easier
jpgAsText = base64.b64encode(jpgImage)
# add the frame to the buffer
outputBuffer.put(image)
success,image = vidcap.read()
print(f'Reading frame {count} {success}')
count += 1
print('Frame extraction complete')
def displayFrames(inputBuffer):
# initialize frame count
count = 0
# go through each frame in the buffer until the buffer is empty
while not inputBuffer.empty():
# get the next frame
frame = inputBuffer.get()
print(f'Displaying frame {count}')
# display the image in a window called "video" and wait 42ms
# before displaying the next frame
cv2.imshow('Video', frame)
if cv2.waitKey(42) and 0xFF == ord("q"):
break
count += 1
print('Finished displaying all frames')
# cleanup the windows
cv2.destroyAllWindows()
# filename of clip to load
filename = 'clip.mp4'
# shared queue
extractionQueue = queue.Queue()
# extract the frames
extractFrames(filename,extractionQueue, 72)
# display the frames
displayFrames(extractionQueue)