基于halcon深度学习之公共场合检测是否戴口罩

2月10号从家赶往深圳的公司上班,被告知延长开工日期,很多在深圳的小伙伴也都是临近公司开工日期才得知又要延长的通知,不得不说这次疫情带来的影响非常大。回家的地铁上打开csdnAPP上的推文看到一标题为“用xxx实现检测是否戴口罩”的文章,顿时想法油然而生,我也要实现这么一功能。

其实通过视觉软件实现这一功能的方法有很多种。我这里稍微懒一点就使用halcon里面的深度学习框架来实现这一功能。

1、准备数据集

由于数据来源有限,我只好从百度图库里面找这么些图片。分别选好40张“戴了口罩”的图片和40张“没有戴口罩”的图片(想要识别效果更加精准可以选用更多不同人物、角度以及场景的图片进行训练)

戴了口罩的数据集:

没戴口罩的数据集:

2、halcon源代码(这里的代码从我一个demo里面拷过来的没做过多修改)


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)
*数据预处理
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 ('图片预处理阶段完成!', 'window', 'top', 'left', 'black', [], [])
endif

read_dl_classifier_data_set (PreprocessedFolder, 'last_folder', ImageFiles, Labels, LabelsIndices, Classes) 

TrainingPercent := 70 
ValidationPercent := 15 (ValidationImages, ValidationLabels)、测试集(TestImages, TestLabels)
split_dl_classifier_data_set (ImageFiles, Labels, TrainingPercent, ValidationPercent, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, TestImages, TestLabels) 
stop ()
**设置超参数**
*设置类别超参数
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 

**训练分类器**
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)
*计算混淆矩
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)

*使用训练好的模型进行检测
set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
list_files ('C:/Users/斌/Desktop/基于深度学习的戴口罩检测/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、测试训练的效果:

这里用的是电脑自带的摄像头进行拍摄的本人和另一个小伙伴的外貌,用来测试模型效果

 

 

 

  • 12
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值