Fix: Make model.py's __load compatible with Windows and Linux
Browse files- image_processing/model.py +17 -4
image_processing/model.py
CHANGED
@@ -20,10 +20,23 @@ class Model:
|
|
20 |
if sys.stderr is None:
|
21 |
sys.stderr = open(os.devnull, 'w')
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def __call__(self, *args, **kwds):
|
29 |
if self.model is None:
|
|
|
20 |
if sys.stderr is None:
|
21 |
sys.stderr = open(os.devnull, 'w')
|
22 |
|
23 |
+
# Check if the current operating system is Windows
|
24 |
+
is_windows = (sys.platform == "win32")
|
25 |
+
|
26 |
+
if is_windows:
|
27 |
+
# If on Windows, apply the patch temporarily
|
28 |
+
temp = pathlib.PosixPath
|
29 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
30 |
+
try:
|
31 |
+
# Load the model with the patch applied
|
32 |
+
self.model = torch.hub.load('ultralytics/yolov5', 'custom', path=resource_path('ai-models/2024-11-00/best.pt'))
|
33 |
+
finally:
|
34 |
+
# CRITICAL: Always restore the original class, even if loading fails
|
35 |
+
pathlib.PosixPath = temp
|
36 |
+
else:
|
37 |
+
# If on Linux, macOS, or other systems, load the model directly
|
38 |
+
self.model = torch.hub.load('ultralytics/yolov5', 'custom', path=resource_path('ai-models/2024-11-00/best.pt'))
|
39 |
+
|
40 |
|
41 |
def __call__(self, *args, **kwds):
|
42 |
if self.model is None:
|