{"id":1986,"date":"2024-09-18T14:34:53","date_gmt":"2024-09-18T06:34:53","guid":{"rendered":"https:\/\/www.gnn.club\/?p=1986"},"modified":"2024-09-18T14:39:10","modified_gmt":"2024-09-18T06:39:10","slug":"%e5%9b%b0%e5%80%a6%e6%a3%80%e6%b5%8b%e6%8a%a5%e8%ad%a6%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"http:\/\/gnn.club\/?p=1986","title":{"rendered":"\u56f0\u5026\u68c0\u6d4b\u62a5\u8b66\u9879\u76ee"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u56f0\u5026\u68c0\u6d4b\u7cfb\u7edf<\/h2>\n\n\n\n<p>\u5728\u6b64 Python \u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 OpenCV \u4ece\u7f51\u7edc\u6444\u50cf\u5934\u6536\u96c6\u56fe\u50cf\u5e76\u5c06\u5176\u8f93\u5165\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u8be5\u6a21\u578b\u5c06\u5206\u7c7b\u4eba\u7684\u773c\u775b\u662f\u201c\u7741\u5f00\u201d\u8fd8\u662f\u201c\u95ed\u7740\u201d\uff0c\u6765\u51b3\u5b9a\u662f\u5426\u56f0\u5026\uff0c\u5982\u679c\u5224\u65ad\u56f0\u5026\u5219\u8c03\u7528\u8b66\u94c3\u8fdb\u884c\u62a5\u8b66\u884c\u4e3a\u3002\u6211\u4eec\u5c06\u5728\u8fd9\u4e2a Python \u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 1 \u2013<\/strong>\u5c06\u56fe\u50cf\u4f5c\u4e3a\u6765\u81ea\u76f8\u673a\u7684\u8f93\u5165\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 2 \u2013<\/strong>\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u4eba\u8138\u5e76\u521b\u5efa\u611f\u5174\u8da3\u533a\u57df (ROI)\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 3 \u2013<\/strong>\u4ece ROI \u4e2d\u68c0\u6d4b\u773c\u775b\u5e76\u5c06\u5176\u8f93\u5165\u5206\u7c7b\u5668\u3002<\/p>\n\n\n\n<p><strong>\u7b2c 4 \u6b65 \u2013<\/strong>\u5206\u7c7b\u5668\u5c06\u5bf9\u773c\u775b\u662f\u7741\u7740\u8fd8\u662f\u95ed\u7740\u8fdb\u884c\u5206\u7c7b\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 5 \u2013<\/strong>\u8ba1\u7b97\u5206\u6570\u4ee5\u68c0\u67e5\u8be5\u4eba\u662f\u5426\u56f0\u5026\u3002<\/p>\n\n\n\n<p>\u9879\u76ee\u6548\u679c\u7684\u5c55\u793a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143052886.gif\" alt=\"\" class=\"wp-image-1987\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u56f0\u5026\u68c0\u6d4b\u6570\u636e\u96c6<\/h2>\n\n\n\n<p>\u8be5\u6570\u636e\u5305\u542b\u7ea6 7000 \u5f20\u4e0d\u540c\u5149\u7167\u6761\u4ef6\u4e0b\u4eba\u773c\u7684\u56fe\u50cf\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u6a21\u578b\u6765\u5bf9\u4eba\u7684\u773c\u775b\u662f\u7741\u7740\u8fd8\u662f\u95ed\u7740\u8fdb\u884c\u5206\u7c7b\u3002<\/p>\n\n\n\n<p>\u4e0b\u8f7d\u6570\u636e\u96c6\uff1a<a href=\"https:\/\/www.kaggle.com\/datasets\/serenaraju\/yawn-eye-dataset-new\">https:\/\/www.kaggle.com\/datasets\/serenaraju\/yawn-eye-dataset-new<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u578b\u67b6\u6784<\/h3>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528\u7684\u6a21\u578b\u662f\u4f7f\u7528<strong>\u5377\u79ef\u795e\u7ecf\u7f51\u7edc (CNN)<\/strong>\u901a\u8fc7 Keras \u6784\u5efa\u7684\u3002\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\uff0c\u5728\u56fe\u50cf\u5206\u7c7b\u65b9\u9762\u8868\u73b0\u975e\u5e38\u51fa\u8272\u3002<\/p>\n\n\n\n<p>CNN \u6a21\u578b\u67b6\u6784\u7531\u4ee5\u4e0b\u5c42\u7ec4\u6210\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5377\u79ef\u5c42\uff1b32 \u4e2a\u8282\u70b9\uff0c\u5377\u79ef\u6838\u5927\u5c0f 3<\/li>\n\n\n\n<li>\u5377\u79ef\u5c42\uff1b32 \u4e2a\u8282\u70b9\uff0c\u5377\u79ef\u6838\u5927\u5c0f 3<\/li>\n\n\n\n<li>\u5377\u79ef\u5c42\uff1b64 \u4e2a\u8282\u70b9\uff0c\u5377\u79ef\u6838\u5927\u5c0f 3<\/li>\n\n\n\n<li>\u5168\u8fde\u63a5\u5c42\uff1b128\u4e2a\u8282\u70b9<\/li>\n<\/ul>\n\n\n\n<p>\u6700\u540e\u4e00\u5c42\u4e5f\u662f\u4e00\u4e2a\u6709 2 \u4e2a\u8282\u70b9\u7684\u5168\u8fde\u63a5\u5c42\u3002\u9664\u4e86\u6211\u4eec\u4f7f\u7528 Softmax \u7684\u8f93\u51fa\u5c42\u4e4b\u5916\uff0c\u6240\u6709\u5c42\u90fd\u4f7f\u7528 Relu \u6fc0\u6d3b\u51fd\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9879\u76ee\u5148\u51b3\u6761\u4ef6<\/h3>\n\n\n\n<p>\u8fd9\u4e2a Python \u9879\u76ee\u7684\u8981\u6c42\u662f\u4e00\u4e2a\u7f51\u7edc\u6444\u50cf\u5934\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u5b83\u6355\u83b7\u56fe\u50cf\u3002\u60a8\u9700\u8981\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5Python\uff08\u63a8\u83503.6\u7248\u672c\uff09\uff0c\u7136\u540e\u4f7f\u7528pip\uff0c\u60a8\u53ef\u4ee5\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>OpenCV \u2013<\/strong>&nbsp;pip install opencv-python\uff08\u9762\u90e8\u548c\u773c\u775b\u68c0\u6d4b\uff09\u3002<\/li>\n\n\n\n<li><strong>TensorFlow \u2013<\/strong>&nbsp;pip install tensorflow\uff08keras \u4f7f\u7528 TensorFlow \u4f5c\u4e3a\u540e\u7aef\uff09\u3002<\/li>\n\n\n\n<li><strong>Keras \u2013<\/strong>&nbsp;pip install keras\uff08\u6784\u5efa\u6211\u4eec\u7684\u5206\u7c7b\u6a21\u578b\uff09\u3002<\/li>\n\n\n\n<li><strong>Pygame \u2013<\/strong>&nbsp;pip install pygame\uff08\u64ad\u653e\u95f9\u949f\u58f0\u97f3\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u6267\u884c\u56f0\u5026\u68c0\u6d4b\u7684\u6b65\u9aa4<\/h3>\n\n\n\n<p>\u9879\u76ee\u538b\u7f29\u5305\u4e2d\u7684zip \u7684\u5185\u5bb9\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u201chaar Cascade files\u201d\u6587\u4ef6\u5939\u5305\u542b\u4ece\u56fe\u50cf\u4e2d\u68c0\u6d4b\u5bf9\u8c61\u6240\u9700\u7684 xml \u6587\u4ef6\u3002\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6b63\u5728\u68c0\u6d4b\u4eba\u7684\u9762\u90e8\u548c\u773c\u775b\u3002<\/li>\n\n\n\n<li>models \u6587\u4ef6\u5939\u5305\u542b\u6211\u4eec\u7684\u6a21\u578b\u6587\u4ef6\u201ccnnCat2.h5\u201d\uff0c\u8be5\u6587\u4ef6\u662f\u5728\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u4e0a\u8fdb\u884c\u8bad\u7ec3\u7684\u3002<\/li>\n\n\n\n<li>\u6211\u4eec\u6709\u4e00\u4e2a\u97f3\u9891\u526a\u8f91\u201calarm.wav\u201d\uff0c\u5f53\u4eba\u4eec\u611f\u5230\u660f\u660f\u6b32\u7761\u65f6\u4f1a\u64ad\u653e\u8be5\u97f3\u9891\u526a\u8f91\u3002<\/li>\n\n\n\n<li>\u201cModel.py\u201d\u6587\u4ef6\u5305\u542b\u6211\u4eec\u901a\u8fc7\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u8bad\u7ec3\u6765\u6784\u5efa\u5206\u7c7b\u6a21\u578b\u7684\u7a0b\u5e8f\u3002\u60a8\u53ef\u4ee5\u5728\u8be5\u6587\u4ef6\u4e2d\u770b\u5230\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u7684\u5b9e\u73b0\u3002<\/li>\n\n\n\n<li>\u201c\u7761\u610f\u68c0\u6d4b.py\u201d\u662f\u6211\u4eec\u9879\u76ee\u7684\u4e3b\u6587\u4ef6\u3002\u8981\u5f00\u59cb\u68c0\u6d4b\u8fc7\u7a0b\uff0c\u6211\u4eec\u5fc5\u987b\u8fd0\u884c\u8be5\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u9010\u6b65\u4e86\u89e3\u6211\u4eec\u7684\u7b97\u6cd5\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 1 \u2013 \u5c06\u56fe\u50cf\u4f5c\u4e3a\u6765\u81ea\u76f8\u673a\u7684\u8f93\u5165<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528\u7f51\u7edc\u6444\u50cf\u5934\uff0c\u6211\u4eec\u5c06\u56fe\u50cf\u4f5c\u4e3a\u8f93\u5165\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u8bbf\u95ee\u7f51\u7edc\u6444\u50cf\u5934\uff0c\u6211\u4eec\u5236\u4f5c\u4e86\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u6765\u6355\u83b7\u6bcf\u4e00\u5e27\u3002\u6211\u4eec\u4f7f\u7528OpenCV\u63d0\u4f9b\u7684\u65b9\u6cd5<strong>cv2.VideoCapture(0)<\/strong>\u6765\u8bbf\u95ee\u76f8\u673a\u5e76\u8bbe\u7f6e\u6355\u83b7\u5bf9\u8c61(cap)\u3002<strong>cap.read()<\/strong>\u5c06\u8bfb\u53d6\u6bcf\u4e00\u5e27\uff0c\u5e76\u5c06\u56fe\u50cf\u5b58\u50a8\u5728\u5e27\u53d8\u91cf\u4e2d\u3002<\/p>\n\n\n\n<p><strong>\u7b2c 2 \u6b65 \u2013 \u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u4eba\u8138\u5e76\u521b\u5efa\u611f\u5174\u8da3\u533a\u57df (ROI)<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4e86\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u4eba\u8138\uff0c\u6211\u4eec\u9700\u8981\u9996\u5148\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff0c\u56e0\u4e3a\u7528\u4e8e\u5bf9\u8c61\u68c0\u6d4b\u7684 OpenCV \u7b97\u6cd5\u5728\u8f93\u5165\u4e2d\u91c7\u7528\u7070\u5ea6\u56fe\u50cf\u3002\u6211\u4eec\u4e0d\u9700\u8981\u989c\u8272\u4fe1\u606f\u6765\u68c0\u6d4b\u5bf9\u8c61\u3002\u6211\u4eec\u5c06\u4f7f\u7528 haar \u7ea7\u8054\u5206\u7c7b\u5668\u6765\u68c0\u6d4b\u4eba\u8138\u3002<\/p>\n\n\n\n<p><strong>face = cv2.CascadeClassifier('path to our haar Cascade xml file')<\/strong>\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u4f7f\u7528 <strong>faces =face.detectMultiScale(gray)<\/strong> \u6267\u884c\u68c0\u6d4b\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u68c0\u6d4b\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b x\u3001y \u5750\u6807\u548c\u9ad8\u5ea6\uff08\u5bf9\u8c61\u8fb9\u754c\u6846\u7684\u5bbd\u5ea6\uff09\u3002\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u8fed\u4ee3\u8fd9\u4e9b\u9762\u5e76\u4e3a\u6bcf\u4e2a\u9762\u7ed8\u5236\u8fb9\u754c\u6846\u3002<\/p>\n\n\n\n<p><strong>\u7b2c 3 \u6b65 \u2013 \u4ece ROI \u4e2d\u68c0\u6d4b\u773c\u775b\u5e76\u5c06\u5176\u8f93\u5165\u5206\u7c7b\u5668<\/strong><\/p>\n\n\n\n<p>\u68c0\u6d4b\u773c\u775b\u7684\u8fc7\u7a0b\u4e0e\u68c0\u6d4b\u9762\u90e8\u7684\u8fc7\u7a0b\u76f8\u540c\u3002\u9996\u5148\uff0c\u6211\u4eec\u5206\u522b\u4e3a<strong>leye<\/strong>\u548c<strong>reye<\/strong>\u4e2d\u7684\u773c\u775b\u8bbe\u7f6e\u7ea7\u8054\u5206\u7c7b\u5668\uff0c\u7136\u540e\u4f7f\u7528<strong>left_eye = leye.detectMultiScale(gray)<\/strong>\u68c0\u6d4b\u773c\u775b\u3002\u73b0\u5728\u6211\u4eec\u53ea\u9700\u8981\u4ece\u5b8c\u6574\u56fe\u50cf\u4e2d\u63d0\u53d6\u773c\u775b\u6570\u636e\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u63d0\u53d6\u773c\u775b\u7684\u8fb9\u754c\u6846\u6765\u5b9e\u73b0\uff0c\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u4ee3\u7801\u4ece\u5e27\u4e2d\u63d0\u53d6\u773c\u775b\u56fe\u50cf\u3002<\/p>\n\n\n\n<p><strong>l_eye<\/strong>\u4ec5\u5305\u542b\u773c\u775b\u7684\u56fe\u50cf\u6570\u636e\u3002\u8fd9\u5c06\u88ab\u8f93\u5165\u5230\u6211\u4eec\u7684 CNN \u5206\u7c7b\u5668\u4e2d\uff0c\u8be5\u5206\u7c7b\u5668\u5c06\u9884\u6d4b\u773c\u775b\u662f\u7741\u7740\u8fd8\u662f\u95ed\u7740\u3002\u540c\u6837\uff0c\u6211\u4eec\u5c06\u628a\u53f3\u773c\u63d0\u53d6\u5230<strong>r_eye<\/strong>\u4e2d\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 4 \u2013 \u5206\u7c7b\u5668\u5c06\u5bf9\u773c\u775b\u662f\u7741\u7740\u8fd8\u662f\u95ed\u7740\u8fdb\u884c\u5206\u7c7b<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528\u5377\u79ef\u6a21\u578b\u5206\u7c7b\u5668\u6765\u9884\u6d4b\u773c\u775b\u72b6\u6001\u3002\u4e3a\u4e86\u5c06\u56fe\u50cf\u8f93\u5165\u6a21\u578b\uff0c\u6211\u4eec\u9700\u8981\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\uff0c\u56e0\u4e3a\u6a21\u578b\u9700\u8981\u6b63\u786e\u7684\u5c3a\u5bf8\u6765\u5f00\u59cb\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528<strong>r_eye = cv2.cvtColor(r_eye, cv2.COLOR_BGR2GRAY)<\/strong>\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5c06\u56fe\u50cf\u5927\u5c0f\u8c03\u6574\u4e3a 24*24 \u50cf\u7d20\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u6a21\u578b\u662f\u5728 24*24 \u50cf\u7d20\u56fe\u50cf<strong>cv2.resize(r_eye, (24,24))<\/strong>\u4e0a\u8fdb\u884c\u8bad\u7ec3\u7684\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5bf9\u6570\u636e\u8fdb\u884c\u5f52\u4e00\u5316\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6536\u655b\u6027<strong>r_eye = r_eye\/255&nbsp;<\/strong>\uff08\u6240\u6709\u503c\u90fd\u5728 0-1 \u4e4b\u95f4\uff09\u3002\u6269\u5c55\u7ef4\u5ea6\u4ee5\u8f93\u5165\u5230\u6211\u4eec\u7684\u5206\u7c7b\u5668\u4e2d\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<strong>model = load_model('models\/cnnCat2.h5')<\/strong>\u52a0\u8f7d\u6a21\u578b\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u4f7f\u7528<strong>lpred = model.predict_classes(l_eye)<\/strong>\u6765\u9884\u6d4b\u6bcf\u53ea\u773c\u775b\u3002\u5982\u679clpred[0]\u7684\u503c= 1\uff0c\u5219\u8868\u793a\u773c\u775b\u662f\u7741\u5f00\u7684\uff0c\u5982\u679clpred[0]\u7684\u503c= 0\uff0c\u5219\u8868\u793a\u773c\u775b\u662f\u95ed\u7740\u7684\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 5 \u2013 \u8ba1\u7b97\u5206\u6570\u4ee5\u68c0\u67e5\u4eba\u662f\u5426\u660f\u660f\u6b32\u7761<\/strong><\/p>\n\n\n\n<p>\u8be5\u5206\u6570\u57fa\u672c\u4e0a\u662f\u6211\u4eec\u7528\u6765\u786e\u5b9a\u8be5\u4eba\u95ed\u4e0a\u773c\u775b\u7684\u65f6\u95f4\u7684\u503c\u3002\u56e0\u6b64\uff0c\u5982\u679c\u53cc\u773c\u95ed\u4e0a\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u589e\u52a0\u5206\u6570\uff0c\u800c\u5f53\u773c\u775b\u7741\u5f00\u65f6\uff0c\u6211\u4eec\u5c06\u51cf\u5c11\u5206\u6570\u3002\u6211\u4eec\u4f7f\u7528 cv2.putText() \u51fd\u6570\u5728\u5c4f\u5e55\u4e0a\u7ed8\u5236\u7ed3\u679c\uff0c\u8be5\u51fd\u6570\u5c06\u663e\u793a\u4eba\u5458\u7684\u5b9e\u65f6\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u5b8c\u6574\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import cv2\nimport os\nfrom keras.models import load_model\nimport numpy as np\nfrom pygame import mixer\nimport time\n\n\nmixer.init()\nsound = mixer.Sound('alarm.wav')\n\nface = cv2.CascadeClassifier('haar cascade files\\haarcascade_frontalface_alt.xml')\nleye = cv2.CascadeClassifier('haar cascade files\\haarcascade_lefteye_2splits.xml')\nreye = cv2.CascadeClassifier('haar cascade files\\haarcascade_righteye_2splits.xml')\n\n\n\nlbl=&#91;'Close','Open']\n\nmodel = load_model('models\/cnncat2.h5')\npath = os.getcwd()\ncap = cv2.VideoCapture(0)\nfont = cv2.FONT_HERSHEY_COMPLEX_SMALL\ncount=0\nscore=0\nthicc=2\nrpred=&#91;99]\nlpred=&#91;99]\n\nwhile(True):\n    ret, frame = cap.read()\n    height,width = frame.shape&#91;:2] \n\n    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n    \n    faces = face.detectMultiScale(gray,minNeighbors=5,scaleFactor=1.1,minSize=(25,25))\n    left_eye = leye.detectMultiScale(gray)\n    right_eye =  reye.detectMultiScale(gray)\n\n    cv2.rectangle(frame, (0,height-50) , (200,height) , (0,0,0) , thickness=cv2.FILLED )\n\n    for (x,y,w,h) in faces:\n        cv2.rectangle(frame, (x,y) , (x+w,y+h) , (100,100,100) , 1 )\n\n    for (x,y,w,h) in right_eye:\n        r_eye=frame&#91;y:y+h,x:x+w]\n        count=count+1\n        r_eye = cv2.cvtColor(r_eye,cv2.COLOR_BGR2GRAY)\n        r_eye = cv2.resize(r_eye,(24,24))\n        r_eye= r_eye\/255\n        r_eye=  r_eye.reshape(24,24,-1)\n        r_eye = np.expand_dims(r_eye,axis=0)\n        rpred = np.argmax(model.predict(r_eye), axis=-1)\n        if(rpred&#91;0]==1):\n            lbl='Open' \n        if(rpred&#91;0]==0):\n            lbl='Closed'\n        break\n\n    for (x,y,w,h) in left_eye:\n        l_eye=frame&#91;y:y+h,x:x+w]\n        count=count+1\n        l_eye = cv2.cvtColor(l_eye,cv2.COLOR_BGR2GRAY)  \n        l_eye = cv2.resize(l_eye,(24,24))\n        l_eye= l_eye\/255\n        l_eye=l_eye.reshape(24,24,-1)\n        l_eye = np.expand_dims(l_eye,axis=0)\n        lpred = np.argmax(model.predict(l_eye), axis=-1)\n        if(lpred&#91;0]==1):\n            lbl='Open'   \n        if(lpred&#91;0]==0):\n            lbl='Closed'\n        break\n\n    if(rpred&#91;0]==0 and lpred&#91;0]==0):\n        score=score+1\n        cv2.putText(frame,\"Closed\",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)\n    # if(rpred&#91;0]==1 or lpred&#91;0]==1):\n    else:\n        score=score-1\n        cv2.putText(frame,\"Open\",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)\n    \n        \n    if(score&lt;0):\n        score=0   \n    cv2.putText(frame,'Score:'+str(score),(100,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)\n    if(score>15):\n        #person is feeling sleepy so we beep the alarm\n        cv2.imwrite(os.path.join(path,'image.jpg'),frame)\n        try:\n            sound.play()\n            \n        except:  # isplaying = False\n            pass\n        if(thicc&lt;16):\n            thicc= thicc+2\n        else:\n            thicc=thicc-2\n            if(thicc&lt;2):\n                thicc=2\n        cv2.rectangle(frame,(0,0),(width,height),(0,0,255),thicc) \n    cv2.imshow('frame',frame)\n    if cv2.waitKey(1) &amp; 0xFF == ord('q'):\n        break\ncap.release()\ncv2.destroyAllWindows()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6267\u884c\u56f0\u5026\u68c0\u6d4b\u9879\u76ee<\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u6267\u884c\u9879\u76ee\u5e76\u67e5\u770b\u6211\u4eec\u7684\u673a\u5668\u5b66\u4e60\u9879\u76ee\u7684\u5de5\u4f5c\u60c5\u51b5\u3002\u8981\u542f\u52a8\u9879\u76ee\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728IDE\u5e73\u53f0\u8fd0\u884cdrowsiness detector.py\uff0c\u4e5f\u53ef\u4ee5\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u8fdb\u5165\u4e3b\u6587\u4ef6\u201cdrowsiness detector.py\u201d\u6240\u5728\u7684\u76ee\u5f55\u3002\u4f7f\u7528\u6b64\u547d\u4ee4\u8fd0\u884c\u811a\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python drowsiness detector.py<\/code><\/pre>\n\n\n\n<p>\u6253\u5f00\u7f51\u7edc\u6444\u50cf\u5934\u5e76\u5f00\u59cb\u68c0\u6d4b\u53ef\u80fd\u9700\u8981\u51e0\u79d2\u949f\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<p><strong>\u622a\u56fe\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"560\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143358231.png\" alt=\"\" class=\"wp-image-1988\" srcset=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143358231.png 1024w, https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143358231-300x164.png 300w, https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143358231-768x420.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"568\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143425930.png\" alt=\"\" class=\"wp-image-1989\" srcset=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143425930.png 1024w, https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143425930-300x166.png 300w, https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143425930-768x426.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>\u9879\u76ee\u4ee3\u7801\u94fe\u63a5\uff1a<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-0bbc798e-a1f3-4830-bbdc-73740269c157\" href=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143704370.zip\">20231212180430120<\/a><a href=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2024\/09\/20240918143704370.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-0bbc798e-a1f3-4830-bbdc-73740269c157\">\u4e0b\u8f7d<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u56f0\u5026\u68c0\u6d4b\u7cfb\u7edf \u5728\u6b64 Python \u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 OpenCV \u4ece\u7f51\u7edc\u6444\u50cf\u5934\u6536\u96c6\u56fe\u50cf\u5e76\u5c06\u5176\u8f93\u5165\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1987,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1986","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/1986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1986"}],"version-history":[{"count":3,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/1986\/revisions"}],"predecessor-version":[{"id":1995,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/1986\/revisions\/1995"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/media\/1987"}],"wp:attachment":[{"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1986"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}