-
Notifications
You must be signed in to change notification settings - Fork 14
/
decoder.py
52 lines (45 loc) · 1.52 KB
/
decoder.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
# decoder.py
import cv2
import numpy as np
import time
start_time = time.time()
def video_to_frames(video_path):
"""Extracts frames from a video."""
cap = cv2.VideoCapture(video_path)
frames = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frames.append(frame)
cap.release()
return frames
def frames_to_binary(frames, pixel_size=10):
"""Converts frames to binary data."""
binary_data = ""
for frame in frames:
height, width = frame.shape[:2]
for y in range(0, height, pixel_size):
for x in range(0, width, pixel_size):
pixel = frame[y:y+pixel_size, x:x+pixel_size]
if np.mean(pixel) < 128: # Assuming black pixel represents '1', checking average of pixels
binary_data += '1'
else:
binary_data += '0'
return binary_data
def binary_to_file(binary_data, output_file_path):
"""Converts binary data to a file."""
byte_array = bytearray()
for i in range(0, len(binary_data), 8):
byte = binary_data[i:i+8]
byte_array.append(int(byte, 2))
with open(output_file_path, 'wb') as file:
file.write(byte_array)
# Example usage
if __name__ == "__main__":
video_path = 'data_video.mp4'
frames = video_to_frames(video_path)
binary_data = frames_to_binary(frames)
binary_to_file(binary_data, 'output2.png')
end_time = time.time()
print(f"Decoding completed in {end_time - start_time:.2f} seconds.")