Spaces:
Sleeping
Sleeping
更新加了MLLM
Browse files
main.py
CHANGED
|
@@ -12,15 +12,16 @@ import cv2
|
|
| 12 |
from ultralytics import YOLO
|
| 13 |
import shutil # Import shutil for copying files
|
| 14 |
import zipfile # Import zipfile for creating zip archives
|
|
|
|
| 15 |
|
| 16 |
def multi_model_detection(image_paths_list: list, model_paths_list: list, output_dir: str = 'detection_results', conf_threshold: float = 0.40):
|
| 17 |
"""
|
| 18 |
-
使用多個
|
| 19 |
並將結果繪製在圖片上,同時保存辨識資訊到文字檔案。
|
| 20 |
|
| 21 |
Args:
|
| 22 |
image_paths_list (list): 包含所有待辨識圖片路徑的列表。
|
| 23 |
-
model_paths_list (list): 包含所有模型 (.pt 檔案)
|
| 24 |
output_dir (str): 儲存結果圖片和文字檔案的目錄。
|
| 25 |
如果不存在,函式會自動創建。
|
| 26 |
conf_threshold (float): 置信度閾值,只有高於此值的偵測結果會被標示。
|
|
@@ -38,7 +39,7 @@ def multi_model_detection(image_paths_list: list, model_paths_list: list, output
|
|
| 38 |
# 載入所有模型
|
| 39 |
loaded_models = []
|
| 40 |
print("\n--- 載入模型 ---")
|
| 41 |
-
# If no models are uploaded, use the default
|
| 42 |
if not model_paths_list:
|
| 43 |
default_model_path = 'yoloe-11s-seg-pf.pt' #'yolov8n.pt'
|
| 44 |
try:
|
|
@@ -100,6 +101,7 @@ def multi_model_detection(image_paths_list: list, model_paths_list: list, output
|
|
| 100 |
# 將辨識結果添加到 txt 輸出內容和繪圖列表
|
| 101 |
txt_output_content.append(f"\n--- 模型: {model_name} ---")
|
| 102 |
|
|
|
|
| 103 |
if results.boxes: # 檢查是否有偵測到物件
|
| 104 |
for box in results.boxes:
|
| 105 |
# 取得邊界框座標和置信度
|
|
@@ -121,7 +123,10 @@ def multi_model_detection(image_paths_list: list, model_paths_list: list, output
|
|
| 121 |
txt_output_content.append(f" - {cls_name} (Conf: {conf:.2f}) [x1:{x1}, y1:{y1}, x2:{x2}, y2:{y2}]")
|
| 122 |
else:
|
| 123 |
txt_output_content.append(" 沒有偵測到任何物件。")
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
# 繪製所有模型在當前圖片上的偵測結果
|
| 126 |
# 我們會根據模型來源給予不同的顏色或樣式,讓結果更容易區分
|
| 127 |
|
|
@@ -248,8 +253,8 @@ def gradio_multi_model_detection(image_files, model_files, conf_threshold, outpu
|
|
| 248 |
|
| 249 |
# Create the Gradio interface
|
| 250 |
with gr.Blocks() as demo:
|
| 251 |
-
gr.Markdown("# 支援多模型YOLO
|
| 252 |
-
gr.Markdown("
|
| 253 |
|
| 254 |
with gr.Row():
|
| 255 |
with gr.Column():
|
|
|
|
| 12 |
from ultralytics import YOLO
|
| 13 |
import shutil # Import shutil for copying files
|
| 14 |
import zipfile # Import zipfile for creating zip archives
|
| 15 |
+
import gemini_ai as genai # Assuming gemini_ai is a custom module for Gemini API interactions
|
| 16 |
|
| 17 |
def multi_model_detection(image_paths_list: list, model_paths_list: list, output_dir: str = 'detection_results', conf_threshold: float = 0.40):
|
| 18 |
"""
|
| 19 |
+
使用多個 YOLO 模型對多張圖片進行物件辨識,
|
| 20 |
並將結果繪製在圖片上,同時保存辨識資訊到文字檔案。
|
| 21 |
|
| 22 |
Args:
|
| 23 |
image_paths_list (list): 包含所有待辨識圖片路徑的列表。
|
| 24 |
+
model_paths_list (list): 包含所有模型 (.pt 檔案) 路徑的列表。
|
| 25 |
output_dir (str): 儲存結果圖片和文字檔案的目錄。
|
| 26 |
如果不存在,函式會自動創建。
|
| 27 |
conf_threshold (float): 置信度閾值,只有高於此值的偵測結果會被標示。
|
|
|
|
| 39 |
# 載入所有模型
|
| 40 |
loaded_models = []
|
| 41 |
print("\n--- 載入模型 ---")
|
| 42 |
+
# If no models are uploaded, use the default yolov8n.pt
|
| 43 |
if not model_paths_list:
|
| 44 |
default_model_path = 'yoloe-11s-seg-pf.pt' #'yolov8n.pt'
|
| 45 |
try:
|
|
|
|
| 101 |
# 將辨識結果添加到 txt 輸出內容和繪圖列表
|
| 102 |
txt_output_content.append(f"\n--- 模型: {model_name} ---")
|
| 103 |
|
| 104 |
+
# Example usage of Gemini API
|
| 105 |
if results.boxes: # 檢查是否有偵測到物件
|
| 106 |
for box in results.boxes:
|
| 107 |
# 取得邊界框座標和置信度
|
|
|
|
| 123 |
txt_output_content.append(f" - {cls_name} (Conf: {conf:.2f}) [x1:{x1}, y1:{y1}, x2:{x2}, y2:{y2}]")
|
| 124 |
else:
|
| 125 |
txt_output_content.append(" 沒有偵測到任何物件。")
|
| 126 |
+
|
| 127 |
+
MLLM_str = genai.analyze_content_with_gemini(image_path)
|
| 128 |
+
txt_output_content.append("-MLLM 分析結果為 : " + MLLM_str)
|
| 129 |
+
|
| 130 |
# 繪製所有模型在當前圖片上的偵測結果
|
| 131 |
# 我們會根據模型來源給予不同的顏色或樣式,讓結果更容易區分
|
| 132 |
|
|
|
|
| 253 |
|
| 254 |
# Create the Gradio interface
|
| 255 |
with gr.Blocks() as demo:
|
| 256 |
+
gr.Markdown("# 支援多模型YOLO物件辨識+MLLM(demo)")
|
| 257 |
+
gr.Markdown("上傳您的圖片和模型,並設定置信度閾值進行物件辨識。若未上傳模型,將使用預設模型進行辨識。")
|
| 258 |
|
| 259 |
with gr.Row():
|
| 260 |
with gr.Column():
|