File size: 4,318 Bytes
0465b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Setup script for YOLO Object Detection application.
This script helps set up the environment and install dependencies.
"""

import os
import sys
import subprocess
import platform

def run_command(command, description):
    """Run a command and return success status."""
    print(f"πŸ”„ {description}...")
    try:
        result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
        print(f"βœ… {description} completed successfully")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ {description} failed:")
        print(f"   Command: {command}")
        print(f"   Error: {e.stderr}")
        return False

def check_python_version():
    """Check if Python version is compatible."""
    print("πŸ” Checking Python version...")
    version = sys.version_info
    
    if version.major == 3 and version.minor >= 8:
        print(f"βœ… Python {version.major}.{version.minor}.{version.micro} is compatible")
        return True
    else:
        print(f"❌ Python {version.major}.{version.minor}.{version.micro} is not compatible")
        print("   Required: Python 3.8 or higher")
        return False

def check_virtual_environment():
    """Check if virtual environment exists."""
    print("πŸ” Checking virtual environment...")
    
    if os.path.exists("venv"):
        print("βœ… Virtual environment found")
        return True
    else:
        print("⚠️ Virtual environment not found")
        return False

def create_virtual_environment():
    """Create virtual environment."""
    return run_command(
        f"{sys.executable} -m venv venv",
        "Creating virtual environment"
    )

def get_activation_command():
    """Get the appropriate activation command for the platform."""
    if platform.system() == "Windows":
        return "venv\\Scripts\\activate"
    else:
        return "source venv/bin/activate"

def install_dependencies():
    """Install required dependencies."""
    activation_cmd = get_activation_command()
    
    # Install basic dependencies first
    basic_deps = [
        "pip --upgrade",
        "opencv-python",
        "streamlit",
        "ultralytics",
        "pillow",
        "numpy",
        "pandas"
    ]
    
    for dep in basic_deps:
        if not run_command(
            f"{activation_cmd} && pip install {dep}",
            f"Installing {dep}"
        ):
            return False
    
    return True

def generate_requirements():
    """Generate requirements.txt file."""
    activation_cmd = get_activation_command()
    return run_command(
        f"{activation_cmd} && pip freeze > requirements.txt",
        "Generating requirements.txt"
    )

def test_installation():
    """Test the installation."""
    activation_cmd = get_activation_command()
    return run_command(
        f"{activation_cmd} && python test_app.py",
        "Testing installation"
    )

def main():
    """Main setup function."""
    print("🎯 YOLO Object Detection - Setup Script")
    print("=" * 50)
    
    # Check Python version
    if not check_python_version():
        return 1
    
    # Check/create virtual environment
    if not check_virtual_environment():
        print("πŸ“¦ Creating virtual environment...")
        if not create_virtual_environment():
            return 1
    
    # Install dependencies
    print("πŸ“₯ Installing dependencies...")
    if not install_dependencies():
        print("❌ Failed to install dependencies")
        return 1
    
    # Generate requirements.txt
    print("πŸ“ Generating requirements.txt...")
    generate_requirements()  # Don't fail if this doesn't work
    
    # Test installation
    print("πŸ§ͺ Testing installation...")
    if test_installation():
        print("\nπŸŽ‰ Setup completed successfully!")
        print("\nNext steps:")
        print("1. Activate the virtual environment:")
        print(f"   {get_activation_command()}")
        print("2. Run the application:")
        print("   streamlit run app.py")
        print("3. Open your browser to the URL shown in the terminal")
    else:
        print("\n⚠️ Setup completed but tests failed")
        print("You can still try running the application manually")
    
    return 0

if __name__ == "__main__":
    sys.exit(main())