{"id":354464,"date":"2024-05-20T22:48:56","date_gmt":"2024-05-20T22:48:56","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=354464"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=354464","title":{"rendered":"<span>\u041a\u0430\u043a \u044f Face ID \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0434\u0432\u0435\u0440\u0438 \u0434\u0435\u043b\u0430\u043b<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043e\u043f\u0438\u0448\u0443 \u0441\u0432\u043e\u0439 \u043e\u043f\u044b\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f Face ID \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0434\u0432\u0435\u0440\u0438. \u042f \u043d\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u0448\u0443 \u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0437\u0430 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0434\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0443\u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0441\u044f\u0446\u0435\u0432. <\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0438\u0434\u0435\u0438 \u0443 \u043c\u0435\u043d\u044f \u0443\u0436\u0435 \u0431\u044b\u043b\u043e:<\/p>\n<ul>\n<li>\n<p>\u0423\u043c\u043d\u044b\u0439 \u0434\u043e\u043c \u043d\u0430 \u0431\u0430\u0437\u0435 homeassistant (\u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e)<\/p>\n<\/li>\n<li>\n<p>MQTT \u0441\u0435\u0440\u0432\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u0423\u043c\u043d\u044b\u0439 \u0437\u0430\u043c\u043e\u043a<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043c\u0435\u0440\u0430 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u0431\u0438\u0440\u0430\u0442\u044c \u0441 \u043d\u0435e \u0444\u043e\u0442\u043e<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u043b\u0438\u0446\u0430 \u043d\u0430 \u0444\u043e\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0441 \u043a\u0430\u043c\u0435\u0440\u044b. \u041b\u043e\u0433\u0438\u0447\u043d\u0435\u0435 \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043b\u0430\u0433\u0438\u043d \u0434\u043b\u044f \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430, \u043d\u043e \u044f \u0432\u044b\u0431\u0440\u0430\u043b \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0443\u0442\u044c, \u0445\u043e\u0442\u044f \u0438 \u043d\u0435 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439. \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 python \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0430 <a href=\"https:\/\/pypi.org\/project\/face-recognition\/\" rel=\"noopener noreferrer nofollow\">https:\/\/pypi.org\/project\/face-recognition\/<\/a><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d1e\/61b\/8d2\/d1e61b8d24763217f2ca2c70526cd6b3.png\" alt=\"\u041d\u0430 \u0441\u0445\u0435\u043c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\" title=\"\u041d\u0430 \u0441\u0445\u0435\u043c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\" width=\"1958\" height=\"834\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d1e\/61b\/8d2\/d1e61b8d24763217f2ca2c70526cd6b3.png\"\/><\/p>\n<div><figcaption>\u041d\u0430 \u0441\u0445\u0435\u043c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443 \u043c\u0435\u043d\u044f \u0443\u0436\u0435 \u0431\u044b\u043b MQTT \u0441\u0435\u0440\u0432\u0435\u0440, \u0442\u043e \u044f \u0440\u0435\u0448\u0438\u043b, \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0443\u0442\u044c \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u043c\u043d\u044b\u0439 \u0434\u043e\u043c \u0438 \u0441\u043a\u0440\u0438\u043f\u0442.<\/p>\n<p>\u041e\u043f\u0438\u0448\u0443 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b.<\/p>\n<ol>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u043a\u0430\u043a \u0434\u0435\u043c\u043e\u043d, \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0435\u0442 \u043b\u0438\u0446\u0430 \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 KNOWN \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e\u043f\u0438\u043a.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0443\u043c\u043d\u043e\u043c \u0434\u043e\u043c\u0435 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0430 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f. \u0423\u043c\u043d\u044b\u0439 \u0434\u043e\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u0442\u043e\u043f\u0438\u043a \u0446\u0438\u0444\u0440\u0443 \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u043f\u044b\u0442\u043e\u043a \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f  \u043b\u0438\u0446\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u0432 \u0442\u043e\u043f\u0438\u043a\u0435 \u0446\u0438\u0444\u0440\u0443 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0441 \u043a\u0430\u043c\u0435\u0440\u044b \u0444\u043e\u0442\u043e, \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0431\u0440\u0435\u0437\u0430\u0435\u0442 \u0444\u043e\u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f. \u0423 \u043c\u0435\u043d\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 4\u0445 \u043c\u0435\u0433\u0430\u043f\u0438\u043a\u0441\u0435\u043b\u044c\u043d\u0430\u044f \u043a\u0430\u043c\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u043a\u043e\u0440\u0438\u0434\u043e\u0440, \u0430 \u043b\u0438\u0446\u043e \u0432\u0438\u0434\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0431\u043b\u0438\u0437\u043e\u0441\u0442\u0438 \u043e\u0442 \u043a\u0430\u043c\u0435\u0440\u044b. \u041f\u0440\u0438 \u043e\u0431\u0440\u0435\u0437\u0430\u043d\u0438\u0438 \u0444\u043e\u0442\u043e \u0446\u0438\u043a\u043b \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0441\u043d\u0438\u0437\u0438\u043b\u0441\u044f \u0441 3 \u0441\u0435\u043a\u0443\u043d\u0434, \u0434\u043e \u043c\u0435\u043d\u0435\u0435 1 \u0441\u0435\u043a\u0443\u043d\u0434\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u043b\u0438\u0446\u0430 \u043d\u0430 \u0444\u043e\u0442\u043e, \u0435\u0441\u043b\u0438 \u043b\u0438\u0446\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u044b, \u0442\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0438\u0445 \u0441 \u0442\u0435\u043c\u0438, \u0447\u0442\u043e \u0441\u0447\u0438\u0442\u0430\u043d\u044b \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 KNOWN.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043f\u0438\u0448\u0435\u0442 \u0432 \u0442\u043e\u043f\u0438\u043a MQTT \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0444\u043e\u0442\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043f\u0430\u043f\u043a\u0443.<\/p>\n<\/li>\n<li>\n<p>\u0423\u043c\u043d\u044b\u0439 \u0434\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0432 UNKNOWN \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0432\u0435\u0440\u043d\u043e\u0439 \u0437\u0432\u043e\u043d\u043e\u043a, \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u0438\u043c\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u043c\u043e\u043a \u0438 \u043f\u0440\u043e\u0438\u0437\u043d\u043e\u0441\u0438\u0442: &#171;Username \u043f\u0440\u0438\u0448\u0435\u043b&#187;.<\/p>\n<\/li>\n<\/ol>\n<p>\u041a\u0430\u0442\u0430\u043b\u043e\u0433\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438: known, new, nofaces, unknown \u0438 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438 \u0434\u043b\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u043b\u044e\u0434\u0435\u0439 (\u0438\u043c\u044f \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 = \u0438\u043c\u0435\u043d\u0438 \u0444\u0430\u0439\u043b\u0430 \u0441 \u0444\u043e\u0442\u043e \u0434\u043e \u0442\u043e\u0447\u043a\u0438 \u0432 \u043f\u0430\u043f\u043a\u0443 known). \u041d\u0443 \u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0441 \u0444\u043e\u0442\u043e. \u042f \u0431\u0440\u0430\u043b \u0444\u043e\u0442\u043e \u043f\u0440\u044f\u043c \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 unknown.<\/p>\n<details class=\"spoiler\">\n<summary>\u041d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u043e\u0432<\/summary>\n<div class=\"spoiler__content\">\n<p> \u251c\u2500\u2500 kat<br \/> \u251c\u2500\u2500 known<br \/> \u2502\u00a0\u00a0 \u251c\u2500\u2500 kat.jpg<br \/> \u2502\u00a0\u00a0 \u251c\u2500\u2500 max.jpg<br \/> \u2502\u00a0\u00a0 \u2514\u2500\u2500 sasha.jpg<br \/> \u251c\u2500\u2500 max<br \/> \u251c\u2500\u2500 new<br \/> \u251c\u2500\u2500 nofaces<br \/> \u251c\u2500\u2500 sasha<br \/> \u2514\u2500\u2500 unknown<\/p>\n<\/div>\n<\/details>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 Gitlab <a href=\"https:\/\/gitlab.com\/tmv002\/face-recognition-mqtt\/\" rel=\"noopener noreferrer nofollow\">https:\/\/gitlab.com\/tmv002\/face-recognition-mqtt\/<\/a><\/p>\n<details class=\"spoiler\">\n<summary>__main.py__<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import requests import configparser import logging import os import sys import io import face_recognition import configparser import paho.mqtt.client as mqtt from datetime import datetime from PIL import Image  delimeter = '\/' if(os.name=='nt'):     delimeter = '\\\\'  root_path = os.path.dirname(__file__) + delimeter  config = configparser.ConfigParser() config.read(root_path + \"fr.conf\") url = config['image']['url'] topic_subscribe = config['mqtt']['topic_subscribe'] topic_publish = config['mqtt']['topic_publish'] mqtt_server = config['mqtt']['mqtt_server'] mqtt_port = int(config['mqtt']['mqtt_port']) try:     mqtt_user = config['mqtt']['mqtt_user']     mqtt_password = config['mqtt']['mqtt_password'] except:     pass  known_path = config['dirs']['known'] new_path = config['dirs']['new'] unknown_path = config['dirs']['unknown'] no_faces = config['dirs']['no_faces'] logfile = config['log']['logfile'] loglevel = 10 * int(config['log']['loglevel']) comparison_threshold = float(config['compare']['comparison_threshold'])  def parse_int_tuple(input):     return tuple(int(k.strip()) for k in input[1:-1].split(','))  try:     crop_rect = parse_int_tuple(config['image']['crop_rect']) except:     pass   logging.basicConfig(level=loglevel, filename=(root_path + logfile),filemode=\"w\",format=\"%(asctime)s %(levelname)s %(message)s\") persons = []  def add_known_person():     listdir = None     try:         listdir = os.listdir((root_path + known_path))     except Exception as exc:         logging.error(\"Can't list known person dir \" + (root_path + known_path) + \" with error: \" + str(exc))         sys.exit(1)      for file_path in listdir:         # check if current file_path is a file         if os.path.isfile(os.path.join((root_path + known_path), file_path)):             person_name = file_path.split(\".\")[0]             logging.info(\"Try to add known person from file \" + file_path)             # load image from file             known_image = face_recognition.load_image_file(root_path + known_path + delimeter + file_path)             # recognize face             face_enc = face_recognition.face_encodings(known_image)             # if face found             if(len(face_enc) > 0):                 # get first face                 person_encoding = face_enc[0]                 # add face to known persons array                 persons.append([person_name, [person_encoding]])                 logging.info(\"Add known person: \" + person_name)             else:                 logging.error(\"Face not found in file \" + file_path)  def get_image_from_url(file_name):     result = False     r = None     try:         # Get image from URL         r = requests.get(url)         logging.info(\"Get image success\")     except Exception as exc:          logging.error(\"Can't get image error:\" + str(exc))     if r is not None:         new_file_name = file_name         new_file_path = new_path + delimeter + new_file_name         # Load image from request content         im = Image.open(io.BytesIO(r.content))         try:             crop_rect             # Crop image             im = im.crop(crop_rect)         except:             pass         try:             # Save image to folder new             im.save(root_path + new_file_path)             result = True             logging.info(\"Save image to \" + (root_path + new_file_path))         except Exception as exc:             logging.error(\"Can't save image \" + root_path + new_file_path + \" with error: \" + str(exc))         return result               # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc):     if(rc != 0):         logging.info(\"MQTT connection error with result code \"+str(rc))         print(\"MQTT connection error with result code \"+str(rc))     else:         logging.info(\"MQTT connection successful with result code \"+str(rc))         print(\"MQTT connection successful with result code \"+str(rc))      # Subscribing in on_connect() means that if we lose the connection and     # reconnect then subscriptions will be renewed.     client.subscribe(topic_subscribe)  # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg):     known_person = \"unknown\"     logging.info(msg.topic+\" \"+str(msg.payload.decode()))     # Loop face recognition     i = 0     try:         # Read value from topic         i = int(msg.payload.decode())     except ValueError:         logging.error(\"Receive non int value in topic \" + msg.topic)     for j in range(i):         now = datetime.now()         new_file_name = now.strftime(\"%Y_%m_%d-%H_%M_%S-\") + str(j) + '.jpg'         if get_image_from_url(new_file_name):             new_file_path = new_path + delimeter + new_file_name             # Load image from file             new_image = face_recognition.load_image_file(root_path + new_file_path)             # Check face count on image             face_locations = face_recognition.face_locations(new_image)             logging.info(\"Face location count: \" + str(len(face_locations)))             if(len(face_locations) > 0):                 # Recognize face on image                 unknown_faces = face_recognition.face_encodings(new_image, face_locations)                 logging.info(\"face recognition count: \" + str(len(unknown_faces)))                 # Compare face with known faces                 for unknown_face in unknown_faces:                     for i in range(len(persons)):                         results = face_recognition.compare_faces(persons[i][1], unknown_face, comparison_threshold)                         logging.info(\"compare face with \" + persons[i][0] + \" result: \" + str(results))                         if(results[0]):                             known_person = persons[i][0]                             client.publish(topic_publish + 'person', \"{ \\\"known_person\\\": \\\"\" + known_person + \"\\\" }\", 0, False)                             logging.info(\"client publish { \\\"known_person\\\": \\\"\" + known_person + \"\\\" }\")             else:                 #print(\"no faces\")                 known_person = \"nofaces\"             logging.info(\"person: \" + known_person)             client.publish(topic_publish + known_person, now.strftime(\"%Y-%m-%d %H:%M:%S\"), 0, True)             if(known_person == \"nofaces\"):                 try:                     os.remove(root_path + new_file_path)                     logging.info(\"Remove file \" + root_path + new_file_path)                 except Exception as exc:                     logging.error(\"Can't remove file \" + root_path + new_file_path + \": \" + str(exc))             else:                 try:                     os.replace(root_path + new_file_path, root_path + known_person + delimeter + new_file_name)                     logging.info(\"Move file \" + root_path + new_file_path + \" to \" + root_path + known_person + delimeter + new_file_name)                 except Exception as exc:                     logging.error(\"Can't move file \" + root_path + new_file_path + \" to \" + root_path + known_person + delimeter + new_file_name + \": \" + str(exc))             if(known_person not in (\"nofaces\", \"unknown\")):                 break  if __name__ == \"__main__\":     logging.info(\"Start face recognition\")     add_known_person()     connected_flag = 0     client = mqtt.Client()     try:         mqtt_password         client.username_pw_set(mqtt_user, mqtt_password)     except NameError:         logging.warn(\"MQTT password is not set. Trying to connect without password.\")     client.enable_logger(logger=logging)     client.on_connect = on_connect     client.on_message = on_message     while not connected_flag:         try:             client.connect(mqtt_server, mqtt_port, 60)             connected_flag = 1         except Exception as exc:             logging.error(\"MQTT connection error: \" + str(exc))     client.loop_forever()<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>fr.conf<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">[image] url = http:\/\/user:password@cam.ip.or.dns\/ISAPI\/Streaming\/channels\/1\/picture #Set crop_rect to crop orginal image. Script is faster face locating on smaller image. (left_x, top_y, right_x, bottom_y) #crop_rect = (400, 500, 1350, 1700)  [mqtt] topic_subscribe = homeassistant\/sensor\/facerec\/cam3 topic_publish = homeassistant\/sensor\/facerec\/ mqtt_server = 192.168.12.5 mqtt_port = 1883 #mqtt_user = mqttusername #mqtt_password = mqttpassword  [dirs] known = known new = new unknown = unknown no_faces = nofaces  [log] logfile = face-recognition.log #Loglevel 1 - DEBUG, 2 - INFO, 3 - WARN, 4 - ERROR loglevel = 2  [compare] # Lowest threshold = 1. Highest threshold = 0.1 comparison_threshold = 0.5<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 unit file \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430:<\/p>\n<details class=\"spoiler\">\n<summary>face-recognition.service<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"bash\">[Unit] Description=Face Recognition  [Service] Restart=always RestartSec=30 WorkingDirectory=\/opt\/face-recognition\/ TimeoutStartSec=120 StandardOutput=syslog StandardError=syslog SyslogIdentifier=face-recognition ExecStart=\/usr\/bin\/python3 \/opt\/face-recognition\/  [Install] WantedBy=multi-user.target<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>UPDATE.<\/p>\n<p>\u0420\u0435\u0448\u0438\u043b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u043e\u0442\u043e \u043a\u0430\u043c\u0435\u0440\u0435 \u0438 \u0435\u0441\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0448\u0430\u043d\u0441, \u0447\u0442\u043e face-recognition \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0435\u0442 \u043b\u0438\u0446\u043e \u043f\u043e \u0444\u043e\u0442\u043e, \u043d\u043e \u0443 \u043c\u0435\u043d\u044f \u0434\u0432\u0435\u0440\u044c \u043d\u0430 \u044d\u0442\u0430\u0436 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u043e\u043c\u043e\u0444\u043e\u043d \u0438 \u043f\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u043b\u044e\u0434\u0435\u0439 \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0438\u0447\u043d\u043e \u043c\u0435\u043d\u044f \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043d\u0438 \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u043d\u043e \u0434\u043b\u044f \u043c\u0435\u043d\u044f \u044d\u0442\u043e \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u0435 \u0443\u0440\u043e\u0432\u043d\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0437\u0430\u043c\u043e\u043a \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0434\u0432\u0435\u0440\u0438 \u0431\u044b\u043b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u043a\u0440\u044b\u0442, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u0435\u0442\u0438 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u0442\u0435\u0440\u044f\u043b\u0438 \u043a\u043b\u044e\u0447\u0438 \u0432 \u043e\u043a\u0440\u0443\u0433\u0435. \u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043c\u043e\u043a \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0430 \u0434\u0435\u0442\u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u043b\u044e\u0447\u0438.<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441 \u0441 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u0435\u043c \u0434\u043e\u043c\u043e\u0439 \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043a\u0430\u0437\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0438 \u0437\u0430\u043c\u043a\u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d. \u0418 \u0432 \u044d\u0442\u043e\u043c \u0432\u043e\u043f\u0440\u043e\u0441\u0435 \u044f \u0442\u043e\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043f\u043b\u044e\u0441, \u0442\u0430\u043a \u043a\u0430\u043a \u0440\u0430\u043d\u0435\u0435 \u0434\u0435\u0442\u0438 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0437\u0430\u0431\u044b\u0432\u0430\u043b\u0438 \u043a\u043b\u044e\u0447\u0438 \u0434\u043e\u043c\u0430 \u0438 \u0436\u0434\u0430\u043b\u0438 \u043f\u043e\u0434 \u0434\u0432\u0435\u0440\u044c\u044e \u0438\u043b\u0438 \u0443 \u0434\u0440\u0443\u0437\u0435\u0439, \u043a\u043e\u0433\u0434\u0430 \u0438\u0445 \u043f\u0443\u0441\u0442\u044f\u0442 \u0434\u043e\u043c\u043e\u0439, \u0442\u0435\u043f\u0435\u0440\u044c \u0442\u0430\u043a\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0438\u0441\u0447\u0435\u0437\u043b\u0430 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u042f \u043d\u0438\u043a\u043e\u0433\u043e \u043d\u0435 \u043f\u0440\u0438\u0437\u044b\u0432\u0430\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439, \u043d\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c 2FA \u0432 \u0432\u0438\u0434\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0439 \u043a\u043d\u043e\u043f\u043a\u0438, \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u043f\u0430\u043d\u0435\u043b\u0438, nfc \u043c\u0435\u0442\u043a\u0438, \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u0431\u043e\u0442\u0430, \u0442\u0440\u0435\u043a\u0435\u0440\u043e\u0432: gps, bluetooth, wifi. \u041c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u0437\u043d\u0430\u0442\u044c \u043a\u0442\u043e \u0438 \u043a\u043e\u0433\u0434\u0430 \u043a \u0432\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b. \u041c\u043e\u0436\u0435\u0442\u0435 \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e \u0421\u041a\u0423\u0414 \u0432 \u0444\u0438\u0442\u043d\u0435\u0441\u0435 \u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u044f\u0442\u044c \u0421\u0411 \u043e \u043f\u0440\u043e\u0445\u043e\u0434\u0430\u0445 \u0432 \u0437\u0430\u043b \u043f\u043e \u0447\u0443\u0436\u043e\u043c\u0443 \u0430\u0431\u043e\u043d\u0435\u043c\u0435\u043d\u0442\u0443 \u0438 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u0435\u0449\u0435 \u0441\u043e\u0442\u043d\u044e \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432. \u042f \u043b\u0438\u0448\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u043a\u0430\u043a \u0438\u0437 \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0433\u043e \u043c\u043d\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/760048\/\"> https:\/\/habr.com\/ru\/articles\/760048\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043e\u043f\u0438\u0448\u0443 \u0441\u0432\u043e\u0439 \u043e\u043f\u044b\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f Face ID \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0434\u0432\u0435\u0440\u0438. \u042f \u043d\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u0448\u0443 \u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0437\u0430 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0434\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0443\u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0441\u044f\u0446\u0435\u0432. <\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0438\u0434\u0435\u0438 \u0443 \u043c\u0435\u043d\u044f \u0443\u0436\u0435 \u0431\u044b\u043b\u043e:<\/p>\n<ul>\n<li>\n<p>\u0423\u043c\u043d\u044b\u0439 \u0434\u043e\u043c \u043d\u0430 \u0431\u0430\u0437\u0435 homeassistant (\u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e)<\/p>\n<\/li>\n<li>\n<p>MQTT \u0441\u0435\u0440\u0432\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u0423\u043c\u043d\u044b\u0439 \u0437\u0430\u043c\u043e\u043a<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043c\u0435\u0440\u0430 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u0431\u0438\u0440\u0430\u0442\u044c \u0441 \u043d\u0435e \u0444\u043e\u0442\u043e<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u043b\u0438\u0446\u0430 \u043d\u0430 \u0444\u043e\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0441 \u043a\u0430\u043c\u0435\u0440\u044b. \u041b\u043e\u0433\u0438\u0447\u043d\u0435\u0435 \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043b\u0430\u0433\u0438\u043d \u0434\u043b\u044f \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430, \u043d\u043e \u044f \u0432\u044b\u0431\u0440\u0430\u043b \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0443\u0442\u044c, \u0445\u043e\u0442\u044f \u0438 \u043d\u0435 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439. \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 python \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0430 <a href=\"https:\/\/pypi.org\/project\/face-recognition\/\" rel=\"noopener noreferrer nofollow\">https:\/\/pypi.org\/project\/face-recognition\/<\/a><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041d\u0430 \u0441\u0445\u0435\u043c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443 \u043c\u0435\u043d\u044f \u0443\u0436\u0435 \u0431\u044b\u043b MQTT \u0441\u0435\u0440\u0432\u0435\u0440, \u0442\u043e \u044f \u0440\u0435\u0448\u0438\u043b, \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0443\u0442\u044c \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u043c\u043d\u044b\u0439 \u0434\u043e\u043c \u0438 \u0441\u043a\u0440\u0438\u043f\u0442.<\/p>\n<p>\u041e\u043f\u0438\u0448\u0443 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b.<\/p>\n<ol>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u043a\u0430\u043a \u0434\u0435\u043c\u043e\u043d, \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0435\u0442 \u043b\u0438\u0446\u0430 \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 KNOWN \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e\u043f\u0438\u043a.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0443\u043c\u043d\u043e\u043c \u0434\u043e\u043c\u0435 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0430 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f. \u0423\u043c\u043d\u044b\u0439 \u0434\u043e\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u0442\u043e\u043f\u0438\u043a \u0446\u0438\u0444\u0440\u0443 \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u043f\u044b\u0442\u043e\u043a \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f  \u043b\u0438\u0446\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u0432 \u0442\u043e\u043f\u0438\u043a\u0435 \u0446\u0438\u0444\u0440\u0443 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0441 \u043a\u0430\u043c\u0435\u0440\u044b \u0444\u043e\u0442\u043e, \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0431\u0440\u0435\u0437\u0430\u0435\u0442 \u0444\u043e\u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f. \u0423 \u043c\u0435\u043d\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 4\u0445 \u043c\u0435\u0433\u0430\u043f\u0438\u043a\u0441\u0435\u043b\u044c\u043d\u0430\u044f \u043a\u0430\u043c\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u043a\u043e\u0440\u0438\u0434\u043e\u0440, \u0430 \u043b\u0438\u0446\u043e \u0432\u0438\u0434\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0431\u043b\u0438\u0437\u043e\u0441\u0442\u0438 \u043e\u0442 \u043a\u0430\u043c\u0435\u0440\u044b. \u041f\u0440\u0438 \u043e\u0431\u0440\u0435\u0437\u0430\u043d\u0438\u0438 \u0444\u043e\u0442\u043e \u0446\u0438\u043a\u043b \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0441\u043d\u0438\u0437\u0438\u043b\u0441\u044f \u0441 3 \u0441\u0435\u043a\u0443\u043d\u0434, \u0434\u043e \u043c\u0435\u043d\u0435\u0435 1 \u0441\u0435\u043a\u0443\u043d\u0434\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u043b\u0438\u0446\u0430 \u043d\u0430 \u0444\u043e\u0442\u043e, \u0435\u0441\u043b\u0438 \u043b\u0438\u0446\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u044b, \u0442\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0438\u0445 \u0441 \u0442\u0435\u043c\u0438, \u0447\u0442\u043e \u0441\u0447\u0438\u0442\u0430\u043d\u044b \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 KNOWN.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043f\u0438\u0448\u0435\u0442 \u0432 \u0442\u043e\u043f\u0438\u043a MQTT \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0444\u043e\u0442\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043f\u0430\u043f\u043a\u0443.<\/p>\n<\/li>\n<li>\n<p>\u0423\u043c\u043d\u044b\u0439 \u0434\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0432 UNKNOWN \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0432\u0435\u0440\u043d\u043e\u0439 \u0437\u0432\u043e\u043d\u043e\u043a, \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u0438\u043c\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u043c\u043e\u043a \u0438 \u043f\u0440\u043e\u0438\u0437\u043d\u043e\u0441\u0438\u0442: &#171;Username \u043f\u0440\u0438\u0448\u0435\u043b&#187;.<\/p>\n<\/li>\n<\/ol>\n<p>\u041a\u0430\u0442\u0430\u043b\u043e\u0433\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438: known, new, nofaces, unknown \u0438 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438 \u0434\u043b\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u043b\u044e\u0434\u0435\u0439 (\u0438\u043c\u044f \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 = \u0438\u043c\u0435\u043d\u0438 \u0444\u0430\u0439\u043b\u0430 \u0441 \u0444\u043e\u0442\u043e \u0434\u043e \u0442\u043e\u0447\u043a\u0438 \u0432 \u043f\u0430\u043f\u043a\u0443 known). \u041d\u0443 \u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0441 \u0444\u043e\u0442\u043e. \u042f \u0431\u0440\u0430\u043b \u0444\u043e\u0442\u043e \u043f\u0440\u044f\u043c \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 unknown.<\/p>\n<details class=\"spoiler\">\n<summary>\u041d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u043e\u0432<\/summary>\n<div class=\"spoiler__content\">\n<p> \u251c\u2500\u2500 kat<br \/> \u251c\u2500\u2500 known<br \/> \u2502\u00a0\u00a0 \u251c\u2500\u2500 kat.jpg<br \/> \u2502\u00a0\u00a0 \u251c\u2500\u2500 max.jpg<br \/> \u2502\u00a0\u00a0 \u2514\u2500\u2500 sasha.jpg<br \/> \u251c\u2500\u2500 max<br \/> \u251c\u2500\u2500 new<br \/> \u251c\u2500\u2500 nofaces<br \/> \u251c\u2500\u2500 sasha<br \/> \u2514\u2500\u2500 unknown<\/p>\n<\/div>\n<\/details>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 Gitlab <a href=\"https:\/\/gitlab.com\/tmv002\/face-recognition-mqtt\/\" rel=\"noopener noreferrer nofollow\">https:\/\/gitlab.com\/tmv002\/face-recognition-mqtt\/<\/a><\/p>\n<details class=\"spoiler\">\n<summary>__main.py__<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import requests import configparser import logging import os import sys import io import face_recognition import configparser import paho.mqtt.client as mqtt from datetime import datetime from PIL import Image  delimeter = '\/' if(os.name=='nt'):     delimeter = '\\\\'  root_path = os.path.dirname(__file__) + delimeter  config = configparser.ConfigParser() config.read(root_path + \"fr.conf\") url = config['image']['url'] topic_subscribe = config['mqtt']['topic_subscribe'] topic_publish = config['mqtt']['topic_publish'] mqtt_server = config['mqtt']['mqtt_server'] mqtt_port = int(config['mqtt']['mqtt_port']) try:     mqtt_user = config['mqtt']['mqtt_user']     mqtt_password = config['mqtt']['mqtt_password'] except:     pass  known_path = config['dirs']['known'] new_path = config['dirs']['new'] unknown_path = config['dirs']['unknown'] no_faces = config['dirs']['no_faces'] logfile = config['log']['logfile'] loglevel = 10 * int(config['log']['loglevel']) comparison_threshold = float(config['compare']['comparison_threshold'])  def parse_int_tuple(input):     return tuple(int(k.strip()) for k in input[1:-1].split(','))  try:     crop_rect = parse_int_tuple(config['image']['crop_rect']) except:     pass   logging.basicConfig(level=loglevel, filename=(root_path + logfile),filemode=\"w\",format=\"%(asctime)s %(levelname)s %(message)s\") persons = []  def add_known_person():     listdir = None     try:         listdir = os.listdir((root_path + known_path))     except Exception as exc:         logging.error(\"Can't list known person dir \" + (root_path + known_path) + \" with error: \" + str(exc))         sys.exit(1)      for file_path in listdir:         # check if current file_path is a file         if os.path.isfile(os.path.join((root_path + known_path), file_path)):             person_name = file_path.split(\".\")[0]             logging.info(\"Try to add known person from file \" + file_path)             # load image from file             known_image = face_recognition.load_image_file(root_path + known_path + delimeter + file_path)             # recognize face             face_enc = face_recognition.face_encodings(known_image)             # if face found             if(len(face_enc) > 0):                 # get first face                 person_encoding = face_enc[0]                 # add face to known persons array                 persons.append([person_name, [person_encoding]])                 logging.info(\"Add known person: \" + person_name)             else:                 logging.error(\"Face not found in file \" + file_path)  def get_image_from_url(file_name):     result = False     r = None     try:         # Get image from URL         r = requests.get(url)         logging.info(\"Get image success\")     except Exception as exc:          logging.error(\"Can't get image error:\" + str(exc))     if r is not None:         new_file_name = file_name         new_file_path = new_path + delimeter + new_file_name         # Load image from request content         im = Image.open(io.BytesIO(r.content))         try:             crop_rect             # Crop image             im = im.crop(crop_rect)         except:             pass         try:             # Save image to folder new             im.save(root_path + new_file_path)             result = True             logging.info(\"Save image to \" + (root_path + new_file_path))         except Exception as exc:             logging.error(\"Can't save image \" + root_path + new_file_path + \" with error: \" + str(exc))         return result               # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc):     if(rc != 0):         logging.info(\"MQTT connection error with result code \"+str(rc))         print(\"MQTT connection error with result code \"+str(rc))     else:         logging.info(\"MQTT connection successful with result code \"+str(rc))         print(\"MQTT connection successful with result code \"+str(rc))      # Subscribing in on_connect() means that if we lose the connection and     # reconnect then subscriptions will be renewed.     client.subscribe(topic_subscribe)  # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg):     known_person = \"unknown\"     logging.info(msg.topic+\" \"+str(msg.payload.decode()))     # Loop face recognition     i = 0     try:         # Read value from topic         i = int(msg.payload.decode())     except ValueError:         logging.error(\"Receive non int value in topic \" + msg.topic)     for j in range(i):         now = datetime.now()         new_file_name = now.strftime(\"%Y_%m_%d-%H_%M_%S-\") + str(j) + '.jpg'         if get_image_from_url(new_file_name):             new_file_path = new_path + delimeter + new_file_name             # Load image from file             new_image = face_recognition.load_image_file(root_path + new_file_path)             # Check face count on image             face_locations = face_recognition.face_locations(new_image)             logging.info(\"Face location count: \" + str(len(face_locations)))             if(len(face_locations) > 0):                 # Recognize face on image                 unknown_faces = face_recognition.face_encodings(new_image, face_locations)                 logging.info(\"face recognition count: \" + str(len(unknown_faces)))                 # Compare face with known faces                 for unknown_face in unknown_faces:                     for i in range(len(persons)):                         results = face_recognition.compare_faces(persons[i][1], unknown_face, comparison_threshold)                         logging.info(\"compare face with \" + persons[i][0] + \" result: \" + str(results))                         if(results[0]):                             known_person = persons[i][0]                             client.publish(topic_publish + 'person', \"{ \\\"known_person\\\": \\\"\" + known_person + \"\\\" }\", 0, False)                             logging.info(\"client publish { \\\"known_person\\\": \\\"\" + known_person + \"\\\" }\")             else:                 #print(\"no faces\")                 known_person = \"nofaces\"             logging.info(\"person: \" + known_person)             client.publish(topic_publish + known_person, now.strftime(\"%Y-%m-%d %H:%M:%S\"), 0, True)             if(known_person == \"nofaces\"):                 try:                     os.remove(root_path + new_file_path)                     logging.info(\"Remove file \" + root_path + new_file_path)                 except Exception as exc:                     logging.error(\"Can't remove file \" + root_path + new_file_path + \": \" + str(exc))             else:                 try:                     os.replace(root_path + new_file_path, root_path + known_person + delimeter + new_file_name)                     logging.info(\"Move file \" + root_path + new_file_path + \" to \" + root_path + known_person + delimeter + new_file_name)                 except Exception as exc:                     logging.error(\"Can't move file \" + root_path + new_file_path + \" to \" + root_path + known_person + delimeter + new_file_name + \": \" + str(exc))             if(known_person not in (\"nofaces\", \"unknown\")):                 break  if __name__ == \"__main__\":     logging.info(\"Start face recognition\")     add_known_person()     connected_flag = 0     client = mqtt.Client()     try:         mqtt_password         client.username_pw_set(mqtt_user, mqtt_password)     except NameError:         logging.warn(\"MQTT password is not set. Trying to connect without password.\")     client.enable_logger(logger=logging)     client.on_connect = on_connect     client.on_message = on_message     while not connected_flag:         try:      <\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-354464","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/354464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354464"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/354464\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}