Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +12 -12
api_server.py
CHANGED
|
@@ -35,7 +35,7 @@ if load_type == 'local':
|
|
| 35 |
|
| 36 |
model = YOLO(model_path)
|
| 37 |
|
| 38 |
-
print("
|
| 39 |
#model.eval() # 設定模型為推理模式
|
| 40 |
elif load_type == 'remote_hub_download':
|
| 41 |
from huggingface_hub import hf_hub_download
|
|
@@ -86,10 +86,10 @@ 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()
|
|
@@ -121,10 +121,10 @@ def predict():
|
|
| 121 |
except Exception as e:
|
| 122 |
return jsonify({'error': str(e)}), 400
|
| 123 |
|
| 124 |
-
print("***** Start YOLO predict *****")
|
| 125 |
# Make a prediction using YOLO
|
| 126 |
results = model(image_data)
|
| 127 |
-
print ("
|
| 128 |
print("***** YOLO predict DONE *****")
|
| 129 |
|
| 130 |
check_memory_usage()
|
|
@@ -149,7 +149,7 @@ def predict():
|
|
| 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 |
|
|
@@ -158,7 +158,7 @@ def predict():
|
|
| 158 |
yolo_path = f"{YOLO_DIR}/{message_id}/{element}"
|
| 159 |
yolo_file = get_jpg_files(yolo_path)
|
| 160 |
|
| 161 |
-
print(f"
|
| 162 |
|
| 163 |
if len(yolo_file) == 0:
|
| 164 |
print(f"警告:{element} 沒有找到相關的 JPG 檔案")
|
|
@@ -167,10 +167,10 @@ def predict():
|
|
| 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"
|
| 174 |
|
| 175 |
# if element_counts[element] > 1: #某隻角色的數量>1
|
| 176 |
# yolo_path = f"{YOLO_DIR}/{message_id}/{element}"
|
|
@@ -201,7 +201,7 @@ def predict():
|
|
| 201 |
# 'description': element_list
|
| 202 |
# }
|
| 203 |
|
| 204 |
-
return top_k_words#jsonify(response_data)
|
| 205 |
|
| 206 |
# for label_name in label_names:
|
| 207 |
# yolo_file=f"{YOLO_DIR}/{message_id}/{label_name}/im.jpg.jpg"
|
|
|
|
| 35 |
|
| 36 |
model = YOLO(model_path)
|
| 37 |
|
| 38 |
+
print("***** 1. LOAD YOLO MODEL DONE *****")
|
| 39 |
#model.eval() # 設定模型為推理模式
|
| 40 |
elif load_type == 'remote_hub_download':
|
| 41 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 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()
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
return jsonify({'error': str(e)}), 400
|
| 123 |
|
| 124 |
+
print("***** 2. Start YOLO predict *****")
|
| 125 |
# Make a prediction using YOLO
|
| 126 |
results = model(image_data)
|
| 127 |
+
print ("===== YOLO predict result:",results,"=====")
|
| 128 |
print("***** YOLO predict DONE *****")
|
| 129 |
|
| 130 |
check_memory_usage()
|
|
|
|
| 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"====== 3. YOLO label_names: {label_names}======")
|
| 153 |
|
| 154 |
element_counts = Counter(label_names)
|
| 155 |
|
|
|
|
| 158 |
yolo_path = f"{YOLO_DIR}/{message_id}/{element}"
|
| 159 |
yolo_file = get_jpg_files(yolo_path)
|
| 160 |
|
| 161 |
+
print(f"***** 處理:{yolo_path} *****")
|
| 162 |
|
| 163 |
if len(yolo_file) == 0:
|
| 164 |
print(f"警告:{element} 沒有找到相關的 JPG 檔案")
|
|
|
|
| 167 |
element_list.append(element)
|
| 168 |
|
| 169 |
for yolo_img in yolo_file: # 每張切圖yolo_img
|
| 170 |
+
print("***** 4. 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"===== CLIP result:{top_k_words} =====\n")
|
| 174 |
|
| 175 |
# if element_counts[element] > 1: #某隻角色的數量>1
|
| 176 |
# yolo_path = f"{YOLO_DIR}/{message_id}/{element}"
|
|
|
|
| 201 |
# 'description': element_list
|
| 202 |
# }
|
| 203 |
|
| 204 |
+
return jsonify(top_k_words), 200 #jsonify(response_data)
|
| 205 |
|
| 206 |
# for label_name in label_names:
|
| 207 |
# yolo_file=f"{YOLO_DIR}/{message_id}/{label_name}/im.jpg.jpg"
|