瞳孔检测眼动追踪python实现(基于dlib)

2023-12-16 14:04

本文主要是介绍瞳孔检测眼动追踪python实现(基于dlib),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果展示:
原图:(图片来自 b站up 借我300去洗牙)
在这里插入图片描述

dlib实现的特征点检测
在这里插入图片描述
瞳孔检测结果

完整代码:

# encoding:utf-8import dlib
import numpy as np
import cv2def rect_to_bb(rect): # 获得人脸矩形的坐标信息x = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - yreturn (x, y, w, h)def shape_to_np(shape, dtype="int"): # 将包含68个特征的的shape转换为numpy array格式coords = np.zeros((68, 2), dtype=dtype)for i in range(0, 68):coords[i] = (shape.part(i).x, shape.part(i).y)return coordsdef resize(image, width=1200):  # 将待检测的image进行resizer = width * 1.0 / image.shape[1]dim = (width, int(image.shape[0] * r))resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)return resizeddef feature():image_file = r"F:\project_python\facenet\2.PNG"detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor(r"F:\project_python\facenet\shape_predictor_68_face_landmarks.dat")image = cv2.imread(image_file)image = resize(image, width=1200)gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)rects = detector(gray, 1)shapes = []for (i, rect) in enumerate(rects):shape = predictor(gray, rect)shape = shape_to_np(shape)shapes.append(shape)(x, y, w, h) = rect_to_bb(rect)cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)cv2.putText(image, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)count1 = 0count2 = 0# encoding:utf-8import dlib
import numpy as np
import cv2def rect_to_bb(rect): # 获得人脸矩形的坐标信息x = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - yreturn (x, y, w, h)def shape_to_np(shape, dtype="int"): # 将包含68个特征的的shape转换为numpy array格式coords = np.zeros((68, 2), dtype=dtype)for i in range(0, 68):coords[i] = (shape.part(i).x, shape.part(i).y)return coordsdef resize(image, width=1200):  # 将待检测的image进行resizer = width * 1.0 / image.shape[1]dim = (width, int(image.shape[0] * r))resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)return resizeddef feature():image_file = r"F:\project_python\facenet\2.PNG"detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor(r"F:\project_python\facenet\shape_predictor_68_face_landmarks.dat")image = cv2.imread(image_file)image = resize(image, width=1200)gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)rects = detector(gray, 1)shapes = []for (i, rect) in enumerate(rects):shape = predictor(gray, rect)shape = shape_to_np(shape)shapes.append(shape)(x, y, w, h) = rect_to_bb(rect)cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)cv2.putText(image, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)count1 = 0count2 = 0image1=image.copy()eyes=[]eyes2=[]for shape in shapes:left_eye=[]right_eye=[]left_eye2=[]right_eye2=[]for (x, y) in shape:cv2.circle(image1, (x, y), 2, (0, 0, 255), -1)cv2.putText(image1, str(count1)+"-"+str(count2), (x-3, y-3), cv2.FONT_HERSHEY_COMPLEX, 0.4, (100, 200, 200), 1)if count2>=36 and count2<=41:left_eye.append([x,y])left_eye2.append((x,y))elif count2>=42 and count2<=47:right_eye.append([x,y])right_eye2.append((x,y))count2+=1count1+=1    eyes.append([left_eye,right_eye])eyes2.append([left_eye2,right_eye2])cv2.imshow("Output", image1)cv2.waitKey(0)image2=image.copy()for i in range(len(eyes)):e=eyes[i]for j in range(len(e)):points=e[j]# 六边形的顶点坐标pts = np.array(points, np.int32)pts = pts.reshape((-1, 1, 2))# 创建一个空白图像作为掩模mask = np.zeros_like(image)# 在掩模上绘制填充了白色的六边形cv2.fillPoly(mask, [pts], (255, 255, 255))# 创建一个白色背景图像white_background = np.ones_like(image) * 255# 在白色背景上绘制填充了原始图像的六边形cv2.fillPoly(white_background, [pts], (0, 0, 0))# 将原始图像和白色背景图像按位取反inverse_mask = cv2.bitwise_not(mask)# 将原始图像中六边形内的部分与白色背景中六边形外的部分相结合result = cv2.bitwise_and(image, mask) + cv2.bitwise_and(white_background, inverse_mask)# 显示最终结果cv2.imshow('Result', result)cv2.waitKey(0)# 找到最小长方形的左上角和右下角坐标min_x = min(point[0] for point in points)max_x = max(point[0] for point in points)min_y = min(point[1] for point in points)max_y = max(point[1] for point in points)cv2.rectangle(image2, (min_x, min_y), (max_x, max_y), (0, 255, 255), 2)# 获取最小长方形中的图像部分roi = result[min_y:max_y, min_x:max_x]# 将图像部分转换为灰度图gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)# define a threshold, 128 is the middle of black and white in grey scalethresh = 100# assign blue channel to zerosimg_binary = cv2.threshold(gray_roi, thresh, 255, cv2.THRESH_BINARY)[1]cv2.imshow("Output", img_binary)cv2.waitKey(0)#处理眼睛反光形成的白点# 获取图像高度img_binary1=img_binary.copy()height = img_binary1.shape[0]# 遍历每一列像素for col in range(img_binary1.shape[1]):white_run_length = 0start_index = 0end_index = 0for row in range(height):if img_binary1[row, col] == 255:  # 白色像素if white_run_length == 0:start_index = rowwhite_run_length += 1end_index = rowelse:  # 黑色像素if white_run_length > 0 and white_run_length < height / 2 and (start_index == 0 or img_binary1[start_index - 1, col] == 0) and (end_index == height - 1 or img_binary1[end_index + 1, col] == 0):img_binary1[start_index:end_index + 1, col] = 0  # 将连续的白色像素改为黑色像素white_run_length = 0cv2.imshow("Output", img_binary1)cv2.waitKey(0)# 计算每一列的像素值总和column_sums = np.sum(img_binary1, axis=0)# 计算每一列与相邻的n列的总和neighbor_sums = np.convolve(column_sums, np.ones(1), mode='valid')# 找到结果最小的n列的列号min_indices = np.argpartition(neighbor_sums, 20)[:20]# 将列号按数字排序sorted_indices = np.sort(min_indices)# 找到这7列的最中间的列的列号middle_column = sorted_indices[len(sorted_indices) // 2]print("每一列与相邻的6列的总和:", neighbor_sums)print("结果最小的7列的列号(按数字排序):", sorted_indices)print("这7列的最中间的列的列号:", middle_column)cv2.line(image2, (min_x+middle_column, min_y), (min_x+middle_column, max_y), (255, 0, 0))# 找出结果最小的列min_column = img_binary[:, middle_column]# 找出连续相邻的白色像素点white_pixels = np.where(min_column == 255)[0]# 找出连续相邻的白色像素点中最长的一段longest_start = 0longest_end = 0max_length = 0current_start = 0current_length = 0for i in range(1, len(white_pixels)):if white_pixels[i] == white_pixels[i-1] + 1:current_length += 1else:if current_length > max_length:max_length = current_lengthlongest_start = white_pixels[current_start]longest_end = white_pixels[i-1]current_start = icurrent_length = 0# 判断起始位置和结束位置的中点在图像的上半部分还是下半部分mid_point = (longest_start + longest_end) // 2height = img_binary.shape[0]if mid_point < height // 2:print("最长的白色像素段起始位置:", longest_start)print("最长的白色像素段结束位置:", longest_end)print("连续的长度:", max_length)print("中点在图像的上半部分")cv2.line(image2, (min_x,min_y+ height // 2-max_length), (max_x,min_y+ height // 2-max_length), (255, 0, 0))else:print("最长的白色像素段起始位置:", longest_start)print("最长的白色像素段结束位置:", longest_end)print("连续的长度:", max_length)print("中点在图像的下半部分")cv2.line(image2, (min_x,min_y+ height // 2+max_length), (max_x,min_y+ height // 2+max_length), (255, 0, 0))cv2.imshow("Output", image2)cv2.waitKey(0)if __name__ == "__main__":feature()

文件下载:
1.shape_predictor_68_face_landmarks.dat 下载地址

1.官方下载地址(会比较慢) http://dlib.net/files/

2.我的网盘: 链接:https://pan.baidu.com/s/1ORhqLS1bkHyyYfzbmftNpg 提取码:va12

3.我的资源(免费下载):https://download.csdn.net/download/qq_51985653/15122811

原文链接:https://blog.csdn.net/qq_51985653/article/details/113748025

这篇关于瞳孔检测眼动追踪python实现(基于dlib)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/500739

相关文章

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文