On February 10, the company rushed to work in Shenzhen from home was told to extend the commencement date. Many small partners in Shenzhen were only informed of the notice of another extension when they were close to the commencement date of the company. We have to say that the impact of the epidemic is very big. When I opened the tweet on csdnAPP on the subway home, I saw an article titled "use xxx to detect whether to wear a mask". Suddenly, my idea came to me, and I want to achieve such a function.
In fact, there are many ways to achieve this function through visual software. I am a little lazy here to use the deep learning framework in halcon to achieve this function.
1. Preparing a dataset
Due to limited data sources, I had to look for such pictures from Baidu gallery. Select 40 pictures of "wearing masks" and 40 pictures of "not wearing masks" respectively (for more accurate recognition effect, you can choose more pictures of different people, angles and scenes for training)
Data sets with masks:
Data set without mask:
2. halcon source code:
read_dl_classifier ('pretrained_dl_classifier_compact.hdl', DLClassifierHandle) get_dl_classifier_param (DLClassifierHandle, 'image_width', DlImageWidth) get_dl_classifier_param (DLClassifierHandle, 'image_height', DlImageHeight) get_dl_classifier_param (DLClassifierHandle, 'image_num_channels', DlNumChannels) get_dl_classifier_param (DLClassifierHandle, 'image_range_min', DlRangeMin) get_dl_classifier_param (DLClassifierHandle, 'image_range_max', DlRangeMax) dev_close_window () WindowWidth:=800 WindowHeight:=600 dev_open_window (0, 0, WindowWidth, WindowHeight, 'black', WindowHandle) *Data preprocessing RawDataFolder := 'ImageData/'+['OK','NG'] read_dl_classifier_data_set (RawDataFolder, 'last_folder', RawImageFiles, Labels, LabelIndices, Classes) PreprocessedFolder := 'preprocessedFolder' OverwritePreprocessingFolder := true *RemovePreprocessingAfterExample := true file_exists (PreprocessedFolder, FileExists) if (not FileExists or OverwritePreprocessingFolder) if (FileExists) remove_dir_recursively (PreprocessedFolder) endif make_dir (PreprocessedFolder) for I := 0 to |Classes| - 1 by 1 make_dir (PreprocessedFolder + '/' + Classes[I]) endfor parse_filename (RawImageFiles, BaseNames, Extensions, Directories) ObjectFilesOut := PreprocessedFolder + '/' + Labels + '/' + BaseNames + '.hobj' check_output_file_names_for_duplicates (RawImageFiles, ObjectFilesOut) for I := 0 to |RawImageFiles| - 1 by 1 read_image (Image, RawImageFiles[I]) zoom_image_size (Image, Image, DlImageWidth, DlImageHeight, 'constant') convert_image_type (Image, Image, 'real') RescaleRange:=(DlRangeMax - DlRangeMin)/255.0 scale_image (Image, Image, RescaleRange, DlRangeMin) count_obj (Image, Number) for Index := 1 to Number by 1 select_obj (Image, ObjectSelected, Index) count_channels (ObjectSelected, Channel) if (Channel != DlNumChannels) compose3(ObjectSelected, ObjectSelected, ObjectSelected, ThreeChannelImage) replace_obj (Image, ThreeChannelImage, Image, 1) endif endfor * Write preprocessed image to hobj file. write_object (Image, ObjectFilesOut[I]) endfor dev_clear_window () dev_disp_text ('Image preprocessing stage completed!', 'window', 'top', 'left', 'black', [], []) endif read_dl_classifier_data_set (PreprocessedFolder, 'last_folder', ImageFiles, Labels, LabelsIndices, Classes) TrainingPercent := 70 ValidationPercent := 15 (ValidationImages, ValidationLabels),Test set ( TestImages, TestLabels) split_dl_classifier_data_set (ImageFiles, Labels, TrainingPercent, ValidationPercent, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, TestImages, TestLabels) stop () **Set super parameters** *Set category super parameters set_dl_classifier_param (DLClassifierHandle, 'classes', Classes) BatchSize := 5 set_dl_classifier_param (DLClassifierHandle, 'batch_size', BatchSize) try set_dl_classifier_param (DLClassifierHandle, 'runtime_init', 'immediately') catch (Exception) dev_disp_error_text (Exception) stop () endtry InitialLearningRate := 0.001 set_dl_classifier_param (DLClassifierHandle, 'learning_rate', InitialLearningRate) LearningRateStepEveryNthEpoch := 30 LearningRateStepRatio := 0.1 NumEpochs := 50 **Training classifier** dev_clear_window () * PlotIterationInterval := 20 FileName := 'classifier_minist.hdl' train_fruit_classifier (DLClassifierHandle, FileName, NumEpochs, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, LearningRateStepEveryNthEpoch, LearningRateStepRatio, PlotIterationInterval, WindowHandle) clear_dl_classifier (DLClassifierHandle) read_dl_classifier (FileName, DLClassifierHandle) *Calculation of confusion moment get_error_for_confusion_matrix (ValidationImages, DLClassifierHandle, Top1ClassValidation) gen_confusion_matrix (ValidationLabels, Top1ClassValidation, [], [], WindowHandle, ConfusionMatrix) dev_disp_text ('Validation data', 'window', 'top', 'left', 'gray', 'box', 'false') dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], []) stop () clear_matrix (ConfusionMatrix) dev_clear_window () clear_dl_classifier (DLClassifierHandle) read_dl_classifier (FileName, DLClassifierHandle) set_dl_classifier_param (DLClassifierHandle, 'batch_size', 1) set_dl_classifier_param (DLClassifierHandle, 'runtime_init', 'immediately') dev_resize_window_fit_size (0, 0, WindowWidth, WindowHeight, -1, -1) *Use the trained model for detection set_display_font (WindowHandle, 30, 'mono', 'true', 'false') list_files ('C:/Users/Bin/Desktop/Mask wearing detection based on deep learning/Test', 'files', Files) for Index := 0 to |Files|-1 by 1 read_image (Image, Files[Index]) zoom_image_size (Image, Image, DlImageWidth, DlImageHeight, 'constant') convert_image_type (Image, Image, 'real') RescaleRange:=(DlRangeMax - DlRangeMin)/255.0 scale_image (Image, Image, RescaleRange, DlRangeMin) apply_dl_classifier (Image, DLClassifierHandle, DLClassifierResultHandle) get_dl_classifier_result (DLClassifierResultHandle, 'all', 'predicted_classes', PredictedClass) clear_dl_classifier_result (DLClassifierResultHandle) * dev_display (Image) Text := 'Predicted class: ' + PredictedClass if (PredictedClass == 'OK') disp_message (WindowHandle, Text, 'window', 12, 12, 'green', 'false') else disp_message (WindowHandle, Text, 'window', 12, 12, 'red', 'false') endif stop () endfor clear_dl_classifier (DLClassifierHandle)
3. Test the effect of training:
Here is the appearance of myself and another little friend photographed by the camera provided by the computer, which is used to test the effect of the model