Dev Nagaich commited on
Commit
2c4864e
·
2 Parent(s): 699c01644d9568

Resolve conflicts: remove unnecessary files for security

Browse files
Files changed (10) hide show
  1. .dockerignore +60 -0
  2. .gitignore +62 -0
  3. .streamlit/config.toml +13 -0
  4. app.py +280 -0
  5. deploy.bat +200 -0
  6. packages.txt +2 -0
  7. requirements_deploy.txt +27 -0
  8. segment-anything-2 +1 -0
  9. test_app_local.py +205 -0
  10. windows.bat +91 -0
.dockerignore ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Git
2
+ .git
3
+ .gitignore
4
+
5
+ # Python cache
6
+ __pycache__/
7
+ *.py[cod]
8
+ *$py.class
9
+ *.so
10
+ .Python
11
+
12
+ # Virtual environments
13
+ vreyesam_env/
14
+ venv/
15
+ env/
16
+ ENV/
17
+
18
+ # Build artifacts
19
+ build/
20
+ dist/
21
+ *.egg-info/
22
+
23
+ # Data and models (downloaded during build)
24
+ VRBiomSegM/
25
+ segment-anything-2/
26
+
27
+ # Outputs
28
+ *.jpg
29
+ *.png
30
+ *.jpeg
31
+ loss_plots/
32
+ predictions/
33
+ results/
34
+ output/
35
+ VREyeSAM_results/
36
+
37
+ # Jupyter
38
+ .ipynb_checkpoints/
39
+ *.ipynb
40
+
41
+ # IDE
42
+ .vscode/
43
+ .idea/
44
+ *.swp
45
+ *.swo
46
+ *~
47
+
48
+ # OS
49
+ .DS_Store
50
+ Thumbs.db
51
+
52
+ # Documentation
53
+ docs/`
54
+ *.md
55
+ !README.md
56
+
57
+ # Training scripts (not needed for deployment)
58
+ Training.py
59
+ Test.py
60
+ Inference.py
.gitignore ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Virtual Environment
2
+ vreyesam_env/
3
+ venv/
4
+ env/
5
+ ENV/
6
+
7
+ # Python cache and compiled files
8
+ __pycache__/
9
+ *.py[cod]
10
+ *$py.class
11
+ *.so
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+
29
+ # Large external directories
30
+ segment-anything-2/
31
+
32
+ # Model checkpoints and weights
33
+ *.torch
34
+ *.pth
35
+ *.pt
36
+
37
+ # Data directories
38
+ VRBiomSegM/
39
+
40
+ # Output files
41
+ *.jpg
42
+ *.png
43
+ *.jpeg
44
+ loss_plots/
45
+ predictions/
46
+ results/
47
+ output/
48
+
49
+ # Jupyter Notebook
50
+ .ipynb_checkpoints/
51
+ *.ipynb
52
+
53
+ # IDE
54
+ .vscode/
55
+ .idea/
56
+ *.swp
57
+ *.swo
58
+ *~
59
+
60
+ # OS
61
+ .DS_Store
62
+ Thumbs.db
.streamlit/config.toml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [server]
2
+ maxUploadSize = 500
3
+ headless = true
4
+ port = 7860
5
+ address = "0.0.0.0"
6
+ enableCORS = false
7
+ enableXsrfProtection = false
8
+
9
+ [browser]
10
+ gatherUsageStats = false
11
+
12
+ [theme]
13
+ base = "light"
app.py ADDED
@@ -0,0 +1,280 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import torch
4
+ import numpy as np
5
+ from PIL import Image
6
+ import io
7
+ import sys
8
+ import os
9
+
10
+ # Add segment-anything-2 to path
11
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), "segment-anything-2"))
12
+
13
+ from sam2.build_sam import build_sam2
14
+ from sam2.sam2_image_predictor import SAM2ImagePredictor
15
+
16
+ # Page config
17
+ st.set_page_config(
18
+ page_title="VREyeSAM - Non-frontal Iris Segmentation",
19
+ page_icon="👁️",
20
+ layout="wide",
21
+ initial_sidebar_state="expanded"
22
+ )
23
+
24
+ # Custom CSS
25
+ st.markdown("""
26
+ <style>
27
+ .main {
28
+ padding: 2rem;
29
+ }
30
+ .stButton>button {
31
+ width: 100%;
32
+ background-color: #4CAF50;
33
+ color: white;
34
+ padding: 0.5rem;
35
+ font-size: 16px;
36
+ }
37
+ .result-box {
38
+ border: 2px solid #ddd;
39
+ border-radius: 10px;
40
+ padding: 1rem;
41
+ margin: 1rem 0;
42
+ }
43
+ </style>
44
+ """, unsafe_allow_html=True)
45
+
46
+ @st.cache_resource
47
+ def load_model():
48
+ """Load the VREyeSAM model"""
49
+ try:
50
+ # IMPORTANT: Hydra config system searches within sam2 package
51
+ # Use relative path without "segment-anything-2/" prefix
52
+ model_cfg = "configs/sam2/sam2_hiera_s.yaml"
53
+ sam2_checkpoint = "segment-anything-2/checkpoints/sam2_hiera_small.pt"
54
+ fine_tuned_weights = "segment-anything-2/checkpoints/VREyeSAM_uncertainity_best.torch"
55
+
56
+ # Load model
57
+ device = "cuda" if torch.cuda.is_available() else "cpu"
58
+
59
+ sam2_model = build_sam2(model_cfg, sam2_checkpoint, device=device)
60
+ predictor = SAM2ImagePredictor(sam2_model)
61
+ predictor.model.load_state_dict(torch.load(fine_tuned_weights, map_location=device))
62
+
63
+ return predictor
64
+ except Exception as e:
65
+ st.error(f"Error loading model: {str(e)}")
66
+ return None
67
+
68
+ def read_and_resize_image(image):
69
+ """Read and resize image for processing"""
70
+ img = np.array(image)
71
+ if len(img.shape) == 2: # Grayscale
72
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
73
+ elif img.shape[2] == 4: # RGBA
74
+ img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
75
+
76
+ # Resize if needed
77
+ r = np.min([1024 / img.shape[1], 1024 / img.shape[0]])
78
+ if r < 1:
79
+ img = cv2.resize(img, (int(img.shape[1] * r), int(img.shape[0] * r)))
80
+
81
+ return img
82
+
83
+ def segment_iris(predictor, image):
84
+ """Perform iris segmentation"""
85
+ # Generate random points for inference
86
+ num_samples = 30
87
+ input_points = np.random.randint(0, min(image.shape[:2]), (num_samples, 1, 2))
88
+
89
+ # Inference
90
+ with torch.no_grad():
91
+ predictor.set_image(image)
92
+ masks, scores, _ = predictor.predict(
93
+ point_coords=input_points,
94
+ point_labels=np.ones([input_points.shape[0], 1])
95
+ )
96
+
97
+ # Convert to numpy
98
+ np_masks = np.array(masks[:, 0]).astype(np.float32)
99
+ np_scores = scores[:, 0]
100
+
101
+ # Normalize scores
102
+ score_sum = np.sum(np_scores)
103
+ if score_sum > 0:
104
+ normalized_scores = np_scores / score_sum
105
+ else:
106
+ normalized_scores = np.ones_like(np_scores) / len(np_scores)
107
+
108
+ # Generate probabilistic mask
109
+ prob_mask = np.sum(np_masks * normalized_scores[:, None, None], axis=0)
110
+ prob_mask = np.clip(prob_mask, 0, 1)
111
+
112
+ # Threshold to get binary mask
113
+ binary_mask = (prob_mask > 0.2).astype(np.uint8)
114
+
115
+ return binary_mask, prob_mask
116
+
117
+ def overlay_mask_on_image(image, binary_mask, color=(0, 255, 0), alpha=0.5):
118
+ """Overlay binary mask on original image"""
119
+ overlay = image.copy()
120
+ mask_colored = np.zeros_like(image)
121
+ mask_colored[binary_mask > 0] = color
122
+
123
+ # Blend
124
+ result = cv2.addWeighted(overlay, 1-alpha, mask_colored, alpha, 0)
125
+
126
+ return result
127
+
128
+ # Main App
129
+ def main():
130
+ st.title("👁️ VREyeSAM: Non-Frontal Iris Segmentation")
131
+ st.markdown("""
132
+ Upload a non-frontal iris image captured in VR/AR environments, and VREyeSAM will segment the iris region
133
+ using a fine-tuned SAM2 model with uncertainty-weighted loss.
134
+ """)
135
+
136
+ # Sidebar
137
+ with st.sidebar:
138
+ st.header("About VREyeSAM")
139
+ st.markdown("""
140
+ **VREyeSAM** is a robust non-frontal iris segmentation framework designed for images captured under:
141
+ - Varying gaze directions
142
+ - Partial occlusions
143
+ - Inconsistent lighting conditions
144
+
145
+ **Model Performance:**
146
+ - Recall: 0.870
147
+ - F1-Score: 0.806
148
+ """)
149
+
150
+ st.header("Settings")
151
+ show_overlay = st.checkbox("Show Mask Overlay", value=True)
152
+ show_probabilistic = st.checkbox("Show Probabilistic Mask", value=False)
153
+
154
+ # Load model
155
+ with st.spinner("Loading VREyeSAM model..."):
156
+ predictor = load_model()
157
+
158
+ if predictor is None:
159
+ st.error("Failed to load model. Please check the setup.")
160
+ return
161
+
162
+ st.success("✅ Model loaded successfully!")
163
+
164
+ # File uploader with increased size limit
165
+ uploaded_file = st.file_uploader(
166
+ "Upload an iris image (JPG, PNG, JPEG)",
167
+ type=["jpg", "png", "jpeg"],
168
+ help="Upload a non-frontal iris image for segmentation"
169
+ )
170
+
171
+ if uploaded_file is not None:
172
+ try:
173
+ # Display original image
174
+ image = Image.open(uploaded_file)
175
+
176
+ col1, col2 = st.columns(2)
177
+
178
+ with col1:
179
+ st.subheader("📷 Original Image")
180
+ st.image(image, use_container_width=True)
181
+
182
+ # Process button
183
+ if st.button("🔍 Segment Iris", type="primary"):
184
+ with st.spinner("Segmenting iris..."):
185
+ try:
186
+ # Prepare image
187
+ img_array = read_and_resize_image(image)
188
+
189
+ # Perform segmentation
190
+ binary_mask, prob_mask = segment_iris(predictor, img_array)
191
+
192
+ with col2:
193
+ st.subheader("🎯 Binary Mask")
194
+ binary_mask_img = (binary_mask * 255).astype(np.uint8)
195
+ st.image(binary_mask_img, use_container_width=True)
196
+
197
+ # Additional results
198
+ st.markdown("---")
199
+ st.subheader("📊 Segmentation Results")
200
+
201
+ result_cols = st.columns(2)
202
+
203
+ with result_cols[0]:
204
+ if show_overlay:
205
+ st.markdown("**Overlay View**")
206
+ overlay = overlay_mask_on_image(img_array, binary_mask)
207
+ st.image(overlay, use_container_width=True)
208
+
209
+ with result_cols[1]:
210
+ if show_probabilistic:
211
+ st.markdown("**Probabilistic Mask**")
212
+ prob_mask_img = (prob_mask * 255).astype(np.uint8)
213
+ st.image(prob_mask_img, use_container_width=True)
214
+
215
+ # Download options
216
+ st.markdown("---")
217
+ st.subheader("💾 Download Results")
218
+
219
+ download_cols = st.columns(2)
220
+
221
+ with download_cols[0]:
222
+ # Binary mask download
223
+ binary_pil = Image.fromarray(binary_mask_img)
224
+ buf = io.BytesIO()
225
+ binary_pil.save(buf, format="PNG")
226
+ st.download_button(
227
+ label="Download Binary Mask",
228
+ data=buf.getvalue(),
229
+ file_name="binary_mask.png",
230
+ mime="image/png"
231
+ )
232
+
233
+ with download_cols[1]:
234
+ if show_overlay:
235
+ # Overlay download
236
+ overlay_pil = Image.fromarray(cv2.cvtColor(overlay, cv2.COLOR_BGR2RGB))
237
+ buf = io.BytesIO()
238
+ overlay_pil.save(buf, format="PNG")
239
+ st.download_button(
240
+ label="Download Overlay",
241
+ data=buf.getvalue(),
242
+ file_name="overlay.png",
243
+ mime="image/png"
244
+ )
245
+
246
+ # Statistics
247
+ st.markdown("---")
248
+ st.subheader("📈 Segmentation Statistics")
249
+ stats_cols = st.columns(3)
250
+
251
+ mask_area = np.sum(binary_mask > 0)
252
+ total_area = binary_mask.shape[0] * binary_mask.shape[1]
253
+ coverage = (mask_area / total_area) * 100
254
+
255
+ with stats_cols[0]:
256
+ st.metric("Mask Coverage", f"{coverage:.2f}%")
257
+ with stats_cols[1]:
258
+ st.metric("Image Size", f"{img_array.shape[1]}x{img_array.shape[0]}")
259
+ with stats_cols[2]:
260
+ st.metric("Mask Area (pixels)", f"{mask_area:,}")
261
+
262
+ except Exception as e:
263
+ st.error(f"❌ Error during segmentation: {str(e)}")
264
+
265
+ except Exception as e:
266
+ st.error(f"❌ Error loading image: {str(e)}")
267
+ st.info("Please try uploading a different image or reducing the file size.")
268
+
269
+ # Footer
270
+ st.markdown("---")
271
+ st.markdown("""
272
+ <div style='text-align: center'>
273
+ <p><strong>VREyeSAM</strong> - Virtual Reality Non-Frontal Iris Segmentation</p>
274
+ <p>🔗 <a href='https://github.com/GeetanjaliGTZ/VREyeSAM'>GitHub</a> |
275
+ 📧 <a href='mailto:geetanjalisharma546@gmail.com'>Contact</a></p>
276
+ </div>
277
+ """, unsafe_allow_html=True)
278
+
279
+ if __name__ == "__main__":
280
+ main()
deploy.bat ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+ REM VREyeSAM Deployment Script for Hugging Face Spaces (Windows)
3
+ REM This script automates the deployment process
4
+
5
+ echo ============================================================
6
+ echo VREyeSAM Deployment to Hugging Face Spaces
7
+ echo ============================================================
8
+ echo.
9
+
10
+ REM Step 1: Check prerequisites
11
+ echo [1/8] Checking prerequisites...
12
+
13
+ where git >nul 2>&1
14
+ if errorlevel 1 (
15
+ echo [ERROR] Git is not installed
16
+ echo Install from: https://git-scm.com/
17
+ pause
18
+ exit /b 1
19
+ )
20
+ echo [OK] Git installed
21
+
22
+ where git-lfs >nul 2>&1
23
+ if errorlevel 1 (
24
+ echo [ERROR] Git LFS is not installed
25
+ echo Install from: https://git-lfs.github.com/
26
+ pause
27
+ exit /b 1
28
+ )
29
+ echo [OK] Git LFS installed
30
+ echo.
31
+
32
+ REM Step 2: Get Hugging Face credentials
33
+ echo [2/8] Hugging Face Setup
34
+ set /p HF_USERNAME="Enter your Hugging Face username: "
35
+ set /p HF_SPACE_NAME="Enter your Space name: "
36
+
37
+ set HF_SPACE_URL=https://huggingface.co/spaces/%HF_USERNAME%/%HF_SPACE_NAME%
38
+
39
+ echo Space URL: %HF_SPACE_URL%
40
+ set /p CONFIRM="Is this correct? (y/n): "
41
+
42
+ if /i not "%CONFIRM%"=="y" (
43
+ echo Aborted.
44
+ pause
45
+ exit /b 0
46
+ )
47
+ echo.
48
+
49
+ REM Step 3: Initialize Git repository
50
+ echo [3/8] Initializing Git repository...
51
+
52
+ if not exist ".git" (
53
+ git init
54
+ echo [OK] Git repository initialized
55
+ ) else (
56
+ echo [OK] Git repository already exists
57
+ )
58
+ echo.
59
+
60
+ REM Step 4: Setup Git LFS
61
+ echo [4/8] Setting up Git LFS...
62
+
63
+ git lfs install
64
+ git lfs track "*.pt"
65
+ git lfs track "*.torch"
66
+ git lfs track "*.pth"
67
+ git lfs track "*.bin"
68
+
69
+ echo [OK] Git LFS configured
70
+ echo.
71
+
72
+ REM Step 5: Verify required files
73
+ echo [5/8] Verifying required files...
74
+
75
+ set MISSING=0
76
+
77
+ if exist "app.py" (
78
+ echo [OK] app.py
79
+ ) else (
80
+ echo [ERROR] app.py missing
81
+ set MISSING=1
82
+ )
83
+
84
+ if exist "requirements.txt" (
85
+ echo [OK] requirements.txt
86
+ ) else (
87
+ echo [ERROR] requirements.txt missing
88
+ set MISSING=1
89
+ )
90
+
91
+ if exist "README.md" (
92
+ echo [OK] README.md
93
+ ) else (
94
+ echo [ERROR] README.md missing
95
+ set MISSING=1
96
+ )
97
+
98
+ if exist ".gitattributes" (
99
+ echo [OK] .gitattributes
100
+ ) else (
101
+ echo [ERROR] .gitattributes missing
102
+ set MISSING=1
103
+ )
104
+
105
+ if %MISSING%==1 (
106
+ echo.
107
+ echo [ERROR] Missing required files. Please add them first.
108
+ pause
109
+ exit /b 1
110
+ )
111
+ echo.
112
+
113
+ REM Check SAM2 files
114
+ echo Checking SAM2 files...
115
+
116
+ if not exist "segment-anything-2" (
117
+ echo [ERROR] segment-anything-2 directory not found
118
+ echo Please clone SAM2 repository first.
119
+ pause
120
+ exit /b 1
121
+ )
122
+ echo [OK] segment-anything-2 directory
123
+
124
+ if not exist "segment-anything-2\checkpoints\sam2_hiera_small.pt" (
125
+ echo [ERROR] sam2_hiera_small.pt not found
126
+ pause
127
+ exit /b 1
128
+ )
129
+ echo [OK] sam2_hiera_small.pt
130
+
131
+ if not exist "segment-anything-2\checkpoints\VREyeSAM_uncertainity_best.torch" (
132
+ echo [ERROR] VREyeSAM_uncertainity_best.torch not found
133
+ pause
134
+ exit /b 1
135
+ )
136
+ echo [OK] VREyeSAM_uncertainity_best.torch
137
+ echo.
138
+
139
+ REM Step 6: Add remote
140
+ echo [6/8] Adding Hugging Face remote...
141
+
142
+ git remote remove space >nul 2>&1
143
+ git remote add space %HF_SPACE_URL%
144
+
145
+ echo [OK] Remote added: %HF_SPACE_URL%
146
+ echo.
147
+
148
+ REM Step 7: Commit files
149
+ echo [7/8] Committing files...
150
+
151
+ git add .
152
+ git commit -m "Deploy VREyeSAM to Hugging Face Spaces"
153
+
154
+ echo [OK] Files committed
155
+ echo.
156
+
157
+ REM Step 8: Push to Hugging Face
158
+ echo [8/8] Pushing to Hugging Face Spaces...
159
+ echo This may take several minutes due to large model files...
160
+ echo.
161
+
162
+ set /p PUSH_CONFIRM="Ready to push? (y/n): "
163
+
164
+ if /i not "%PUSH_CONFIRM%"=="y" (
165
+ echo Push cancelled. You can push manually later with:
166
+ echo git push --set-upstream space main --force
167
+ pause
168
+ exit /b 0
169
+ )
170
+
171
+ echo Pushing to Hugging Face...
172
+ git push --set-upstream space main --force
173
+
174
+ if errorlevel 1 (
175
+ echo.
176
+ echo ============================================================
177
+ echo [ERROR] Deployment failed
178
+ echo ============================================================
179
+ echo.
180
+ echo Common issues:
181
+ echo 1. Authentication: Make sure you're logged in
182
+ echo 2. Space doesn't exist: Create it first on Hugging Face
183
+ echo 3. Large files: Ensure Git LFS is properly configured
184
+ echo.
185
+ pause
186
+ exit /b 1
187
+ ) else (
188
+ echo.
189
+ echo ============================================================
190
+ echo [SUCCESS] Deployment successful!
191
+ echo ============================================================
192
+ echo.
193
+ echo Your space is available at:
194
+ echo %HF_SPACE_URL%
195
+ echo.
196
+ echo It may take 10-15 minutes to build. Monitor progress at:
197
+ echo %HF_SPACE_URL%/logs
198
+ echo.
199
+ pause
200
+ )
packages.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ libgl1
2
+ libglib2.0-0
requirements_deploy.txt ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Streamlit for web interface
2
+ streamlit>=1.28.0
3
+
4
+ # Core ML and Deep Learning
5
+ torch>=2.0.0,<2.5.0
6
+ torchvision>=0.15.0,<0.20.0
7
+ numpy>=1.22.0,<2.0.0
8
+
9
+ # Computer Vision
10
+ opencv-python-headless>=4.5.0
11
+ Pillow>=8.0.0
12
+
13
+ # Data Processing
14
+ pandas>=1.3.0
15
+ scikit-learn>=1.0.0
16
+
17
+ # Visualization
18
+ matplotlib>=3.5.0
19
+
20
+ # Utility
21
+ tqdm>=4.62.0
22
+ hydra-core>=1.1.0
23
+
24
+ # For downloading model weights
25
+ huggingface-hub>=0.19.0
26
+
27
+ # Note: SAM2 will be installed from git in Dockerfile
segment-anything-2 ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit 2b90b9f5ceec907a1c18123530e92e794ad901a4
test_app_local.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Local Testing Script for VREyeSAM Streamlit App
4
+
5
+ Run this script to test the app locally before deploying to Hugging Face Spaces.
6
+ Usage: python test_app_local.py
7
+ """
8
+
9
+ import subprocess
10
+ import sys
11
+ import os
12
+ import time
13
+
14
+ def check_dependencies():
15
+ """Check if all required dependencies are installed"""
16
+ print("🔍 Checking dependencies...")
17
+
18
+ required_packages = [
19
+ 'streamlit',
20
+ 'torch',
21
+ 'torchvision',
22
+ 'opencv-python',
23
+ 'numpy',
24
+ 'PIL'
25
+ ]
26
+
27
+ missing = []
28
+ for package in required_packages:
29
+ try:
30
+ __import__(package.replace('-', '_'))
31
+ print(f" ✅ {package}")
32
+ except ImportError:
33
+ print(f" ❌ {package}")
34
+ missing.append(package)
35
+
36
+ if missing:
37
+ print(f"\n⚠️ Missing packages: {', '.join(missing)}")
38
+ print("Install them with: pip install -r requirements_deploy.txt")
39
+ return False
40
+
41
+ print("✅ All dependencies installed\n")
42
+ return True
43
+
44
+ def check_model_files():
45
+ """Check if model files exist"""
46
+ print("🔍 Checking model files...")
47
+
48
+ files_to_check = [
49
+ "segment-anything-2/checkpoints/sam2_hiera_small.pt",
50
+ "segment-anything-2/checkpoints/VREyeSAM_uncertainity_best.torch"
51
+ ]
52
+
53
+ all_exist = True
54
+ for file_path in files_to_check:
55
+ if os.path.exists(file_path):
56
+ size_mb = os.path.getsize(file_path) / (1024 * 1024)
57
+ print(f" ✅ {file_path} ({size_mb:.1f} MB)")
58
+ else:
59
+ print(f" ❌ {file_path} - NOT FOUND")
60
+ all_exist = False
61
+
62
+ if not all_exist:
63
+ print("\n⚠️ Some model files are missing!")
64
+ print("Please run the setup instructions from README.md")
65
+ return False
66
+
67
+ print("✅ All model files present\n")
68
+ return True
69
+
70
+ def check_sam2_installation():
71
+ """Check if SAM2 is properly installed"""
72
+ print("🔍 Checking SAM2 installation...")
73
+
74
+ try:
75
+ sys.path.insert(0, "segment-anything-2")
76
+ from sam2.build_sam import build_sam2
77
+ from sam2.sam2_image_predictor import SAM2ImagePredictor
78
+ print(" ✅ SAM2 modules can be imported")
79
+ print("✅ SAM2 properly installed\n")
80
+ return True
81
+ except ImportError as e:
82
+ print(f" ❌ SAM2 import failed: {e}")
83
+ print("\n⚠️ SAM2 not properly installed!")
84
+ print("Install with:")
85
+ print(" git clone https://github.com/facebookresearch/segment-anything-2")
86
+ print(" cd segment-anything-2")
87
+ print(" pip install -e .")
88
+ return False
89
+
90
+ def test_app_syntax():
91
+ """Check if app.py has syntax errors"""
92
+ print("🔍 Checking app.py syntax...")
93
+
94
+ try:
95
+ with open('app.py', 'r', encoding='utf-8') as f:
96
+ code = f.read()
97
+ compile(code, 'app.py', 'exec')
98
+ print(" ✅ No syntax errors")
99
+ print("✅ app.py syntax valid\n")
100
+ return True
101
+ except SyntaxError as e:
102
+ print(f" ❌ Syntax error in app.py: {e}")
103
+ return False
104
+ except UnicodeDecodeError as e:
105
+ print(f" ⚠️ Unicode encoding issue: {e}")
106
+ print(" Trying with different encoding...")
107
+ try:
108
+ with open('app.py', 'r', encoding='latin-1') as f:
109
+ code = f.read()
110
+ compile(code, 'app.py', 'exec')
111
+ print(" ✅ No syntax errors (latin-1 encoding)")
112
+ print("✅ app.py syntax valid\n")
113
+ return True
114
+ except Exception as e2:
115
+ print(f" ❌ Still failed: {e2}")
116
+ return False
117
+
118
+ def run_streamlit_app():
119
+ """Launch the Streamlit app"""
120
+ print("🚀 Launching Streamlit app...")
121
+ print("=" * 60)
122
+ print("The app will open in your browser at http://localhost:8501")
123
+ print("Press Ctrl+C to stop the app")
124
+ print("=" * 60)
125
+ print()
126
+
127
+ try:
128
+ subprocess.run(['streamlit', 'run', 'app.py'], check=True)
129
+ except KeyboardInterrupt:
130
+ print("\n\n✅ App stopped by user")
131
+ except subprocess.CalledProcessError as e:
132
+ print(f"\n❌ Error running app: {e}")
133
+ return False
134
+
135
+ return True
136
+
137
+ def create_test_image():
138
+ """Create a simple test image if none exists"""
139
+ print("🔍 Checking for test images...")
140
+
141
+ test_dir = "test_images"
142
+ if not os.path.exists(test_dir):
143
+ os.makedirs(test_dir)
144
+ print(f" 📁 Created {test_dir} directory")
145
+
146
+ # Check if there are any test images
147
+ image_files = [f for f in os.listdir(test_dir) if f.endswith(('.jpg', '.png', '.jpeg'))]
148
+
149
+ if image_files:
150
+ print(f" ✅ Found {len(image_files)} test image(s)")
151
+ print(f" 📂 Test images in: {test_dir}/")
152
+ for img in image_files:
153
+ print(f" - {img}")
154
+ else:
155
+ print(f" ℹ️ No test images found in {test_dir}/")
156
+ print(f" 💡 Add some iris images to {test_dir}/ for testing")
157
+
158
+ print()
159
+
160
+ def main():
161
+ """Main testing function"""
162
+ print("\n" + "=" * 60)
163
+ print("VREyeSAM Local Testing Suite")
164
+ print("=" * 60 + "\n")
165
+
166
+ # Run all checks
167
+ checks = [
168
+ ("Dependencies", check_dependencies),
169
+ ("Model Files", check_model_files),
170
+ ("SAM2 Installation", check_sam2_installation),
171
+ ("App Syntax", test_app_syntax),
172
+ ]
173
+
174
+ all_passed = True
175
+ for name, check_func in checks:
176
+ if not check_func():
177
+ all_passed = False
178
+ print(f"❌ {name} check failed\n")
179
+
180
+ # Create test image directory
181
+ create_test_image()
182
+
183
+ if not all_passed:
184
+ print("=" * 60)
185
+ print("⚠️ Some checks failed. Please fix the issues above.")
186
+ print("=" * 60)
187
+ sys.exit(1)
188
+
189
+ print("=" * 60)
190
+ print("✅ All checks passed! Ready to run the app.")
191
+ print("=" * 60)
192
+ print()
193
+
194
+ # Ask user if they want to run the app
195
+ response = input("Do you want to launch the app now? (y/n): ").strip().lower()
196
+
197
+ if response == 'y':
198
+ run_streamlit_app()
199
+ else:
200
+ print("\n✅ Testing complete!")
201
+ print("To run the app manually, execute: streamlit run app.py")
202
+ print()
203
+
204
+ if __name__ == "__main__":
205
+ main()
windows.bat ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+ REM VREyeSAM Setup Script for Windows
3
+ REM This script sets up the environment and downloads required files
4
+
5
+ echo ============================================================
6
+ echo VREyeSAM Windows Setup Script
7
+ echo ============================================================
8
+ echo.
9
+
10
+ REM Check if Python is installed
11
+ python --version >nul 2>&1
12
+ if errorlevel 1 (
13
+ echo [ERROR] Python is not installed or not in PATH
14
+ echo Please install Python 3.11 from https://www.python.org/
15
+ pause
16
+ exit /b 1
17
+ )
18
+
19
+ echo [1/6] Creating virtual environment...
20
+ if exist vreyesam_env (
21
+ echo Virtual environment already exists, skipping...
22
+ ) else (
23
+ python -m venv vreyesam_env
24
+ echo Done!
25
+ )
26
+ echo.
27
+
28
+ echo [2/6] Activating virtual environment...
29
+ call vreyesam_env\Scripts\activate.bat
30
+ echo Done!
31
+ echo.
32
+
33
+ echo [3/6] Installing dependencies...
34
+ echo This may take a few minutes...
35
+ python -m pip install --upgrade pip
36
+ pip install streamlit
37
+ pip install torch==2.3.0 torchvision==0.18.0 --index-url https://download.pytorch.org/whl/cu118
38
+ pip install "numpy<2.0.0"
39
+ pip install opencv-python-headless pillow pandas scikit-learn matplotlib tqdm hydra-core
40
+ echo Done!
41
+ echo.
42
+
43
+ echo [4/6] Cloning SAM2 repository...
44
+ if exist segment-anything-2 (
45
+ echo SAM2 repository already exists, skipping...
46
+ ) else (
47
+ git clone https://github.com/facebookresearch/segment-anything-2
48
+ echo Done!
49
+ )
50
+ echo.
51
+
52
+ echo [5/6] Installing SAM2...
53
+ cd segment-anything-2
54
+ pip install -e .
55
+ cd ..
56
+ echo Done!
57
+ echo.
58
+
59
+ echo [6/6] Downloading model checkpoints...
60
+ if not exist segment-anything-2\checkpoints mkdir segment-anything-2\checkpoints
61
+
62
+ REM Download SAM2 base checkpoint
63
+ if exist segment-anything-2\checkpoints\sam2_hiera_small.pt (
64
+ echo SAM2 checkpoint already exists, skipping...
65
+ ) else (
66
+ echo Downloading SAM2 checkpoint (this may take a few minutes)...
67
+ powershell -Command "Invoke-WebRequest -Uri 'https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_small.pt' -OutFile 'segment-anything-2\checkpoints\sam2_hiera_small.pt'"
68
+ echo Done!
69
+ )
70
+
71
+ REM Download VREyeSAM weights
72
+ if exist segment-anything-2\checkpoints\VREyeSAM_uncertainity_best.torch (
73
+ echo VREyeSAM weights already exist, skipping...
74
+ ) else (
75
+ echo Downloading VREyeSAM weights...
76
+ pip install huggingface-hub
77
+ huggingface-cli download devnagaich/VREyeSAM VREyeSAM_uncertainity_best.torch --local-dir segment-anything-2\checkpoints\
78
+ echo Done!
79
+ )
80
+ echo.
81
+
82
+ echo ============================================================
83
+ echo Setup Complete!
84
+ echo ============================================================
85
+ echo.
86
+ echo To run the app:
87
+ echo 1. Activate the environment: vreyesam_env\Scripts\activate.bat
88
+ echo 2. Run: streamlit run app.py
89
+ echo.
90
+ echo Press any key to exit...
91
+ pause >nul