Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +24 -22
api_server.py
CHANGED
|
@@ -14,6 +14,7 @@ from flask import Flask, jsonify, request, render_template, send_file
|
|
| 14 |
import torch
|
| 15 |
from collections import Counter
|
| 16 |
from clip_model import ClipModel
|
|
|
|
| 17 |
|
| 18 |
# Disable tensorflow warnings
|
| 19 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
|
@@ -75,7 +76,6 @@ def get_jpg_files(path):
|
|
| 75 |
# image_folder = '/content/drive/MyDrive/chiikawa' # 替換成你的目錄路徑
|
| 76 |
# jpg_files = get_jpg_files(image_folder)
|
| 77 |
|
| 78 |
-
import psutil
|
| 79 |
|
| 80 |
def check_memory_usage():
|
| 81 |
# Get memory details
|
|
@@ -86,20 +86,21 @@ def check_memory_usage():
|
|
| 86 |
used_memory = memory_info.used / (1024 * 1024)
|
| 87 |
memory_usage_percent = memory_info.percent
|
| 88 |
|
| 89 |
-
print(f"Total Memory: {total_memory:.2f} MB")
|
| 90 |
-
print(f"Available Memory: {available_memory:.2f} MB")
|
| 91 |
-
print(f"Used Memory: {used_memory:.2f} MB")
|
| 92 |
-
print(f"Memory Usage (%): {memory_usage_percent}
|
| 93 |
|
| 94 |
# Run the function
|
| 95 |
check_memory_usage()
|
| 96 |
-
|
|
|
|
| 97 |
# Initialize the Flask application
|
| 98 |
app = Flask(__name__)
|
| 99 |
# Initialize the ClipModel at the start
|
| 100 |
clip_model = ClipModel()
|
| 101 |
|
| 102 |
-
|
| 103 |
|
| 104 |
# API route for prediction(YOLO)
|
| 105 |
@app.route('/predict', methods=['POST'])
|
|
@@ -125,6 +126,7 @@ def predict():
|
|
| 125 |
results = model(image_data)
|
| 126 |
print ("***** YOLO predict result:",results,"********")
|
| 127 |
print("***** YOLO predict DONE *****")
|
|
|
|
| 128 |
check_memory_usage()
|
| 129 |
|
| 130 |
# 檢查 YOLO 是否返回了有效的結果
|
|
@@ -147,7 +149,7 @@ def predict():
|
|
| 147 |
labels = result.boxes.cls # Get predicted label IDs
|
| 148 |
label_names = [model.names[int(label)] for label in labels] # Convert to names
|
| 149 |
|
| 150 |
-
print(f"======YOLO
|
| 151 |
|
| 152 |
element_counts = Counter(label_names)
|
| 153 |
|
|
@@ -165,10 +167,10 @@ def predict():
|
|
| 165 |
element_list.append(element)
|
| 166 |
|
| 167 |
for yolo_img in yolo_file: # 每張切圖yolo_img
|
| 168 |
-
print("*****START CLIP *****")
|
| 169 |
top_k_words.append(clip_model.clip_result(yolo_img)) # CLIP預測3個結果(top_k_words)
|
| 170 |
#encoded_images.append(image_to_base64(yolo_img))
|
| 171 |
-
print(f"
|
| 172 |
|
| 173 |
# if element_counts[element] > 1: #某隻角色的數量>1
|
| 174 |
# yolo_path = f"{YOLO_DIR}/{message_id}/{element}"
|
|
@@ -181,18 +183,18 @@ def predict():
|
|
| 181 |
# yolo_path = f"{YOLO_DIR}/{message_id}/{element}/im.jpg.jpg"
|
| 182 |
# encoded_images.append(image_to_base64(yolo_path))
|
| 183 |
|
| 184 |
-
|
| 185 |
-
response_data = {
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
}
|
| 196 |
# response_data = {
|
| 197 |
# 'message_id': message_id,
|
| 198 |
# 'images': encoded_images,
|
|
|
|
| 14 |
import torch
|
| 15 |
from collections import Counter
|
| 16 |
from clip_model import ClipModel
|
| 17 |
+
import psutil
|
| 18 |
|
| 19 |
# Disable tensorflow warnings
|
| 20 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
|
|
|
| 76 |
# image_folder = '/content/drive/MyDrive/chiikawa' # 替換成你的目錄路徑
|
| 77 |
# jpg_files = get_jpg_files(image_folder)
|
| 78 |
|
|
|
|
| 79 |
|
| 80 |
def check_memory_usage():
|
| 81 |
# Get memory details
|
|
|
|
| 86 |
used_memory = memory_info.used / (1024 * 1024)
|
| 87 |
memory_usage_percent = memory_info.percent
|
| 88 |
|
| 89 |
+
print(f"^^^^^^Total Memory: {total_memory:.2f} MB^^^^^^")
|
| 90 |
+
print(f"^^^^^^Available Memory: {available_memory:.2f} MB^^^^^^")
|
| 91 |
+
print(f"^^^^^^Used Memory: {used_memory:.2f} MB^^^^^^")
|
| 92 |
+
print(f"^^^^^^Memory Usage (%): {memory_usage_percent}%^^^^^^")
|
| 93 |
|
| 94 |
# Run the function
|
| 95 |
check_memory_usage()
|
| 96 |
+
|
| 97 |
+
|
| 98 |
# Initialize the Flask application
|
| 99 |
app = Flask(__name__)
|
| 100 |
# Initialize the ClipModel at the start
|
| 101 |
clip_model = ClipModel()
|
| 102 |
|
| 103 |
+
|
| 104 |
|
| 105 |
# API route for prediction(YOLO)
|
| 106 |
@app.route('/predict', methods=['POST'])
|
|
|
|
| 126 |
results = model(image_data)
|
| 127 |
print ("***** YOLO predict result:",results,"********")
|
| 128 |
print("***** YOLO predict DONE *****")
|
| 129 |
+
|
| 130 |
check_memory_usage()
|
| 131 |
|
| 132 |
# 檢查 YOLO 是否返回了有效的結果
|
|
|
|
| 149 |
labels = result.boxes.cls # Get predicted label IDs
|
| 150 |
label_names = [model.names[int(label)] for label in labels] # Convert to names
|
| 151 |
|
| 152 |
+
print(f"======YOLO label_names: {label_names}======")
|
| 153 |
|
| 154 |
element_counts = Counter(label_names)
|
| 155 |
|
|
|
|
| 167 |
element_list.append(element)
|
| 168 |
|
| 169 |
for yolo_img in yolo_file: # 每張切圖yolo_img
|
| 170 |
+
print("***** START CLIP *****")
|
| 171 |
top_k_words.append(clip_model.clip_result(yolo_img)) # CLIP預測3個結果(top_k_words)
|
| 172 |
#encoded_images.append(image_to_base64(yolo_img))
|
| 173 |
+
print(f"{yolo_img}:{top_k_words}\n")
|
| 174 |
|
| 175 |
# if element_counts[element] > 1: #某隻角色的數量>1
|
| 176 |
# yolo_path = f"{YOLO_DIR}/{message_id}/{element}"
|
|
|
|
| 183 |
# yolo_path = f"{YOLO_DIR}/{message_id}/{element}/im.jpg.jpg"
|
| 184 |
# encoded_images.append(image_to_base64(yolo_path))
|
| 185 |
|
| 186 |
+
## 建立回應資料
|
| 187 |
+
# response_data = {
|
| 188 |
+
# 'message_id': message_id,
|
| 189 |
+
# 'description': element_list,
|
| 190 |
+
# 'images': [
|
| 191 |
+
# {
|
| 192 |
+
# 'encoded_image': encoded_image,
|
| 193 |
+
# 'description_list': top_k_words
|
| 194 |
+
# }
|
| 195 |
+
# for encoded_image, description_list in zip(encoded_images, top_k_words)
|
| 196 |
+
# ]
|
| 197 |
+
# }
|
| 198 |
# response_data = {
|
| 199 |
# 'message_id': message_id,
|
| 200 |
# 'images': encoded_images,
|