Skip to content

Commit

Permalink
add autosegmatation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
YikiDragon committed Jul 11, 2022
1 parent dca03fe commit 83f76e0
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 25 deletions.
72 changes: 55 additions & 17 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,81 @@
## 获取代码

可以以下通过git指令获取代码:
`git clone https://github.com/YikiDragon/SolarPanelDefectDetect.git`
```
git clone https://github.com/YikiDragon/SolarPanelDefectDetect.git
```

## 环境依赖

可以通过输入以下指令以满足最基本配置:
`conda install tensorflow==2.3.0 opencv==4.5.1 numpy matplotlib alive_progress cmd`
```
conda install tensorflow==2.3.0 opencv==4.5.1 numpy matplotlib alive_progress cmd
```
或者在获取代码后在代码根目录输入:
`conda env create -f require.yaml`
```
conda env create -f require.yaml
```

## 目录配置
在获取代码后,在根目录下新建文件夹`photos`用于存放待校正分割的光伏电池板原图。
## 快速使用
1. 在项目根目录下运行以下指令进入命令交互界面
`python main.py`
在获取代码后,在根目录下新建文件夹`photos`
```
mkdir photos
```
`photos`文件夹中存放待校正分割的光伏电池板原图。
>光伏电池板原图须为".jpg"或".JPG"
## 快速使用交互式命令行检测
1. 在项目根目录下运行以下指令启动交互式命令行
```
python main.py
```
2. 查看可用文件夹列表
`show folder`
```
show folder
```
3. 选择`photos`文件夹
`set folder photos``set folder <文件夹对应编号>`
```
set folder photos
```
`set folder <文件夹对应编号>`
4. 查看可用图片文件
`show image`
```
show image
```
5. 选择***.jpg图片
`set image ***.jpg``set image <图片对应编号>`
```
set image ***.jpg
```
`set image <图片对应编号>`
6. 查看可用缺陷识别模型
`show model`
```
show model
```
7. 选择非线性SVM模型
`set model SVM``set model <模型对应编号>`
```
set model SVM
```
`set model <模型对应编号>`
8. 开始检测
`detect`
```
detect
```
## 指令说明
有以下指令可用:
有以下指令可用:
`show`: 显示可用选项
`set`: 设置指定选项
`detect`: 开始检测
`help`: 帮助
`about`: 作者信息
`exit`: 退出交互式命令行
可以使用`help <指令>`获取指令的相应用法
`exit`: 退出交互式命令行
>可以使用`help <指令>`获取指令的相应用法
## 模型训练
1. 新建数据集文件夹`dataset`
```
mkdir dataset
cd dataset
```

2.
## 文件目录解释
下图列出本项目的核心结构:
.
Expand Down
28 changes: 28 additions & 0 deletions autosegmatation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from image_utils import correct, segment
import cv2
import pathlib

