smpas 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2026 guowei_ni@bit. All rights reserved.
2
+
3
+ This software and associated documentation files (the "Software") are proprietary and
4
+ confidential. No permission is granted to use, copy, modify, merge, publish, distribute,
5
+ sublicense, or sell the Software, in whole or in part, in any form.
6
+
7
+ Any use of the Software requires explicit written permission from the copyright holder.
@@ -0,0 +1,16 @@
1
+ include README.md
2
+ include LICENSE.txt
3
+ include requirements.txt
4
+ recursive-include src/templates *.html
5
+
6
+ exclude *.pdf
7
+ prune data
8
+ prune outputs
9
+ prune models
10
+ prune paper
11
+ prune clean_code
12
+
13
+ # Skip OS / cache files
14
+ global-exclude __pycache__
15
+ global-exclude *.pyc
16
+ global-exclude *Zone.Identifier
smpas-0.1.1/PKG-INFO ADDED
@@ -0,0 +1,433 @@
1
+ Metadata-Version: 2.4
2
+ Name: smpas
3
+ Version: 0.1.1
4
+ Summary: AprilTag-calibrated dried shiitake mushroom cap analysis (diameter, crack ratio, pattern classification).
5
+ License-Expression: LicenseRef-Proprietary
6
+ Keywords: mushroom,apriltag,computer-vision,yolo,sam,flask
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.10
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE.txt
12
+ Requires-Dist: torch>=2.0.0
13
+ Requires-Dist: torchvision>=0.15.0
14
+ Requires-Dist: opencv-python>=4.8.0
15
+ Requires-Dist: numpy>=1.24.0
16
+ Requires-Dist: scipy>=1.10.0
17
+ Requires-Dist: ultralytics>=8.0.0
18
+ Requires-Dist: apriltag>=0.4.0
19
+ Requires-Dist: Pillow>=9.5.0
20
+ Requires-Dist: pandas>=2.0.0
21
+ Requires-Dist: tqdm>=4.65.0
22
+ Requires-Dist: matplotlib>=3.7.0
23
+ Requires-Dist: pyyaml>=6.0
24
+ Requires-Dist: Flask>=2.3.0
25
+ Requires-Dist: Werkzeug>=2.3.0
26
+ Dynamic: license-file
27
+
28
+ # 香菇菌盖分析系统(AprilTag标定版)
29
+
30
+ 基于AprilTag标定的香菇菌盖最大直径和开裂比自动分析系统。
31
+
32
+ ## 项目特色
33
+
34
+ ### ✨ 核心功能
35
+ 1. **AprilTag精确标定**:使用37.58mm精确已知尺寸的AprilTag进行像素/毫米比例标定
36
+ 2. **最大直径测量**:旋转卡壳算法计算菌盖真实最大直径(mm级精度)
37
+ 3. **开裂比分析**:HSV双通道Otsu阈值法自动提取花纹并计算开裂比
38
+ 4. **五级评定**:根据开裂比自动评定香菇等级(一级至五级)
39
+
40
+ ### 🚀 性能优化(ROI处理哲学)
41
+ - **分层并行架构**:AprilTag检测和YOLO检测并行进行
42
+ - **ROI聚焦处理**:只处理感兴趣区域,而非全图
43
+ - AprilTag区域:~100×100像素
44
+ - 香菇ROI:~500×500像素
45
+ - 全图:4000×3000 = 1200万像素
46
+ - **实际处理量减少94%**(76万 vs 1200万像素)
47
+ - **可选并行模式**:多个香菇同时处理,3倍速度提升
48
+
49
+ ### 🎯 技术栈
50
+ - **YOLO v8**:香菇快速检测(mAP50: 99.5%)
51
+ - **SAM (Segment Anything)**:菌盖精确分割
52
+ - **AprilTag 36h11**:亚像素级精度标定
53
+ - **旋转卡壳算法**:计算真实最远点对直径
54
+ - **HSV Otsu阈值**:自适应花纹提取
55
+
56
+ ---
57
+
58
+ ## 项目结构
59
+
60
+ ```
61
+ mushroom_apriltag/
62
+ ├── src/ # 源代码
63
+ │ ├── apriltag_calibration.py # AprilTag检测与标定
64
+ │ ├── diameter_calculator.py # 最大直径计算(旋转卡壳)
65
+ │ ├── crack_analyzer.py # 开裂比分析(HSV+Otsu)
66
+ │ ├── mushroom_detector.py # YOLO检测 + SAM分割
67
+ │ ├── integrated_analyzer.py # 集成分析器(ROI优化)
68
+ │ ├── parallel_processor.py # 并行处理模块
69
+ │ └── pipeline.py # 批量处理脚本
70
+
71
+ ├── models/ # 模型文件(需下载)
72
+ │ ├── yolo_mushroom.pt # YOLO检测模型
73
+ │ └── sam_vit_b.pth # SAM分割模型
74
+
75
+ ├── data/
76
+ │ └── raw_images/ # 原始图片(放置你的数据)
77
+
78
+ ├── outputs/ # 输出结果
79
+ │ ├── analysis_results.csv # 分析结果表格
80
+ │ ├── visualizations/ # 可视化图像
81
+ │ └── analysis_report.txt # 统计报告
82
+
83
+ ├── requirements.txt # Python依赖
84
+ └── README.md # 本文档
85
+ ```
86
+
87
+ ---
88
+
89
+ ## 快速开始
90
+
91
+ ### 0. PyPI 安装(发布后)
92
+
93
+ ```bash
94
+ pip install smpas
95
+
96
+ # 一键下载模型(YOLO + SAM)
97
+ smpas-download-models
98
+
99
+ # 启动 Web(默认数据目录:~/.smpas)
100
+ smpas-web
101
+ ```
102
+
103
+ 注意:SAM 需要单独安装(PyPI 不支持直链依赖):
104
+
105
+ ```bash
106
+ pip install git+https://github.com/facebookresearch/segment-anything.git
107
+ ```
108
+
109
+ 模型与数据默认目录:
110
+ - `~/.smpas/models`:放置 `yolo_mushroom.pt` 和 `sam_vit_b.pth`
111
+ - `~/.smpas/data`:放置待分析图片(可选)
112
+
113
+ 也可以用环境变量指定路径:
114
+
115
+ ```bash
116
+ export SMPAS_MODELS_DIR=/path/to/models
117
+ export SMPAS_DATA_DIR=/path/to/data
118
+ smpas-web
119
+ ```
120
+
121
+ 如需修改默认根目录:
122
+
123
+ ```bash
124
+ export SMPAS_HOME=/custom/smpas_home
125
+ smpas-web
126
+ ```
127
+
128
+ ### 1. 环境配置
129
+
130
+ ```bash
131
+ # 创建虚拟环境
132
+ conda create -n mushroom_apriltag python=3.10
133
+ conda activate mushroom_apriltag
134
+
135
+ # 安装PyTorch(CUDA版本)
136
+ pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
137
+
138
+ # 安装其他依赖
139
+ pip install -r requirements.txt
140
+
141
+ # 安装SAM
142
+ pip install git+https://github.com/facebookresearch/segment-anything.git
143
+
144
+ # 安装AprilTag
145
+ pip install apriltag
146
+ ```
147
+
148
+ ### 2. 下载模型文件
149
+
150
+ 推荐使用一键下载命令(会创建 `~/.smpas/models`):
151
+
152
+ ```bash
153
+ smpas-download-models
154
+ ```
155
+
156
+ 如果手动放置模型,请放到 `~/.smpas/models/`:
157
+
158
+ - `yolo_mushroom.pt`
159
+ - `sam_vit_b.pth`
160
+
161
+ ### 3. 准备数据
162
+
163
+ 将包含AprilTag的香菇图片放入 `data/raw_images/` 目录。
164
+
165
+ **数据要求**:
166
+ - 图片中包含一个AprilTag tag16h5 (ID: 25)
167
+ - AprilTag外部黑框边长:37.58mm(精确已知)
168
+ - 尽量垂直俯拍(角度差异<10°最佳)
169
+ - 推荐分辨率:2000×2000像素以上
170
+
171
+ ### 4. 运行分析
172
+
173
+ ```bash
174
+ cd src
175
+
176
+ # 批量处理(默认模式)
177
+ python pipeline.py ../data/raw_images --output-dir ../outputs
178
+
179
+ # 启用并行处理(推荐,多香菇加速)
180
+ python pipeline.py ../data/raw_images \
181
+ --output-dir ../outputs \
182
+ --parallel \
183
+ --max-workers 4
184
+
185
+ # 自定义AprilTag尺寸
186
+ python pipeline.py ../data/raw_images \
187
+ --apriltag-size 37.58 # AprilTag的最大黑色正方形边长(毫米)
188
+ ```
189
+
190
+ ### 5. 查看结果
191
+
192
+ - **CSV结果**:`outputs/analysis_results.csv`
193
+ - **统计报告**:`outputs/analysis_report.txt`
194
+ - **可视化图像**:`outputs/visualizations/`
195
+
196
+ ---
197
+
198
+ ## 输出结果说明
199
+
200
+ ### CSV表格字段
201
+
202
+ | 字段 | 说明 | 单位 |
203
+ |------|------|------|
204
+ | filename | 文件名 | - |
205
+ | apriltag_detected | AprilTag检测成功 | True/False |
206
+ | px_per_mm | 像素/毫米比例 | px/mm |
207
+ | mushroom_id | 香菇编号(同一图多个香菇)| - |
208
+ | detection_confidence | YOLO检测置信度 | 0-1 |
209
+ | diameter_px | 菌盖最大直径(像素)| px |
210
+ | diameter_mm | 菌盖最大直径(毫米)| mm |
211
+ | cap_area_px | 菌盖面积(像素)| px² |
212
+ | cap_area_mm2 | 菌盖面积(毫米)| mm² |
213
+ | crack_area_px | 花纹面积(像素)| px² |
214
+ | crack_ratio | 开裂比 | 0-1 |
215
+ | crack_ratio_percent | 开裂比百分比 | % |
216
+ | grade | 等级评定 | 一级-五级 |
217
+ | v_threshold | V通道Otsu阈值 | 0-255 |
218
+ | s_threshold | S通道Otsu阈值 | 0-255 |
219
+
220
+ ### 等级评定标准
221
+
222
+ | 等级 | 开裂比范围 | 描述 |
223
+ |------|-----------|------|
224
+ | 一级 | 0-20% | 花纹较少 |
225
+ | 二级 | 20-40% | 花纹适中 |
226
+ | 三级 | 40-60% | 花纹较多 |
227
+ | 四级 | 60-80% | 花纹丰富 |
228
+ | 五级 | 80-100% | 花纹非常丰富 |
229
+
230
+ ---
231
+
232
+ ## 高级用法
233
+
234
+ ### 单张图片分析
235
+
236
+ ```python
237
+ from integrated_analyzer import IntegratedMushroomAnalyzer
238
+
239
+ # 创建分析器
240
+ analyzer = IntegratedMushroomAnalyzer(
241
+ yolo_model_path='../models/yolo_mushroom.pt',
242
+ sam_model_path='../models/sam_vit_b.pth',
243
+ apriltag_size_mm=37.58,
244
+ device='cuda',
245
+ enable_parallel=True, # 启用并行处理
246
+ max_workers=4
247
+ )
248
+
249
+ # 分析单张图片
250
+ result = analyzer.analyze_image(
251
+ 'path/to/image.jpg',
252
+ visualize=True
253
+ )
254
+
255
+ # 查看结果
256
+ print(f"检测到 {result['mushroom_count']} 个香菇")
257
+ for mushroom in result['mushrooms']:
258
+ print(f"直径: {mushroom['diameter_mm']:.2f}mm")
259
+ print(f"开裂比: {mushroom['crack_ratio_percent']:.2f}%")
260
+ print(f"等级: {mushroom['grade']}")
261
+ ```
262
+
263
+ ### 测试单个模块
264
+
265
+ ```bash
266
+ # 测试AprilTag检测
267
+ python apriltag_calibration.py ../data/raw_images/test.jpg
268
+
269
+ # 测试直径计算
270
+ python diameter_calculator.py <mask_path>
271
+
272
+ # 测试开裂比分析
273
+ python crack_analyzer.py <image_path> <mask_path>
274
+ ```
275
+
276
+ ---
277
+
278
+ ## 性能优化说明
279
+
280
+ ### ROI处理哲学
281
+
282
+ 本项目采用**分层ROI处理架构**,显著减少计算量:
283
+
284
+ ```
285
+ 原始图像(4000×3000 = 1200万像素)
286
+
287
+ 并行分解:
288
+ ├─ AprilTag检测 → 只处理Tag区域(~100×100 = 1万像素)
289
+ │ ↓
290
+ │ 计算px/mm标定参数
291
+
292
+ └─ YOLO检测香菇 → 快速全图扫描(~50ms)
293
+
294
+ 裁剪3个香菇ROI(每个~500×500 = 25万像素)
295
+
296
+ 并行处理每个ROI:
297
+ ├─ 香菇1:SAM分割 → 直径 → 开裂比
298
+ ├─ 香菇2:SAM分割 → 直径 → 开裂比
299
+ └─ 香菇3:SAM分割 → 直径 → 开裂比
300
+
301
+ 总计算量:1万 + 3×25万 = 76万像素(减少94%)
302
+ ```
303
+
304
+ ### 性能对比
305
+
306
+ | 模式 | 处理方式 | 单图耗时 | 相对速度 |
307
+ |------|---------|---------|---------|
308
+ | 全图暴力 | SAM处理1200万像素 | ~10秒 | 1x |
309
+ | ROI串行 | SAM处理76万像素 | ~3秒 | 3.3x |
310
+ | ROI并行 | 3个ROI同时处理 | ~1秒 | **10x** |
311
+
312
+ ### 并行处理建议
313
+
314
+ - **单个香菇**:无需并行
315
+ - **2-4个香菇**:启用并行,`max_workers=2`
316
+ - **5个以上**:启用并行,`max_workers=4`
317
+
318
+ ---
319
+
320
+ ## 常见问题
321
+
322
+ ### Q1: AprilTag检测失败
323
+
324
+ **原因**:
325
+ - Tag尺寸太小(在图像中<50像素)
326
+ - 光照过暗或过曝
327
+ - 角度太大导致畸变严重
328
+
329
+ **解决**:
330
+ - 提高图像分辨率
331
+ - 打印更大的Tag(推荐10mm+)
332
+ - 垂直俯拍,减小角度
333
+
334
+ ### Q2: 直径测量不准确
335
+
336
+ **原因**:
337
+ - AprilTag标定误差
338
+ - 透视畸变未校正
339
+ - 分割掩码不准确
340
+
341
+ **解决**:
342
+ - 检查Tag是否平整
343
+ - 确保Tag和香菇在同一平面
344
+ - 减小拍摄角度(<10°)
345
+
346
+ ### Q3: 开裂比异常
347
+
348
+ **原因**:
349
+ - 光照不均匀
350
+ - 香菇颜色与通常不同
351
+ - 分割掩码错误
352
+
353
+ **解决**:
354
+ - 使用均匀光照
355
+ - 检查可视化图像的分割质量
356
+ - 调整HSV阈值(高级功能)
357
+
358
+ ---
359
+
360
+ ## 算法原理
361
+
362
+ ### 1. AprilTag标定
363
+
364
+ ```python
365
+ # 检测4个角点
366
+ corners = [左下, 右下, 右上, 左上]
367
+
368
+ # 计算边长
369
+ side_lengths = [底边, 右边, 顶边, 左边]
370
+ avg_side_px = mean(side_lengths)
371
+
372
+ # 计算比例
373
+ px_per_mm = avg_side_px / 3.76
374
+ ```
375
+
376
+ ### 2. 最大直径计算(旋转卡壳)
377
+
378
+ ```python
379
+ # 提取轮廓并计算凸包
380
+ hull = ConvexHull(contour_points)
381
+
382
+ # 暴力搜索凸包上最远点对
383
+ max_dist = 0
384
+ for i in hull_vertices:
385
+ for j in hull_vertices:
386
+ dist = ||point[i] - point[j]||
387
+ max_dist = max(max_dist, dist)
388
+
389
+ diameter_mm = max_dist / px_per_mm
390
+ ```
391
+
392
+ ### 3. 开裂比提取(HSV+Otsu)
393
+
394
+ ```python
395
+ # HSV颜色空间转换
396
+ hsv = cv2.cvtColor(roi_image, cv2.COLOR_BGR2HSV)
397
+
398
+ # Otsu自适应阈值
399
+ v_threshold = otsu(V_channel[cap_mask])
400
+ s_threshold = otsu(S_channel[cap_mask])
401
+
402
+ # 花纹条件:高亮度 + 低饱和度
403
+ crack_mask = (V > v_threshold) AND (S < s_threshold)
404
+
405
+ # 开裂比
406
+ crack_ratio = crack_area / cap_area
407
+ ```
408
+
409
+ ---
410
+
411
+ ## 许可证
412
+
413
+ MIT License - 详见 LICENSE 文件
414
+
415
+ ---
416
+
417
+ ## 致谢
418
+
419
+ - 基于原项目 `shiitake_mushroom` 的开裂比算法
420
+ - 使用 Meta AI 的 Segment Anything Model (SAM)
421
+ - 使用 Ultralytics 的 YOLOv8
422
+ - 使用 AprilTag 视觉标记系统
423
+
424
+ ---
425
+
426
+ ## 更新日志
427
+
428
+ ### v1.0.0 (2025-01-24)
429
+ - ✨ 完全ROI处理架构,减少94%计算量
430
+ - ✨ AprilTag精确标定,支持3.76mm尺寸
431
+ - ✨ 旋转卡壳算法计算真实最大直径
432
+ - ✨ 可选并行处理,3倍速度提升
433
+ - ✨ 完整的批量处理和统计报告