-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfire_to_random_bit_stream.py
More file actions
66 lines (60 loc) · 2.75 KB
/
fire_to_random_bit_stream.py
File metadata and controls
66 lines (60 loc) · 2.75 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
# video processing and hashlib import
import cv2
import hashlib
# open video file of fire
vid = cv2.VideoCapture("resources/burning-charcoal-fire.mp4")
# open file to write random bit stream to
f = open("outputs/random-bit-stream.txt", "w")
# hex to binary function definition
def hex_to_bin(h):
return bin(int(h, 16))[2:].zfill(len(h) * 4)
# loop through frames in video file, storing each frames pixel seeds
frameSeedsArray = []
frameCount = 0
while (True):
# initialize this frames seeds array
seeds = []
# read the current frame, get its dimensions
success, frame = vid.read()
rows, cols, _ = frame.shape
# iterate over frames pixels, skipping four each step to avoid patterns
for i in range(rows):
if i % 4 != 0:
continue
for j in range(cols):
if j % 4 != 0:
continue
# extract and test weather this pixel is within an "orange fire" range
b, g, r = frame[i, j]
if (120 <= r) and (20 <= g <= 160) and (0 <= b <= 70):
# write seed for this pixel to seeds array
hashIngest = format(r, '03d') + format(int(i/4), '03d') + format(g, '03d') + format(int(j/4), '03d') + format(b, '02d')
# NOTE: There are 136 red, 141 green, 71 blue, 270 i, and 480 j possibilities for each value respectively,
# this gives (136 * 141 * 71 * 270 * 480) = 176,449,881,600 possible pixel seeds!
seeds.append(hashlib.md5(hashIngest.encode('utf-8')).hexdigest())
# store seeds array (consisting of pixel seeds in md5 hex form) for this frame
frameSeedsArray.append(seeds)
# show and increment frame counter, display seed count
print('FINISHED FRAME: ' + str(frameCount) + " WITH " + str(len(seeds)) + " FIRE PIXEL SEEDS")
frameCount += 1
# stop when 240 frames reached (end of the example video)
if frameCount == 240:
break
# loop through all frame seeds now that they are in memory
for i in range(len(frameSeedsArray) - 12):
# load this frames seeds and 4/8/12 frames ahead seeds
a = frameSeedsArray[i]
b = frameSeedsArray[i+4]
c = frameSeedsArray[i+8]
d = frameSeedsArray[i+12]
# find which frame has the least seeds,
# this will determine the amount of cross-frame seeds we make for this set of four
size = min(len(a), len(b), len(c), len(d))
# perform mixed cross-frame hash operation on pixel seeds to create and write final seeds
for i in range(size):
hashIngest = a[i] + b[size - 1 - i] + c[i] + d[size - 1 - i]
hashResultHex = hashlib.sha512((hashIngest).encode('utf-8')).hexdigest()
# write final seeds data as a ascii binary stream
f.write(hex_to_bin(hashResultHex))
# close random bit stream file
f.close()