Advertisement
Kali_prasad

Face mask detection using viola jones

May 26th, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. clc %clears command line window
  2. clear all; %clears variables
  3. close all; %closes all figures
  4.  
  5. handles.fig=figure;
  6.  
  7. handles.pb1=uicontrol('style','pushbutton','position',[150 10 80 40],'callback',@start_cb,'string','Start');
  8. handles.pb2=uicontrol('style','pushbutton','position',[250 10 80 40],'callback',@stop_cb,'string','Stop');
  9. handles.pb3=uicontrol('style','pushbutton','position',[350 10 80 40],'callback',@pause_cb,'string','pause');
  10.  
  11. handles.run=1;
  12. guidata(handles.fig,handles)
  13.  
  14. function speak(string)
  15. title(string);
  16. NET.addAssembly('System.Speech');
  17. obj = System.Speech.Synthesis.SpeechSynthesizer;
  18. obj.Volume = 100;
  19. % Speak(obj, string);
  20. end
  21.  
  22. function start_cb(hObj,~)
  23. cam=webcam('Integrated Webcam');
  24. while true
  25. if ~isvalid(hObj); break; end
  26. handles=guidata(hObj);
  27. if handles.run==1
  28. disp('running')
  29. e=cam.snapshot;
  30. FDetect=vision.CascadeObjectDetector('Mouth','MergeThreshold',100);
  31. I=e;
  32. BB_Mouth=step(FDetect,I);
  33. imshow(I);
  34. hold on;
  35. %%
  36. FDetect=vision.CascadeObjectDetector('Nose','MergeThreshold',16);
  37. I=e;
  38. BB_Nose=step(FDetect,I);
  39. imshow(I);
  40. hold on;
  41. %%
  42. FDetect=vision.CascadeObjectDetector('EyePairBig','MergeThreshold',1);
  43. I=e;
  44. BB_bEyes=step(FDetect,I);
  45. imshow(I);
  46. hold on;
  47. FDetect=vision.CascadeObjectDetector('EyePairSmall','MergeThreshold',1);
  48. I=e;
  49. BB_sEyes=step(FDetect,I);
  50. imshow(I);
  51. if(sum(sum(BB_bEyes))==0&&sum(sum(BB_sEyes))==0)
  52. speak('Face not detected');
  53. elseif(sum(sum(BB_Nose))==0 && sum(sum(BB_Mouth))==0)
  54. FDetect=vision.CascadeObjectDetector('FrontalFaceLBP','MergeThreshold',10);
  55. BB_hand=step(FDetect,I);
  56. if(sum(sum(BB_hand))~=0)
  57. speak('Remove Hand from face.');
  58. else
  59. speak('Thank You for wearing Mask.');
  60. end
  61. elseif((sum(sum(BB_Nose))~=0 && sum(sum(BB_Mouth))==0)||(sum(sum(BB_Nose))==0 && sum(sum(BB_Mouth))~=0))
  62. for i=1:size(BB_Nose,1)
  63. rectangle('Position',BB_Nose(i,:),'Linewidth',5,'LineStyle','-','EdgeColor','r');
  64. end
  65. for i=1:size(BB_Mouth,1)
  66. rectangle('Position',BB_Mouth(i,:),'Linewidth',5,'LineStyle','-','EdgeColor','r');
  67. end
  68. speak( 'Please wear mask properly.');
  69. else
  70. for i=1:size(BB_Nose,1)
  71. rectangle('Position',BB_Nose(i,:),'Linewidth',5,'LineStyle','-','EdgeColor','r');
  72. end
  73. for i=1:size(BB_Mouth,1)
  74. rectangle('Position',BB_Mouth(i,:),'Linewidth',5,'LineStyle','-','EdgeColor','r');
  75. end
  76. speak( 'Please wear Mask.')
  77. end
  78. pause(1)
  79. else
  80. handles.run=1;
  81. guidata(hObj,handles)
  82. disp('paused')
  83. break
  84. end
  85. end
  86. end
  87.  
  88. function stop_cb(hObj,~)
  89. handles.run=0;
  90. guidata(hObj,handles)
  91. close all;
  92. clc;
  93. clear all;
  94. end
  95.  
  96. function pause_cb(hObj,~)
  97. handles.run=0;
  98. guidata(hObj,handles)
  99. title('Paused');
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement