def predict_emotion(self, detected_face): ''' 预测表情,返回一个形状为(7, )的向量 ''' tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) return result
def facial_detact(modelPath, showBox=True): face_x = tf.placeholder(tf.float32, [None, 2304]) y_conv = deepnn(face_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!!\n\nNOTE: Press SPACE on keyboard to capture face.' ) 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: [x, y, w, h] = face_coor cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) if cv2.waitKey(1) & 0xFF == ord(' '): if detected_face is not None: cv2.imwrite('a.jpg', detected_face) tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) print(EMOTIONS[np.argmax(result[0])]) if result is not None: for index, emotion in enumerate(EMOTIONS): cv2.putText(frame, emotion, (10, index * 20 + 20), cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 255, 0), 1) cv2.rectangle(frame, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1) emoji_face = feelings_faces[np.argmax(result[0])] for c in range(0, 3): frame[200:320, 10:130, c] = emoji_face[:, :, c] * ( emoji_face[:, :, 3] / 255.0) + frame[200:320, 10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0) cv2.imshow('face', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_captor.release() cv2.destroyAllWindows()
def demo(modelPath, showBox=True): face_x = tf.placeholder(tf.float32, [None, 2304]) y_conv = deepnn(face_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) feelings_faces = [] for index, emotion in enumerate(EMOTIONS): feelings_faces.append( cv2.imread('./data/emojis/' + emotion + '.png', -1)) emoji_face = [] img = input("Enter the image file name: ") while True: frame = cv2.imread(img) detected_face, face_coor = format_image(frame) if face_coor is not None: [x, y, w, h] = face_coor cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) result = None if detected_face is not None: cv2.imwrite('face.jpg', detected_face) tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) if result is not None: for index, emotion in enumerate(EMOTIONS): cv2.putText(frame, emotion, (10, index * 20 + 20), cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 255, 0), 1) cv2.rectangle(frame, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1) emoji_face = feelings_faces[np.argmax(result[0])] for c in range(0, 3): frame[200:320, 10:130, c] = emoji_face[:, :, c] * ( emoji_face[:, :, 3] / 255.0) + frame[200:320, 10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0) else: comment = 'We cannot detect a face....Too bad!' cv2.putText(frame, comment, (115, 250), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 1) cv2.imwrite('result1.jpg', frame) cv2.namedWindow('Guess how I feel', cv2.WINDOW_NORMAL) cv2.resizeWindow('Guess how I feel', 450, 600) cv2.imshow('Guess how I feel', frame) k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video if k == 27: break
def demo(modelPath, showBox=False,SAMPLE_IMAGE_PATH=SAMPLE_IMAGE): face_x = tf.placeholder(tf.float32, [None, 2304]) y_conv = deepnn(face_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) result = None frame = SAMPLE_IMAGE_PATH detected_face, face_coor = format_image(frame) #face Coordinates if showBox: if face_coor is not None: [x,y,w,h] = face_coor cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0), 2) if detected_face is not None: cv2.imwrite('temp.jpg', detected_face) tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) print(result) jsonString='{' maximum=0.00 FINAL_EMOTION='' if result is not None: for index, emotion in enumerate(EMOTIONS): if(index!=0): jsonString+=',' percentage= float("{0:.2f}".format(result[0][index]*100)) if(percentage==0.0): percentage=0 jsonString+='"'+str(emotion)+'":'+str(percentage) if(maximum<percentage): maximum=copy.copy(percentage) FINAL_EMOTION=emotion print('--------------------------------') print(' ') print(str(emotion)+" : "+str(percentage)+"%") print(' ') print('=========================================================') print('The Result for Input Image is : '+str(FINAL_EMOTION)+" ( "+str(maximum)+"% )") print('=========================================================') jsonString+='}' return jsonString
def stdface_expression_recognition(stdface): """ 直接识别stdface中人物的表情 :param stdface: 传入的stdface :return: 返回值1为表情id(int) 返回值2位表情指数数组(ndarray) """ # 先对stdface进行format操作 再将其拉直变为一维张量 tensor = image_to_tensor(format_image(stdface)) # 将图片转为的一维张量输入到神经网络中得到输出 result = sess.run(probs, feed_dict={face_x: tensor}) # 表情识别结果为指数最大的那个 expression_id = np.argmax(result[0]) return expression_id, result[0]
def demo(modelPath, showBox=False): face_x = tf.placeholder(tf.float32, [None, 2304]) y_conv = deepnn(face_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!!\nNOTE: Press SPACE on keyboard to capture face.') 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: [x,y,w,h] = face_coor cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0), 2) if cv2.waitKey(1) & 0xFF == ord(' '): if detected_face is not None: cv2.imwrite('a.jpg', detected_face) tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) # print(result) if result is not None: for index, emotion in enumerate(EMOTIONS): cv2.putText(frame, emotion, (10, index * 20 + 20), cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 255, 0), 1) cv2.rectangle(frame, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1) emoji_face = feelings_faces[np.argmax(result[0])] for c in range(0, 3): frame[200:320, 10:130, c] = emoji_face[:, :, c] * (emoji_face[:, :, 3] / 255.0) + frame[200:320, 10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0) cv2.imshow('face', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
def main(): #create RKNN object rknn = RKNN(verbose=True) #Direct Load RKNN Model rknn.load_rknn('./emotion.rknn') #만들어진 rknn을 로드 print('--> load success') #성공 메세지 출력 result = None #이미지 읽기 input_image = cv2.imread('./data/image/happy.jpg', cv2.IMREAD_COLOR) #esize한 이미지, 가장 큰 얼굴 object detected_face, face_coor = format_image(input_image) #탐지된 이미지가 있다면, if detected_face is not None: #image를 tenxor로 변환 & float32로 변환 (rknn이 float64는 지원하지 않음) "tensor 사이즈는 (1,2304), detected_face는 48X48" tensor = image_to_tensor(detected_face).astype(np.float32) #init runtime environment print('--> Init runtime environment') ret = rknn.init_runtime() #오류 메세지 출력 if ret != 0: print('Init runtime environment failed') #rknn 모델 실행 result = rknn.inference(inputs=[tensor]) print('run success') #list를 array로 변환 #result는 감정 예측 배열 result = np.array(result) #result가 존재하면 if result is not None: #감정 배열이 7개의 값을 가지므로 range(7)의 범위를 가짐 for i in range(7): #감정 배열 중 1인 값이 있다면, if result[0][0][i] == 1: #감정 예측 메세지 출력 print('당신의 감정은 ' + EMOTIONS[i] + '입니다.')
def expression_recognition(image): """ 识别图像中人物的表情 :param image: 传入的图像 :return: 返回值1为表情id(int) 返回值2位表情指数数组(ndarray) """ stdface = Faces.get_stdface(image) if stdface is None: return None, None # 先对stdface进行format操作 再将其拉直变为一维张量 tensor = image_to_tensor(format_image(stdface)) # 将图片转为的一维张量输入到神经网络中得到输出 result = sess.run(probs, feed_dict={face_x: tensor}) # 表情识别结果为指数最大的那个 expression_id = np.argmax(result[0]) return expression_id, result[0]
def demo(modelPath, showBox=False): face_x = tf.compat.v1.placeholder(tf.float32, [None, 2304]) y_conv = deepnn(face_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!!\nNOTE: Press SPACE on keyboard to capture face.' ) video_path = "test.mp4" cap = cv2.VideoCapture(video_path) fps = cap.get(cv2.CAP_PROP_FPS) # 获取视频的帧率 size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) # 获取视频的大小 fourcc = cv2.VideoWriter_fourcc(*'mpeg') # 要保存的视频格式 # 把处理过的视频保存下来 output_viedo = cv2.VideoWriter() # 保存的视频地址 video_save_path = 'trans.mp4' output_viedo.open(video_save_path, fourcc, fps, size, True) result = None while True: ret, frame = cap.read() detected_face, face_coor = format_image(frame) if showBox: if face_coor is not None: specify(face_coor, frame) if detected_face is not None: tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) print(result) output_viedo.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
def demo(modelPath, showBox=True): face_x = tf.placeholder(tf.float32, [None, 2304]) y_conv = deepnn(face_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!!\nNOTE: Press SPACE on keyboard to capture face.' ) feelings_faces = [] for index, emotion in enumerate(EMOTIONS): feelings_faces.append( cv2.imread('./data/emojis/' + emotion + '.png', -1)) # video_captor = cv2.VideoCapture(0) demo_image = cv2.imread('./demo_image.jpg') detected_face, face_coor = format_image(demo_image) if showBox: if face_coor is not None: [x, y, w, h] = face_coor cv2.rectangle(demo_image, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.imshow('demo_image', demo_image) cv2.waitKey(0) cv2.destroyAllWindows() if detected_face is not None: cv2.imwrite('a.jpg', detected_face) tensor = image_to_tensor(detected_face) result = sess.run(y_conv, feed_dict={face_x: tensor}) print('result.shape: ', result.shape) print('result: ', result) most_likely_index = np.argmax(result) # print(most_likely_index) print(EMOTIONS[most_likely_index]) sess.close()
def process_image(ip_img): img=ip_img img_bkup = img alignedFaces = [] img_matlab = np.copy(img) tmp = img_matlab[:,:,2].copy() img_matlab[:,:,2] = img_matlab[:,:,0] img_matlab[:,:,0] = tmp # Getting the bounding box and the landmarks and drawing the bounding box around the image boundingboxes, points = detect_face(img_matlab, minsize, PNet, RNet, ONet, threshold, False, factor) x1 = boundingboxes[:,0] #print(x1) # Code for aligning the detected faces imgSize = (112, 96) # This part is needed by the DeepFace code x_ = [30.2946, 65.5318, 48.0252, 33.5493, 62.7299] y_ = [51.6963, 51.5014, 71.7366, 92.3655, 92.2041] src = np.array( zip(x_, y_) ).astype(np.float32).reshape(1,5,2) out = None emotion = "" features = [] name_dict_arr = [] for i in range(0, len(x1)): # Drawing the landmarks x = points[i][0:5] y = points[i][5:10] # Code for alginign the faces dst = np.array( zip(x, y) ).astype(np.float32).reshape(1,5,2) transmat = cv2.estimateRigidTransform( dst, src, False ) if(transmat is not None): sys.stdout.flush() print "Person found" out = cv2.warpAffine(img_bkup, transmat, (imgSize[1], imgSize[0])) # DEBUG cv2.imwrite("new"+str(count_num)+".jpg", out) alignedFaces.append(out) if INFERENCE: npstore = out npstore = npstore.astype(float) npstore = (npstore - 127.5)/128 npstore = np.expand_dims(npstore, axis=0) npstore = np.swapaxes(npstore,1,3) npstore = np.swapaxes(npstore,2,3) center_facenet.blobs['data'].data[...] = npstore center_facenet.forward() features.append(copy.deepcopy(center_facenet.blobs["fc5"].data[0])) # Face expression part if EXPRESSION_DETECTION_ENABLED: face_coor = np.ceil(boundingboxes[i]).astype(int) image = img[face_coor[1]:face_coor[3], face_coor[0]:face_coor[2]] image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #print("coord size", len(face_coor)) try: image = cv2.resize(image, (48, 48), interpolation=cv2.INTER_CUBIC) import scipy.misc scipy.misc.imsave('outfile.jpg', image) except Exception: continue tensor = image_to_tensor(image) result = sess.run(probs, feed_dict={face_x: tensor}) print("debug", result, np.argmax(result[0])) emotion = EMOTIONS[np.argmax(result[0])] else: print "none detected" return alignedFaces, boundingboxes, features, emotion
# Check if confidence is less them 100 ==> "0" is perfect match if (confidence < 100): id = names[id] confidence = " {0}%".format(round(100 - confidence)) else: id = "unknown" confidence = " {0}%".format(round(100 - confidence)) cv2.putText(frame, str(id), (x + 5, y - 5), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 0), 2) cv2.putText(frame, str(confidence), (x + 5, y + h - 5), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 255, 255), 1) if cv2.waitKey(1) & 0xFF == ord(' '): result = None if detected_face is not None: tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) if result is not None: for index, emotion in enumerate(EMOTIONS): cv2.putText(frame, emotion, (10, index * 20 + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 1) cv2.rectangle(frame, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1) emoji_face = feelings_faces[np.argmax(result[0])] for c in range(0, 3): frame[200:320, 10:130, c] = emoji_face[:, :, c] * ( emoji_face[:, :, 3] / 255.0) + frame[200:320, 10:130, c] * ( 1.0 - emoji_face[:, :, 3] / 255.0) else: comment = 'We cannot detect your emotion!'
def demo(modelPath, showBox=False): face_x = tf.placeholder(dtype=tf.float32, name='inputs', shape=[None, 2304]) #parser = argparse.ArgumentParser() #parser.add_argument("--fz_model_fn",default = "./emotion_model_frozen.pb",type=str,help="Frozen model file to import") #args = parser.parse_args() #graph = load_graph(args.fz_model_fn) #for op in graph.get_operations(): #print(op.name,op.values()) #x = graph.get_tensor_by_name('prefix/inputs:0') #probs = graph.get_tensor_by_name('prefix/output_node:0') y_conv = deepnn(face_x) probs = tf.nn.softmax(y_conv, name='output_node') print("probs",probs) 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!!\nNOTE: Press SPACE on keyboard to capture face.') 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: [x,y,w,h] = face_coor cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0), 2) if cv2.waitKey(1): if detected_face is not None: cv2.imwrite('a.jpg', detected_face) tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) print(result) print("probs",probs) tf.train.write_graph(sess.graph_def, './', "nn_model.pbtxt", as_text=True) for op in tf.get_default_graph().get_operations(): print(op.name) # print(result) if result is not None: for index, emotion in enumerate(EMOTIONS): cv2.putText(frame, emotion, (10, index * 20 + 20), cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 255, 0), 1) cv2.rectangle(frame, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1) emoji_face = feelings_faces[np.argmax(result[0])] for c in range(0, 3): frame[200:320, 10:130, c] = emoji_face[:, :, c] * (emoji_face[:, :, 3] / 255.0) + frame[200:320, 10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0) cv2.imshow('face', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
def demo(modelPath, showBox=False): face_x = tf.placeholder(tf.float32, [None, 2304]) y_conv = deepnn(face_x) probs = tf.nn.softmax(y_conv) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(modelPath) sess = tf.Session() # Get the model if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print('Restore model sucsses!!') # print('NOTE: Press SPACE on keyboard to capture face.') 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() # time.sleep(0.1) detected_face, face_coor = format_image(frame) if showBox: if face_coor is not None: [x, y, w, h] = face_coor cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # if cv2.waitKey(1) & 0xFF == ord(' '): # Not using because it calls the model again and again if detected_face is not None: # cv2.imwrite('a.jpg', detected_face) tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) # print(result) if result is not None: for index, emotion in enumerate(EMOTIONS): # Label(emotion) display cv2.putText(frame, emotion, (10, index * 20 + 20), cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 255, 0), 1) # Histogram display cv2.rectangle(frame, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1) # emoji<-emotion of the highest score emoji_face = feelings_faces[np.argmax(result[0])] # Emoji(120*120) display for c in range(0, 3): frame[200:320, 10:130, c] = emoji_face[:, :, c] * ( emoji_face[:, :, 3] / 255.0) + frame[200:320, 10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0) # Real-time capture cv2.imshow('face', frame) if cv2.waitKey(1) & 0xFF == ord(' '): cv2.imwrite('fer_result/' + str(uuid.uuid4().hex) + '.png', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
def demo(modelPath, showBox=True): face_x = tf.placeholder(tf.float32, [None, 2304]) y_conv = deepnn(face_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!!\nNOTE: Press SPACE on keyboard to capture face.' ) # feelings_faces = [] # for index, emotion in enumerate(EMOTIONS): # num = random.randint(1,3) # feelings_faces.append(cv2.imread('./data/emojis/' + emotion + str(num) + '.jpg', -1)) # emoji_face = [] result = None video_captor = cv2.VideoCapture(0) while True: ret, frame = video_captor.read() detected_face, face_coor = format_image(frame) window_name = 'Face Expression Recognition' cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN) cv2.moveWindow(window_name, 0, 0) if showBox: if face_coor is not None: [x, y, w, h] = face_coor cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) ratio = 0.8 frame = cv2.resize(frame, None, fx=ratio, fy=ratio, interpolation=cv2.INTER_CUBIC) move_dx = (1 - ratio) * 1280 move_dy = (1 - ratio) * 360 M = np.float32([[1, 0, move_dx], [0, 1, move_dy]]) new_height, new_width, _ = frame.shape frame = cv2.warpAffine(frame, M, (1280, 720)) cv2.rectangle(frame, (0, 0), (int(move_dx), 720), (255, 255, 255), -1) if cv2.waitKey(10) & 0xFF == ord(' '): if detected_face is not None: cv2.imwrite('a.jpg', detected_face) tensor = image_to_tensor(detected_face) result = sess.run(probs, feed_dict={face_x: tensor}) # print(result) if result is not None: for index, emotion in enumerate(EMOTIONS): cv2.putText(frame, emotion, (10, index * 20 + 100), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), 1) cv2.rectangle(frame, (130, index * 20 + 90), (130 + int(result[0][index] * 100), (index + 1) * 20 + 84), (255, 0, 0), -1) emotion_type = EMOTIONS[np.argmax(result[0])] prob = float(np.max(result[0]) * 100) cv2.putText(frame, emotion_type, (20, 490), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 1) cv2.putText(frame, str('%.2f' % prob + "%"), (150, 490), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 1) # emoji_face = feelings_faces[np.argmax(result[0])] # print("1") # for c in range(0, 3): # frame[200:320, 10:130, c] = emoji_face[:, :, c] * (emoji_face[:, :, 3] / 255.0) + frame[200:320, 10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0) detected_img = cv2.imread('a.jpg', 0) detected_img = cv2.resize(detected_img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) for c in range(0, 3): frame[340:436, int(move_dx / 2) - 48:int(move_dx / 2) + 48, c] = detected_img cv2.imshow(window_name, frame) if cv2.waitKey(10) & 0xFF == ord('q'): break
def demo(modelPath, showBox=False): face_x = tf.placeholder(tf.float32, [None, 2304]) # 定义一个TensorFlow的占位符 y_conv = deepnn(face_x) # 输出一个维度为(1*7)的结果 probs = tf.nn.softmax(y_conv) # 使用softmax激活函数将结果计算为哪种表情的概率 saver = tf.train.Saver() # 定义一个Saver对象 ckpt = tf.train.get_checkpoint_state( modelPath) # 得到check_point路径,生成一个check_point对象 sess = tf.Session() # 定义一个TensorFlow的对话,在对话中去执行代码和训练模型 if ckpt and ckpt.model_checkpoint_path: # 如果check_point对象存在,并且check_point的路径存在 saver.restore( sess, ckpt.model_checkpoint_path) # 保存Session对话 这一行代码可能实在训练完之后才会执行 print( 'Restore model sucsses!!\nNOTE: Press SPACE on keyboard to capture face.' ) feelings_faces = [] for index, emotion in enumerate(EMOTIONS): # 将EMOTION 转换为枚举类型后提取 索引和值 feelings_faces.append( cv2.imread('./data/emojis/' + emotion + '.png', -1)) # cv2.imread()读取emojis文件夹下的表情图像转化为二进制数据存储到felling_facs中 video_captor = cv2.VideoCapture(0) # CV2.VideoCapture()打开摄像头获取照片 按空格拍照 emoji_face = [] result = None while True: ret, frame = video_captor.read() # 读取拍到的照片转化为二进制数据 存储到frame中 detected_face, face_coor = format_image( frame) # format_image()函数定义在 14 行,得到脸部图像和坐标 if showBox: if face_coor is not None: [x, y, w, h] = face_coor cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) if cv2.waitKey(1) & 0xFF == ord(' '): # 不知道干嘛用的,以后再说 if detected_face is not None: cv2.imwrite('a.jpg', detected_face) # 保存脸的图像 tensor = image_to_tensor(detected_face) # 将图像转化为一个48*48的0到255灰度图像并转化为np.array数据 result = sess.run(probs, feed_dict={ face_x: tensor }) # 运行TensorFlow模型,计算表情的概率,返回模型训练结果 # print(result) if result is not None: for index, emotion in enumerate(EMOTIONS): cv2.putText(frame, emotion, (10, index * 20 + 20), cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 255, 0), 1) # 将人脸的表情的文字添加到人脸图片上, 参数是什么以后站再说 cv2.rectangle(frame, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1) emoji_face = feelings_faces[np.argmax(result[0])] for c in range(0, 3): frame[200:320, 10:130, c] = emoji_face[:, :, c] * ( emoji_face[:, :, 3] / 255.0) + frame[200:320, 10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0) cv2.imshow('face', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
import model # import graph if __name__ == '__main__': if len(sys.argv) >= 2: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") encoder = model.encoder_load("models/encoder.pth") encoder.eval() # graph.show(encoder) encoder.to(device) decoder = model.decoder_load("models/decoder.pth") # graph.show(decoder) decoder.eval() decoder.to(device) img = Image.open(sys.argv[1]) # x -> z -> y x = model.image_to_tensor(img).to(device) z = encoder(x) y = decoder(z) y = torch.Tensor(y.cpu()) y.clamp_(0, 1) img = model.image_from_tensor(y) img.show()