Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,6 @@ from PIL import Image
|
|
| 9 |
import tempfile
|
| 10 |
import io
|
| 11 |
import threading
|
| 12 |
-
import tkinter as tk
|
| 13 |
-
from tkinter import filedialog, Label, Button, Frame
|
| 14 |
from shiny import App, render, ui, reactive
|
| 15 |
from shiny.types import ImgData
|
| 16 |
import base64
|
|
@@ -19,6 +17,9 @@ import base64
|
|
| 19 |
MODEL_DIR = "models"
|
| 20 |
TEMP_DIR = "temp"
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
def parse_args():
|
| 23 |
parser = argparse.ArgumentParser(description='Advanced Virtual Try-On')
|
| 24 |
parser.add_argument('--garment', type=str, help='Path to garment image')
|
|
@@ -870,6 +871,16 @@ class TkinterUI:
|
|
| 870 |
"""Tkinter UI for the virtual try-on application"""
|
| 871 |
|
| 872 |
def __init__(self, webcam_index=0, resolution="640x480"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 873 |
self.webcam_index = webcam_index
|
| 874 |
self.width, self.height = map(int, resolution.split('x'))
|
| 875 |
self.app = None
|
|
@@ -881,6 +892,14 @@ class TkinterUI:
|
|
| 881 |
|
| 882 |
def start(self):
|
| 883 |
"""Start the Tkinter UI"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 884 |
self.root = tk.Tk()
|
| 885 |
self.root.title("Virtual Try-On")
|
| 886 |
self.root.geometry(f"{self.width + 300}x{self.height + 100}")
|
|
@@ -1114,6 +1133,16 @@ class TkinterUI:
|
|
| 1114 |
|
| 1115 |
def run_tkinter_app(webcam_index=0, resolution="640x480"):
|
| 1116 |
"""Run the application with Tkinter UI"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1117 |
ui = TkinterUI(webcam_index, resolution)
|
| 1118 |
ui.start()
|
| 1119 |
|
|
@@ -1341,8 +1370,15 @@ def main():
|
|
| 1341 |
app = run_shiny_app()
|
| 1342 |
app.run(host="0.0.0.0", port=8000)
|
| 1343 |
elif args.tkinter or (not args.garment and not args.shiny):
|
| 1344 |
-
#
|
| 1345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1346 |
else:
|
| 1347 |
# Traditional command-line mode if garment is specified
|
| 1348 |
width, height = map(int, args.resolution.split('x'))
|
|
|
|
| 9 |
import tempfile
|
| 10 |
import io
|
| 11 |
import threading
|
|
|
|
|
|
|
| 12 |
from shiny import App, render, ui, reactive
|
| 13 |
from shiny.types import ImgData
|
| 14 |
import base64
|
|
|
|
| 17 |
MODEL_DIR = "models"
|
| 18 |
TEMP_DIR = "temp"
|
| 19 |
|
| 20 |
+
# Don't import tkinter at the top level
|
| 21 |
+
# This allows the app to run in environments without Tkinter (like Hugging Face)
|
| 22 |
+
|
| 23 |
def parse_args():
|
| 24 |
parser = argparse.ArgumentParser(description='Advanced Virtual Try-On')
|
| 25 |
parser.add_argument('--garment', type=str, help='Path to garment image')
|
|
|
|
| 871 |
"""Tkinter UI for the virtual try-on application"""
|
| 872 |
|
| 873 |
def __init__(self, webcam_index=0, resolution="640x480"):
|
| 874 |
+
# Import tkinter here instead of at the top level
|
| 875 |
+
try:
|
| 876 |
+
import tkinter as tk
|
| 877 |
+
from tkinter import filedialog, Label, Button, Frame
|
| 878 |
+
self.tk_available = True
|
| 879 |
+
except ImportError:
|
| 880 |
+
print("Tkinter is not available in this environment.")
|
| 881 |
+
self.tk_available = False
|
| 882 |
+
return
|
| 883 |
+
|
| 884 |
self.webcam_index = webcam_index
|
| 885 |
self.width, self.height = map(int, resolution.split('x'))
|
| 886 |
self.app = None
|
|
|
|
| 892 |
|
| 893 |
def start(self):
|
| 894 |
"""Start the Tkinter UI"""
|
| 895 |
+
if not self.tk_available:
|
| 896 |
+
print("Cannot start Tkinter UI: Tkinter is not available.")
|
| 897 |
+
return
|
| 898 |
+
|
| 899 |
+
# Import tkinter here to use its symbols
|
| 900 |
+
import tkinter as tk
|
| 901 |
+
from tkinter import filedialog, Label, Button, Frame
|
| 902 |
+
|
| 903 |
self.root = tk.Tk()
|
| 904 |
self.root.title("Virtual Try-On")
|
| 905 |
self.root.geometry(f"{self.width + 300}x{self.height + 100}")
|
|
|
|
| 1133 |
|
| 1134 |
def run_tkinter_app(webcam_index=0, resolution="640x480"):
|
| 1135 |
"""Run the application with Tkinter UI"""
|
| 1136 |
+
# Check if Tkinter is available
|
| 1137 |
+
try:
|
| 1138 |
+
import tkinter as tk
|
| 1139 |
+
tk_available = True
|
| 1140 |
+
except ImportError:
|
| 1141 |
+
print("Tkinter is not available in this environment. Cannot run Tkinter UI.")
|
| 1142 |
+
print("Try running with --shiny instead.")
|
| 1143 |
+
tk_available = False
|
| 1144 |
+
return
|
| 1145 |
+
|
| 1146 |
ui = TkinterUI(webcam_index, resolution)
|
| 1147 |
ui.start()
|
| 1148 |
|
|
|
|
| 1370 |
app = run_shiny_app()
|
| 1371 |
app.run(host="0.0.0.0", port=8000)
|
| 1372 |
elif args.tkinter or (not args.garment and not args.shiny):
|
| 1373 |
+
# Check if Tkinter is available before trying to use it
|
| 1374 |
+
try:
|
| 1375 |
+
import tkinter
|
| 1376 |
+
# Use tkinter by default if no garment is specified
|
| 1377 |
+
run_tkinter_app(args.webcam, args.resolution)
|
| 1378 |
+
except ImportError:
|
| 1379 |
+
print("Tkinter is not available. Falling back to Shiny mode.")
|
| 1380 |
+
app = run_shiny_app()
|
| 1381 |
+
app.run(host="0.0.0.0", port=8000)
|
| 1382 |
else:
|
| 1383 |
# Traditional command-line mode if garment is specified
|
| 1384 |
width, height = map(int, args.resolution.split('x'))
|