Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +3 -56
api_server.py
CHANGED
@@ -24,8 +24,8 @@ load_type = 'local'
|
|
24 |
MODEL_YOLO = "yolo11_detect_best_241024_1.pt"
|
25 |
MODEL_DIR = "./artifacts/models"
|
26 |
YOLO_DIR = "./artifacts/yolo"
|
27 |
-
GRADIO_URL = "https://
|
28 |
-
|
29 |
|
30 |
# Load the saved YOLO model into memory
|
31 |
if load_type == 'local':
|
@@ -98,10 +98,6 @@ check_memory_usage()
|
|
98 |
|
99 |
# Initialize the Flask application
|
100 |
app = Flask(__name__)
|
101 |
-
# # Initialize the ClipModel at the start
|
102 |
-
# clip_model = ClipModel()
|
103 |
-
|
104 |
-
|
105 |
|
106 |
# API route for prediction(YOLO)
|
107 |
@app.route('/predict', methods=['POST'])
|
@@ -114,7 +110,6 @@ def predict():
|
|
114 |
if 'image' not in request.files:
|
115 |
# Handle if no file is selected
|
116 |
return jsonify({"error": "No image part"}), 400
|
117 |
-
#return 'No file selected'
|
118 |
|
119 |
# 讀取圖像
|
120 |
try:
|
@@ -177,17 +172,6 @@ def predict():
|
|
177 |
encoded_images.append(image_to_base64(yolo_img))
|
178 |
element_list.append(element)
|
179 |
print(f"===== CLIP result:{top_k_words} =====\n")
|
180 |
-
|
181 |
-
# if element_counts[element] > 1: #某隻角色的數量>1
|
182 |
-
# yolo_path = f"{YOLO_DIR}/{message_id}/{element}"
|
183 |
-
# yolo_file = get_jpg_files(yolo_path)
|
184 |
-
|
185 |
-
# for yolo_img in yolo_file: # 取得每張圖的路徑
|
186 |
-
# encoded_images.append(image_to_base64(yolo_img))
|
187 |
-
|
188 |
-
# else : #某隻角色的數量=1
|
189 |
-
# yolo_path = f"{YOLO_DIR}/{message_id}/{element}/im.jpg.jpg"
|
190 |
-
# encoded_images.append(image_to_base64(yolo_path))
|
191 |
|
192 |
# 建立回應資料
|
193 |
response_data = {
|
@@ -205,44 +189,7 @@ def predict():
|
|
205 |
]
|
206 |
}
|
207 |
|
208 |
-
return jsonify(response_data), 200
|
209 |
-
|
210 |
-
# for label_name in label_names:
|
211 |
-
# yolo_file=f"{YOLO_DIR}/{message_id}/{label_name}/im.jpg.jpg"
|
212 |
-
# # 將圖片轉換為 base64 編碼
|
213 |
-
# encoded_images.append(image_to_base64(yolo_file))
|
214 |
-
|
215 |
-
|
216 |
-
# # dictionary is not a JSON: https://www.quora.com/What-is-the-difference-between-JSON-and-a-dictionary
|
217 |
-
# # flask.jsonify vs json.dumps https://sentry.io/answers/difference-between-json-dumps-and-flask-jsonify/
|
218 |
-
# # The flask.jsonify() function returns a Response object with Serializable JSON and content_type=application/json.
|
219 |
-
# return jsonify(response)
|
220 |
-
|
221 |
-
|
222 |
-
# # Helper function to preprocess the image
|
223 |
-
# def preprocess_image(image_data):
|
224 |
-
# """Preprocess image for YOLO Model Inference
|
225 |
-
|
226 |
-
# :param image_data: Raw image (PIL.Image)
|
227 |
-
# :return: image: Preprocessed Image (Tensor)
|
228 |
-
# """
|
229 |
-
# # Define the YOLO input size (example 640x640, you can modify this based on your model)
|
230 |
-
# input_size = (640, 640)
|
231 |
-
|
232 |
-
# # Define transformation: Resize the image, convert to Tensor, and normalize pixel values
|
233 |
-
# transform = transforms.Compose([
|
234 |
-
# transforms.Resize(input_size), # Resize to YOLO input size
|
235 |
-
# transforms.ToTensor(), # Convert image to PyTorch Tensor (通道數、影像高度和寬度)
|
236 |
-
# transforms.Normalize([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]) # Normalization (if needed)
|
237 |
-
# ])
|
238 |
-
|
239 |
-
# # Apply transformations to the image
|
240 |
-
# image = transform(image_data)
|
241 |
-
|
242 |
-
# # Add batch dimension (1, C, H, W) since YOLO expects a batch
|
243 |
-
# image = image.unsqueeze(0)
|
244 |
-
|
245 |
-
# return image
|
246 |
|
247 |
|
248 |
# API route for health check
|
|
|
24 |
MODEL_YOLO = "yolo11_detect_best_241024_1.pt"
|
25 |
MODEL_DIR = "./artifacts/models"
|
26 |
YOLO_DIR = "./artifacts/yolo"
|
27 |
+
GRADIO_URL = "https://fd39e54bcb191a37bf.gradio.live/"
|
28 |
+
|
29 |
|
30 |
# Load the saved YOLO model into memory
|
31 |
if load_type == 'local':
|
|
|
98 |
|
99 |
# Initialize the Flask application
|
100 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
101 |
|
102 |
# API route for prediction(YOLO)
|
103 |
@app.route('/predict', methods=['POST'])
|
|
|
110 |
if 'image' not in request.files:
|
111 |
# Handle if no file is selected
|
112 |
return jsonify({"error": "No image part"}), 400
|
|
|
113 |
|
114 |
# 讀取圖像
|
115 |
try:
|
|
|
172 |
encoded_images.append(image_to_base64(yolo_img))
|
173 |
element_list.append(element)
|
174 |
print(f"===== CLIP result:{top_k_words} =====\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
# 建立回應資料
|
177 |
response_data = {
|
|
|
189 |
]
|
190 |
}
|
191 |
|
192 |
+
return jsonify(response_data), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
|
195 |
# API route for health check
|