-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractGrayDisplay.py
212 lines (165 loc) · 5.96 KB
/
ExtractGrayDisplay.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
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
#!/usr/bin/env python3
import threading
from threading import Thread
import cv2
import numpy as np
import base64
import asyncio
from multiprocessing import Queue
#Creating global variables for Extraction
exLock = threading.Lock()
extractFSem = asyncio.Semaphore(10)
extractESem = asyncio.Semaphore(10)
extractQueue = Queue()
# Creating global variables for conversion
conLock = threading.Lock()
conFSem = asyncio.Semaphore(10)
conESem = asyncio.Semaphore(10)
conQueue = Queue()
# Global filename
filename = 'clip.mp4'
frameCount = 0
class Producer(threading.Thread):
# Producer class takes in a shared queue, two semaphores for full and empty and a lock
# Set up the definition
def __init__(self, sharedque, fullsem, emptysem, qlock):
threading.Thread.__init__(self)
self.sharedque =sharedque
self.fullsem = fullsem
self.emptysem = emptysem
self.qlock = qlock
print("Initialized")
# Method to work with the queue it checks it is able to be used, and then locks the queue adds an item and changes the full semaphore
def produce(self, item):
self.emptysem.acquire()
self.qlock.acquire()
self.sharedque.put(item)
self.qlock.release()
self.fullsem.release()
#Debug produce
def dproduce(self,item):
self.emptysem.acquire()
print("Checked empty semaphore")
self.qlock.acquire()
print("Lock acquired")
self.sharedque.put(item)
print("Added item")
self.qlock.release()
print("Lock relesed")
self.fullsem.release()
print("Full semaphore released")
print("Done with item")
class Consumer(threading.Thread):
# Producer class takes in a shared queue, two semaphores for full and empty and a lock
# Set up the definition
def __init__(self, sharedque, fullsem, emptysem, qlock):
threading.Thread.__init__(self)
self.sharedque =sharedque
self.fullsem = fullsem
self.emptysem = emptysem
self.qlock = qlock
print("Initialized")
# Method to work with the queue it checks it is able to be used, and then locks the queue gets an item and changes the full semaphore
def consume(self):
self.fullsem.acquire()
self.qlock.acquire()
item=self.sharedque.get()
self.qlock.release()
self.emptysem.release()
return item
#Debug consume
def dconsume(self):
self.fullsem.acquire()
print("Checked full semaphore")
self.qlock.acquire()
print("Lock acquired")
item = self.sharedque.get()
print("Obtained item")
self.qlock.release()
print("Lock relesed")
self.emptysem.release()
print("Empty semaphore released")
print("Done with item")
return item
ExtPro = Producer(extractQueue, extractFSem, extractESem, exLock)
ExtCon = Consumer(extractQueue, extractFSem, extractESem, exLock)
ConPro = Producer(conQueue, conFSem, conESem, conLock)
ConCon = Consumer(conQueue, conFSem, conESem, conLock)
# Method for extracting the frames
def extractFrames(fileName, proBuf):
# 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
proBuf.produce(jpgAsText)
# debug use buffer
#proBuf.put(jpgAsText)
success, image = vidcap.read()
print('Reading frame {} {}'.format(count, success))
count+=1
print("Frame extraction complete")
# Method for converting frames to grey
def convertFrames(conBuf, convPro):
# Initialize frame count
count = 0
while (count < 739):
# Get the frame
frameAsText = conBuf.consume()
# 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_COLOR)
#Convert frame
print("Converting frame {}".format(count))
grayscaleFrame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Reencode frame
#convJpgImage = cv2.imencode('.jpg', grayscaleFrame)
# Encode the frame as base 64
convJpgAsText = base64.b64encode(grayscaleFrame)
# Add to second buffer
convPro.produce(convJpgAsText)
print("Adding converted frame {}".format(count))
count +=1
print("Done converting")
# Displaying method
def displayFrames(convCon):
# Initialize frame count
count = 0
while(count < 739):
# Get the frame
frameAsText = convCon.consume()
# Decode the frame
jpgRawImage = base64.b64decode(frameAsText)
# Convert the raw frame toa 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 teh next frame
cv2.imshow("Video", img)
if cv2.waitKey(42) and 0xFF == ord("q"):
break
count += 1
print("Finished displaying all frames")
cv2.destroyAllWindows()
threads = []
thread0 = threading.Thread(target=extractFrames(filename, ExtPro))
thread1 = threading.Thread(target=convertFrames(ExtCon, ConPro))
thread2 = threading.Thread(target=displayFrames(ConCon))
threads.append(thread0)
threads.append(thread1)
threads.append(thread2)
for thread in threads:
thread.start()