Желілердің негізгі түсінігі 9



бет23/23
Дата30.05.2022
өлшемі0.55 Mb.
#458795
1   ...   15   16   17   18   19   20   21   22   23
Сламбек Б.Нейрондық желілер негізінде адамның бет-әлпет эмоциясын тану.2019

Б қосымшасының жалғасы


model.summary()


log_file_path = base_path + '_emotion_training.log' csv_logger = CSVLogger(log_file_path, append=False) early_stop = EarlyStopping('val_loss', patience=patience)
reduce_lr = ReduceLROnPlateau('val_loss', factor = 0.1, patience = int(patience/4), verbose = 1)
trained_models_path = base_path + '_mini_XCEPTION'
model_names = trained_models_path + '.{epoch:02d}-{val_acc:.2f}.hdf5' model_checkpoint = ModelCheckpoint(model_names, 'val_loss', verbose = 1,
save_best_only = True)
callbacks = [model_checkpoint, csv_logger, early_stop, reduce_lr] faces, emotions = 'test’
faces = preprocess_input(faces) num_samples, num_classes = emotions.shape
xtrain, xtest, ytrain, ytest = train_test_split(faces, emotions, test_size=0.2, shuffle = True)
model.fit_generator(data_generator.flow(xtrain, ytrain, batch_size), steps_per_epoch=len(xtrain) / batch_size, epochs=num_epochs,
verbose=1, callbacks=callbacks, validation_data=(xtest,ytest)) from keras.preprocessing.image import img_to_array import imutils
import cv2
from keras.models import load_model

import numpy as npdetection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml' emotion_model_path = 'models/ABS_XCEPTION.hdf5' face_detection = cv2.CascadeClassifier(detection_model_path)


emotion_classifier = load_model(emotion_model_path, compile=False) EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised", "neutral"]
cv2.namedWindow('Emotion!') camera = cv2.VideoCapture(0)
while True:
frame = camera.read()[1]
frame = imutils.resize(frame,width=400)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces =
face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=30, 30),flags=cv2.CASCADE_SCALE_IMAGE)
canvas = np.zeros((250, 300, 3), dtype="uint8")

Б қосымшасының жалғасы


frameClone = frame.copy() if len(faces) > 0:


faces = sorted(faces, reverse=True, key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0] (fX, fY, fW, fH) = faces
roi = gray[fY:fY + fH, fX:fX + fW] roi = cv2.resize(roi, (48, 48))
roi = roi.astype("float") / 255.0 roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
preds = emotion_classifier.predict(roi)[0] emotion_probability = np.max(preds)
label = EMOTIONS[preds.argmax()]
for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
#Тексттік белгі құру
text = "{}: {:.2f}%".format ( emotion, prob * 100) w = int ( prob * 300)
cv2.rectangle(canvas, (7, (i * 35) + 5),
(w, (i * 35) + 35), (0, 0, 255), -1)
cv2.putText(canvas, text, (10, (i * 35) + 23),
cv2.FONT_HERSHEY_SIMPLEX, 0.45,
(255, 255, 255), 2)
cv2.putText(frameClone, label, (fX, fY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2) cv2.imshow('Emotion', frameClone)
cv2.imshow("Text Emotion", canvas) if cv2.waitKey(1) & 0xFF == ord('q'):
break camera.release() cv2.destroyAllWindows()



Достарыңызбен бөлісу:
1   ...   15   16   17   18   19   20   21   22   23




©dereksiz.org 2024
әкімшілігінің қараңыз

    Басты бет