đã áp dụng file shapefile vào train và predict

This commit is contained in:
Victor Phan
2026-01-05 11:19:34 +07:00
parent 03048d9503
commit 10219df149
23 changed files with 4148 additions and 85 deletions
+283
View File
@@ -0,0 +1,283 @@
# Tóm tắt cập nhật Training Interface & API
## 📋 Những gì đã cập nhật
### 1. **Backend API (api_server.py)**
#### ✅ Cập nhật giá trị mặc định từ 01.train_ODC.ipynb:
- **Bbox mới**: `[105.5, 9.2, 106.4, 10.0]` (thay vì `[105.6, 9.3, 106.2, 9.8]`)
- **Thời gian mới**: `2023-03-01``2023-12-31` (thay vì `2023-03-01``2023-05-31`)
#### ✅ Thêm Label Mapping Constants:
```python
DEFAULT_LABEL_MAPPING = {
"Lua tom": "0",
"Lua": "1",
"CHN": "2",
"CLN": "3",
"TS": "4",
"Song": "5",
"Dat xay dung": "6",
"Rung": "7",
}
```
#### ✅ API Endpoints mới:
**1. `GET /api/training/labels`**
- Trả về danh sách tất cả labels và label mapping
- Response:
```json
{
"label_mapping": {...},
"label_names": {...},
"count": 8,
"labels": [...]
}
```
**2. `GET /api/training/files`**
- List tất cả shapefile trong thư mục `/train`
- Hiển thị: filename, size, số điểm, label column, unique labels
- Response:
```json
{
"files": [
{
"filename": "ST_training data_updated_1130points_new.shp",
"path": "train/...",
"size_mb": 0.15,
"point_count": 1130,
"label_column": "Hientrang",
"unique_labels": [...],
"label_count": 8
}
],
"count": 2,
"directory": "train/"
}
```
**3. `GET /api/training/shapefile/{filename}/labels`**
- Đọc chi tiết labels từ một shapefile cụ thể
- Trả về: số điểm, unique labels, label counts, bbox, columns
- Response:
```json
{
"filename": "...",
"label_column": "Hientrang",
"point_count": 1130,
"unique_labels": [...],
"label_count": 8,
"labels": [
{
"name": "Lua tom",
"code": "0",
"count": 150,
"mapped": true
},
...
],
"bbox": [105.5, 9.2, 106.4, 10.0],
"columns": [...]
}
```
#### ✅ Cập nhật Presets:
- Preset 1: "PC - Nhỏ" với bbox mới
- Preset 2: "Server - Trung bình" với bbox mới
- Preset 3: "Full - ODC" - PRESET MỚI từ 01.train_ODC.ipynb
- Bbox: `[105.5, 9.2, 106.4, 10.0]`
- Time: `2023-03-01``2023-12-31`
- Max scenes: 1
- Resolution: 10m
---
### 2. **Frontend UI (training_interface.html)**
#### ✅ Cập nhật giá trị mặc định trong form:
- **Hidden inputs bbox**:
- `minLon: 105.5, minLat: 9.2, maxLon: 106.4, maxLat: 10.0`
- **Date inputs**:
- `startDate: 2023-03-01, endDate: 2023-12-31`
#### ✅ Thêm section "Training Data (Shapefile)":
```html
<h3>📊 Training Data (Shapefile)</h3>
<select id="trainingShapefile">...</select>
```
Features:
- Dropdown chọn shapefile từ thư mục `/train`
- Tự động load default: `ST_training data_updated_1130points_new.shp`
- Hiển thị thông tin: số điểm, label column, số lớp, bbox
#### ✅ Thêm phần hiển thị thông tin Shapefile:
```html
<div id="shapefileInfo">
- Số điểm
- Label column
- Số lớp
- Bbox
- Phân bố labels (với icon ✅/⚠️)
- Button "Áp dụng Bbox từ Shapefile"
</div>
```
#### ✅ JavaScript Functions mới:
**1. `loadTrainingFiles()`**
- Load danh sách shapefile từ API
- Populate dropdown
- Auto-select default shapefile
**2. `loadShapefileLabels(filename)`**
- Load chi tiết labels từ shapefile
- Hiển thị phân bố labels
- Highlight labels đã map vs chưa map
**3. `applyShapefileBbox()`**
- Áp dụng bbox từ shapefile đã chọn
- Cập nhật form inputs
- Vẽ rectangle trên map
- Hiển thị notification
**4. `showNotification(type, message)`**
- Helper function để hiển thị notifications
- Support types: success, error, warning
#### ✅ Cập nhật form submission:
- Thêm `training_shapefile` vào config
- Default: `train/ST_training data_updated_1130points_new.shp`
#### ✅ Event listeners:
```javascript
document.getElementById('trainingShapefile').addEventListener('change',
(e) => loadShapefileLabels(e.target.value)
);
```
---
### 3. **Bản đồ (Map)**
#### ✅ Initial rectangle với bbox mới:
- Tự động vẽ rectangle với bbox từ backend
- Fit map bounds để hiển thị khu vực
#### ✅ Dynamic update từ shapefile:
- Khi chọn shapefile → có thể áp dụng bbox
- Màu khác biệt (xanh dương) để dễ nhận biết
---
## 🧪 Test Script
File `test_training_api.py` để test các endpoints:
```bash
# Run API server (terminal 1)
conda activate env_01
python api_server.py
# Run test script (terminal 2)
conda activate env_01
python test_training_api.py
```
Test coverage:
1. ✅ GET /api/training/labels
2. ✅ GET /api/training/files
3. ✅ GET /api/training/shapefile/{filename}/labels
4. ✅ GET /api/config/presets
---
## 📊 Workflow mới
### Cách sử dụng trên giao diện:
1. **Mở Training Interface**: http://localhost:8000/training
2. **Chọn Training Data**:
- Chọn shapefile từ dropdown "📊 Training Data"
- Xem thông tin: số điểm, labels, bbox
- (Optional) Click "📍 Áp dụng Bbox từ Shapefile"
3. **Chọn Khu vực**:
- Option 1: Chọn tỉnh thành
- Option 2: Vẽ rectangle trên map
- Option 3: Áp dụng bbox từ shapefile
- Option 4: Chọn preset
4. **Cấu hình thời gian và parameters**:
- Thời gian mặc định: 2023-03-01 → 2023-12-31
- Bbox mặc định: [105.5, 9.2, 106.4, 10.0]
5. **Start Training**:
- Form tự động gửi `training_shapefile` parameter
- Backend sẽ dùng đúng shapefile đã chọn
---
## 🎯 Kết quả
### ✅ Backend:
- 3 API endpoints mới hoạt động
- Default values khớp với notebook
- Label mapping được share
### ✅ Frontend:
- UI mới để chọn shapefile
- Hiển thị chi tiết labels
- Auto-load default shapefile
- Bbox từ shapefile có thể áp dụng
### ✅ Map:
- Initial bbox khớp với backend
- Update bbox từ nhiều nguồn
- Visual feedback rõ ràng
---
## 🔍 Debug & Verify
### Check API:
```bash
# List training files
curl http://localhost:8000/api/training/files
# Get labels
curl http://localhost:8000/api/training/labels
# Get shapefile labels
curl "http://localhost:8000/api/training/shapefile/ST_training data_updated_1130points_new.shp/labels"
```
### Check Browser Console:
- F12 → Console
- Xem logs khi chọn shapefile
- Check network requests
---
## 📝 Notes
1. **Training shapefile path format**:
- Frontend select value: `ST_training data_updated_1130points_new.shp`
- Backend receives: `train/ST_training data_updated_1130points_new.shp`
- Auto-prepend `train/` prefix in form submission
2. **Label mapping**:
- ✅ icon: Label có trong DEFAULT_LABEL_MAPPING
- ⚠️ icon: Label chưa có trong mapping
3. **Bbox sources**:
- Default từ backend
- Từ tỉnh thành
- Từ shapefile
- Từ preset
- Vẽ thủ công
Tất cả đều hoạt động đồng bộ!