-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoEncryption.py
295 lines (268 loc) · 11.6 KB
/
videoEncryption.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
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
import time
try:
import cv2
except:
exit()
import os
try:
from PyQt5 import Qt, uic
from PyQt5.QtCore import pyqtSlot, QTimer, Qt, QPoint
from PyQt5.QtGui import QImage, QPixmap, QIcon, QPen, QPainter
from PyQt5.QtWidgets import QFileDialog, QApplication, QDialog, QMessageBox, QAction, QMainWindow
except:
print('Install PyQt5 first!')
print('pip3 install pyqt5')
time.sleep(3)
exit()
def convertData(self):
pass # self.binaryData
def encryptVideoHelper(self, data):
# Creates set of images taken from the video that is loaded. This way a huge amount of data can be loaded
# into the images and processed.
# FOLLOWING PROCESS IS FOLLOWED:
# convert to binary
# initialize capture
# initialize out.
# read data from frame and write to the frame and out the frame to the video
# if the read data has ended then don't process the other frames and return
data += '\n\n!@#$%^&*' # stopper so that the image doesnot read garbage. This stopper is read bt python easily
frameCount = 0
writeFlag = True
binaryData = self.string2Binary(data)
cap = cv2.VideoCapture(self.videoName)
if cap.isOpened() == False:
print('Input Video is not loaded!')
return
ret, frame = cap.read()
w = None
h = None
myEndFlag = False
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
outputVideo = cv2.VideoWriter('output.mp4', -1, 100.0, (int(cap.get(3)), int(cap.get(4))), True)
# so we have the data as character string
index = 0
frameCount = int(0)
while cap.isOpened():
# print("Here")
frameCount += 1
ret, frame = cap.read()
if ret == False:
break
if ret:
# do some processing on the frame
if writeFlag:
print('Loading:', writeFlag, 'Processing frame: ', frameCount)
if len(frame.shape) == 2:
rows, cols = frame.shape
else:
rows, cols, channel = frame.shape
if len(frame.shape) == 2:
# index = 0
for row in range(rows):
for col in range(cols):
num = frame[row][col]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
temp = bitstring[0:6]
if index + 1 < len(binaryData):
temp += binaryData[index]
temp += binaryData[index + 1]
index += 2
else:
index = -1
frame[row][col] = self.getNumFromBin(temp)
myEndFlag = True
break
# index = index % len(binaryData) # we dont want to do that the index circles around the image. Instread we want to break it.
frame[row][col] = self.getNumFromBin(temp)
if myEndFlag:
break
if index == -1:
print('Loading Complete')
writeFlag = False
# break
elif len(frame.shape) >= 3:
for row in range(rows):
for col in range(cols):
if index == -1:
myEndFlag = True
break
num = frame[row][col][0]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
temp = ''
temp += bitstring[0:6]
# after converting it to binary change the last positions of the image number, the green channel has all the data
if index + 1 < len(binaryData):
temp += binaryData[index]
temp += binaryData[index + 1]
index += 2
else:
index = -1
frame[row][col][0] = self.getNumFromBin(temp)
myEndFlag = True
break
# index = index % len(binaryData)
frame[row][col][0] = self.getNumFromBin(temp)
if myEndFlag:
break
print('Current index: ', index)
if index == -1:
print('Loading Complete')
writeFlag = False
outputVideo.write(frame)
# break
# break
# processing the frame ends
# instead of writing a frame, we can just save the frame as an image and then
# some library to compile those images as a video without using compression
# FFMPEG does that, though I am not in favour of using an external library, I have to
# WRITE CODE FOR DISPLAYING IMAGE IN QLABEL VIA QPIXMAP
try:
myPath = 'EncryptedImages/' + str(frameCount) + '.png'
imagePath = os.path.abspath(myPath)
print(imagePath)
cv2.imwrite(imagePath, frame)
outputVideo.write(frame)
except:
print('Cant write to image, sorry!')
if index == -1:
break
# print(frameCount,end==' ')
# print('Here')
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release everything if job is finished
print('Job Finished')
cap.release()
outputVideo.release()
cv2.destroyAllWindows()
def bringEncryptedDataFromVideo(self):
cap = cv2.VideoCapture(self.videoName)
# cv2.imshow('Some Frame',cap.read()[1])
if (cap.isOpened() == False):
print('Video not loaded!')
return
print('Video Loaded Successfully')
# ret, frame = cap.read()
endChecker = '!@#$%^&'
dataFromImage = '' # this will contain a binary string that will be read from the image
dataFromImageReturn = ''
rows = 0
cols = 0
channel = 0
# -- Debug --
# print(len(frame))
# print(type(frame))
# try:
# print(frame.shape)
# except:
# print('Shape of frame is not defined')
timeToReturn = False
rows = int(cap.get(4))
cols = int(cap.get(3))
# else:
# rows, cols, _ = [cap.get(3), cap.get(4), 3]
print('Calling the loop')
while (cap.isOpened() and not timeToReturn):
ret, frame = cap.read()
if ret and not timeToReturn:
# read frame into a string and return that string.
if len(frame.shape) == 2:
for row in range(rows):
for col in range(cols):
num = frame[row][col]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
dataFromImage += bitstring[6:8]
dataFromImageReturn += bitstring[6:8]
if len(dataFromImage) >= 8: # to check the end of the file statement from data flag
if len(endChecker) == 7:
if endChecker == '!@#$%^&':
timeToReturn = True
break
else:
endChecker = endChecker[1:]
endChecker += self.binary2String(dataFromImage)
elif len(endChecker) < 7:
endChecker += self.binary2String(dataFromImage)
else:
endChecker = ''
print(self.binary2String(dataFromImage), end='')
dataFromImage = ''
if timeToReturn:
print('\nEnd Found')
break
elif len(frame.shape) == 3:
for row in range(rows):
for col in range(cols):
num = frame[row][col][0]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
dataFromImage += bitstring[6:8]
dataFromImageReturn += bitstring[6:8]
if len(dataFromImage) >= 8: # to check the end of the file statement from data flag
if len(endChecker) == 7:
if endChecker == '!@#$%^&':
timeToReturn = True
break
else:
endChecker = endChecker[1:]
endChecker += self.binary2String(dataFromImage)
elif len(endChecker) < 7:
endChecker += self.binary2String(dataFromImage)
else:
endChecker = ''
print(self.binary2String(dataFromImage), end='')
dataFromImage = ''
if timeToReturn:
print('\nEnd Found')
break
if timeToReturn:
break
# this is inside while loop
cap.release()
cv2.destroyAllWindows()
return self.binary2String(dataFromImageReturn)
def readDataToVideo(self):
print("Here")
self.encrpytVideoCodeHelper()
fname, _ = QFileDialog.getOpenFileName(self, 'Open Text File to load text from!', '', 'Text File (*.txt)')
# fname='2mb.txt'
data = None
myfile = None
if fname:
with open(fname, 'r') as myfile:
data = myfile.read()
# self.waterMarkImageClicked(data)
self.encryptVideoHelper(data)
myfile.close()
else:
print('Please reselect a valid file!')
def decryptDataFromVideo(self):
print('Decrypting: ')
print('Input text file to load the data to -- ')
fname, _ = QFileDialog.getOpenFileName(self, 'Open File to save text from video!', '', 'Text File (*.txt)')
# fname = 'decryptData.txt'
data = None
myfile = None
self.videoName = 'output.mp4'
if fname:
with open(fname, 'w') as myfile:
self.videoData = self.bringEncryptedDataFromVideo()
myfile.write(self.videoData)
myfile.close()
else:
print('Please reselect a valid file!')
functions = (convertData, decryptDataFromVideo, readDataToVideo, bringEncryptedDataFromVideo, encryptVideoHelper)