Spaces:
Sleeping
Sleeping
updated app.py
Browse files- .gitattributes +1 -0
- app.py +80 -56
- video_sample.mp4 +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
video_sample.mp4 filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -1,76 +1,100 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Image processing function (you can replace this)
|
| 15 |
-
def process_image(img: Image.Image) -> Image.Image:
|
| 16 |
-
# Read image using matplotlib
|
| 17 |
-
img_np = plt.imread(img)
|
| 18 |
if img_np.ndim == 3 and img_np.shape[2] > 3:
|
| 19 |
img_np = img_np[:, :, :3]
|
| 20 |
|
| 21 |
-
|
| 22 |
-
gray_img = ImageOps.grayscale(Image.fromarray((img_np * 255).astype(np.uint8)))
|
| 23 |
-
|
| 24 |
-
# Run your WTA processing (dummy if not available)
|
| 25 |
-
# Replace this line with actual WTA processing
|
| 26 |
-
img_tensor = wta.wta(img_np).numpy() # Assuming returns shape (1, H, W)
|
| 27 |
-
|
| 28 |
-
# Convert processed image to inferno colormap
|
| 29 |
inferno_colored = plt.cm.inferno(img_tensor[0])
|
| 30 |
inferno_img = Image.fromarray((inferno_colored[:, :, :3] * 255).astype(np.uint8))
|
| 31 |
|
| 32 |
-
return
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
def
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
processed_imgs = []
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
original_imgs.append(original_gray_scale_img)
|
| 45 |
-
processed_imgs.append(inferno_img)
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
file_input = gr.File(file_types=["image"],file_count="multiple", label="Upload Images")
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
# example_files = [["sample1.jpg", "sample2.jpg"], ["sample2.jpg"]]
|
| 75 |
-
# gr.Examples(examples=example_files, inputs=[file_input], label="Try one of our example samples")
|
| 76 |
demo.launch(debug=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from tibbtech import neuroai
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
import imageio
|
| 9 |
+
from matplotlib import cm
|
| 10 |
+
|
| 11 |
+
# Image processing
|
| 12 |
+
def process_image(img_path: str):
|
| 13 |
+
img_np = plt.imread(img_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
if img_np.ndim == 3 and img_np.shape[2] > 3:
|
| 15 |
img_np = img_np[:, :, :3]
|
| 16 |
|
| 17 |
+
img_tensor = neuroai.wta(img_np).numpy() # (1,H,W)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
inferno_colored = plt.cm.inferno(img_tensor[0])
|
| 19 |
inferno_img = Image.fromarray((inferno_colored[:, :, :3] * 255).astype(np.uint8))
|
| 20 |
|
| 21 |
+
return img_path, inferno_img # return path for original gallery
|
| 22 |
|
| 23 |
+
# Video processing with imageio (more browser-compatible)
|
| 24 |
+
def process_video(video_path: str):
|
| 25 |
+
video_tensor = neuroai.run_wta(video_path) # (T,H,W)
|
| 26 |
+
T, H, W = video_tensor.shape
|
| 27 |
|
| 28 |
+
temp_path = tempfile.mktemp(suffix=".mp4")
|
|
|
|
| 29 |
|
| 30 |
+
# Normalize video to [0,1]
|
| 31 |
+
video_tensor = np.clip(video_tensor, 0, 1)
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
writer = imageio.get_writer(
|
| 34 |
+
temp_path, fps=30, codec="libx264", ffmpeg_params=["-pix_fmt", "yuv420p"]
|
| 35 |
+
)
|
| 36 |
|
| 37 |
+
cmap = cm.get_cmap("inferno")
|
| 38 |
+
for t in range(T):
|
| 39 |
+
frame = video_tensor[t]
|
| 40 |
+
colored = (cmap(frame)[..., :3] * 255).astype(np.uint8)
|
| 41 |
+
writer.append_data(colored)
|
| 42 |
+
writer.close()
|
| 43 |
|
| 44 |
+
return video_path, temp_path
|
|
|
|
| 45 |
|
| 46 |
+
# Unified processing
|
| 47 |
+
def process_files(files):
|
| 48 |
+
if not isinstance(files, list):
|
| 49 |
+
files = [files]
|
| 50 |
|
| 51 |
+
originals = []
|
| 52 |
+
processed = []
|
| 53 |
+
|
| 54 |
+
for file in files:
|
| 55 |
+
ext = os.path.splitext(file.name)[1].lower()
|
| 56 |
+
if ext in [".jpg", ".jpeg", ".png", ".bmp", ".tiff"]:
|
| 57 |
+
orig, proc = process_image(file.name)
|
| 58 |
+
originals.append(orig)
|
| 59 |
+
processed.append(proc)
|
| 60 |
+
elif ext in [".mp4", ".avi", ".mov", ".mkv"]:
|
| 61 |
+
orig, proc = process_video(file.name)
|
| 62 |
+
originals.append(orig)
|
| 63 |
+
processed.append(proc)
|
| 64 |
+
|
| 65 |
+
return (
|
| 66 |
+
gr.update(visible=bool(originals), value=originals),
|
| 67 |
+
gr.update(visible=bool(processed), value=processed)
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
# Gradio interface
|
| 71 |
+
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("## Upload Image(s) or Video(s) for Processing")
|
| 73 |
+
|
| 74 |
+
file_input = gr.File(file_types=["image", "video"], label="Upload Images/Videos", )
|
| 75 |
|
| 76 |
+
with gr.Row():
|
| 77 |
+
original_gallery = gr.Gallery(label="Original", visible=False)
|
| 78 |
+
processed_gallery = gr.Gallery(label="Processed", visible=False)
|
| 79 |
+
|
| 80 |
+
file_input.change(
|
| 81 |
+
fn=process_files,
|
| 82 |
+
inputs=file_input,
|
| 83 |
+
outputs=[original_gallery, processed_gallery]
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
example_files = [
|
| 87 |
+
["sample1.jpg"], # image
|
| 88 |
+
["sample2.jpg"], # image
|
| 89 |
+
["video_sample.mp4"] # video
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
gr.Examples(
|
| 93 |
+
examples=example_files,
|
| 94 |
+
inputs=file_input,
|
| 95 |
+
fn=process_files,
|
| 96 |
+
outputs=[original_gallery, processed_gallery],
|
| 97 |
+
label="Try our sample images or video"
|
| 98 |
+
)
|
| 99 |
|
|
|
|
|
|
|
| 100 |
demo.launch(debug=True)
|
video_sample.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2783b6f1f3a78a08086512d50babc8f5d90fa0e630ec7ef49acada9be759af4d
|
| 3 |
+
size 242175
|