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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image, ImageFilter, ImageEnhance, ImageOps
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# ---- Load models (cached on first use) ----
|
| 7 |
+
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 8 |
+
detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
| 9 |
+
segmenter = pipeline("image-segmentation", model="facebook/detr-resnet-50-panoptic")
|
| 10 |
+
|
| 11 |
+
# ---- Tab 1: Filters & Effects ----
|
| 12 |
+
def apply_filter(image, effect, intensity):
|
| 13 |
+
if image is None:
|
| 14 |
+
raise gr.Error("Please upload an image first.")
|
| 15 |
+
|
| 16 |
+
img = Image.fromarray(image)
|
| 17 |
+
|
| 18 |
+
if effect == "Grayscale":
|
| 19 |
+
filtered = ImageOps.grayscale(img).convert("RGB")
|
| 20 |
+
if intensity < 1.0:
|
| 21 |
+
filtered = Image.blend(img, filtered, intensity)
|
| 22 |
+
elif effect == "Sepia":
|
| 23 |
+
gray = ImageOps.grayscale(img)
|
| 24 |
+
sepia = ImageOps.colorize(gray, "#704214", "#C0A080")
|
| 25 |
+
filtered = Image.blend(img, sepia, intensity)
|
| 26 |
+
elif effect == "Blur":
|
| 27 |
+
radius = int(intensity * 10)
|
| 28 |
+
filtered = img.filter(ImageFilter.GaussianBlur(radius=max(1, radius)))
|
| 29 |
+
elif effect == "Sharpen":
|
| 30 |
+
enhancer = ImageEnhance.Sharpness(img)
|
| 31 |
+
filtered = enhancer.enhance(1 + intensity * 4)
|
| 32 |
+
elif effect == "Edge Detect":
|
| 33 |
+
edges = img.filter(ImageFilter.FIND_EDGES)
|
| 34 |
+
filtered = Image.blend(img, edges, intensity)
|
| 35 |
+
elif effect == "Emboss":
|
| 36 |
+
embossed = img.filter(ImageFilter.EMBOSS)
|
| 37 |
+
filtered = Image.blend(img, embossed, intensity)
|
| 38 |
+
elif effect == "Invert":
|
| 39 |
+
inverted = ImageOps.invert(img.convert("RGB"))
|
| 40 |
+
filtered = Image.blend(img, inverted, intensity)
|
| 41 |
+
elif effect == "Posterize":
|
| 42 |
+
bits = max(1, int(8 - intensity * 6))
|
| 43 |
+
filtered = ImageOps.posterize(img.convert("RGB"), bits)
|
| 44 |
+
elif effect == "Brightness":
|
| 45 |
+
enhancer = ImageEnhance.Brightness(img)
|
| 46 |
+
filtered = enhancer.enhance(0.5 + intensity * 1.5)
|
| 47 |
+
elif effect == "Contrast":
|
| 48 |
+
enhancer = ImageEnhance.Contrast(img)
|
| 49 |
+
filtered = enhancer.enhance(0.5 + intensity * 2)
|
| 50 |
+
else:
|
| 51 |
+
filtered = img
|
| 52 |
+
|
| 53 |
+
return np.array(filtered)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# ---- Tab 2: Image Classification ----
|
| 57 |
+
def classify_image(image):
|
| 58 |
+
if image is None:
|
| 59 |
+
raise gr.Error("Please upload an image first.")
|
| 60 |
+
img = Image.fromarray(image)
|
| 61 |
+
results = classifier(img)
|
| 62 |
+
return {r["label"]: r["score"] for r in results}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# ---- Tab 3: Object Detection ----
|
| 66 |
+
def detect_objects(image, threshold):
|
| 67 |
+
if image is None:
|
| 68 |
+
raise gr.Error("Please upload an image first.")
|
| 69 |
+
img = Image.fromarray(image)
|
| 70 |
+
results = detector(img, threshold=threshold)
|
| 71 |
+
|
| 72 |
+
annotations = []
|
| 73 |
+
for r in results:
|
| 74 |
+
box = r["box"]
|
| 75 |
+
annotations.append((
|
| 76 |
+
(box["xmin"], box["ymin"], box["xmax"], box["ymax"]),
|
| 77 |
+
f"{r['label']} ({r['score']:.0%})"
|
| 78 |
+
))
|
| 79 |
+
|
| 80 |
+
return (image, annotations)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# ---- Tab 4: Segmentation ----
|
| 84 |
+
def segment_image(image):
|
| 85 |
+
if image is None:
|
| 86 |
+
raise gr.Error("Please upload an image first.")
|
| 87 |
+
img = Image.fromarray(image)
|
| 88 |
+
results = segmenter(img)
|
| 89 |
+
|
| 90 |
+
annotations = []
|
| 91 |
+
for r in results:
|
| 92 |
+
mask = np.array(r["mask"])
|
| 93 |
+
annotations.append((mask, r["label"]))
|
| 94 |
+
|
| 95 |
+
return (image, annotations)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# ---- Build the UI ----
|
| 99 |
+
css = """
|
| 100 |
+
.main-title { text-align: center; margin-bottom: 0.5em; }
|
| 101 |
+
.subtitle { text-align: center; color: #666; margin-top: 0; }
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 105 |
+
gr.Markdown("# Image Processing Studio", elem_classes="main-title")
|
| 106 |
+
gr.Markdown(
|
| 107 |
+
"Upload an image and explore filters, classification, object detection, and segmentation -- all powered by state-of-the-art models.",
|
| 108 |
+
elem_classes="subtitle"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
with gr.Tab("Filters & Effects"):
|
| 112 |
+
with gr.Row():
|
| 113 |
+
with gr.Column():
|
| 114 |
+
filter_input = gr.Image(label="Upload Image", type="numpy")
|
| 115 |
+
filter_effect = gr.Dropdown(
|
| 116 |
+
choices=[
|
| 117 |
+
"Grayscale", "Sepia", "Blur", "Sharpen",
|
| 118 |
+
"Edge Detect", "Emboss", "Invert", "Posterize",
|
| 119 |
+
"Brightness", "Contrast"
|
| 120 |
+
],
|
| 121 |
+
value="Sepia",
|
| 122 |
+
label="Effect"
|
| 123 |
+
)
|
| 124 |
+
filter_intensity = gr.Slider(
|
| 125 |
+
minimum=0.0, maximum=1.0, value=0.7, step=0.05,
|
| 126 |
+
label="Intensity"
|
| 127 |
+
)
|
| 128 |
+
filter_btn = gr.Button("Apply Filter", variant="primary")
|
| 129 |
+
with gr.Column():
|
| 130 |
+
filter_output = gr.Image(label="Result", type="numpy")
|
| 131 |
+
|
| 132 |
+
filter_btn.click(
|
| 133 |
+
fn=apply_filter,
|
| 134 |
+
inputs=[filter_input, filter_effect, filter_intensity],
|
| 135 |
+
outputs=filter_output
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
with gr.Tab("Image Classification"):
|
| 139 |
+
with gr.Row():
|
| 140 |
+
with gr.Column():
|
| 141 |
+
cls_input = gr.Image(label="Upload Image", type="numpy")
|
| 142 |
+
cls_btn = gr.Button("Classify", variant="primary")
|
| 143 |
+
with gr.Column():
|
| 144 |
+
cls_output = gr.Label(label="Predictions", num_top_classes=5)
|
| 145 |
+
|
| 146 |
+
cls_btn.click(
|
| 147 |
+
fn=classify_image,
|
| 148 |
+
inputs=cls_input,
|
| 149 |
+
outputs=cls_output
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
with gr.Tab("Object Detection"):
|
| 153 |
+
with gr.Row():
|
| 154 |
+
with gr.Column():
|
| 155 |
+
det_input = gr.Image(label="Upload Image", type="numpy")
|
| 156 |
+
det_threshold = gr.Slider(
|
| 157 |
+
minimum=0.1, maximum=0.95, value=0.5, step=0.05,
|
| 158 |
+
label="Confidence Threshold"
|
| 159 |
+
)
|
| 160 |
+
det_btn = gr.Button("Detect Objects", variant="primary")
|
| 161 |
+
with gr.Column():
|
| 162 |
+
det_output = gr.AnnotatedImage(label="Detections")
|
| 163 |
+
|
| 164 |
+
det_btn.click(
|
| 165 |
+
fn=detect_objects,
|
| 166 |
+
inputs=[det_input, det_threshold],
|
| 167 |
+
outputs=det_output
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
with gr.Tab("Segmentation"):
|
| 171 |
+
with gr.Row():
|
| 172 |
+
with gr.Column():
|
| 173 |
+
seg_input = gr.Image(label="Upload Image", type="numpy")
|
| 174 |
+
seg_btn = gr.Button("Segment", variant="primary")
|
| 175 |
+
with gr.Column():
|
| 176 |
+
seg_output = gr.AnnotatedImage(label="Segmentation Map")
|
| 177 |
+
|
| 178 |
+
seg_btn.click(
|
| 179 |
+
fn=segment_image,
|
| 180 |
+
inputs=seg_input,
|
| 181 |
+
outputs=seg_output
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
demo.launch()
|