Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import torchvision.transforms as transforms
|
|
5 |
from torchvision import models
|
6 |
import gradio as gr
|
7 |
import webbrowser
|
|
|
8 |
|
9 |
# 🔧 Set device
|
10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
@@ -36,8 +37,9 @@ CONFIDENCE_THRESHOLD = 85.0 # Minimum confidence considered safe enough to show
|
|
36 |
|
37 |
def classify_mushroom(image: Image.Image):
|
38 |
try:
|
39 |
-
image = image.convert("
|
40 |
-
|
|
|
41 |
|
42 |
with torch.no_grad():
|
43 |
outputs = model(tensor)
|
@@ -57,10 +59,11 @@ def classify_mushroom(image: Image.Image):
|
|
57 |
print(f"❌ Error: {e}")
|
58 |
return "Error", "ผิดพลาด", "N/A", "Invalid image. Please upload a valid mushroom photo."
|
59 |
|
60 |
-
# 🔗 Open user manual
|
61 |
MANUAL_URL = "https://drive.google.com/drive/folders/19lUCEaLstrRjCzqpDlWErhRd1EXWUGbf?usp=sharing"
|
62 |
def open_manual():
|
63 |
-
webbrowser
|
|
|
64 |
return "", "", "", "Opening user manual..."
|
65 |
|
66 |
# 🎛️ Gradio UI
|
|
|
5 |
from torchvision import models
|
6 |
import gradio as gr
|
7 |
import webbrowser
|
8 |
+
from rembg import remove # 🆕 Background removal
|
9 |
|
10 |
# 🔧 Set device
|
11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
37 |
|
38 |
def classify_mushroom(image: Image.Image):
|
39 |
try:
|
40 |
+
image = image.convert("RGBA") # Required format for rembg
|
41 |
+
image_no_bg = remove(image).convert("RGB") # 🆕 Remove background
|
42 |
+
tensor = transform(image_no_bg).unsqueeze(0).to(device)
|
43 |
|
44 |
with torch.no_grad():
|
45 |
outputs = model(tensor)
|
|
|
59 |
print(f"❌ Error: {e}")
|
60 |
return "Error", "ผิดพลาด", "N/A", "Invalid image. Please upload a valid mushroom photo."
|
61 |
|
62 |
+
# 🔗 Open user manual in new browser tab
|
63 |
MANUAL_URL = "https://drive.google.com/drive/folders/19lUCEaLstrRjCzqpDlWErhRd1EXWUGbf?usp=sharing"
|
64 |
def open_manual():
|
65 |
+
import webbrowser
|
66 |
+
webbrowser.open_new_tab(MANUAL_URL) # 🆕 Ensure it opens in new tab
|
67 |
return "", "", "", "Opening user manual..."
|
68 |
|
69 |
# 🎛️ Gradio UI
|