-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractAndDisplay.py
executable file
·80 lines (56 loc) · 1.94 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
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
import threading
import cv2
import numpy as np
import base64
import queue
def extractFrames(fileName, outputBuffer):
# Initialize frame count
count = 0
# open video file
vidcap = cv2.VideoCapture(fileName)
# read first image
success,image = vidcap.read()
print("Reading frame {} {} ".format(count, success))
while success:
# 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(jpgAsText)
success,image = vidcap.read()
print('Reading frame {} {}'.format(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
frameAsText = inputBuffer.get()
# decode the frame
jpgRawImage = base64.b64decode(frameAsText)
# convert the raw frame to a numpy array
jpgImage = np.asarray(bytearray(jpgRawImage), dtype=np.uint8)
# get a jpg encoded frame
img = cv2.imdecode( jpgImage ,cv2.IMREAD_UNCHANGED)
print("Displaying frame {}".format(count))
# display the image in a window called "video" and wait 42ms
# before displaying the next frame
cv2.imshow("Video", img)
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)
# display the frames
displayFrames(extractionQueue)