Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import cv2
|
|
| 6 |
from PIL import Image
|
| 7 |
from tqdm import tqdm
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
|
| 10 |
# Constants
|
| 11 |
PADDING_TOKEN = b"<PAD>"
|
|
@@ -39,8 +40,10 @@ def encode(file_path):
|
|
| 39 |
chunk_file.write(chunk)
|
| 40 |
# Convert each chunk to an image
|
| 41 |
chunk_files = [os.path.join('./parts', f"{i}.part") for i in range(num_chunks)]
|
| 42 |
-
|
| 43 |
-
convert_part_to_image
|
|
|
|
|
|
|
| 44 |
# Create a video from the frames
|
| 45 |
create_video_from_frames(FRAMES_DIR, VIDEO_PATH)
|
| 46 |
return VIDEO_PATH
|
|
@@ -54,18 +57,24 @@ def decode(video_path, output_file_path):
|
|
| 54 |
extracted_frame_files = sorted([os.path.join(extracted_frames_dir, f) for f in os.listdir(extracted_frames_dir) if f.endswith('.png')])
|
| 55 |
# Convert frames back to chunk files
|
| 56 |
chunk_files = []
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
chunk_files.append(chunk_file_path)
|
| 63 |
# Merge the chunks into the original file
|
| 64 |
merge_chunks(chunk_files, output_file_path)
|
| 65 |
# Verify file integrity
|
| 66 |
original_hash = calculate_sha256(output_file_path)
|
| 67 |
return f"SHA-256 hash of the decoded file: {original_hash}"
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def binary_string_to_rgb_image(binary_string, width=1920, height=1080, output_path='output_rgb.png'):
|
| 70 |
"""Convert a binary string to an RGB image and save it."""
|
| 71 |
if len(binary_string) != REQUIRED_LENGTH * 3: # 3 bits per pixel for RGB
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from tqdm import tqdm
|
| 8 |
import gradio as gr
|
| 9 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 10 |
|
| 11 |
# Constants
|
| 12 |
PADDING_TOKEN = b"<PAD>"
|
|
|
|
| 40 |
chunk_file.write(chunk)
|
| 41 |
# Convert each chunk to an image
|
| 42 |
chunk_files = [os.path.join('./parts', f"{i}.part") for i in range(num_chunks)]
|
| 43 |
+
with ThreadPoolExecutor() as executor:
|
| 44 |
+
futures = [executor.submit(convert_part_to_image, chunk_file, i) for i, chunk_file in enumerate(chunk_files)]
|
| 45 |
+
for future in tqdm(as_completed(futures), desc="Converting chunks to images", unit="chunk", total=len(chunk_files)):
|
| 46 |
+
future.result()
|
| 47 |
# Create a video from the frames
|
| 48 |
create_video_from_frames(FRAMES_DIR, VIDEO_PATH)
|
| 49 |
return VIDEO_PATH
|
|
|
|
| 57 |
extracted_frame_files = sorted([os.path.join(extracted_frames_dir, f) for f in os.listdir(extracted_frames_dir) if f.endswith('.png')])
|
| 58 |
# Convert frames back to chunk files
|
| 59 |
chunk_files = []
|
| 60 |
+
with ThreadPoolExecutor() as executor:
|
| 61 |
+
futures = [executor.submit(convert_frame_to_chunk, frame_file, i) for i, frame_file in enumerate(extracted_frame_files)]
|
| 62 |
+
for future in tqdm(as_completed(futures), desc="Converting frames to chunks", unit="frame", total=len(extracted_frame_files)):
|
| 63 |
+
chunk_file_path = future.result()
|
| 64 |
+
chunk_files.append(chunk_file_path)
|
|
|
|
| 65 |
# Merge the chunks into the original file
|
| 66 |
merge_chunks(chunk_files, output_file_path)
|
| 67 |
# Verify file integrity
|
| 68 |
original_hash = calculate_sha256(output_file_path)
|
| 69 |
return f"SHA-256 hash of the decoded file: {original_hash}"
|
| 70 |
|
| 71 |
+
def convert_frame_to_chunk(frame_file, i):
|
| 72 |
+
binary_string = hd_rgb_image_to_binary(frame_file)
|
| 73 |
+
chunk_file_path = os.path.join('./parts', f"{i}.part")
|
| 74 |
+
with open(chunk_file_path, 'wb') as chunk_file:
|
| 75 |
+
chunk_file.write(int(binary_string, 2).to_bytes(len(binary_string) // 8, byteorder='big'))
|
| 76 |
+
return chunk_file_path
|
| 77 |
+
|
| 78 |
def binary_string_to_rgb_image(binary_string, width=1920, height=1080, output_path='output_rgb.png'):
|
| 79 |
"""Convert a binary string to an RGB image and save it."""
|
| 80 |
if len(binary_string) != REQUIRED_LENGTH * 3: # 3 bits per pixel for RGB
|