簡單人臉識別分析
阿新 • • 發佈:2018-12-15
由於在計算機視覺或者說人臉識別領域,我也是剛瞭解一些,以下都是我看了相關視訊和文章以後得出的個人看法和觀點. 主要有以下幾個步驟: 抽取人臉圖片的主要特徵 進行區域性特徵分析 生成特徵臉 基於彈性模型 隱馬爾可夫模型 1.用pip安裝對應的包 face_recognition,cv2
face_picture.py
# -*- coding: utf-8 -*-
import face_recognition
import cv2
# 讀取圖片
img = face_recognition.load_image_file("/home/zq/Pictures/o_neo.jpg")
# 得到人臉座標
face_locations = face_recognition.face_locations(img)
print(face_locations)
# 顯示原始圖片
img = cv2.imread("/home/zq/Pictures/o_neo.jpg")
cv2.namedWindow("original")
cv2.imshow("original", img)
# 遍歷每個人臉
faceNum = len(face_locations)
for i in range(0, faceNum):
top = face_locations[i][0]
right = face_locations[ i][1]
bottom = face_locations[i][2]
left = face_locations[i][3]
start = (left, top)
end = (right, bottom)
color = (247, 230, 16)
thickness = 2
cv2.rectangle(img, start, end, color, thickness)
# 顯示識別後的圖片
cv2.namedWindow("recognition")
cv2.imshow("recognition", img)
cv2.waitKey( 0)
cv2.destroyAllWindows()
face_video.py
# -*- coding: utf-8 -*-
import face_recognition
import cv2
from gevent import os
import freetype
import copy
from numpy import unicode
class ChineseTextUtil(object):
def __init__(self, ttf):
self._face = freetype.Face(ttf)
def draw_text(self, image, pos, text, text_size, text_color):
'''
使用ttf字型庫中的字型設定姓名
:param image: 用於將text生成在某個image影象上
:param pos: 畫text的位置
:param text: unicode編碼的text
:param text_size: 字型大小
:param text_color:字型顏色
:return: 返回點陣圖
'''
self._face.set_char_size(text_size * 64)
metrics = self._face.size
ascender = metrics.ascender / 64.0
# descender = metrics.descender / 64.0
# height = metrics.height / 64.0
# linegap = height - ascender + descender
ypos = int(ascender)
if not isinstance(text, unicode):
text = text.decode('utf-8')
img = self.string_2_bitmap(image, pos[0], pos[1], text, text_color)
return img
def string_2_bitmap(self, img, x_pos, y_pos, text, color):
'''
將字串繪製為圖片
:param x_pos: text繪製的x起始座標
:param y_pos: text繪製的y起始座標
:param text: text的unicode編碼
:param color: text的RGB顏色編碼
:return: 返回image點陣圖
'''
prev_char = 0
pen = freetype.Vector()
pen.x = x_pos << 6 # div 64
pen.y = y_pos << 6
hscale = 1.0
matrix = freetype.Matrix(int(hscale) * 0x10000L, int(0.2 * 0x10000L), int(0.0 * 0x10000L), int(1.1 * 0x10000L))
cur_pen = freetype.Vector()
pen_translate = freetype.Vector()
image = copy.deepcopy(img)
for cur_char in text:
self._face.set_transform(matrix, pen_translate)
self._face.load_char(cur_char)
kerning = self._face.get_kerning(prev_char, cur_char)
pen.x += kerning.x
slot = self._face.glyph
bitmap = slot.bitmap
cur_pen.x = pen.x
cur_pen.y = pen.y - slot.bitmap_top * 64
self.draw_ft_bitmap(image, bitmap, cur_pen, color)
pen.x += slot.advance.x
prev_char = cur_char
return image
def draw_ft_bitmap(self, img, bitmap, pen, color):
'''
draw each char
:param bitmap: 點陣圖
:param pen: 畫筆
:param color: 畫筆顏色
:return: 返回加工後的點陣圖
'''
x_pos = pen.x >> 6
y_pos = pen.y >> 6
cols = bitmap.width
rows = bitmap.rows
glyph_pixels = bitmap.buffer
for row in range(rows):
for col in range(cols):
if glyph_pixels[row * cols + col] != 0:
img[y_pos + row][x_pos + col][0] = color[0]
img[y_pos + row][x_pos + col][1] = color[1]
img[y_pos + row][x_pos + col][2] = color[2]
if __name__ == '__main__':
# 讀取圖片識別樣例
face_file_list = []
names_list = []
face_encoding_list = []
rootdir = '/Users/z/Desktop/group_face1/'
list = os.listdir(rootdir)
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
if os.path.isfile(path) and ".jpg" in list[i]:
face_file_list.append(rootdir + list[i])
print(list[i][:-4])
names_list.append(list[i][:-4])
for path in face_file_list:
print(path)
face_image = face_recognition.load_image_file(path)
face_encoding = face_recognition.face_encodings(face_image)[0]
face_encoding_list.append(face_encoding)
# 初始化一些變數用於,面部位置,編碼,姓名等
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
video_capture = cv2.VideoCapture(0)
while True:
# 得到當前攝像頭拍攝的每一幀
ret, frame = video_capture.read()
# 縮放當前幀到4分支1大小,以加快識別程序的效率
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# 每次只處理當前幀的視訊,以節省時間
if process_this_frame:
# 在當前幀中,找到所有的面部的位置以及面部的編碼
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# 找到能夠與已知面部匹配的面部
match = face_recognition.compare_faces(face_encoding_list, face_encoding, 0.6)
name = "Unknown"
for i in range(0, len(match)):
if match[i]:
name = names_list[i]
face_names.append(name)
process_this_frame = not process_this_frame
# 顯示結果
for (top, right, bottom, left), name in zip(face_locations, face_names):
# 將剛才縮放至4分支1的幀恢復到原來大小,並得到與每一個面部與姓名的對映關係
top *= 4
right *= 4
bottom *= 4
left *= 4
# 在臉上畫一個框框
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# 在框框的下邊畫一個label用於顯示姓名
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.cv.CV_FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
# 在當前幀中顯示我們識別的結果
color_ = (255, 255, 255)
pos = (left + 6, bottom - 6)
text_size = 24
# 使用自定義字型
ft = ChineseTextUtil('wqy-zenhei.ttc')
image = ft.draw_text(frame, pos, name, text_size, color_)
cv2.imshow('VideoZH', image)
# cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# cv2.imshow('Video', frame)
# 按q退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 釋放資源
video_capture.release()
cv2.destroyAllWindows()
後面還需進一步研究.