From 83f76e04d2d028caaae164a82ea5c282f5599669 Mon Sep 17 00:00:00 2001 From: YikiDragon <1658959262@qq.com> Date: Mon, 11 Jul 2022 16:29:13 +0800 Subject: [PATCH] add autosegmatation.py --- Readme.md | 72 +++++++++++++----- autosegmatation.py | 28 +++++++ image_utils.py | 16 ++-- saved_plot/AdaptiveBinarization.pdf | Bin 207826 -> 207826 bytes saved_plot/Binarization.pdf | Bin 103252 -> 103252 bytes saved_plot/ContourApproximation.pdf | Bin 1024696 -> 1024696 bytes saved_plot/EffectBeforeAndAfterCorrection.pdf | Bin 420832 -> 420832 bytes saved_plot/FFTSpectrumAnalysisDiagram.pdf | Bin 19892 -> 19892 bytes saved_plot/GrayscaleImageHistogram.pdf | Bin 13516 -> 13516 bytes .../HorizontalAndVerticalClosureOperation.pdf | Bin 15575 -> 15575 bytes saved_plot/RowAndColumnWaveform.pdf | Bin 13854 -> 13854 bytes saved_plot/SegmentationDiagram.pdf | Bin 987054 -> 987054 bytes 12 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 autosegmatation.py diff --git a/Readme.md b/Readme.md index 7d3b256..54ba868 100644 --- a/Readme.md +++ b/Readme.md @@ -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. ## 文件目录解释 下图列出本项目的核心结构: . diff --git a/autosegmatation.py b/autosegmatation.py new file mode 100644 index 0000000..349d74f --- /dev/null +++ b/autosegmatation.py @@ -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]) \ No newline at end of file diff --git a/image_utils.py b/image_utils.py index b1eca83..d635fde 100644 --- a/image_utils.py +++ b/image_utils.py @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/saved_plot/AdaptiveBinarization.pdf b/saved_plot/AdaptiveBinarization.pdf index 9d2aabb68bf4cc64b2b83b8126fd86dd2cb65fa2..6218bb4aa8788deb2b8d3e53e1a4a6922fd516ef 100644 GIT binary patch delta 28 kcmca~o#)bZo`x327N#xCZ#38p4b2QqjJAK$V1B>^0IRMFtN;K2 delta 28 kcmca~o#)bZo`x327N#xCZ#38p42_HpOtyd0V1B>^0IPZmrvLx| diff --git a/saved_plot/Binarization.pdf b/saved_plot/Binarization.pdf index ef1af5f674f8fbbe5e5486aa47538695e5f12939..9ca031db731e38cf06cf78c104953153dfed2c22 100644 GIT binary patch delta 25 hcmcbzjP1%YwuUW?O%vD+4b2Qq3@o;HPGCIE1OSB-2~q$6 delta 25 hcmcbzjP1%YwuUW?O%vD+42_Hpj19MUPGCIE1OSAw2}b|` diff --git a/saved_plot/ContourApproximation.pdf b/saved_plot/ContourApproximation.pdf index 18b6572b95c1885d4ea734474fdd41e5f31b3b3e..0d14507a0cdf075b4e55c28a61966b6664bfadf7 100644 GIT binary patch delta 53 zcmdmS)NaR7yM`9V7N!>F7M2#)7Pc1l7LFFq7OocV7M>Q~7QQX~Pv^568k!lJ7@BN< JGoSwq6956f5@!Ga delta 53 zcmdmS)NaR7yM`9V7N!>F7M2#)7Pc1l7LFFq7OocV7M>Q~7QQX~Pv^567#bNH7@KW> JGoSwq6955}5@Y}X diff --git a/saved_plot/EffectBeforeAndAfterCorrection.pdf b/saved_plot/EffectBeforeAndAfterCorrection.pdf index b318ed81583ddd62fc99a2211730643176177cb6..b6feab80a12391d8d58629da02fe096b8346b510 100644 GIT binary patch delta 34 pcmaEGUGl+o$%Yoj7N!>F7M2#)Eo@O4Y=(wrh9*Ya6EfHum;ve&3w;0p delta 34 pcmaEGUGl+o$%Yoj7N!>F7M2#)Eo@O4YzBr##s((a6EfHum;veE3wQtk diff --git a/saved_plot/FFTSpectrumAnalysisDiagram.pdf b/saved_plot/FFTSpectrumAnalysisDiagram.pdf index 8fd5a3bc506852a09c546467e4a7c1920201994b..8afd5ddbc1f885118584b1fc5009b9caaa8df17b 100644 GIT binary patch delta 22 ecmdlon{mr*#tpYU*bNQM3{8wIHb3&X!~y_j-3XNc delta 22 ecmdlon{mr*#tpYU*bNMgj15f8H$U>Y!~y_jqX>@x diff --git a/saved_plot/GrayscaleImageHistogram.pdf b/saved_plot/GrayscaleImageHistogram.pdf index f05bc726eaca65933d605fa0bece11df25d34828..f6a9eb2aab3c7f6203fe4aa6d68a582bc2419eea 100644 GIT binary patch delta 20 bcmX?;c_wp1wgJ1Lp_!qHf!XFFgD4gNR9^qHvBBmdgD4gNR1OBo diff --git a/saved_plot/HorizontalAndVerticalClosureOperation.pdf b/saved_plot/HorizontalAndVerticalClosureOperation.pdf index 6c58497353b243bc87f5ce0b31299e390ce7beb5..1eeed545bf45a2fc62b06eadab66123b39b0f747 100644 GIT binary patch delta 19 bcmca!dA)MOI}0{LLo-7Yqs?C|9y0>~T%ZVX delta 19 bcmca!dA)MOI}0`gLnC7Ylg(c&9y0>~TyY3& diff --git a/saved_plot/RowAndColumnWaveform.pdf b/saved_plot/RowAndColumnWaveform.pdf index c61560a9c3cfd531ebad5dcf87618e4d191e49f1..be7d80f2dd064b0197120b0bd7d4847dda36bf6b 100644 GIT binary patch delta 20 bcmbQ2GcRYuR6}+{Lo-7YBlFF344YU0QHlos delta 20 bcmbQ2GcRYuR6}+HLnC7Y6SK{844YU0QBVf> diff --git a/saved_plot/SegmentationDiagram.pdf b/saved_plot/SegmentationDiagram.pdf index 4b195c3c746af88d7128ea954ba00a40ab686544..77e1373cedd4d2c9afcb41db513f8f031a7e49bd 100644 GIT binary patch delta 52 zcmZ2C-)7x>n}!y~7N!>F7M2#)7Pc1l7LFFq7OocV7M>Q~7QQ9?JnV*sW`-t4mfP8R I_?I&S0K*Rsn}!y~7N!>F7M2#)7Pc1l7LFFq7OocV7M>Q~7QQ9?JnROBM#csv7Tei* I_?I&S0K(4>-v9sr