Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,31 +41,6 @@ from ultralytics import YOLO
|
|
| 41 |
import asyncio
|
| 42 |
import traceback
|
| 43 |
|
| 44 |
-
# def setup_environment():
|
| 45 |
-
# """配置適合 ZeroGPU 環境的設置"""
|
| 46 |
-
# # 啟用 CUDA 錯誤的同步報告,幫助診斷問題
|
| 47 |
-
# os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
| 48 |
-
|
| 49 |
-
# # 檢查 CUDA 是否可用
|
| 50 |
-
# if torch.cuda.is_available():
|
| 51 |
-
# # 顯示 GPU 信息
|
| 52 |
-
# device_name = torch.cuda.get_device_name(0)
|
| 53 |
-
# print(f"使用 GPU: {device_name}")
|
| 54 |
-
|
| 55 |
-
# # 針對 A100 的優化設置
|
| 56 |
-
# if "A100" in device_name:
|
| 57 |
-
# print("檢測到 A100 GPU,應用特殊優化...")
|
| 58 |
-
# torch.backends.cudnn.benchmark = True
|
| 59 |
-
|
| 60 |
-
# # 清理 GPU 內存
|
| 61 |
-
# torch.cuda.empty_cache()
|
| 62 |
-
# return True
|
| 63 |
-
# else:
|
| 64 |
-
# print("CUDA 不可用,使用 CPU 模式")
|
| 65 |
-
# return False
|
| 66 |
-
|
| 67 |
-
# cuda_available = setup_environment()
|
| 68 |
-
|
| 69 |
history_manager = UserHistoryManager()
|
| 70 |
|
| 71 |
class ModelManager:
|
|
@@ -101,36 +76,6 @@ class ModelManager:
|
|
| 101 |
self._yolo_model = YOLO('yolov8x.pt')
|
| 102 |
return self._yolo_model
|
| 103 |
|
| 104 |
-
# @property
|
| 105 |
-
# def yolo_model(self):
|
| 106 |
-
# if self._yolo_model is None:
|
| 107 |
-
# try:
|
| 108 |
-
# print("正在加載 YOLO 模型...")
|
| 109 |
-
# # 不指定設備,讓 YOLO 自動選擇
|
| 110 |
-
# self._yolo_model = YOLO('yolov8x.pt')
|
| 111 |
-
|
| 112 |
-
# # 禁用模型融合來避免 CUDA 錯誤
|
| 113 |
-
# if hasattr(self._yolo_model, 'model') and hasattr(self._yolo_model.model, 'fuse'):
|
| 114 |
-
# print("在 ZeroGPU 環境下禁用模型融合以避免 CUDA 錯誤")
|
| 115 |
-
|
| 116 |
-
# # 備份原始融合方法
|
| 117 |
-
# original_fuse = self._yolo_model.model.fuse
|
| 118 |
-
|
| 119 |
-
# # 創建一個空的融合方法
|
| 120 |
-
# def no_fuse(*args, **kwargs):
|
| 121 |
-
# print("已跳過融合操作")
|
| 122 |
-
# return self._yolo_model.model
|
| 123 |
-
|
| 124 |
-
# # 替換融合方法
|
| 125 |
-
# self._yolo_model.model.fuse = no_fuse
|
| 126 |
-
# except Exception as e:
|
| 127 |
-
# print(f"加載 YOLO 模型時出錯: {str(e)}")
|
| 128 |
-
# print("嘗試降級到較小的模型和 CPU 模式...")
|
| 129 |
-
# # 降級到較小的模型並明確使用 CPU
|
| 130 |
-
# self._yolo_model = YOLO('yolov8n.pt', device='cpu')
|
| 131 |
-
|
| 132 |
-
# return self._yolo_model
|
| 133 |
-
|
| 134 |
@property
|
| 135 |
def breed_model(self):
|
| 136 |
if self._breed_model is None:
|
|
@@ -252,7 +197,7 @@ def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.3):
|
|
| 252 |
})
|
| 253 |
|
| 254 |
if not detected_boxes:
|
| 255 |
-
return [(image, 1
|
| 256 |
|
| 257 |
# Phase 2: Analysis of detection relationships
|
| 258 |
avg_height = sum(box['height'] for box in detected_boxes) / len(detected_boxes)
|
|
@@ -266,7 +211,7 @@ def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.3):
|
|
| 266 |
y2 = min(box1['coords'][3], box2['coords'][3])
|
| 267 |
|
| 268 |
if x2 <= x1 or y2 <= y1:
|
| 269 |
-
return 0
|
| 270 |
|
| 271 |
intersection = (x2 - x1) * (y2 - y1)
|
| 272 |
area1 = box1['area']
|
|
@@ -604,4 +549,4 @@ def main():
|
|
| 604 |
|
| 605 |
if __name__ == "__main__":
|
| 606 |
iface = main()
|
| 607 |
-
iface.launch()
|
|
|
|
| 41 |
import asyncio
|
| 42 |
import traceback
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
history_manager = UserHistoryManager()
|
| 45 |
|
| 46 |
class ModelManager:
|
|
|
|
| 76 |
self._yolo_model = YOLO('yolov8x.pt')
|
| 77 |
return self._yolo_model
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
@property
|
| 80 |
def breed_model(self):
|
| 81 |
if self._breed_model is None:
|
|
|
|
| 197 |
})
|
| 198 |
|
| 199 |
if not detected_boxes:
|
| 200 |
+
return [(image, 1, [0, 0, img_width, img_height], False)]
|
| 201 |
|
| 202 |
# Phase 2: Analysis of detection relationships
|
| 203 |
avg_height = sum(box['height'] for box in detected_boxes) / len(detected_boxes)
|
|
|
|
| 211 |
y2 = min(box1['coords'][3], box2['coords'][3])
|
| 212 |
|
| 213 |
if x2 <= x1 or y2 <= y1:
|
| 214 |
+
return 0
|
| 215 |
|
| 216 |
intersection = (x2 - x1) * (y2 - y1)
|
| 217 |
area1 = box1['area']
|
|
|
|
| 549 |
|
| 550 |
if __name__ == "__main__":
|
| 551 |
iface = main()
|
| 552 |
+
iface.launch()
|