Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,13 @@ from PIL import Image
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
|
| 7 |
# Simple app: convert user input into ASCII codes and binary labels
|
| 8 |
|
| 9 |
def string_to_binary_labels(s: str) -> list[int]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
bits: list[int] = []
|
| 11 |
for char in s:
|
| 12 |
ascii_code = ord(char)
|
|
@@ -14,15 +17,27 @@ def string_to_binary_labels(s: str) -> list[int]:
|
|
| 14 |
bits.extend(char_bits)
|
| 15 |
return bits
|
| 16 |
|
| 17 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
img = img.convert("RGB")
|
| 19 |
img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels))))
|
| 20 |
img_array = np.array(img)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def binary_labels_to_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
|
|
|
|
|
|
|
|
|
|
| 26 |
total_pixels = len(binary_labels)
|
| 27 |
if width is None or height is None:
|
| 28 |
side = int(np.ceil(np.sqrt(total_pixels)))
|
|
@@ -36,14 +51,19 @@ def binary_labels_to_image(binary_labels: list[int], width: int = None, height:
|
|
| 36 |
return img
|
| 37 |
|
| 38 |
def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
|
|
|
|
|
|
|
|
|
|
| 39 |
total_pixels = len(binary_labels) // 24
|
| 40 |
if width is None or height is None:
|
| 41 |
side = int(np.ceil(np.sqrt(total_pixels)))
|
| 42 |
width = height = side
|
|
|
|
| 43 |
needed_pixels = width * height
|
| 44 |
needed_bits = needed_pixels * 24
|
| 45 |
if len(binary_labels) < needed_bits:
|
| 46 |
binary_labels += [0] * (needed_bits - len(binary_labels))
|
|
|
|
| 47 |
pixels = []
|
| 48 |
for i in range(0, needed_bits, 24):
|
| 49 |
r_bits = binary_labels[i:i+8]
|
|
@@ -53,10 +73,12 @@ def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, heig
|
|
| 53 |
g = sum(b << (7-j) for j, b in enumerate(g_bits))
|
| 54 |
b = sum(b << (7-j) for j, b in enumerate(b_bits))
|
| 55 |
pixels.append((r, g, b))
|
|
|
|
| 56 |
array = np.array(pixels, dtype=np.uint8).reshape((height, width, 3))
|
| 57 |
img = Image.fromarray(array, mode='RGB')
|
| 58 |
return img
|
| 59 |
|
|
|
|
| 60 |
mutation_site_headers = [
|
| 61 |
3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
|
| 62 |
3665, 3720, 3773, 3824, 3879, 3933, 3985, 4039,
|
|
@@ -66,6 +88,7 @@ mutation_site_headers = [
|
|
| 66 |
|
| 67 |
st.title("ASCII & Binary Label Converter")
|
| 68 |
|
|
|
|
| 69 |
tab1, tab2 = st.tabs(["Text to Binary Labels", "Image to Binary Labels"])
|
| 70 |
|
| 71 |
with tab1:
|
|
@@ -115,9 +138,9 @@ with tab2:
|
|
| 115 |
img = Image.open(uploaded_file)
|
| 116 |
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 117 |
|
| 118 |
-
max_pixels = st.slider("Max number of pixels to encode", min_value=32, max_value=
|
| 119 |
|
| 120 |
-
binary_labels =
|
| 121 |
|
| 122 |
st.subheader("Binary Labels from Image")
|
| 123 |
num_groups = (len(binary_labels) + 31) // 32
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
|
|
|
|
| 6 |
# Simple app: convert user input into ASCII codes and binary labels
|
| 7 |
|
| 8 |
def string_to_binary_labels(s: str) -> list[int]:
|
| 9 |
+
"""
|
| 10 |
+
Convert a string into a flat list of binary labels (0 or 1) representing
|
| 11 |
+
each character's 8-bit ASCII code.
|
| 12 |
+
"""
|
| 13 |
bits: list[int] = []
|
| 14 |
for char in s:
|
| 15 |
ascii_code = ord(char)
|
|
|
|
| 17 |
bits.extend(char_bits)
|
| 18 |
return bits
|
| 19 |
|
| 20 |
+
def image_to_binary_labels_rgb(img: Image.Image, max_pixels: int = 256) -> list[int]:
|
| 21 |
+
"""
|
| 22 |
+
Convert an RGB image to binary labels (0/1).
|
| 23 |
+
Store full RGB values (24 bits per pixel).
|
| 24 |
+
"""
|
| 25 |
img = img.convert("RGB")
|
| 26 |
img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels))))
|
| 27 |
img_array = np.array(img)
|
| 28 |
+
flat_pixels = img_array.reshape(-1, 3)
|
| 29 |
+
|
| 30 |
+
bits = []
|
| 31 |
+
for pixel in flat_pixels:
|
| 32 |
+
for channel in pixel: # R, G, B
|
| 33 |
+
channel_bits = [(channel >> bit) & 1 for bit in range(7, -1, -1)]
|
| 34 |
+
bits.extend(channel_bits)
|
| 35 |
+
return bits
|
| 36 |
|
| 37 |
def binary_labels_to_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
|
| 38 |
+
"""
|
| 39 |
+
Convert binary labels (0/1) into a grayscale image.
|
| 40 |
+
"""
|
| 41 |
total_pixels = len(binary_labels)
|
| 42 |
if width is None or height is None:
|
| 43 |
side = int(np.ceil(np.sqrt(total_pixels)))
|
|
|
|
| 51 |
return img
|
| 52 |
|
| 53 |
def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
|
| 54 |
+
"""
|
| 55 |
+
Convert binary labels (0/1) into an RGB image.
|
| 56 |
+
"""
|
| 57 |
total_pixels = len(binary_labels) // 24
|
| 58 |
if width is None or height is None:
|
| 59 |
side = int(np.ceil(np.sqrt(total_pixels)))
|
| 60 |
width = height = side
|
| 61 |
+
|
| 62 |
needed_pixels = width * height
|
| 63 |
needed_bits = needed_pixels * 24
|
| 64 |
if len(binary_labels) < needed_bits:
|
| 65 |
binary_labels += [0] * (needed_bits - len(binary_labels))
|
| 66 |
+
|
| 67 |
pixels = []
|
| 68 |
for i in range(0, needed_bits, 24):
|
| 69 |
r_bits = binary_labels[i:i+8]
|
|
|
|
| 73 |
g = sum(b << (7-j) for j, b in enumerate(g_bits))
|
| 74 |
b = sum(b << (7-j) for j, b in enumerate(b_bits))
|
| 75 |
pixels.append((r, g, b))
|
| 76 |
+
|
| 77 |
array = np.array(pixels, dtype=np.uint8).reshape((height, width, 3))
|
| 78 |
img = Image.fromarray(array, mode='RGB')
|
| 79 |
return img
|
| 80 |
|
| 81 |
+
# Predefined headers for the 32 mutation sites
|
| 82 |
mutation_site_headers = [
|
| 83 |
3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
|
| 84 |
3665, 3720, 3773, 3824, 3879, 3933, 3985, 4039,
|
|
|
|
| 88 |
|
| 89 |
st.title("ASCII & Binary Label Converter")
|
| 90 |
|
| 91 |
+
# Create tabs
|
| 92 |
tab1, tab2 = st.tabs(["Text to Binary Labels", "Image to Binary Labels"])
|
| 93 |
|
| 94 |
with tab1:
|
|
|
|
| 138 |
img = Image.open(uploaded_file)
|
| 139 |
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 140 |
|
| 141 |
+
max_pixels = st.slider("Max number of pixels to encode", min_value=32, max_value=1024, value=256, step=32)
|
| 142 |
|
| 143 |
+
binary_labels = image_to_binary_labels_rgb(img, max_pixels=max_pixels)
|
| 144 |
|
| 145 |
st.subheader("Binary Labels from Image")
|
| 146 |
num_groups = (len(binary_labels) + 31) // 32
|