if __name__ == '__main__':
all = False
save_dir = "./dataset/all/"
data_root = pathlib.Path('./photos')
all_image_names = sorted(
item.name for item in data_root.glob('*.JPG')) # 获取文件名
if not all:
image = all_image_names[0]
print("image name: " + image)
img_src = cv2.imread("photos/" + image)
image_corrected = correct(img_src, debug=False)
seg = segment(image_corrected, seg_method=4, debug=False)
for seg_ele in seg:
savename = image[0:-4]+"_"+str(seg_ele[2][0])+"row"+str(seg_ele[2][1])+"col.jpg"
cv2.imwrite(save_dir+savename, seg_ele[1])
elif all:
for image in all_image_names:
print("image name: " + image)
img_src = cv2.imread("photos/" + image)
image_corrected = correct(img_src, debug=False)
seg = segment(image_corrected, seg_method=4, debug=False)
for seg_ele in seg:
savename = image[0:-4] + "_" + str(seg_ele[2][0]) + "row" + str(seg_ele[2][1]) + "col.jpg"
cv2.imwrite(save_dir + savename, seg_ele[1])
16 changes: 8 additions & 8 deletions image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def segment(image_corrected: np.ndarray, trough_th=200, seg_method=0, debug=Fals
:param trough_th: 波谷判别阈值,用于确定分割位置(弃用)
:param seg_method: 分割方法,0-纯波谷分割,1-平均分割, 2-均值间隔探测波谷, 3-均值间隔+波谷, 4-FFT频谱分析
:param debug: 调试开关
:return: segmentations 列表,每个元素格式为[[起始点坐标,终止点坐标], 分割图]
:return: segmentations 列表,每个元素格式为[[起始点坐标,终止点坐标], 分割图, 晶片行列编号]
'''
img = np.array([])
# 缩小到1/5
Expand Down Expand Up @@ -219,7 +219,7 @@ def trough_clustering(trough, th=10):
img_segment = image_corrected[trough_row[i]:trough_row[i + 1], trough_col[j]:trough_col[j + 1], :]
segmentations.append(
[np.array([[trough_col[j], trough_row[i]], [trough_col[j + 1], trough_row[i + 1]]], np.int32),
img_segment])
img_segment, np.array([i+1, j+1])])
if debug:
plt.title("Debug demo")
plt.imshow(copy)
Expand Down Expand Up @@ -271,7 +271,7 @@ def trough_clustering(trough, th=10):
(0, 255, 0), 10)
img_segment = image_corrected[start_height:end_height, start_width:end_width, :]
segmentations.append(
[np.array([[start_width, start_height], [end_width, end_height]], np.int32), img_segment])
[np.array([[start_width, start_height], [end_width, end_height]], np.int32), img_segment, np.array([i+1, j+1])])
if debug:
plt.title("Debug demo")
plt.imshow(copy)
Expand Down Expand Up @@ -331,7 +331,7 @@ def trough_clustering(trough, th=10):
img_segment = image_corrected[trough_row[i]:trough_row[i + 1], trough_col[j]:trough_col[j + 1], :]
segmentations.append(
[np.array([[trough_col[j], trough_row[i]], [trough_col[j + 1], trough_row[i + 1]]], np.int32),
img_segment])
img_segment, np.array([i+1, j+1])])
if debug:
plt.title("Debug demo")
plt.imshow(copy)
Expand Down Expand Up @@ -417,7 +417,7 @@ def trough_clustering(trough, th=10):
img_segment = image_corrected[trough_row[i]:trough_row[i + 1], trough_col[j]:trough_col[j + 1], :]
segmentations.append(
[np.array([[trough_col[j], trough_row[i]], [trough_col[j + 1], trough_row[i + 1]]], np.int32),
img_segment])
img_segment, np.array([i+1, j+1])])
if debug:
plt.title("Debug demo")
plt.imshow(copy)
Expand Down Expand Up @@ -484,7 +484,7 @@ def fft_to_rc(wave, r=100):
img_segment = image_corrected[trough_row[i]:trough_row[i + 1], trough_col[j]:trough_col[j + 1], :]
segmentations.append(
[np.array([[trough_col[j], trough_row[i]], [trough_col[j + 1], trough_row[i + 1]]], np.int32),
img_segment])
img_segment, np.array([i+1, j+1])])
if debug:
plt.title("Segmentation diagram")
plt.imshow(copy)
Expand All @@ -507,8 +507,8 @@ def fft_to_rc(wave, r=100):
image = '41191510617617.JPG'
print("image name: "+image)
img_src = cv2.imread("photos/" + image)
image_corrected = correct(img_src, debug=True)
seg = segment(image_corrected, seg_method=4, debug=True)
image_corrected = correct(img_src, debug=False)
seg = segment(image_corrected, seg_method=4, debug=False)
copy = image_corrected.copy()
elif all:
i = 1
Expand Down
Binary file modified saved_plot/AdaptiveBinarization.pdf
Binary file not shown.
Binary file modified saved_plot/Binarization.pdf
Binary file not shown.
Binary file modified saved_plot/ContourApproximation.pdf
Binary file not shown.
Binary file modified saved_plot/EffectBeforeAndAfterCorrection.pdf
Binary file not shown.
Binary file modified saved_plot/FFTSpectrumAnalysisDiagram.pdf
Binary file not shown.
Binary file modified saved_plot/GrayscaleImageHistogram.pdf
Binary file not shown.
Binary file modified saved_plot/HorizontalAndVerticalClosureOperation.pdf
Binary file not shown.
Binary file modified saved_plot/RowAndColumnWaveform.pdf
Binary file not shown.
Binary file modified saved_plot/SegmentationDiagram.pdf
Binary file not shown.

0 comments on commit 83f76e0

Please sign in to comment.