Spaces:
Sleeping
Sleeping
Update app.py
Browse filesreturn to gradio
app.py
CHANGED
|
@@ -1,28 +1,21 @@
|
|
| 1 |
-
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
-
|
| 6 |
-
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import time
|
| 9 |
import tempfile
|
| 10 |
-
from sklearn.cluster import MiniBatchKMeans
|
| 11 |
-
from PIL import Image
|
| 12 |
from transformers import ViTImageProcessor, ViTForImageClassification
|
| 13 |
import torch
|
| 14 |
import concurrent.futures
|
| 15 |
-
import io
|
| 16 |
-
import os
|
| 17 |
|
| 18 |
-
#
|
| 19 |
model_name = "google/vit-base-patch16-224"
|
| 20 |
image_processor = ViTImageProcessor.from_pretrained(model_name)
|
| 21 |
model = ViTForImageClassification.from_pretrained(model_name)
|
| 22 |
|
| 23 |
-
app = Flask(__name__)
|
| 24 |
-
CORS(app) # 启用 CORS
|
| 25 |
-
|
| 26 |
def create_color_palette(colors, size=100):
|
| 27 |
palette = np.zeros((4*size, 4*size, 3), dtype=np.uint8)
|
| 28 |
for i in range(4):
|
|
@@ -47,7 +40,7 @@ def identify_image(image):
|
|
| 47 |
|
| 48 |
return ", ".join(results)
|
| 49 |
|
| 50 |
-
def analyze_image(image_data):
|
| 51 |
"""Analyzes the uploaded image."""
|
| 52 |
# Load the image data into a PIL Image object
|
| 53 |
image = Image.open(io.BytesIO(image_data))
|
|
@@ -118,25 +111,24 @@ def analyze_image(image_data):
|
|
| 118 |
|
| 119 |
return top_5_classes, histogram_path, edges_path, palette_path, timing_output
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
app.run(host="0.0.0.0", port=8080)
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import time
|
| 8 |
import tempfile
|
|
|
|
|
|
|
| 9 |
from transformers import ViTImageProcessor, ViTForImageClassification
|
| 10 |
import torch
|
| 11 |
import concurrent.futures
|
| 12 |
+
import io # Import io for handling byte streams
|
|
|
|
| 13 |
|
| 14 |
+
# Load the pre-trained Vision Transformer model and image processor
|
| 15 |
model_name = "google/vit-base-patch16-224"
|
| 16 |
image_processor = ViTImageProcessor.from_pretrained(model_name)
|
| 17 |
model = ViTForImageClassification.from_pretrained(model_name)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
def create_color_palette(colors, size=100):
|
| 20 |
palette = np.zeros((4*size, 4*size, 3), dtype=np.uint8)
|
| 21 |
for i in range(4):
|
|
|
|
| 40 |
|
| 41 |
return ", ".join(results)
|
| 42 |
|
| 43 |
+
def analyze_image(image_data): # Modified to accept image data
|
| 44 |
"""Analyzes the uploaded image."""
|
| 45 |
# Load the image data into a PIL Image object
|
| 46 |
image = Image.open(io.BytesIO(image_data))
|
|
|
|
| 111 |
|
| 112 |
return top_5_classes, histogram_path, edges_path, palette_path, timing_output
|
| 113 |
|
| 114 |
+
# Define Gradio interface
|
| 115 |
+
interface = gr.Interface(
|
| 116 |
+
fn=analyze_image,
|
| 117 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 118 |
+
outputs=[
|
| 119 |
+
gr.Textbox(label="Image Classification (Top 5)"),
|
| 120 |
+
gr.Image(label="Histogram"),
|
| 121 |
+
gr.Image(label="Edge Detection"),
|
| 122 |
+
gr.Image(label="Main Colors Palette"),
|
| 123 |
+
gr.Textbox(label="Processing Times")
|
| 124 |
+
],
|
| 125 |
+
title="Advanced Image Analysis",
|
| 126 |
+
description="Upload an image to analyze its classification, histogram, edges, and main colors.",
|
| 127 |
+
allow_flagging="never"
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# Set request queue parameters
|
| 131 |
+
interface.queue(max_size=5)
|
| 132 |
+
|
| 133 |
+
# Launch the interface
|
| 134 |
+
interface.launch()
|
|
|