利用D415读取 需要标记的人脸face_recognition的距离 Python + wind10
生活随笔
收集整理的這篇文章主要介紹了
利用D415读取 需要标记的人脸face_recognition的距离 Python + wind10
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、下載安裝face_recognition
face_recognition 鏈接
2、文件結構如下
3、上代碼
read_data.py
#-*-coding:utf8-*- import os #import cv2 #import numpy as np import face_recognition from read_img import endwith import scipy.miscdef read_file(path):label_list = []dir_counter = 0img_encoding = [[] for i in range(5)]for child_dir in os.listdir(path):child_path = os.path.join(path, child_dir)for dir_image in os.listdir(child_path):if endwith(dir_image,'jpg'):img = scipy.misc.imread(os.path.join(child_path, dir_image)) # resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) # recolored_img = cv2.cvtColor(resized_img,cv2.COLOR_BGR2GRAY)img_encoding[dir_counter].append(face_recognition.face_encodings(img)[0]) # img_encoding[dir_counter].append(face_recognition.face_encodings(img)[1]) # img_encoding[dir_counter].append(face_recognition.face_encodings(img)[2])label_list.append(dir_counter)dir_counter += 1return img_encoding,label_list,dir_counterdef read_name_list(path):name_list = []for child_dir in os.listdir(path):name_list.append(child_dir)return name_listif __name__ == '__main__':img_list,label_lsit,counter = read_file('./dataset')tt = read_name_list('./dataset')print (img_list)print( tt)read_img.py
import os import cv2def readAllImg(path,*suffix):try:s = os.listdir(path)resultArray = []fileName = os.path.basename(path)resultArray.append(fileName)for i in s:if endwith(i, suffix):document = os.path.join(path, i)img = cv2.imread(document)resultArray.append(img)except IOError:print ("Error")else:print ("讀取成功")return resultArraydef endwith(s,*endstring):resultArray = map(s.endswith,endstring)if True in resultArray:return Trueelse:return Falsecamera_face.py
# -*- coding: utf-8 -*- import face_recognition import cv2 from read_data import read_name_list from read_data import read_file video_capture = cv2.VideoCapture(0) #video_capture = cv2.VideoCapture('images/2.mp4')all_encoding, lable_list, counter = read_file('./dataset') name_list = read_name_list('./dataset') face_locations = [] face_encodings = [] face_names = [] process_this_frame = Truewhile True:ret, frame = video_capture.read()# small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)small_frame = cv2.resize(frame, (0, 0), fx=1, fy=1)if process_this_frame:face_locations = face_recognition.face_locations(small_frame)face_encodings = face_recognition.face_encodings(small_frame, face_locations) # print(face_locations) # print(face_encodings)face_names = []for face_encoding in face_encodings:i = 0j = 0for t in all_encoding:for k in t:match = face_recognition.compare_faces([k], face_encoding)if match[0]:name = name_list[i]j=1i = i+1if j == 0:name = "unknown"face_names.append(name)print(i,j) process_this_frame = not process_this_framefor (top, right, bottom, left), name in zip(face_locations, face_names): # top *= 4 # right *= 4 # bottom *= 4 # left *= 4top *= 1right *= 1bottom *= 1left *= 1cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)cv2.imshow('Video', frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakvideo_capture.release() cv2.destroyAllWindows()rgb_dface.py
import pyrealsense2 as rs import numpy as np import cv2 import face_recognition from read_data import read_name_list from read_data import read_file#video_capture = cv2.VideoCapture(0)#讀取攝像頭,并識別攝像頭中的人臉,進行匹配。all_encoding, lable_list, counter = read_file('./dataset') name_list = read_name_list('./dataset') face_locations = [] face_encodings = [] face_names = [] process_this_frame = True# Configure depth and color streams pipeline = rs.pipeline() config = rs.config() config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)# Start streaming pipeline.start(config) try:while True:# Wait for a coherent pair of frames: depth and colorframes = pipeline.wait_for_frames()depth_frame = frames.get_depth_frame()color_frame = frames.get_color_frame()if not depth_frame or not color_frame:continue# Convert images to numpy arrays 把圖像轉換為numpy datadepth_image = np.asanyarray(depth_frame.get_data())color_image = np.asanyarray(color_frame.get_data())# Apply colormap on depth image (image must be converted to 8-bit per pixel first) 在深度圖上用顏色渲染depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)if process_this_frame:face_locations = face_recognition.face_locations(color_image)face_encodings = face_recognition.face_encodings(color_image, face_locations)print(face_locations)sum=0 # distance=0 # centerdistance=0if face_locations:w=face_locations[0][2]-face_locations[0][0]ll0=face_locations[0][0]+w//2ll2=face_locations[0][2]+w//2cdistance=depth_image[ll0][ll2]cdistance=cdistance/1000print('the center of distace is',cdistance)l0=face_locations[0][0]+w//3l1=face_locations[0][1]-w//3l2=face_locations[0][2]-w//3l3=face_locations[0][3]+w//3w1=w//3for num in range(l3,l1):for i in range(l0,l2):sum=sum+depth_image[num][i].astype(np.float32)# print(num,i) # print(depth_image[num][i] )distance=sum/w1/w1distance=distance/1000print('the object of distance is ',distance)face_names = []#匹配,并賦值for face_encoding in face_encodings:i = 0j = 0for t in all_encoding:for k in t:match = face_recognition.compare_faces([k], face_encoding)if match[0]:name = name_list[i]j=1i = i+1if j == 0:name = "unknown"face_names.append(name)process_this_frame = not process_this_framefor (top, right, bottom, left), name in zip(face_locations, face_names):top *= 1right *= 1bottom *= 1left *= 1cv2.rectangle(color_image, (left, top), (right, bottom), (0, 0, 255), 2)cv2.rectangle(color_image, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(color_image, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)# cv2.imshow('RealSense', depth_colormap)# Stack both images horizontally 把兩個圖片水平拼在一起images = np.hstack((color_image, depth_colormap))# Show images 展示一下圖片cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', images) #key = cv2.waitKey(1)# Press esc or 'q' to close the image windowif key & 0xFF == ord('q') or key == 27:cv2.destroyAllWindows()breakfinally:# Stop streamingpipeline.stop()4.實測圖片
?
歡迎留言討論?
總結
以上是生活随笔為你收集整理的利用D415读取 需要标记的人脸face_recognition的距离 Python + wind10的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Modern UI for WPF的
- 下一篇: 水星路由器上网设置服务器无响应,怎么防止