Spaces:
Sleeping
Sleeping
add mongodb log
Browse files
main.py
CHANGED
|
@@ -17,6 +17,11 @@ import zipfile
|
|
| 17 |
import uuid # 匯入 uuid 以生成唯一的執行 ID
|
| 18 |
from pathlib import Path # 匯入 Path 以更方便地操作路徑
|
| 19 |
import gemini_ai as genai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def create_zip_archive(files, zip_filename):
|
| 22 |
"""
|
|
@@ -60,6 +65,20 @@ def gradio_multi_model_detection(
|
|
| 60 |
Yields:
|
| 61 |
dict: 用於更新 Gradio 介面元件的字典。
|
| 62 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if not image_files:
|
| 64 |
yield {
|
| 65 |
output_status: gr.update(value="錯誤:請至少上傳一張圖片。"),
|
|
@@ -175,6 +194,7 @@ def gradio_multi_model_detection(
|
|
| 175 |
|
| 176 |
# --- 3b. MLLM 分析 (如果啟用) ---
|
| 177 |
output_mllm_txt_path = None
|
|
|
|
| 178 |
if enable_mllm:
|
| 179 |
try:
|
| 180 |
prompt_to_use = mllm_prompt if mllm_prompt and mllm_prompt.strip() else None
|
|
@@ -186,6 +206,17 @@ def gradio_multi_model_detection(
|
|
| 186 |
output_mllm_txt_path = run_output_dir / f"{image_base_name}_mllm_result.txt"
|
| 187 |
output_mllm_txt_path.write_text(mllm_result_content, encoding='utf-8')
|
| 188 |
all_result_files.append(str(output_mllm_txt_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
# 將本次圖片的結果加入到總列表中
|
| 191 |
all_texts.append("\n".join(yolo_output_content))
|
|
@@ -200,6 +231,9 @@ def gradio_multi_model_detection(
|
|
| 200 |
|
| 201 |
final_status = f"處理完成!共 {total_images} 張圖片。結果儲存於: {run_output_dir.absolute()}"
|
| 202 |
combined_text_output = "\n\n".join(all_texts)
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
yield {
|
| 205 |
output_status: gr.update(value=final_status),
|
|
@@ -217,7 +251,7 @@ def toggle_mllm_prompt(is_enabled):
|
|
| 217 |
# --- Gradio Interface ---
|
| 218 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 219 |
gr.Markdown("# 智慧影像分析工具 (YOLO + MLLM)")
|
| 220 |
-
gr.Markdown("上傳圖片與YOLO模型進行物件偵測,並可選用MLLM進行進階圖像理解。")
|
| 221 |
|
| 222 |
with gr.Row():
|
| 223 |
with gr.Column(scale=1):
|
|
@@ -260,3 +294,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 260 |
if __name__ == "__main__":
|
| 261 |
demo.launch(debug=True)
|
| 262 |
#demo.launch(share=True)
|
|
|
|
|
|
|
|
|
| 17 |
import uuid # 匯入 uuid 以生成唯一的執行 ID
|
| 18 |
from pathlib import Path # 匯入 Path 以更方便地操作路徑
|
| 19 |
import gemini_ai as genai
|
| 20 |
+
from datetime import datetime
|
| 21 |
+
import mongo_lib as mongo
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
|
| 26 |
def create_zip_archive(files, zip_filename):
|
| 27 |
"""
|
|
|
|
| 65 |
Yields:
|
| 66 |
dict: 用於更新 Gradio 介面元件的字典。
|
| 67 |
"""
|
| 68 |
+
global_datetime = datetime.now()
|
| 69 |
+
|
| 70 |
+
#寫主表log
|
| 71 |
+
document = {"log_style":"master",
|
| 72 |
+
"create_datetime": str(global_datetime),
|
| 73 |
+
"image_files": image_files,
|
| 74 |
+
"model_files": model_files,
|
| 75 |
+
"conf_threshold":conf_threshold,
|
| 76 |
+
"enable_mllm":enable_mllm,
|
| 77 |
+
"mllm_prompt":mllm_prompt
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
mongo.insert_mongodb_log("multi_model_detection",document) #寫入log方便日後查驗
|
| 81 |
+
|
| 82 |
if not image_files:
|
| 83 |
yield {
|
| 84 |
output_status: gr.update(value="錯誤:請至少上傳一張圖片。"),
|
|
|
|
| 194 |
|
| 195 |
# --- 3b. MLLM 分析 (如果啟用) ---
|
| 196 |
output_mllm_txt_path = None
|
| 197 |
+
mllm_result_content = ""
|
| 198 |
if enable_mllm:
|
| 199 |
try:
|
| 200 |
prompt_to_use = mllm_prompt if mllm_prompt and mllm_prompt.strip() else None
|
|
|
|
| 206 |
output_mllm_txt_path = run_output_dir / f"{image_base_name}_mllm_result.txt"
|
| 207 |
output_mllm_txt_path.write_text(mllm_result_content, encoding='utf-8')
|
| 208 |
all_result_files.append(str(output_mllm_txt_path))
|
| 209 |
+
|
| 210 |
+
#寫明細表log
|
| 211 |
+
document = {"log_style":"detail",
|
| 212 |
+
"create_datetime": str(global_datetime),
|
| 213 |
+
"image_path": str(image_path),
|
| 214 |
+
"yolo_result": yolo_output_content,
|
| 215 |
+
"enable_mllm": enable_mllm,
|
| 216 |
+
"mllm_prompt": mllm_prompt,
|
| 217 |
+
"mllm_result": mllm_result_content}
|
| 218 |
+
|
| 219 |
+
mongo.insert_mongodb_log("multi_model_detection",document) #寫入log方便日後查驗
|
| 220 |
|
| 221 |
# 將本次圖片的結果加入到總列表中
|
| 222 |
all_texts.append("\n".join(yolo_output_content))
|
|
|
|
| 231 |
|
| 232 |
final_status = f"處理完成!共 {total_images} 張圖片。結果儲存於: {run_output_dir.absolute()}"
|
| 233 |
combined_text_output = "\n\n".join(all_texts)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
|
| 237 |
|
| 238 |
yield {
|
| 239 |
output_status: gr.update(value=final_status),
|
|
|
|
| 251 |
# --- Gradio Interface ---
|
| 252 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 253 |
gr.Markdown("# 智慧影像分析工具 (YOLO + MLLM)")
|
| 254 |
+
gr.Markdown("上傳圖片與YOLO模型進行物件偵測,並可選用MLLM進行進階圖像理解。 ver.250813.1")
|
| 255 |
|
| 256 |
with gr.Row():
|
| 257 |
with gr.Column(scale=1):
|
|
|
|
| 294 |
if __name__ == "__main__":
|
| 295 |
demo.launch(debug=True)
|
| 296 |
#demo.launch(share=True)
|
| 297 |
+
|
| 298 |
+
|