-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
296 lines (248 loc) · 10.4 KB
/
Copy pathrun.py
File metadata and controls
296 lines (248 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import os
import sys
import subprocess
import webbrowser
import time
import importlib.util
import platform
from flask import Flask, render_template, Response, jsonify, request
import cv2
import numpy as np
from backend.video_processor import VideoProcessor
from backend.utils import get_video_info
from datetime import datetime
def check_dependency(package_name):
"""
Check if a package is installed.
Args:
package_name: Name of the package to check
Returns:
True if installed, False otherwise
"""
return importlib.util.find_spec(package_name) is not None
def check_dependencies():
"""
Check if all required dependencies are installed.
"""
dependencies = {
'cv2': 'opencv-python',
'torch': 'torch',
'ultralytics': 'ultralytics',
'flask': 'flask',
'numpy': 'numpy'
}
missing_deps = []
for module_name, package_name in dependencies.items():
if not check_dependency(module_name):
missing_deps.append(package_name)
if missing_deps:
print("Missing dependencies:")
for dep in missing_deps:
print(f" - {dep}")
print("\nYou can install them using:")
print(f"pip install {' '.join(missing_deps)}")
# Special note for ultralytics
if 'ultralytics' in missing_deps:
print("\nNote: The application can run without ultralytics, but object detection will be disabled.")
print("If you want to use object detection, please install ultralytics.")
# Ask if user wants to install dependencies automatically
if input("\nDo you want to install missing dependencies now? (y/n): ").lower() == 'y':
try:
print(f"Installing: {' '.join(missing_deps)}")
subprocess.check_call([sys.executable, "-m", "pip", "install"] + missing_deps)
print("Dependencies installed successfully.")
return True
except subprocess.CalledProcessError:
print("Failed to install dependencies. Please install them manually.")
return False
return False
print("✓ All required dependencies are installed.")
return True
def check_models():
"""
Check if required models are available.
"""
models_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models')
os.makedirs(models_dir, exist_ok=True)
# Check for YOLOv8 model
yolo_model_path = os.path.join(models_dir, 'yolov8n.pt')
if not os.path.exists(yolo_model_path):
print("! YOLOv8 model not found. It will be downloaded automatically when the application starts.")
else:
print("✓ YOLOv8 model found.")
# Check for SAM 2 model
sam_model_path = os.path.join(models_dir, 'sam2_checkpoint.pth')
if not os.path.exists(sam_model_path):
print("! SAM 2 model not found. Please download it manually and place it in the 'models' directory.")
print(" For testing purposes, the application will use a dummy SAM 2 model.")
else:
print("✓ SAM 2 model found.")
def check_sample_video():
"""
Check if a sample video is available.
"""
data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
os.makedirs(data_dir, exist_ok=True)
video_path = os.path.join(data_dir, 'test_video.mp4')
if not os.path.exists(video_path):
print("! Sample video not found.")
print(" You can download one using: python download_sample_video.py")
print(" Alternatively, you can use your webcam or provide a path to a video file.")
# Ask if user wants to download a sample video
if input("\nDo you want to download a sample video now? (y/n): ").lower() == 'y':
try:
download_script = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'download_sample_video.py')
if os.path.exists(download_script):
subprocess.check_call([sys.executable, download_script])
print("Sample video downloaded successfully.")
else:
print("Download script not found. Please download a sample video manually.")
except subprocess.CalledProcessError:
print("Failed to download sample video. Please download it manually.")
else:
print("✓ Sample video found.")
def run_application():
"""
Run the Flask application.
"""
flask_app_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'frontend', 'app.py')
if not os.path.exists(flask_app_path):
print(f"! Flask application not found at {flask_app_path}")
return False
print("\nStarting the Manufacturing Vision System...")
# Run the Flask app in a subprocess
if platform.system() == 'Windows':
# Hide console window on Windows
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
flask_process = subprocess.Popen([sys.executable, flask_app_path],
startupinfo=startupinfo)
else:
flask_process = subprocess.Popen([sys.executable, flask_app_path])
# Wait a moment for the server to start
print("Initializing server...")
for _ in range(5):
time.sleep(0.5)
print(".", end="", flush=True)
print("\n")
# Open the web browser
print("Opening web interface...")
webbrowser.open('http://localhost:5000')
print("\n" + "="*50)
print("Manufacturing Vision System is now running!")
print("Access the web interface at: http://localhost:5000")
print("Press Ctrl+C to stop the application.")
print("="*50 + "\n")
try:
# Keep the script running until interrupted
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nStopping the application...")
flask_process.terminate()
flask_process.wait()
print("Application stopped successfully.")
return True
def print_banner():
"""
Print a banner for the application.
"""
banner = """
╔═══════════════════════════════════════════════════╗
║ ║
║ MANUFACTURING VISION SYSTEM ║
║ Object Detection & Tracking ║
║ ║
╚═══════════════════════════════════════════════════╝
"""
print(banner)
app = Flask(__name__, template_folder='frontend/templates', static_folder='frontend/static')
# Initialize video processor
try:
from ultralytics import YOLO
from backend.segmentation import SegmentationProcessor
# Load YOLO model
yolo_model = YOLO('yolov8n.pt')
# Initialize segmentation processor
segmentation_processor = SegmentationProcessor()
# Initialize video processor with dependencies
video_processor = VideoProcessor(yolo_model=yolo_model, segmentation_processor=segmentation_processor)
except Exception as e:
print(f"Warning: Could not initialize object detection: {str(e)}")
print("Running in basic mode without object detection...")
video_processor = None
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/change_source', methods=['POST'])
def change_source():
source = request.json.get('source')
file = request.files.get('file')
if source == 'sample':
video_processor.set_source(0) # Use sample video
elif source == 'webcam':
video_processor.set_source(0) # Use webcam
elif source == 'custom' and file:
# Save uploaded video file
file_path = 'uploads/custom_video.mp4'
file.save(file_path)
video_processor.set_source(file_path)
return jsonify({'status': 'success'})
def generate_frames():
while True:
frame = video_processor.get_processed_frame()
if frame is None:
continue
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
continue
frame_bytes = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
@app.route('/get_video_info')
def video_info():
info = get_video_info(video_processor.get_current_frame())
return jsonify(info)
@app.route('/toggle_detection')
def toggle_detection():
video_processor.toggle_detection()
return jsonify({'status': 'success'})
@app.route('/toggle_tracking')
def toggle_tracking():
video_processor.toggle_tracking()
return jsonify({'status': 'success'})
@app.route('/toggle_statistics')
def toggle_statistics():
video_processor.toggle_statistics()
return jsonify({'status': 'success'})
@app.route('/capture_screenshot')
def capture_screenshot():
frame = video_processor.get_current_frame()
if frame is not None:
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f'screenshot_{timestamp}.jpg'
filepath = os.path.join('frontend/static/screenshots', filename)
cv2.imwrite(filepath, frame)
return jsonify({
'status': 'success',
'filename': filename
})
return jsonify({'status': 'error'})
if __name__ == "__main__":
print_banner()
# Check dependencies
deps_ok = check_dependencies()
# Check models
check_models()
# Check sample video
check_sample_video()
# Run the application if dependencies are OK or user chose to continue
if deps_ok or input("\nContinue without all dependencies? (y/n): ").lower() == 'y':
if not run_application():
sys.exit(1)
else:
print("\nPlease install the required dependencies and try again.")
sys.exit(1)