diff --git a/README-zh-tw.md b/README-zh-tw.md index cdb8a91..d0ea953 100644 --- a/README-zh-tw.md +++ b/README-zh-tw.md @@ -254,59 +254,153 @@ 要在終端運行危險檢測系統,您需要在機器上安裝 Python。按照以下步驟來啟動系統: - 1. 將存儲庫克隆到本地機器。 - ```bash - git clone https://github.com/yihong1120/Construction-Hazard-Detection.git - ``` + ### 1. 將專案複製至本機 + 使用以下指令將專案從 GitHub 複製到本機: - 2. 進入克隆的目錄。 - ```bash - cd Construction-Hazard-Detection - ``` + ```bash + git clone https://github.com/yihong1120/Construction-Hazard-Detection.git + ``` - 3. 安裝所需的軟體包: - ```bash - pip install -r requirements.txt - ``` + --- + + ### 2. 進入專案目錄 + 進入剛剛複製的專案目錄: + + ```bash + cd Construction-Hazard-Detection + ``` + + --- - 4. 安裝並啟動 MySQL 服務: + ### 3. 安裝所需的套件 + 執行以下指令安裝專案所需的套件: - Linux 使用者: + ```bash + pip install -r requirements.txt + ``` + + --- + + ### 4. 安裝並啟動 MySQL 服務(如需要) + + #### Ubuntu 系統使用者: + + 1. 開啟終端機,執行以下指令安裝 MySQL: ```bash + sudo apt update sudo apt install mysql-server + ``` + + 2. 安裝完成後,啟動 MySQL 服務: + ```bash sudo systemctl start mysql.service ``` - 對於其他人,您可以在此[連結](https://dev.mysql.com/downloads/)下載並安裝適用於您的作業系統的MySQL。 + #### 其他作業系統使用者: + + 請從 [MySQL 下載頁面](https://dev.mysql.com/downloads/)下載適合您作業系統的 MySQL 並完成安裝。 + + --- + + #### 初始化資料庫: + + 完成 MySQL 安裝後,執行初始化腳本來建立 `construction_hazard_detection` 資料庫及 `users` 資料表: + + ```bash + mysql -u root -p < scripts/init.sql + ``` - 5. 設置用戶帳戶和密碼。使用以下命令啟動用戶管理 API: + 執行指令後,系統會提示您輸入 MySQL 的 root 密碼。請確認 `scripts/init.sql` 檔案內已包含建立資料庫和資料表的 SQL 指令。 + + --- + + ### 5. 設定 Redis 伺服器(僅限於 Streaming Web 功能) + + Redis 僅在使用 **Streaming Web** 功能時需要。請依以下步驟安裝及設定 Redis: + + #### Ubuntu 系統使用者: + + 1. **安裝 Redis**: + 執行以下指令安裝 Redis: ```bash - gunicorn -w 1 -b 0.0.0.0:8000 "examples.user_management.app:user-managements-app" + sudo apt update + sudo apt install redis-server ``` - 建議使用 Postman 應用程式與 API 進行互動。 - 6. 要運行物體檢測 API,使用以下命令: + 2. **設定 Redis(可選)**: + - 如果需要進一步設定,請編輯 Redis 設定檔案: ```bash - gunicorn -w 1 -b 0.0.0.0:8001 "examples.YOLO_server_api.app:YOLO-server-api-app" + sudo vim /etc/redis/redis.conf + ``` + - 如需啟用密碼保護,請找到以下行,取消註解並設置密碼: + ```conf + requirepass YourStrongPassword ``` + 將 `YourStrongPassword` 替換為強密碼。 - 7. 使用特定的配置文件運行主應用程序,使用以下命令: + 3. **啟動並設置 Redis 服務開機自動啟動**: + - 啟動 Redis 服務: ```bash - python3 main.py --config config/configuration.json + sudo systemctl start redis.service ``` - 將 `config/configuration.json` 替換為您的配置文件的實際路徑。 + - 設置開機自動啟動: + ```bash + sudo systemctl enable redis.service + ``` + + #### 其他作業系統使用者: + + 請參考官方文件:[Redis 安裝指南](https://redis.io/docs/getting-started/installation/)。 + + --- + + ### 6. 啟動物件偵測 API + 執行以下指令啟動物件偵測 API: + + ```bash + uvicorn examples.YOLO_server.backend.app:sio_app --host 0.0.0.0 --port 8001 + ``` + + --- + + ### 7. 使用特定配置檔案執行主應用程式 + 執行以下指令啟動主應用程式,並指定配置檔案: + + ```bash + python3 main.py --config config/configuration.json + ``` + + 請將 `config/configuration.json` 替換為您的實際配置檔案路徑。 + + --- + + ### 8. 啟動 Streaming Web 服務 + + #### 啟動後端服務 + + ##### Linux 系統使用者: + 執行以下指令啟動後端服務: + + ```bash + uvicorn examples.streaming_web.backend.app:sio_app --host 127.0.0.1 --port 8002 + ``` + + ##### Windows 系統使用者: + 使用以下 `waitress-serve` 指令啟動後端服務: + + ```cmd + waitress-serve --host=127.0.0.1 --port=8002 "examples.streaming_web.backend.app:streaming-web-app" + ``` + + --- - 8. 要啟動串流 Web 服務,執行以下命令: + #### 設定前端服務 - 對於 Linux 使用者: - ````bash - gunicorn -w 1 -k eventlet -b 127.0.0.1:8002 "examples.streaming_web.app:streaming-web-app" - ```` + 請參考 `examples/YOLO_server_api/frontend/nginx.conf` 文件進行部署,並將靜態網頁檔案放置於以下目錄: - 對於 Windows 使用者: - ```` - waitress-serve --host=127.0.0.1 --port=8002 "examples.streaming_web.app:streaming-web-app" - ```` + ```plaintext + examples/YOLO_server_api/frontend/dist + ``` diff --git a/README.md b/README.md index 575d1bc..d22e7ec 100644 --- a/README.md +++ b/README.md @@ -260,59 +260,169 @@ Now, you could launch the hazard-detection system in Docker or Python env: To run the hazard detection system with Python, follow these steps: - 1. Clone the repository to your local machine: - ```bash - git clone https://github.com/yihong1120/Construction-Hazard-Detection.git - ``` + ### **1. Clone the Repository to Your Local Machine** - 2. Navigate to the cloned directory: - ```bash - cd Construction-Hazard-Detection - ``` + Use the following command to clone the repository from GitHub: - 3. Install required packages: - ```bash - pip install -r requirements.txt - ``` + ```bash + git clone https://github.com/yihong1120/Construction-Hazard-Detection.git + ``` + + --- + + ### **2. Navigate to the Cloned Directory** + + Change the directory to the newly cloned repository: + + ```bash + cd Construction-Hazard-Detection + ``` + + --- + + ### **3. Install Required Packages** + + Run the following command to install the necessary Python packages: + + ```bash + pip install -r requirements.txt + ``` - 4. Install and launch MySQL service (if required): + --- + + ### **4. Install and Launch MySQL Service (if Required)** + + #### **For Ubuntu Users** + + 1. Open the terminal and execute the following commands to install and start the MySQL server: - For Ubuntu users: ```bash + sudo apt update sudo apt install mysql-server sudo systemctl start mysql.service ``` - For others, you can download and install MySQL that works in your operation system in this [link](https://dev.mysql.com/downloads/). + #### **For Other Operating Systems** + + 1. Download and install the appropriate version of MySQL for your operating system from the [MySQL Downloads](https://dev.mysql.com/downloads/) page. + + --- + + #### **Initialise the Database** + + After installing MySQL, use the following command to initialise the `construction_hazard_detection` database and create the `users` table: + + ```bash + mysql -u root -p < scripts/init.sql + ``` + + You will be prompted to enter the MySQL root password. Ensure that the `scripts/init.sql` file contains the necessary SQL commands to set up the database and tables as described earlier. + + --- + + ### **5. Set Up Redis Server (Required Only for Streaming Web)** + + Redis is needed only when using the **Streaming Web** functionality. Follow the steps below to set up Redis. + + #### **For Ubuntu Users** + + 1. **Install Redis** + + Open the terminal and run the following commands: - 5. Start user management API: ```bash - gunicorn -w 1 -b 0.0.0.0:8000 "examples.user_management.app:user-managements-app" + sudo apt update + sudo apt install redis-server ``` - 6. Run object detection API: + 2. **Configure Redis (Optional)** + + If you need custom settings, edit the Redis configuration file: + ```bash - uvicorn examples.YOLO_server.app:sio_app --host 0.0.0.0 --port 8001 + sudo vim /etc/redis/redis.conf ``` - 7. Run the main application with a specific configuration file: - ```bash - python3 main.py --config config/configuration.json + To enhance security, enable password protection by adding or modifying the following line: + + ```conf + requirepass YourStrongPassword ``` - Replace `config/configuration.json` with the actual path to your configuration file. - 8. Start the streaming web service: + Replace `YourStrongPassword` with a secure password. + + 3. **Start and Enable Redis Service** + + Start the Redis service: - For linux users: ```bash - uvicorn examples.streaming_web.app:sio_app --host 0.0.0.0 --port 8002 + sudo systemctl start redis.service ``` - For windows users: - ``` - waitress-serve --host=127.0.0.1 --port=8002 "examples.streaming_web.app:streaming-web-app" + Enable Redis to start automatically on boot: + + ```bash + sudo systemctl enable redis.service ``` + #### **For Other Operating Systems** + + Refer to the official [Redis installation guide](https://redis.io/docs/getting-started/installation/) for instructions specific to your operating system. + + --- + + ### **6. Run the Object Detection API** + + Start the object detection API with the following command: + + ```bash + uvicorn examples.YOLO_server.backend.app:sio_app --host 0.0.0.0 --port 8001 + ``` + + --- + + ### **7. Run the Main Application with a Specific Configuration File** + + Use the following command to run the main application and specify the configuration file: + + ```bash + python3 main.py --config config/configuration.json + ``` + + Replace `config/configuration.json` with the actual path to your configuration file. + + --- + + ### **8. Start the Streaming Web Service** + + #### **Launching the Backend** + + ##### **For Linux Users** + + Run the following command to start the backend service on a Linux system: + + ```bash + uvicorn examples.streaming_web.backend.app:sio_app --host 127.0.0.1 --port 8002 + ``` + + ##### **For Windows Users** + + To start the backend service on a Windows system, use the following command: + + ```cmd + waitress-serve --host=127.0.0.1 --port=8002 "examples.streaming_web.backend.app:streaming-web-app" + ``` + + --- + + #### **Setting Up the Frontend** + + Refer to the `examples/YOLO_server_api/frontend/nginx.conf` file for deployment instructions. Place the static web files in the following directory: + + ```plaintext + examples/YOLO_server_api/frontend/dist + ``` + ## Additional Information diff --git a/examples/YOLO_server_api/frontend/nginx.conf b/examples/YOLO_server_api/frontend/nginx.conf new file mode 100644 index 0000000..15ed3e2 --- /dev/null +++ b/examples/YOLO_server_api/frontend/nginx.conf @@ -0,0 +1,58 @@ +# HTTP Configuration: Automatically redirect HTTP to HTTPS +server { + listen 80; + server_name your-domain.com; + + # Redirect all HTTP requests to HTTPS + return 301 https://$host$request_uri; +} + +# HTTPS Configuration +server { + listen 443 ssl http2; + server_name your-domain.com; + + # Set the website's root directory to point to the frontend build output directory + root /path/to/frontend/dist; + + # Specify the default index file + index index.html; + + # Configure SSL certificates + ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem; + + # Recommended SSL settings for enhanced security + ssl_protocols TLSv1.2 TLSv1.3; + ssl_prefer_server_ciphers on; + ssl_ciphers HIGH:!aNULL:!MD5; + + # Handle static files + location / { + # Check if the requested file exists, otherwise fallback to index.html (suitable for Single Page Applications) + try_files $uri /index.html; + } + + # Proxy /api requests to the backend service + location /api/ { + proxy_pass http://backend-server.com:port; # Backend service address + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + # Necessary configuration to support WebSocket upgrades + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } + + # Configure caching for static resources + location ~* \.(?:ico|css|js|gif|jpe?g|png|woff2?|eot|ttf|svg|mp4)$ { + expires 6M; + access_log off; + add_header Cache-Control "public"; + } + + # Configure the 404 error page + error_page 404 /index.html; +} diff --git a/scripts/init.sql b/scripts/init.sql index 1bda42b..703da8b 100644 --- a/scripts/init.sql +++ b/scripts/init.sql @@ -1,3 +1,10 @@ +-- Create the construction_hazard_detection database +CREATE DATABASE IF NOT EXISTS construction_hazard_detection; + +-- Select the construction_hazard_detection database +USE construction_hazard_detection; + +-- Create the users table CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(80) NOT NULL UNIQUE, @@ -7,3 +14,10 @@ CREATE TABLE IF NOT EXISTS users ( created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); + +-- Insert initial user +INSERT INTO users (username, password_hash, role, is_active) VALUES +('user', 'scrypt:32768:8:1$T2AeQL78js2oJaUz$5c51c110fca53b8f9f2d312a662b5f60cb1134ec0c1395243966b673fcd8cae59da66a66198f5880611cd9b68d2abba789d7523c4cccfa5882125667f0451f7e', 'admin', 1); + +-- Create the projects table +-- mysql -u root -p < scripts/init.sql