資源簡介
使用多層CNN卷積神經網絡構建模型,分析人臉的輪廓,將人臉照片數據放入data中進行訓練,并且能夠對人臉的表情進行分析(高興、憤怒、難過、一般)

代碼片段和文件信息
#?/usr/bin/python3
import?cv2
import?numpy?as?np
import?sys
import?tensorflow?as?tf
from?model?import?predict?image_to_tensor?deepnn
CASC_PATH?=?‘./data/haarcascade_files/haarcascade_frontalface_default.xml‘
cascade_classifier?=?cv2.CascadeClassifier(CASC_PATH)
EMOTIONS?=?[‘angry‘?‘disgusted‘?‘fearful‘?‘happy‘?‘sad‘?‘surprised‘?‘neutral‘]
def?format_image(image):
??if?len(image.shape)?>?2?and?image.shape[2]?==?3:
????image?=?cv2.cvtColor(image?cv2.COLOR_BGR2GRAY)
??faces?=?cascade_classifier.detectMultiScale(
????image
????scaleFactor?=?1.3
????minNeighbors?=?5
??)
??#?None?is?no?face?found?in?image
??if?not?len(faces)?>?0:
????return?None?None
??max_are_face?=?faces[0]
??for?face?in?faces:
????if?face[2]?*?face[3]?>?max_are_face[2]?*?max_are_face[3]:
??????max_are_face?=?face
??#?face?to?image
??face_coor?=??max_are_face
??image?=?image[face_coor[1]:(face_coor[1]?+?face_coor[2])?face_coor[0]:(face_coor[0]?+?face_coor[3])]
??#?Resize?image?to?network?size
??try:
????image?=?cv2.resize(image?(48?48)?interpolation=cv2.INTER_CUBIC)
??except?Exception:
????print(“[+}?Problem?during?resize“)
????return?None?None
??return??image?face_coor
def?face_dect(image):
??“““
??Detecting?faces?in?image
??:param?image:?
??:return:??the?coordinate?of?max?face
??“““
??if?len(image.shape)?>?2?and?image.shape[2]?==?3:
????image?=?cv2.cvtColor(image?cv2.COLOR_BGR2GRAY)
??faces?=?cascade_classifier.detectMultiScale(
????image
????scaleFactor?=?1.3
????minNeighbors?=?5
??)
??if?not?len(faces)?>?0:
????return?None
??max_face?=?faces[0]
??for?face?in?faces:
????if?face[2]?*?face[3]?>?max_face[2]?*?max_face[3]:
??????max_face?=?face
??face_image?=?image[max_face[1]:(max_face[1]?+?max_face[2])?max_face[0]:(max_face[0]?+?max_face[3])]
??try:
????image?=?cv2.resize(face_image?(48?48)?interpolation=cv2.INTER_CUBIC)?/?255.
??except?Exception:
????print(“[+}?Problem?during?resize“)
????return?None
??return?face_image
def?resize_image(image?size):
??try:
????image?=?cv2.resize(image?size?interpolation=cv2.INTER_CUBIC)?/?255.
??except?Exception:
????print(“+}?Problem?during?resize“)
????return?None
??return?image
def?draw_emotion():
??pass
def?demo(modelPath?showBox=False):
??x?=?tf.placeholder(tf.float32?[None?2304])
??y_conv?=?deepnn(x)
??probs?=?tf.nn.softmax(y_conv)
??saver?=?tf.train.Saver()
??ckpt?=?tf.train.get_checkpoint_state(modelPath)
??sess?=?tf.Session()
??if?ckpt?and?ckpt.model_checkpoint_path:
????saver.restore(sess?ckpt.model_checkpoint_path)
????print(‘Restore?model?sucsses!!‘)
??feelings_faces?=?[]
??for?index?emotion?in?enumerate(EMOTIONS):
????feelings_faces.append(cv2.imread(‘./data/emojis/‘?+?emotion?+?‘.png‘?-1))
??video_captor?=?cv2.VideoCapture(0)
??emoji_face?=?[]
??result?=?None
??while?True:
????ret?frame?=?video_captor.read()
????detected_face?face_coor?=?format_image(frame)
????if?showBox:
??????if?face_coor?is?not?None:
????????[xywh]?=?face_coor
????????cv2.rectangle(frame?(xy)?(x+wy+h)?(25500
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-01-11?10:45??Facial-ex
?????文件???????35140??2018-01-11?10:45??Facial-ex
?????文件????????1438??2018-01-11?10:45??Facial-ex
?????文件??????629880??2018-01-11?10:45??Facial-ex
?????目錄???????????0??2018-01-11?10:45??Facial-ex
?????目錄???????????0??2018-01-11?10:45??Facial-ex
?????文件???????14628??2018-01-11?10:45??Facial-ex
?????文件???????16186??2018-01-11?10:45??Facial-ex
?????文件???????16175??2018-01-11?10:45??Facial-ex
?????文件???????16613??2018-01-11?10:45??Facial-ex
?????文件???????12094??2018-01-11?10:45??Facial-ex
?????文件???????16397??2018-01-11?10:45??Facial-ex
?????文件???????14663??2018-01-11?10:45??Facial-ex
?????目錄???????????0??2018-01-11?10:45??Facial-ex
?????文件??????341406??2018-01-11?10:45??Facial-ex
?????文件??????930126??2018-01-11?10:45??Facial-ex
?????文件????????3961??2018-01-11?10:45??Facial-ex
?????文件????????1052??2018-01-11?10:45??Facial-ex
?????文件????????4815??2018-01-11?10:45??Facial-ex
?????文件????????4227??2018-01-11?10:45??Facial-ex
- 上一篇:編譯原理 算符優先分析程序
- 下一篇:labview小游戲之登陸月球
評論
共有 條評論