先上传效果图

main.py

// An highlighted block"""
Demonstration of the GazeTracking library.
Check the README.md for complete documentation.
"""
import pandas as pd
import cv2
import dlib
import saccademodel
import fixationmodel
from imutils import resize
import os
from GazeTracking_master.gaze_tracking.gaze_tracking import GazeTracking
import matplotlib.pyplot as plt
from GazeTracking_master.gaze_tracking.FaceAligner import FaceAligner
# import calibration_
import collections
def gaze_():
# calibration_matrix = calibration_.main()
# def gaze_(path, path_csv):gaze = GazeTracking()webcam = cv2.VideoCapture(0)frame_cnt = 0Frame = [];Gaze = [];Left_gaze = [];Right_gaze = []calibration_points = [];X_gaze = []while True:# We get a new frame from the webcam_, frame = webcam.read()if not _:print('no frame')break# We send this frame to GazeTracking to analyze it# frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)#转化角度90度# frame = cv2.resize(src=frame, dsize=(720, 1280))#压缩尺寸cv2.imshow('frame',frame)_face_detector = dlib.get_frontal_face_detector()_predictor = dlib.shape_predictor("C:/Users/zhangjing/Anaconda3/envs/Django_1/GazeTracking_master/gaze_tracking/trained_models/shape_predictor_68_face_landmarks.dat")gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)faces = _face_detector(frame)Fa = FaceAligner(_face_detector, _predictor, (0.35, 0.35), desiredFaceWidth=720, desiredFaceHeight=1280)aligned_face = Fa.align(frame, gray, faces[0])frame = aligned_facegaze.refresh(frame)frame,left,right = gaze.annotated_frame()Left_gaze.append(left);Right_gaze.append(right)text = ""if gaze.is_blinking():text = "Blinking"elif gaze.is_right():text = "Looking right"elif gaze.is_left():text = "Looking left"elif gaze.is_center():text = "Looking center"cv2.putText(frame, text, (20, 60), cv2.FONT_HERSHEY_DUPLEX, 1.4, (147, 58, 31), 2)left_pupil = gaze.pupil_left_coords()right_pupil = gaze.pupil_right_coords()cv2.putText(frame, "Left pupil:  " + str(left_pupil), (20, 130), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1)cv2.putText(frame, "Right pupil: " + str(right_pupil), (20, 165), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1)if left_pupil == None:continueleft_pupil = list(left_pupil)right_pupil = list(right_pupil)Gaze.append(left_pupil)# point = [left_pupil[0],left_pupil[1], left_pupil[0]*left_pupil[1], 1]# calibration_point = np.dot(calibration_matrix[0][0], point)# print(calibration_point)# calibration_point_1 = np.transpose(calibration_point)# print(calibration_point_1)# calibration_point_2 = np.asarray(calibration_point_1)# print(calibration_point_2)# calibration_point = list(calibration_point)# calibration_points.append([np.float(calibration_point[0]),abs(np.float(calibration_point[1]))])Frame.append(frame_cnt)# cv2.moveWindow("trans:" + frame, 1000, 100)cv2.imshow('Dome',frame)# cv2.imwrite("E:/ffmpeg-latest-win64-static/eye_frame_2/face/"+str(frame_cnt)+'.png', frame)frame_cnt = frame_cnt + 1# print(frame_cnt)if cv2.waitKey(1) == 27:breakif frame_cnt% 10 ==0:Left_gaze = [x for x in Left_gaze if x]  # 删除空列表[]x_left = [x[0] for x in Left_gaze]x_left = collections.Counter(x_left)left_x =[];left_y = [];left_r =[]for c in x_left:left_x.append((c / frame_cnt)*(x_left[c]))y_left = [x[1] for x in Left_gaze]y_left = collections.Counter(y_left)for c in y_left:left_y.append((c / frame_cnt) * (y_left[c]))r_left = [x[2] for x in Left_gaze]r_left = collections.Counter(r_left)for c in r_left:left_r.append((c / frame_cnt) * (r_left[c]))print(int(np.sum(left_x)),int(np.sum(left_y)),int(np.sum(left_r)))"""if frame_cnt%30 == 0:framerate = 30.0# for i in range(len(calibration_points)):# a = calibration_points[0][0]/640# b = calibration_points[0][1] / 360# x = calibration_points[i][0] / a# y = calibration_points[i][1] / b# print(x,y)# Gaze.append([x,y])# Y_gaze.append(y)saccad = saccademodel.fit(Gaze)fixation = fixationmodel.fit(Gaze)source_points = saccad.get('source_points')saccade_points = saccad.get('saccade_points')target_points = saccad.get('target_points')mean_squared_error_s = saccad.get('mean_squared_error')centroid = fixation.get('centroid')mean_squared_error_f = fixation.get('mean_squared_error')print(saccad)print('source_points:',source_points)print('saccade_points:',saccade_points)print('target_points:',target_points)print('mean_squared_error:',mean_squared_error_s)print(fixation)print('centroid:',centroid)print('mean_squared_error:', mean_squared_error_f)saccade_X=[];saccade_Y=[];target_X=[];target_Y=[]for i in range(len(saccade_points)):saccade_x = saccade_points[i][0]saccade_y = saccade_points[i][1]saccade_X.append(saccade_x)saccade_Y.append(saccade_y)for j in range(len(target_points)):target_x = target_points[j][0]target_y = target_points[j][1]target_X.append(target_x)target_Y.append(target_y)plt.figure(figsize=(12.8, 7.2))# plt.xlim([0,1280])# plt.ylim([0, 720])plt.plot(saccade_X,saccade_Y)plt.scatter(target_X,target_Y, marker='o', label='target', s=20., c='b')plt.scatter(centroid[0],centroid[1],marker='o', label='centroid', s=35., c='r')# plt.savefig('C:/Users/zhangjing/Documents/Bandicam/0_left/'+str(frame_cnt)+'.png')Gaze.clear()
"""import numpy as np
def calibration(gaze_x_points, gaze_y_points, calibration_matrix):calibration_points = []for x, y in zip(gaze_x_points, gaze_y_points):point = [x, y, 1, 1]calibration_point = np.dot(calibration_matrix, point)calibration_point = np.transpose(calibration_point)calibration_point = np.asarray(calibration_point)calibration_points.append([int(calibration_point[0][0]), int(calibration_point[1][0])])return calibration_points# data = {'Frame':Frame,'L_pupil_X': L_pupil_X,'L_pupil_Y': L_pupil_Y, 'R_pupil_X': R_pupil_X,'R_pupil_Y': R_pupil_Y}# cols = ['Frame','L_pupil_X','L_pupil_Y', 'R_pupil_X','R_pupil_Y']# result = pd.DataFrame(data, columns=cols,)# result.to_csv(path_csv + 'gaze.csv',index=False)gaze_()
var foo = 'bar';

pupil.py

// An highlighted blockimport numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy.misc import imresize
from skimage.morphology import erosion
from PIL import Image, ImageDrawnp.set_printoptions(threshold=np.inf)
from math import sqrt
import time
class Pupil(object):"""This class detects the iris of an eye and estimatesthe position of the pupil"""def __init__(self, eye_frame, threshold):self.iris_frame = Noneself.threshold = thresholdself.x = Noneself.y = Noneself.r = Noneself.detect_iris(eye_frame)@staticmethoddef image_processing(eye_frame, threshold):"""Performs operations on the eye frame to isolate the irisArguments:eye_frame (numpy.ndarray): Frame containing an eye and nothing elsethreshold (int): Threshold value used to binarize the eye frameReturns:A frame with a single element representing the iris"""cv2.imshow("eye", eye_frame)kernel = np.ones((3, 3), np.uint8)new_frame = cv2.bilateralFilter(eye_frame, 10, 15, 15)clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))new_frame = clahe.apply(new_frame)new_frame = cv2.erode(new_frame, kernel, iterations=3)new_frame = cv2.threshold(new_frame, threshold, 255, cv2.THRESH_BINARY)[1]circles = cv2.HoughCircles(eye_frame, cv2.HOUGH_GRADIENT, 1, 1, param1=50, param2=30, minRadius=0,maxRadius=0) # image - 8位,单通道,灰度输入图像;circles - 找到的圆的输出向量。每个向量被编码为3元素的浮点向量(x,y,半径)#circle_storage 在C函数中,这是一个将包含找到的圆的输出序列的内存存储。 method - 使用检测方法。目前唯一实现的方法是cv_hough_gradient; dp - 累加器分辨率与图像分辨率的反比。# minDist - 检测到的圆的中心之间的最小距离。有助于监测相邻圆; 倒数第二个为canny边缘检测时的高阈值#倒数第一个为圆心的累加器阈值,越小检测出的圆越多。# eye_frame = cv2.resize(eye_frame,(0,0),fx=4, fy=4, interpolation=cv2.INTER_CUBIC)return new_frame,circles,eye_framedef detect_iris(self, eye_frame):"""Detects the iris and estimates the position of the iris bycalculating the centroid.Arguments:eye_frame (numpy.ndarray): Frame containing an eye and nothing else"""self.iris_frame,circles,pupil_frame = self.image_processing(eye_frame, self.threshold)contours, _ = cv2.findContours(self.iris_frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[-2:]contours = sorted(contours, key=cv2.contourArea)if str(circles) == 'None':try:moments = cv2.moments(contours[-2])self.x = int(moments['m10'] / moments['m00'])self.y = int(moments['m01'] / moments['m00'])self.r = 15mask = np.zeros(pupil_frame.shape[:2], np.uint8)  # 方形掩膜mask = cv2.circle(mask, (self.x, self.y), 16, (255, 255, 255), -1)image = cv2.add(pupil_frame, np.zeros(np.shape(pupil_frame), dtype=np.uint8), mask=mask)m_hist = cv2.calcHist([pupil_frame], [0], mask, [256], [0, 256])  # 使用calchist计算每个像素点的频数plt.imshow(image, 'gray')plt.plot(m_hist, 'red')plt.savefig('E:/ffmpeg-latest-win64-static/eye_frame_2/hist/' + str(time.time()) + '.png')plt.clf()pupil_frame = cv2.threshold(pupil_frame, 25, 255, cv2.THRESH_BINARY)[1]cv2.imwrite('E:/ffmpeg-latest-win64-static/eye_frame_2/eye/' + str(time.time()) + '.png',pupil_frame)# ellipse = np.zeros((row, col, dim))# cv2.circle(ellipse, (center_row, center_col), radius, (255, 255, 255), 2)# print('원이없음_iris:', self.x, self.y)except (IndexError, ZeroDivisionError):passelse:circles = np.uint16(np.around(circles))  # 把circles包含的圆心和半径的值变成整数if len(circles[0, :]) == 1:if circles[0][0][0] == 0:#원이 없으면 원래 모양 잡은 방법 사용함try:moments = cv2.moments(contours[-2])self.x = int(moments['m10'] / moments['m00'])self.y = int(moments['m01'] / moments['m00'])self.r = 15mask = np.zeros(pupil_frame.shape[:2], np.uint8)  # 方形掩膜mask = cv2.circle(mask, (self.x, self.y), 16, (255, 255, 255), -1)image = cv2.add(pupil_frame, np.zeros(np.shape(pupil_frame), dtype=np.uint8), mask=mask)m_hist = cv2.calcHist([pupil_frame], [0], mask, [256], [0, 256])  # 使用calchist计算每个像素点的频数plt.imshow(image, 'gray')plt.plot(m_hist, 'red')plt.savefig('E:/ffmpeg-latest-win64-static/eye_frame_2/hist/' + str(time.time()) + '.png')plt.clf()pupil_frame = cv2.threshold(pupil_frame, 25, 255, cv2.THRESH_BINARY)[1]cv2.imwrite('E:/ffmpeg-latest-win64-static/eye_frame_2/eye/' + str(time.time()) + '.png',pupil_frame)# print('원이없음_iris:',self.x,self.y)except (IndexError, ZeroDivisionError):passelse :#원이 한개 있으면for i in circles[0, :]:# cv2.circle(eye_frame, (i[0], i[1]), i[2], (0, 255, 0), 2)  # 画圆# cv2.circle(eye_frame, (i[0], i[1]), 2, (0, 255, 0), 2)  # 画圆心try:moments = cv2.moments(contours[-2])self.x = int(i[0])self.y = int(i[1])self.r = int(i[2])mask = np.zeros(pupil_frame.shape[:2], np.uint8)#方形掩膜mask = cv2.circle(mask,(self.x,self.y),16,(255,255,255),-1)image = cv2.add(pupil_frame, np.zeros(np.shape(pupil_frame), dtype=np.uint8), mask=mask)m_hist = cv2.calcHist([pupil_frame], [0], mask, [256], [0, 256])  # 使用calchist计算每个像素点的频数plt.imshow(image, 'gray')plt.plot(m_hist, 'red')plt.savefig('E:/ffmpeg-latest-win64-static/eye_frame_2/hist/' + str(time.time()) + '.png')plt.clf()pupil_frame = cv2.threshold(pupil_frame, 25, 255, cv2.THRESH_BINARY)[1]cv2.imwrite('E:/ffmpeg-latest-win64-static/eye_frame_2/eye/' + str(time.time()) + '.png',pupil_frame)except (IndexError, ZeroDivisionError):passelse:#원이 여러 개 있으면X=[];Y =[];R =[]for i in circles[0, :]:# cv2.circle(eye_frame, (i[0], i[1]), i[2], (0, 255, 0), 2)  # 画圆# cv2.circle(eye_frame, (i[0], i[1]), 2, (0, 255, 0), 2)  # 画圆心X.append(i[0])Y.append(i[1])R.append(i[2])x_mean = np.mean(X)y_mean = np.mean(Y)r_mean = np.mean(R)try:moments = cv2.moments(contours[-2])self.x = int(x_mean)self.y = int(y_mean)self.r = int(r_mean)mask = np.zeros(pupil_frame.shape[:2], np.uint8)  # 方形掩膜mask = cv2.circle(mask, (self.x, self.y), 16, (255, 255, 255), -1)image = cv2.add(pupil_frame, np.zeros(np.shape(pupil_frame), dtype=np.uint8), mask=mask)m_hist = cv2.calcHist([pupil_frame], [0], mask, [256], [0, 256])  # 使用calchist计算每个像素点的频数plt.imshow(image, 'gray')plt.plot(m_hist,'red')plt.savefig('E:/ffmpeg-latest-win64-static/eye_frame_2/hist/' + str(time.time()) + '.png')plt.clf()pupil_frame = cv2.threshold(pupil_frame, 25, 255, cv2.THRESH_BINARY)[1]cv2.imwrite('E:/ffmpeg-latest-win64-static/eye_frame_2/eye/' + str(time.time()) + '.png',pupil_frame)# print('원이여러개_iris:', self.x, self.y)except (IndexError, ZeroDivisionError):passvar foo = 'bar';
// An highlighted blockvar foo = 'bar';

其他代码请到资料库下载

虹膜识别系统 python实现相关推荐

  1. 中国虹膜识别系统市场主要应用领域与投资战略规划研究报告2022年版

    中国虹膜识别系统市场主要应用领域与投资战略规划研究报告2022年版   第一章 虹膜识别系统行业发展综述 15 1.1 虹膜识别系统行业定义及分类 15 1.1.1 行业定义 15 1.1.2 行业产 ...

  2. 计算机专业徐向东教授,计算机虹膜识别系统的分析-控制理论与控制工程专业论文.docx...

    浙江大学硕士学位论文摘要 浙江大学硕士学位论文 摘要 @着社会经济的发展,作为生物特征识别技术中一类的虹膜识别技术正在 兴起,并显示了很大的优越性,如虹膜特征在人的一生中均保持相当高的稳定 性:虹膜图 ...

  3. 移动网流量用户身份识别系统的源代码_虹膜识别系统助力疫情防控

    光明网讯日前,航天科工二院203所接到一项人员管控需求,要求在疫情防控的关键卡口.岗哨等重要位置,在人员不摘下口罩的情况下,进行人员身份甄别.203所紧急组织力量开展虹膜识别设备生产,以最快的速度将设 ...

  4. 基于Matlab的虹膜识别系统(GUI界面)

    点击查看:基于Matlab的虹膜识别系统(GUI界面) 文件大小:56M 操作系统:Windows10旗舰版 开发工具:Matlab2016.2018.2019.2020 开发语言:.m 代码注释:

  5. 利用SEAL库进行加密的虹膜识别系统制作过程记录

    正在制作一个利用C++编写的基于全同态加密的虹膜识别系统.中间遇到部分问题,记录如下: 1.记2021.1.25: 用SOCKET传递账户密码的时候,如下设置服务器的接收信息代码,会出现跳包,及接收到 ...

  6. 动物识别系统 python实现+UI

    完整代码在最下方 实验目的 利用动物识别系统来验证基于符号的推理,通过实验理解简单的专家系统所包含的模块:人机结构,知识获取机构,知识库及管理系系统,推理机,解释机构,数据库及管理系统". ...

  7. 基于python的文字识别系统,python神经网络识别图片

    如何快速使用Python神经网络识别手写字符 CNN卷积神经网络是一种深度模型.它其实老早就已经可以成功训练并且应用了(最近可能deeplearning太火了,CNNs也往这里面靠. 虽然CNNs也属 ...

  8. 商品识别系统Python,基于深度学习卷积神经网络

    介绍 商品识别系统采用了Python.TensorFlow.ResNet50算法以及Django等技术栈.其中,Python作为主要的编程语言,它的清晰简洁的语法使得代码易于阅读和编写.TensorF ...

  9. 垃圾识别系统Python

    介绍 垃圾识别系统,使用Python作为主要开发语言,基于深度学习TensorFlow框架,搭建卷积神经网络算法.并通过对5种垃圾数据集进行训练,最后得到一个识别精度较高的模型.并基于Django框架 ...

最新文章

  1. 百度Q2日进2.9个亿,新基建推动Apollo上位!李彦宏开招管培生:亲自选亲自带...
  2. 小程序上让随机的两个点都显示在地图可视区域
  3. 【笔记】buck/boost/buck-boost相关计算公式
  4. 机器学习笔记 (聚类) 层次聚类 Agglomerative Clutsering(Single-linkage、Complete-linkage,Group average)
  5. java的部署目录在哪里_Java:Tomcat的部署实例之资源目录
  6. 信使(信息学奥赛一本通-T1376)
  7. python开发mes系统_MES系统开发
  8. k8s核心技术-Pod(两种实现机制)_Pod底层实现机制_共享网络_共享存储_Pause根容器_Pod数据卷---K8S_Google工作笔记0021
  9. 系统学习NLP(二十七)--EMLo
  10. 通通玩blend美工(3)——可爱的云
  11. Android开发中的SQLite事务处理,即beginTransaction()方法
  12. Darknet网络模型结构可视化
  13. axure iphone8元件库_Axure中移动端原型设计方法(附IPhoneX和IPhone8最新模板)
  14. 为什么模板不支持分离编译
  15. 100层楼和两个玻璃球的问题
  16. CCF-CSP 201403-1 相反数 (python)
  17. 955.WLB 不加班公司名单!再新增 5 家公司!
  18. 【C语言】字符个数统计 笔试常见题型
  19. LCD1602液晶 - 开发技术详解
  20. php mysql 别名_php和mysql的一些奇怪之处

热门文章

  1. 当事情推动不了时,投诉或许是一种好的解决方案 | 每天成就更大成功
  2. CEO, CFO,CTO,COO的含义?
  3. 家庭哑铃健身训练计划
  4. 金立拍卖211宗外观设计专利 起拍价2.11万元
  5. 图像处理算法(二)---图像常用颜色空间
  6. 华硕X43S关闭触控板
  7. 【Turtlebot移动机器人实验记录】实验4. Turtlebot 机器人自主导航实验
  8. 如何避免新代码变包袱?阿里通用方法来了!
  9. VUE将两条数据组合成一条数据
  10. 阅读笔记《Changer: Feature Interaction is What You Need for Change Detection》