TibbtechUser commited on
Commit
acbd068
·
verified ·
1 Parent(s): 0f66727

updated app.py

Browse files
Files changed (3) hide show
  1. .gitattributes +1 -0
  2. app.py +80 -56
  3. 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, ImageOps
3
  import numpy as np
4
- from tibblingai import wta
5
- import matplotlib.pyplot as plt # needed for plt.imread
6
- def load_sample_images():
7
- sample_paths = ["sample1.jpg", "sample2.jpg"] # Must be in the same folder as your script
8
- original_imgs = [Image.open(path) for path in sample_paths]
9
- processed_imgs = [process_image(path) for path in sample_paths]
10
- return gr.update(visible=True, value=original_imgs), gr.update(visible=True, value=processed_imgs)
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
- # Convert to grayscale for original image display
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 gray_img, inferno_img
33
 
34
- # Function to process uploaded images
35
- def process_images(images):
36
- if not isinstance(images, list):
37
- images = [images]
38
 
39
- original_imgs = []
40
- processed_imgs = []
41
 
42
- for img in images:
43
- original_gray_scale_img, inferno_img = process_image(img)
44
- original_imgs.append(original_gray_scale_img)
45
- processed_imgs.append(inferno_img)
46
 
47
- return gr.update(visible=True, value=original_imgs), gr.update(visible=True, value=processed_imgs)
 
 
48
 
49
- # Gradio Interface
50
- with gr.Blocks() as demo:
51
- gr.Markdown("## Upload Image(s) for Processing")
 
 
 
52
 
53
- # File component (no preview shown here)
54
- file_input = gr.File(file_types=["image"],file_count="multiple", label="Upload Images")
55
 
56
- with gr.Row():
57
- original_gallery = gr.Gallery(label="Original Images", visible=False)
58
- processed_gallery = gr.Gallery(label="Processed Images", visible=False)
 
59
 
60
- file_input.change(fn=process_images, inputs=file_input, outputs=[original_gallery, processed_gallery])
61
- with gr.Row():
62
- with gr.Column(scale=0, min_width=300):
63
- load_examples_btn = gr.Button("Run WTA on sample images")
64
- load_examples_btn.click(fn=load_sample_images, outputs=[original_gallery, processed_gallery])
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
 
 
 
 
 
66
 
67
- # 🆕 Examples section
68
- # examples = gr.Examples(
69
- # examples=[["sample1.jpg"], ["sample2.jpg"]],
70
- # inputs=file_input,
71
- # label="Example Images"
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