Cookiesukaze commited on
Commit
67ec2b5
·
verified ·
1 Parent(s): 314fb7d

Update app.py

Browse files

return to gradio

Files changed (1) hide show
  1. app.py +27 -35
app.py CHANGED
@@ -1,28 +1,21 @@
1
- from flask import Flask, request, jsonify
2
- from flask_cors import CORS
3
  import cv2
4
  import numpy as np
5
- import matplotlib
6
- matplotlib.use('Agg') # 非GUI后端
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
- # 加载预训练的 ViT 模型和图像处理器
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
- @app.route('/spawn', methods=['POST'])
122
- def spawn():
123
- # 获取上传的图像数据
124
- image_data = request.files['image'].read()
125
-
126
- # 调用分析图像的函数
127
- top_5_classes, histogram_path, edges_path, palette_path, timing_output = analyze_image(image_data)
128
-
129
- # 构建响应数据
130
- response_data = {
131
- 'top_5_classes': top_5_classes,
132
- 'histogram_path': histogram_path,
133
- 'edges_path': edges_path,
134
- 'palette_path': palette_path,
135
- 'timing_output': timing_output
136
- }
137
-
138
- # 返回 JSON 响应
139
- return jsonify(response_data)
140
-
141
- if __name__ == "__main__":
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()