Commit 95264ad6 authored by 任婷婷's avatar 任婷婷

first commit

parents
1.jpg

43.3 KB

1.requirements
dlib
opencv-python
2.demo
python eyedist.py
import dlib
import os
import math
#from align import AlignDlib
import cv2
import pandas as pd
from numpy import array
import io
import requests
from PIL import Image
import numpy as np
class TooManyFaces(Exception):
pass
class NoFaces(Exception):
pass
def load_model(PREDICTOR_PATH):
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(PREDICTOR_PATH)
return detector,predictor
def eyedist(img,predictor,detector):
rects = None
if img is not None:
rects=detector(img,1)
else:
print("input img is empty")
if len(rects)>1:
raise TooManyFaces
if len(rects)==0:
raise NoFaces
lks = predictor(img,rects[0]).parts()
left_corner = lks[39]
right_corner = lks[42]
left_tail = lks[36]
right_tail = lks[45]
left_dist = math.hypot(left_corner.x-left_tail.x,left_corner.y-left_tail.y)
right_dist = math.hypot(right_corner.x-right_tail.x,right_corner.y-right_tail.y)
corner_dist = math.hypot(left_corner.x-right_corner.x,left_corner.y-right_corner.y)
if left_dist >= right_dist:
ratio = float(corner_dist/left_dist)
else:
ratio = float(corner_dist/right_dist)
cate = None
if ratio>1.3:
cate = 'guokuan'
elif 1.1<ratio<=1.3:
#elif ratio>1.1 and ratio<=1.3:
cate = 'luekuan'
elif 1<ratio<=1.1:
cate = 'shizhong'
elif 0.95<ratio<=1:
cate = 'luezhai'
elif ratio<= 0.95:
cate = 'guozhai'
return cate,ratio
if __name__ == '__main__':
PREDICTOR_PATH = 'shape_predictor_68_face_landmarks.dat'
detector,predictor = load_model(PREDICTOR_PATH)
img = cv2.imread('1.jpg')
category,ratio = eyedist(img,predictor,detector)
print('category:',category,'ratio:',ratio)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment