当前位置:   article > 正文

tensorflow从0开始(7)——利用tensorflow进行开发的准备工作_tesorflow开发流程

tesorflow开发流程

tensorflow开发流程——表情分析

前期准备

在利用tensorflow做表情分析时,需要很多处理模块进行辅助,由于对这些模块并不熟悉,因此,本文中会针对每个模块进行测试。

CK+数据提取与label标识

本文采用CK+作为数据库,网上可以下载到(自行google)。该数据库是一个基于视频帧的表情库。目前,我们第一版本的表情分析,利用图片作为输入,对这个表情库进行提取,每个人的每种表情提取一张图片并表上标签,python代码如下:

  1. import os
  2. list_tuple=[]
  3. list_filepath=[]
  4. list_label=[]
  5. for root, dir, files in os.walk('/home/beast/Code/emotiondata/cohn-kanade/'):
  6. files_num = len(files)
  7. if files_num > 0:
  8. file_fullpath=os.path.join(root,files[files_num/2])
  9. label = int(file_fullpath.split('_')[1])
  10. list_tuple.append([file_fullpath, label])
  11. list_filepath.append(file_fullpath)
  12. list_label.append(label)
  13. print list_filepath

cohn-kanade即为下载的CK+数据库解压后的存放位置。

opencv读取数据

  • opencv读取图像数据与显示:
  1. import cv2
  2. im = cv2.imread(list_filepath[0])
  3. cv2.namedWindow('emotion')
  4. cv2.imshow('emotion',im)
  5. cv2.waitKey(-1)

显示结果如下:


  • opencv数据转换: 
    opencv读取数据后,数据的排布格式如下:
  1. import cv2
  2. im = cv2.imread(list_filepath[0])
  3. print im.size
  4. print im.shape
  5. print im

显示结果如下: 


opencv读取的图像的格式.png 
图中是rgb的图像,490行640列,每个像素的rgb通道是连续排列的。

  • 利用opencv的数据是可以直接初始化tensorflow中的tensor的,但是能不能直接使用,这是后话,测试代码如下:
  1. import tensorflow as tf
  2. flags = tf.app.flags
  3. FLAGS = flags.FLAGS
  4. flags.DEFINE_string('summaries_dir', './tf_logs', 'Summaries directory')
  5. t1=tf.constant(im)
  6. t2=tf.Variable(im)
  7. print t1
  8. print t2
  9. with tf.Session() as sess:
  10. sess.run(tf.initialize_all_variables())
  11. merged = tf.merge_all_summaries()
  12. train_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/', sess.graph)

显示结果如下: 

  • 利用numpy将opencv提取出来的数据,针对某一个分量(如R),单独提取出来,代码如下:
  1. import numpy
  2. import cv2
  3. im_r = im[:,:,0].astype(numpy.float32)/255
  4. print im_r.shape
  5. print im_r

numpy的数据格式,也是可以直接用来初始化tensorflow变量的。

python中glob的使用

可以用来提取文件:

  1. import glob
  2. for i in glob.glob('/home/beast/Code/emotiondata/cohn-kanade/S010/001/*.png'):
  3. print i

python中zip的使用

zip用来合并两个list,实例代码如下:

python dlib的使用

  1. import sys
  2. import os
  3. import dlib
  4. import glob
  5. from skimage import io
  6. predictor_path = '/home/beast/Code/model/shape_predictor_68_face_landmarks.dat'
  7. faces_folder_path = '/home/beast/Code/Pic/haijun'
  8. detector = dlib.get_frontal_face_detector()
  9. predictor = dlib.shape_predictor(predictor_path)
  10. win = dlib.image_window()
  11. for f in glob.glob(os.path.join(faces_folder_path, "*.png")):
  12. print("Processing file: {}".format(f))
  13. img = io.imread(f)
  14. win.clear_overlay()
  15. win.set_image(img)
  16. # Ask the detector to find the bounding boxes of each face. The 1 in the
  17. # second argument indicates that we should upsample the image 1 time. This
  18. # will make everything bigger and allow us to detect more faces.
  19. dets = detector(img, 1)
  20. print("Number of faces detected: {}".format(len(dets)))
  21. for k, d in enumerate(dets):
  22. print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
  23. k, d.left(), d.top(), d.right(), d.bottom()))
  24. # Get the landmarks/parts for the face in box d.
  25. shape = predictor(img, d)
  26. print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
  27. shape.part(1)))
  28. # Draw the face landmarks on the screen.
  29. win.add_overlay(shape)
  30. win.add_overlay(dets)
  31. dlib.hit_enter_to_continue()

opencv和dlib结合使用

经验证在python中,opencv和dlib的数据结构是可以通用的,本例中,利用opencv开启摄像头采集数据,利用dlib进行人脸的检测以及人脸关键点的检测。该示例的目的是能够在视频中检测出人脸并作为机器学习的输入,python代码如下:

  • 单张图片的dlib检测代码如下:
  1. import sys
  2. import os
  3. import dlib
  4. import glob
  5. from skimage import io
  6. predictor_path = '/home/beast/Code/model/shape_predictor_68_face_landmarks.dat'
  7. faces_folder_path = '/home/beast/Code/Pic/haijun'
  8. detector = dlib.get_frontal_face_detector()
  9. predictor = dlib.shape_predictor(predictor_path)
  10. win = dlib.image_window()
  11. for f in glob.glob(os.path.join(faces_folder_path, "*.png")):
  12. print("Processing file: {}".format(f))
  13. img = io.imread(f)
  14. win.clear_overlay()
  15. win.set_image(img)
  16. # Ask the detector to find the bounding boxes of each face. The 1 in the
  17. # second argument indicates that we should upsample the image 1 time. This
  18. # will make everything bigger and allow us to detect more faces.
  19. dets = detector(img, 1)
  20. print("Number of faces detected: {}".format(len(dets)))
  21. for k, d in enumerate(dets):
  22. print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
  23. k, d.left(), d.top(), d.right(), d.bottom()))
  24. # Get the landmarks/parts for the face in box d.
  25. shape = predictor(img, d)
  26. print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
  27. shape.part(1)))
  28. # Draw the face landmarks on the screen.
  29. win.add_overlay(shape)
  30. win.add_overlay(dets)
  31. dlib.hit_enter_to_continue()

显示结果如下: 


dlib对图片的人脸关键点定位.png

  • 基于视频的人脸关键点检测代码如下:
  1. import numpy as np
  2. import cv2
  3. import cv2.cv as cv
  4. from video import create_capture
  5. from common import clock, draw_str
  6. import dlib
  7. predictor_path = '/home/beast/Code/model/shape_predictor_68_face_landmarks.dat'
  8. detector = dlib.get_frontal_face_detector()
  9. predictor = dlib.shape_predictor(predictor_path)
  10. win = dlib.image_window()
  11. cam = create_capture(0, fallback='synth:bg=../cpp/lena.jpg:noise=0.05')
  12. while True:
  13. ret, img = cam.read()
  14. dets = detector(img, 1)
  15. print("Number of faces detected: {}".format(len(dets)))
  16. win.clear_overlay()
  17. for k, d in enumerate(dets):
  18. print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
  19. k, d.left(), d.top(), d.right(), d.bottom()))
  20. # Get the landmarks/parts for the face in box d.
  21. shape = predictor(img, d)
  22. print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
  23. shape.part(1)))
  24. # Draw the face landmarks on the screen.
  25. win.add_overlay(shape)
  26. win.set_image(img)
  27. win.add_overlay(dets)
  28. dlib.hit_enter_to_continue()
  29. if 0xFF & cv2.waitKey(5) == 27:
  30. break
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/123735
推荐阅读
相关标签
  

闽ICP备14008679号