读取摄像头信息并且带上文字
import cv2
from PIL import Image, ImageDraw
import numpy as np
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
ret, frame = capture.read()
image_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(image_pil)
draw.text((100, 100), "press q to quit", fill=(255, 255, 255))
frame = cv2.cvtColor(np.asarray(image_pil), cv2.COLOR_RGB2BGR)
cv2.imshow("capture", frame)
k = cv2.waitKey(1) & 0xff
if k == 27:
cv2.imwrite("../images/out.jpg", frame)
break
capture.release()
将多人照中的每个头像勾勒出来
import face_recognition
from PIL import Image, ImageDraw
image = face_recognition.load_image_file("../images/3.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
image_pil = Image.fromarray(image)
d = ImageDraw.Draw(image_pil)
i = 1
for face_landmarks in face_landmarks_list:
facial_features = [
'chin',
'left_eyebrow',
'right_eyebrow',
'nose_tip',
'left_eye',
'right_eye',
'top_lip',
'bottom_lip'
]
for facial_feature in facial_features:
print("第{}人的{}的位置在{}".format(i, facial_feature, face_landmarks[facial_feature]))
d.line(face_landmarks[facial_feature], width=2)
i = i+1
image_pil.show()
用矩形选框标识人脸
import face_recognition
from PIL import Image
import cv2
image = face_recognition.load_image_file("../images/3.jpg")
locations = face_recognition.face_locations(image)
for location in locations:
top, right, bottom, left = location
print("已识别到人脸部位的像素区域:top:{}, right:{}, bottom:{}, left:{}".format(top, right, bottom, left))
start = (left, top)
end = (right, bottom)
cv2.rectangle(image, start, end, (0, 0, 255), thickness=2)
cv2.imshow('win1', image)
cv2.waitKey()
显示未知图片中的已知头像
import face_recognition
import cv2
known_image = face_recognition.load_image_file("../images/雷子.jpg")
unknown_image = face_recognition.load_image_file("../images/2.jpg")
results = []
known_face_encoding = face_recognition.face_encodings(known_image)[0]
unknown_face_encodings = face_recognition.face_encodings(unknown_image)
face_locations = face_recognition.face_locations(unknown_image)
for i in range(len(face_locations)):
top, right, bottom, left = face_locations[i]
face_image = unknown_image[top:bottom, left:right]
face_encoding = face_recognition.face_encodings(face_image)
if face_encoding:
result = {}
matches = face_recognition.compare_faces([unknown_face_encodings[i]], known_face_encoding, tolerance=0.39)
print(matches)
if True in matches:
print("在未知图片中找到了已知面孔")
result['face_encoding'] = face_encoding
result['isView'] = True
result['location'] = face_locations[i]
result['face_id'] = i+1
results.append(result)
for result in results:
if result['isView']:
print("已知面孔匹配上第{}张脸".format(result['face_id']))
top, right, bottom, left = result['location']
start = (left, top)
end = (right, bottom)
cv2.rectangle(unknown_image, start, end, (0, 0, 255), thickness=2)
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(unknown_image, "yuxinxin", (left+6, bottom+24), font, 1.0, (255, 2555, 255))
cv2.imshow('win1', unknown_image)
cv2.waitKey()
最终的监控系统
import face_recognition
import os
import cv2
from PIL import ImageDraw, Image, ImageFont
import numpy as np
def load_image(path):
print("正在加载已知人员的图片")
for dirpath, dirnames, filenames in os.walk(path):
print(dirpath, dirnames, filenames)
facelib = []
for filename in filenames:
filepath = os.sep.join([dirpath, filename])
face_image = face_recognition.load_image_file(filepath)
face_encodings = face_recognition.face_encodings(face_image)[0]
facelib.append(face_encodings)
return facelib, filenames
facelib, filenames = load_image("./images")
capture = cv2.VideoCapture(0)
while True:
ret, frame = capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(facelib, face_encoding, tolerance=0.3)
print(matches)
face_names = []
name = "未知头像"
if True in matches:
first_match_index = matches.index(True)
name = filenames[first_match_index][:-4]
face_names.append(name)
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
start = (left, top)
end = (right, bottom)
cv2.rectangle(frame, start, end, (0, 0, 255), thickness=2)
image_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
font = ImageFont.truetype("simhei.ttf", 40)
draw = ImageDraw.Draw(image_pil)
draw.text((100, 100), name, font=font, fill=(255, 255, 255))
frame = cv2.cvtColor(np.asarray(image_pil), cv2.COLOR_RGB2BGR)
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xff == ord("q"):
break