Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,21 @@ import numpy as np
|
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
import random
|
|
|
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
yolo_model = YOLO("yolov8n-seg.pt") # change to yolov8s-seg.pt for more accuracy
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
| 12 |
def segment_image(image: Image.Image):
|
| 13 |
results = yolo_model.predict(np.array(image))[0]
|
| 14 |
|
|
@@ -25,7 +35,9 @@ def segment_image(image: Image.Image):
|
|
| 25 |
overlay_img = Image.fromarray(overlay)
|
| 26 |
return (overlay_img, annotations)
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
| 29 |
def segment_video(video):
|
| 30 |
cap = cv2.VideoCapture(video)
|
| 31 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
|
@@ -54,18 +66,33 @@ def segment_video(video):
|
|
| 54 |
out.release()
|
| 55 |
return out_path
|
| 56 |
|
| 57 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
with gr.Tab("Image Segmentation"):
|
| 62 |
inp_img = gr.Image(type="pil", label="Upload Image")
|
| 63 |
out_img = gr.Image(type="pil", label="Segmented Image")
|
| 64 |
out_ann = gr.JSON(label="Annotations")
|
| 65 |
btn_img = gr.Button("Run Segmentation")
|
| 66 |
btn_img.click(segment_image, inputs=inp_img, outputs=[out_img, out_ann])
|
| 67 |
|
| 68 |
-
with gr.Tab("Video Segmentation"):
|
| 69 |
inp_vid = gr.Video(label="Upload Video")
|
| 70 |
out_vid = gr.Video(label="Segmented Video")
|
| 71 |
btn_vid = gr.Button("Run Segmentation")
|
|
|
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
import random
|
| 7 |
+
from transformers import pipeline
|
| 8 |
|
| 9 |
+
# ---------------------------
|
| 10 |
+
# Load Models
|
| 11 |
+
# ---------------------------
|
| 12 |
+
|
| 13 |
+
# Text model (tiny LLM)
|
| 14 |
+
text_gen = pipeline("text-generation", model="tiny-random-gpt2")
|
| 15 |
+
|
| 16 |
+
# YOLOv8 segmentation (nano version for speed)
|
| 17 |
yolo_model = YOLO("yolov8n-seg.pt") # change to yolov8s-seg.pt for more accuracy
|
| 18 |
|
| 19 |
+
# ---------------------------
|
| 20 |
+
# Image Segmentation
|
| 21 |
+
# ---------------------------
|
| 22 |
def segment_image(image: Image.Image):
|
| 23 |
results = yolo_model.predict(np.array(image))[0]
|
| 24 |
|
|
|
|
| 35 |
overlay_img = Image.fromarray(overlay)
|
| 36 |
return (overlay_img, annotations)
|
| 37 |
|
| 38 |
+
# ---------------------------
|
| 39 |
+
# Video Segmentation
|
| 40 |
+
# ---------------------------
|
| 41 |
def segment_video(video):
|
| 42 |
cap = cv2.VideoCapture(video)
|
| 43 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
|
|
|
| 66 |
out.release()
|
| 67 |
return out_path
|
| 68 |
|
| 69 |
+
# ---------------------------
|
| 70 |
+
# Text Generation
|
| 71 |
+
# ---------------------------
|
| 72 |
+
def generate_text(prompt):
|
| 73 |
+
result = text_gen(prompt, max_length=100, num_return_sequences=1)
|
| 74 |
+
return result[0]["generated_text"]
|
| 75 |
+
|
| 76 |
+
# ---------------------------
|
| 77 |
+
# Gradio UI
|
| 78 |
+
# ---------------------------
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("# 🔥 Multi-Modal Playground\nTry out **Text + Image + Video Segmentation** in one app!")
|
| 81 |
+
|
| 82 |
+
with gr.Tab("💬 Text Generation"):
|
| 83 |
+
inp_text = gr.Textbox(label="Enter your prompt")
|
| 84 |
+
out_text = gr.Textbox(label="Generated text")
|
| 85 |
+
btn_text = gr.Button("Generate")
|
| 86 |
+
btn_text.click(generate_text, inputs=inp_text, outputs=out_text)
|
| 87 |
|
| 88 |
+
with gr.Tab("🖼️ Image Segmentation"):
|
| 89 |
inp_img = gr.Image(type="pil", label="Upload Image")
|
| 90 |
out_img = gr.Image(type="pil", label="Segmented Image")
|
| 91 |
out_ann = gr.JSON(label="Annotations")
|
| 92 |
btn_img = gr.Button("Run Segmentation")
|
| 93 |
btn_img.click(segment_image, inputs=inp_img, outputs=[out_img, out_ann])
|
| 94 |
|
| 95 |
+
with gr.Tab("🎥 Video Segmentation"):
|
| 96 |
inp_vid = gr.Video(label="Upload Video")
|
| 97 |
out_vid = gr.Video(label="Segmented Video")
|
| 98 |
btn_vid = gr.Button("Run Segmentation")
|