{"id":322334,"date":"2021-04-29T15:01:12","date_gmt":"2021-04-29T15:01:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=322334"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=322334","title":{"rendered":"\u0414\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043b\u0438\u0446 \u0432 \u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u0438\u0445 \u043c\u0430\u0441\u043a\u0430\u0445"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/62f\/04c\/c46\/62f04cc4637a174f4e8517cc94ad529b.jpeg\" width=\"1280\" height=\"720\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u2014 \u044d\u0442\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442 (\u0432\u044b\u0431\u043e\u0440\u043a\u0430 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439) \u043b\u044e\u0434\u0435\u0439 \u0441 \u043c\u0430\u0441\u043a\u0430\u043c\u0438 \u0438 \u0431\u0435\u0437 \u0434\u043b\u044f \u0434\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 MobileNetV2, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435. \u0423 \u043c\u0435\u043d\u044f \u0431\u044b\u043b \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0432 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 981 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439 \u043b\u044e\u0434\u0435\u0439 \u0432 \u043c\u0430\u0441\u043a\u0430\u0445 \u0438 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0436\u0435 \u0431\u0435\u0437, \u043e\u0434\u043d\u0438\u0445 \u0438 \u0442\u0435\u0445 \u0436\u0435 \u043b\u044e\u0434\u0435\u0439.<\/p>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0432\u0430\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u0447\u0442\u043e \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c MobileNetV2,&nbsp; \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043e\u043e\u0431\u0443\u0447\u0438\u0442\u044c \u0435\u0451 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0430, \u0438\u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0432 \u043e\u0447\u043a\u0430\u0445 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u0438\u043b\u0438 \u043d\u0435\u0442, \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0437\u0430\u043c\u043e\u0440\u0430\u0436\u0438\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0441\u043b\u043e\u0438 \u043c\u043e\u0434\u0435\u043b\u0438, \u0430 \u0432 \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u0441\u043b\u043e\u0439 \u043f\u043e\u0434\u0430\u0451\u043c \u0442\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041d\u043e \u043c\u044b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0435 \u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u043e\u0439 \u043c\u0430\u0441\u043a\u0438, \u043a\u0430\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u043c \u043d\u0430\u0448 \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0438\u0437 1962 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439 \u0432 \u0434\u0432\u0443\u0445 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430\u0445 \u0432 \u043f\u0430\u043f\u043a\u0435 dataset \u0432 \u043c\u0430\u0441\u043a\u0430\u0445 \u0432 \u00abWithMask\u00bb \u0438 \u0431\u0435\u0437 \u043c\u0430\u0441\u043a\u0438 \u0432 \u00abWithoutmask\u00bb \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0412 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e 981 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438. \u0415\u0449\u0451 \u043e\u0434\u043d\u043e \u0432\u0430\u0436\u043d\u043e\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u0434\u043e\u043e\u0431\u0443\u0447\u0430\u0435\u043c \u043c\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u043b\u0438\u0446\u0430\u0445, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u0447\u0442\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0432 \u043c\u0430\u0441\u043a\u0435 \u0438\u043b\u0438 \u0431\u0435\u0437, \u0445\u043e\u0442\u044f \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0438 \u0442\u0430\u043a.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code>from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.applications import MobileNetV2 from tensorflow.keras.layers import AveragePooling2D from tensorflow.keras.layers import Dropout from tensorflow.keras.layers import Flatten from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Input from tensorflow.keras.models import Model from tensorflow.keras.optimizers import Adam from tensorflow.keras.applications.mobilenet_v2 import preprocess_input from tensorflow.keras.preprocessing.image import img_to_array from tensorflow.keras.preprocessing.image import load_img from tensorflow.keras.utils import to_categorical from sklearn.preprocessing import LabelBinarizer from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from imutils import paths import matplotlib.pyplot as plt import numpy as np import argparse import os<\/code><\/pre>\n<p><strong># \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b<\/strong><\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u2014 \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u043e\u043d \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0441 \u043a\u0430\u043a\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e \u043c\u044b \u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u043f\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0442\u0435\u0440\u044c<\/p>\n<p>INIT_LR = 0,004<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u2014 \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043f\u043e\u0445, \u043e\u0434\u043d\u0430 \u044d\u043f\u043e\u0445\u0430 \u044d\u0442\u043e \u043e\u0434\u0438\u043d&nbsp; \u043f\u0440\u043e\u0445\u043e\u0434 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0432\u0441\u0435\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<p>EPOCHS = 20<\/p>\n<p>\u0422\u0440\u0435\u0442\u0438\u0439 \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u0430 \u0438\u043b\u0438 \u0431\u0430\u0442\u0447\u0430, \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043e\u0434\u043d\u043e\u0439 \u043f\u0430\u0440\u0442\u0438\u0438.<\/p>\n<p>BS = 32<\/p>\n<p>\u041f\u043e\u0434\u0431\u043e\u0440 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043b\u0435\u0436\u0438\u0442 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u0441\u043a\u0430\u0436\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0435\u0442 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430, \u0438 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u0434\u043e \u0438\u0445 \u043f\u043e\u0434\u0431\u0438\u0440\u0430\u0442\u044c \u044d\u043c\u043f\u0438\u0440\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<pre><code>imagePaths = list(paths.list_images (r'C:\\dataset'))  # \u0412 \u044d\u0442\u043e\u0439 \u043f\u0430\u043f\u043a\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0434\u0432\u0430 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 \u0441 \u043c\u0430\u0441\u043a\u0430\u043c\u0438 \u0438 \u0431\u0435\u0437 data , labels = [] , [] for imagePath in imagePaths: \t# \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438\u0437 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 (\u0441 \u043c\u0430\u0441\u043a\u043e\u0439 \u0438\u043b\u0438 \u0431\u0435\u0437) \tlabel = imagePath.split(os.path.sep)[-2] \t# \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u043c \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 224\u0445224 \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0435\u0433\u043e \timage = load_img(imagePath, target_size = (224, 224)) \timage = img_to_array(image) \timage = preprocess_input(image) \t# \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0430\u0439\u043b\u043e\u0432 \u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \tdata.append(image) \tlabels.append(label) # \u041f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u0432 NumPy \u043c\u0430\u0441\u0441\u0438\u0432 data = np.array(data, dtype=\"float32\") labels = np.array(labels) # \u041f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u044b \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0432\u0438\u0434, \u0442.\u0435. 0 \u0431\u0435\u0437 \u043c\u0430\u0441\u043a\u0438 1 \u0441 \u043c\u0430\u0441\u043a\u043e\u0439 lb = LabelBinarizer() labels = lb.fit_transform(labels) labels = to_categorical(labels) # \u0420\u0430\u0437\u043e\u0431\u044c\u0451\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442  \u043d\u0430 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 80% \u043d\u0430 20%; (trainX, testX, trainY, testY) = train_test_split(data, labels,  test_size = 0.20, stratify = labels, random_state = 42) # \u0410\u0443\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 \u043f\u0443\u0442\u0435\u043c \u043f\u043e\u0432\u043e\u0440\u043e\u0442\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 aug = ImageDataGenerator(rotation_range = 20, zoom_range = 0.15, \twidth_shift_range = 0.2, height_shift_range = 0.2, shear_range=0.15, horizontal_flip = True, fill_mode = \"nearest\") # \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c c \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438 path_weights = \u2018mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5'     baseModel = MobileNetV2(weights=path_weights,  include_top=False, input_tensor=Input(shape=(224, 224, 3)) \u0417\u0430\u043f\u0438\u0448\u0435\u043c \u0432 \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0441\u043b\u043e\u0439 \u0438\u0437 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 headModel = baseModel.output headModel = AveragePooling2D(pool_size = (7, 7))(headModel) headModel = Flatten(name = \"flatten\")(headModel) headModel = Dense(128, activation = \"relu\")(headModel) headModel = Dropout(0.5)(headModel) headModel = Dense(2, activation = \"softmax\")(headModel) model = Model(inputs = baseModel.input, outputs = headModel) # \u0417\u0430\u043c\u043e\u0440\u043e\u0437\u043a\u0430 \u0441\u043b\u043e\u0435\u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438  for layer in baseModel.layers: \tlayer.trainable = False # \u0421\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c opt = Adam(lr = INIT_LR, decay = INIT_LR \/ EPOCHS) model.compile(loss = \"binary_crossentropy\", optimizer = opt, metrics = [\"accuracy\"]) # \u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0443 \u0441\u0435\u0442\u044c H = model.fit( aug.flow(trainX, trainY, batch_size = BS), steps_per_epoch = len(trainX) \/\/ BS, \tvalidation_data = (testX, testY), validation_steps = len(testX) \/\/ BS, epochs = EPOCHS) # \u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 predIdxs = model.predict(testX, batch_size = BS) #  \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435, \u0438\u0449\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c predIdxs = np.argmax(predIdxs, axis=1) # \u041f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0442\u0447\u0435\u0442 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f  print(classification_report(testY.argmax(axis = 1), predIdxs, target_names = lb.classes_))<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c12\/7c1\/3fd\/c127c13fd9b8f34b5a45b2600bb96bf0.jpeg\" width=\"734\" height=\"223\"><figcaption><\/figcaption><\/figure>\n<p># \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u0434\u0438\u0441\u043a \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0435\u0451  <\/p>\n<pre><code>model.save('model_mask_FACE', save_format = \"h5\") model_mask = tf.keras.models.load_model('model_mask_FACE)<\/code><\/pre>\n<p>\u041f\u043e\u0438\u0441\u043a \u043c\u0430\u0441\u043a\u0438 \u043d\u0430 \u043b\u0438\u0446\u0435, \u043d\u0430 &nbsp;\u043f\u0440\u0438\u043c\u0435\u0440\u0435<\/p>\n<p># \u041d\u0430\u0439\u0434\u0435\u043c \u043b\u0438\u0446\u043e \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 MTCNN<\/p>\n<pre><code>frame = cv2.cvtColor(cv2.imread(\u2018house.png'), cv2.COLOR_BGR2RGB) frame_image = Image.fromarray(frame) boxes, probs, landmarks = mtcnn.detect(frame_image, landmarks = True) x1, y1, x2, y2 = [int(bx) for bx in boxes[0]] image = Image.fromarray(frame[y1:y2, x1:x2]).resize((224,224)) face = img_to_array(image)<\/code><\/pre>\n<p># \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c  <\/p>\n<pre><code>face = preprocess_input(face) face = np.expand_dims(face, axis=0)<\/code><\/pre>\n<p># \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043b\u0438\u0446\u0430&nbsp; \u0432 \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c  <\/p>\n<pre><code>(mask, withoutMask) = model_mask.predict(face)[0] image = cv2.imread(\u2018house.png\u2019)<\/code><\/pre>\n<p># \u041f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u0440\u0430\u043c\u043a\u0438 \u0438 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438  <\/p>\n<pre><code>if mask &gt; withoutMask and max(mask, withoutMask) &gt; 0.8: # \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044c     label = \"Mask\" if mask &gt; withoutMask else \"No Mask\"     color = (0, 122, 0) if label == \"Mask\" else (0, 0, 122)     label = \"{}: {:.2f}%\".format(label, max(mask, withoutMask) * 100)     cv2.putText(image, label, (x1, y1 - 10),cv2.FONT_HERSHEY_SIMPLEX, 2, color, 5)     cv2.rectangle(image, (x1, y1), (x2, y2), color, 5)      y = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0438, \u043a\u0430\u043a \u0434\u043e\u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c MobileNetV2, \u0447\u0442\u043e\u0431\u044b \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043b\u0438\u0446 \u043b\u044e\u0434\u0435\u0439 \u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u044f\u0449\u0438\u0445\u0441\u044f \u0432 \u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u0438\u0445 \u043c\u0430\u0441\u043a\u0430\u0445 \u0438 \u0431\u0435\u0437 \u043d\u0438\u0445.  <\/p>\n<\/div>\n<p> \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\/post\/555078\/\"> https:\/\/habr.com\/ru\/post\/555078\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u2014 \u044d\u0442\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442 (\u0432\u044b\u0431\u043e\u0440\u043a\u0430 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439) \u043b\u044e\u0434\u0435\u0439 \u0441 \u043c\u0430\u0441\u043a\u0430\u043c\u0438 \u0438 \u0431\u0435\u0437 \u0434\u043b\u044f \u0434\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 MobileNetV2, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435. \u0423 \u043c\u0435\u043d\u044f \u0431\u044b\u043b \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0432 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 981 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439 \u043b\u044e\u0434\u0435\u0439 \u0432 \u043c\u0430\u0441\u043a\u0430\u0445 \u0438 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0436\u0435 \u0431\u0435\u0437, \u043e\u0434\u043d\u0438\u0445 \u0438 \u0442\u0435\u0445 \u0436\u0435 \u043b\u044e\u0434\u0435\u0439.<\/p>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0432\u0430\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u0447\u0442\u043e \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c MobileNetV2,&nbsp; \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043e\u043e\u0431\u0443\u0447\u0438\u0442\u044c \u0435\u0451 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0430, \u0438\u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0432 \u043e\u0447\u043a\u0430\u0445 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u0438\u043b\u0438 \u043d\u0435\u0442, \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0437\u0430\u043c\u043e\u0440\u0430\u0436\u0438\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0441\u043b\u043e\u0438 \u043c\u043e\u0434\u0435\u043b\u0438, \u0430 \u0432 \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u0441\u043b\u043e\u0439 \u043f\u043e\u0434\u0430\u0451\u043c \u0442\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041d\u043e \u043c\u044b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0435 \u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u043e\u0439 \u043c\u0430\u0441\u043a\u0438, \u043a\u0430\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u043c \u043d\u0430\u0448 \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0438\u0437 1962 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439 \u0432 \u0434\u0432\u0443\u0445 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430\u0445 \u0432 \u043f\u0430\u043f\u043a\u0435 dataset \u0432 \u043c\u0430\u0441\u043a\u0430\u0445 \u0432 \u00abWithMask\u00bb \u0438 \u0431\u0435\u0437 \u043c\u0430\u0441\u043a\u0438 \u0432 \u00abWithoutmask\u00bb \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0412 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e 981 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438. \u0415\u0449\u0451 \u043e\u0434\u043d\u043e \u0432\u0430\u0436\u043d\u043e\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u0434\u043e\u043e\u0431\u0443\u0447\u0430\u0435\u043c \u043c\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u043b\u0438\u0446\u0430\u0445, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u0447\u0442\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0432 \u043c\u0430\u0441\u043a\u0435 \u0438\u043b\u0438 \u0431\u0435\u0437, \u0445\u043e\u0442\u044f \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0438 \u0442\u0430\u043a.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code>from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.applications import MobileNetV2 from tensorflow.keras.layers import AveragePooling2D from tensorflow.keras.layers import Dropout from tensorflow.keras.layers import Flatten from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Input from tensorflow.keras.models import Model from tensorflow.keras.optimizers import Adam from tensorflow.keras.applications.mobilenet_v2 import preprocess_input from tensorflow.keras.preprocessing.image import img_to_array from tensorflow.keras.preprocessing.image import load_img from tensorflow.keras.utils import to_categorical from sklearn.preprocessing import LabelBinarizer from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from imutils import paths import matplotlib.pyplot as plt import numpy as np import argparse import os<\/code><\/pre>\n<p><strong># \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b<\/strong><\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u2014 \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u043e\u043d \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0441 \u043a\u0430\u043a\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e \u043c\u044b \u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u043f\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0442\u0435\u0440\u044c<\/p>\n<p>INIT_LR = 0,004<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u2014 \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043f\u043e\u0445, \u043e\u0434\u043d\u0430 \u044d\u043f\u043e\u0445\u0430 \u044d\u0442\u043e \u043e\u0434\u0438\u043d&nbsp; \u043f\u0440\u043e\u0445\u043e\u0434 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0432\u0441\u0435\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<p>EPOCHS = 20<\/p>\n<p>\u0422\u0440\u0435\u0442\u0438\u0439 \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u0430 \u0438\u043b\u0438 \u0431\u0430\u0442\u0447\u0430, \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043e\u0434\u043d\u043e\u0439 \u043f\u0430\u0440\u0442\u0438\u0438.<\/p>\n<p>BS = 32<\/p>\n<p>\u041f\u043e\u0434\u0431\u043e\u0440 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043b\u0435\u0436\u0438\u0442 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u0441\u043a\u0430\u0436\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0435\u0442 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430, \u0438 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u0434\u043e \u0438\u0445 \u043f\u043e\u0434\u0431\u0438\u0440\u0430\u0442\u044c \u044d\u043c\u043f\u0438\u0440\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<pre><code>imagePaths = list(paths.list_images (r'C:\\dataset'))  # \u0412 \u044d\u0442\u043e\u0439 \u043f\u0430\u043f\u043a\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0434\u0432\u0430 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 \u0441 \u043c\u0430\u0441\u043a\u0430\u043c\u0438 \u0438 \u0431\u0435\u0437 data , labels = [] , [] for imagePath in imagePaths: \t# \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438\u0437 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 (\u0441 \u043c\u0430\u0441\u043a\u043e\u0439 \u0438\u043b\u0438 \u0431\u0435\u0437) \tlabel = imagePath.split(os.path.sep)[-2] \t# \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u043c \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 224\u0445224 \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0435\u0433\u043e \timage = load_img(imagePath, target_size = (224, 224)) \timage = img_to_array(image) \timage = preprocess_input(image) \t# \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0430\u0439\u043b\u043e\u0432 \u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \tdata.append(image) \tlabels.append(label) # \u041f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u0432 NumPy \u043c\u0430\u0441\u0441\u0438\u0432 data = np.array(data, dtype=\"float32\") labels = np.array(labels) # \u041f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u044b \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0432\u0438\u0434, \u0442.\u0435. 0 \u0431\u0435\u0437 \u043c\u0430\u0441\u043a\u0438 1 \u0441 \u043c\u0430\u0441\u043a\u043e\u0439 lb = LabelBinarizer() labels = lb.fit_transform(labels) labels = to_categorical(labels) # \u0420\u0430\u0437\u043e\u0431\u044c\u0451\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442  \u043d\u0430 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 80% \u043d\u0430 20%; (trainX, testX, trainY, testY) = train_test_split(data, labels,  test_size = 0.20, stratify = labels, random_state = 42) # \u0410\u0443\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 \u043f\u0443\u0442\u0435\u043c \u043f\u043e\u0432\u043e\u0440\u043e\u0442\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 aug = ImageDataGenerator(rotation_range = 20, zoom_range = 0.15, \twidth_shift_range = 0.2, height_shift_range = 0.2, shear_range=0.15, horizontal_flip = True, fill_mode = \"nearest\") # \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c c \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438 path_weights = \u2018mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5'     baseModel = MobileNetV2(weights=path_weights,  include_top=False, input_tensor=Input(shape=(224, 224, 3)) \u0417\u0430\u043f\u0438\u0448\u0435\u043c \u0432 \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0441\u043b\u043e\u0439 \u0438\u0437 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 headModel = baseModel.output headModel = AveragePooling2D(pool_size = (7, 7))(headModel) headModel = Flatten(name = \"flatten\")(headModel) headModel = Dense(128, activation = \"relu\")(headModel) headModel = Dropout(0.5)(headModel) headModel = Dense(2, activation = \"softmax\")(headModel) model = Model(inputs = baseModel.input, outputs = headModel) # \u0417\u0430\u043c\u043e\u0440\u043e\u0437\u043a\u0430 \u0441\u043b\u043e\u0435\u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438  for layer in baseModel.layers: \tlayer.trainable = False # \u0421\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c opt = Adam(lr = INIT_LR, decay = INIT_LR \/ EPOCHS) model.compile(loss = \"binary_crossentropy\", optimizer = opt, metrics = [\"accuracy\"]) # \u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0443 \u0441\u0435\u0442\u044c H = model.fit( aug.flow(trainX, trainY, batch_size = BS), steps_per_epoch = len(trainX) \/\/ BS, \tvalidation_data = (testX, testY), validation_steps = len(testX) \/\/ BS, epochs = EPOCHS) # \u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 predIdxs = model.predict(testX, batch_size = BS) #  \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435, \u0438\u0449\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c predIdxs = np.argmax(predIdxs, axis=1) # \u041f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0442\u0447\u0435\u0442 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f  print(classification_report(testY.argmax(axis = 1), predIdxs, target_names = lb.classes_))<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p># \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u0434\u0438\u0441\u043a \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0435\u0451  <\/p>\n<pre><code>model.save('model_mask_FACE', save_format = \"h5\") model_mask = tf.keras.models.load_model('model_mask_FACE)<\/code><\/pre>\n<p>\u041f\u043e\u0438\u0441\u043a \u043c\u0430\u0441\u043a\u0438 \u043d\u0430 \u043b\u0438\u0446\u0435, \u043d\u0430 &nbsp;\u043f\u0440\u0438\u043c\u0435\u0440\u0435<\/p>\n<p># \u041d\u0430\u0439\u0434\u0435\u043c \u043b\u0438\u0446\u043e \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 MTCNN<\/p>\n<pre><code>frame = cv2.cvtColor(cv2.imread(\u2018house.png'), cv2.COLOR_BGR2RGB) frame_image = Image.fromarray(frame) boxes, probs, landmarks = mtcnn.detect(frame_image, landmarks = True) x1, y1, x2, y2 = [int(bx) for bx in boxes[0]] image = Image.fromarray(frame[y1:y2, x1:x2]).resize((224,224)) face = img_to_array(image)<\/code><\/pre>\n<p># \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c  <\/p>\n<pre><code>face = preprocess_input(face) face = np.expand_dims(face, axis=0)<\/code><\/pre>\n<p># \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043b\u0438\u0446\u0430&nbsp; \u0432 \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c  <\/p>\n<pre><code>(mask, withoutMask) = model_mask.predict(face)[0] image = cv2.imread(\u2018house.png\u2019)<\/code><\/pre>\n<p># \u041f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u0440\u0430\u043c\u043a\u0438 \u0438 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438  <\/p>\n<pre><code>if mask &gt; withoutMask and max(mask, withoutMask) &gt; 0.8: # \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044c     label = \"Mask\" if mask &gt; withoutMask else \"No Mask\"     color = (0, 122, 0) if label == \"Mask\" else (0, 0, 122)     label = \"{}: {:.2f}%\".format(label, max(mask, withoutMask) * 100)     cv2.putText(image, label, (x1, y1 - 10),cv2.FONT_HERSHEY_SIMPLEX, 2, color, 5)     cv2.rectangle(image, (x1, y1), (x2, y2), color, 5)      y = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0438, \u043a\u0430\u043a \u0434\u043e\u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c MobileNetV2, \u0447\u0442\u043e\u0431\u044b \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043b\u0438\u0446 \u043b\u044e\u0434\u0435\u0439 \u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u044f\u0449\u0438\u0445\u0441\u044f \u0432 \u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u0438\u0445 \u043c\u0430\u0441\u043a\u0430\u0445 \u0438 \u0431\u0435\u0437 \u043d\u0438\u0445.  <\/p>\n<\/div>\n<p> \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\/post\/555078\/\"> https:\/\/habr.com\/ru\/post\/555078\/<\/a><br \/><\/br><\/br><\/p>\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-322334","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/322334","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=322334"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/322334\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}