3.2. Neyron to‘rlaridan foydalangan holda hissiyotlarni tanib olish intellektual tizimining algoritmini va foydalanuvchi interfeysini yaratish.
Ushbu dastur python dasturlash tilida VSCode platformasida yaratilgan. Neyron to‘rlaridan foydalangan holda hissiyotlarni tanib olish intellektual tizimi quydagicha qurilgan:
Dastur avvalida bizga kerak bo‘lgan kutubxonalarni yuklab olamiz
from datetime import datetime
from threading import Timer
from keras.models import load_model
from time import sleep
from keras.preprocessing.image import img_to_array
from keras.preprocessing import image
import cv2
import numpy as np
import mediapipe as mp
import time
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askyesno
from tkinter import *
from PIL import ImageTk, Image
import os
import shutil
Dasturni avvalida kerakli Dastur Kegel (https://www.kaggle.com/) kutubxonasidagi haarcascade_frontalface_default.xml faylidan foydalanadi (https://www.kaggle.com/datasets/gxy19980906/haarcascade-frontalface-defaultxml).
face_classifier = cv2.CascadeClassifier(r'C:\Users\Rasulbek\Desktop\Emotion_Detection_CNN-main\haarcascade_frontalface_default.xml')
Bu xml faylidan model.h5 nomli fayl (model) hosil bo‘ladi bizga shu model kerak bo‘ladi.
classifier =load_model(r'C:\Users\Rasulbek\Desktop\Emotion_Detection_CNN-main\model.h5')
Mavjud bo‘lgan hissiyotlar nomini o‘zida saqlovchi emotion_labels nomli massiv olamiz
emotion_labels = ['Jahldor','G\'azab','Qo\'rquv','Xursand','Betaraf(odatiy)', 'Xafa', 'Ajablanish']
Dastur real vaqt rejimida ishlashi uchun u kompyuterning video kamerasiga bog‘lab olamiz
cap = cv2.VideoCapture(0)
Dastur ishga tushgandan so‘ng 20 soniya davomida sizning hissiyotlaringizni o‘rganadi va keyin tizim avtomatik sizning yuz ifodangizni suratga oladi, shu sababli keyinchalik foydalanish uchun myTime nomli o‘zgaruvchi, start_time nomli o‘zgaruvchi olib uni kopyuter vaqtiga bog‘laymiz undan esa time_global nomli o‘zgaruvchisiga vaqtni string ko‘rinishini o‘zlashtiramiz
myTime = 0
start_time = datetime.now()
time_global = int(start_time.strftime('%S'))
Dastur doim ishlab turishi uchun biz while operatoridan foydalanamiz
while True:
_, frame = cap.read()
labels = []
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray)
While ichida ekran hosil qilamiz va video kamerani o‘qiydi, cv2 hamda face_classifier obektini oladi.
start_time = datetime.now()
time_local = int(start_time.strftime('%S'))
if time_global != time_local:
time_global = time_local
myTime += 1
Yuqoridagi kod natijasida bizning myTime nomli o‘zgaruvchimiz qiymati xar bir soniyada birga ortib boradi.
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(127,255,0),1)
cv2.line(frame, (x,y), (x + 25, y),(127,255,0),5)
cv2.line(frame, (x,y), (x, y + 25),(127,255,0),5)
cv2.line(frame, (x + w,y), ((x + w) - 25, y),(127,255,0),5)
cv2.line(frame, (x + w,y), (x + w, y + 25),(127,255,0),5)
cv2.line(frame, (x,y + h), (x + 25, y + h),(127,255,0),5)
cv2.line(frame, (x,y + h), (x, (y + h) - 25),(127,255,0),5)
cv2.line(frame, (x + w,y + h), ((x + w) - 25, y + h),(127,255,0),5)
cv2.line(frame, (x + w,y + h), (x + w, (y + h) - 25),(127,255,0),5)
roi_gray = gray[y:y+h,x:x+w]
roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
Tizim ekranda yuz qiyofasini atrofida yuz shaklidan kelib chiqib to‘g‘ri to‘rtburchak shakl hosil qiladi.
if np.sum([roi_gray])!=0:
roi = roi_gray.astype('float')/255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi,axis=0)
prediction = classifier.predict(roi)[0]
label=emotion_labels[prediction.argmax()]
my_label = label
label_position = (x,y-10)
cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_COMPLEX,1,(255,165,0),2)
if(myTime <= 20): cv2.putText(frame,str(20 - myTime),(x+50,y-50),cv2.FONT_HERSHEY_COMPLEX,1,(255,165,0),2)
else:
cv2.putText(frame,'Yuz aniqlanmadi',(30,80),cv2.FONT_HERSHEY_COMPLEX,1,(255,165,0),2)
cv2.imshow('Hissiyotlarni aniqlash tizimi',frame)
Agar tizim sizning hissiyotlaringizni aniqlasa shunga mos matnni ekranga chiqaradi agar yuzni aniqlay olmasa bu haqda ham xabar beradi.
if (myTime == 20) or (cv2.waitKey(1) & 0xFF == ord('r')):
rn = datetime.now().strftime('%Y') + "."+ datetime.now().strftime('%m') + "." + datetime.now().strftime("%d") + "." + datetime.now().strftime('%H') + "." + datetime.now().strftime('%M') + "." + datetime.now().strftime("%S")
rasm_nomi = "{}.png".format(rn)
cv2.imwrite(rasm_nomi, frame)
Dastur 20 soniyada yoki ‘r’ tugmasini bosilganda tizim avtomatik sizning yuz ifodangizni suratga oladi va bu faylni (yil.oy.kun.soat.minut.sekund.png) formatda rasm hosil qiladi va vaqtinchalik kompyuter xotirasida saqlab turadi.
root = tk.Tk()
root.title('Hissiyotlarni aniqlash tizimi')
root.geometry('600x400')
frame = Frame(root, width=600, height=400)
frame.pack()
frame.place(anchor='center', relx=0.5, rely=0.5)
img = ImageTk.PhotoImage(Image.open(rn + ".png"))
label = Label(frame, image = img)
label.pack()
Olingan surat quydagi dastur kodi natijasida ekranda namoyon bo‘ladi.
def confirm():
answer = askyesno(title=label,
message='Tizim sizning hissiyotlaringizni to\'g\'ri aniqladimi-?')
if answer:
print("{} saqlandi".format(rasm_nomi))
src_path = r"" + rasm_nomi
if(my_label == 'Jahldor'):
dst_path = r"images/validation/angry/"+ rasm_nomi
elif(my_label == 'G\'azab'):
dst_path = r"images/validation/disgust/"+ rasm_nomi
elif(my_label == 'Qo\'rquv'):
dst_path = r"images/validation/fear/"+ rasm_nomi
elif(my_label == 'Xursand'):
dst_path = r"images/validation/happy/"+ rasm_nomi
elif(my_label == 'Betaraf(odatiy)'):
dst_path = r"images/validation/neutral/"+ rasm_nomi
elif(my_label == 'Xafa'):
dst_path = r"images/validation/sad/"+ rasm_nomi
elif(my_label == 'Ajablanish'):
dst_path = r"images/validation/surprise/"+ rasm_nomi
shutil.move(src_path,dst_path)
root.destroy()
else:
os.remove(rn + '.png‘)
root.destroy()
ttk.Button(root, text='Yopish',command=confirm).pack(expand=True)
root.mainloop()
Tizim olgan surati va bunga mos keladigan hissiyotni to‘g‘riligi haqida munosabat bildishingizni so‘raydi. Agar siz tizim aniqlagan hissiyot to‘g‘riligini tasdiqlasangiz tizim bu suratni sizning hissiyotingizga mos keluvchi o‘zining images nomli papkasiga joylaydi va shu bilan o‘zining bazasini kengaytirib boradi, aksincha siz noto‘g‘ri deb hissoblasangiz tizim bu suratni o‘chirib yuboradi.
if cv2.waitKey(1) & 0xFF == ord('x'):
break
cap.release()
cv2.destroyAllWindows()
Do'stlaringiz bilan baham: |