Skip to content

Commit

Permalink
eye完善,grabcut功能提交
Browse files Browse the repository at this point in the history
  • Loading branch information
vipstone committed May 11, 2018
1 parent 5f0c8da commit abe00a6
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 22 deletions.
52 changes: 30 additions & 22 deletions faceai/eye.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@

#获取眼球中心
def houghCircles(path, counter):
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = cv2.imread(path, 0)
# img = cv2.medianBlur(img, 5)

x = cv2.Sobel(img, -1, 1, 0, ksize=3)
y = cv2.Sobel(img, -1, 0, 1, ksize=3)
absx = cv2.convertScaleAbs(x)
absy = cv2.convertScaleAbs(y)
img = cv2.addWeighted(absx, 0.5, absy, 0.5, 0)

# ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
# channels = cv2.split(ycrcb)
# cv2.equalizeHist(channels[0], channels[0]) #输入通道、输出通道矩阵
# cv2.merge(channels, ycrcb) #合并结果通道
# cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR, img)

# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

# cv2.imshow("img2", img)
Expand All @@ -50,9 +57,9 @@ def houghCircles(path, counter):
cv2.HOUGH_GRADIENT,
1,
50,
param1=50 / 2,
param2=30 / 2,
minRadius=0,
param1=50,
param2=10,
minRadius=2,
maxRadius=0)

circles = np.uint16(np.around(circles))
Expand All @@ -62,7 +69,7 @@ def houghCircles(path, counter):
# draw the center of the circle
cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 2)
# cv2.imshow("img" + str(counter), cimg)
return (i[0], i[1])
return (i[0] + 3, i[1] + 3)


#彩色直方图均衡化
Expand All @@ -83,22 +90,22 @@ def hist(img):
def discern(img, counter):
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
color = (0, 255, 0)
# 调用识别人脸
faceRects = classifier.detectMultiScale(
grayImg, scaleFactor=1.2, minNeighbors=3, minSize=(58, 58))
if len(faceRects):
for faceRect in faceRects:
x, y, w, h = faceRect
rightEyeImg = img[(y):(y + h), (x):(x + w)]
cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)

cv2.imwrite("img/temp.png", hist(rightEyeImg))
# cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
rightEyeImg = cv2.GaussianBlur(rightEyeImg, (5, 5), 1)
# rightEyeImg = hist(rightEyeImg)
cv2.imwrite("img/temp.png", rightEyeImg)
# cv2.imwrite("img/temp.png", rightEyeImg)
circleCenter = houghCircles("img/temp.png", counter) #(x,y)
cv2.circle(img, (x + circleCenter[0], y + circleCenter[1]), 2,
(0, 0, 255), 2)
(128, 0, 0), 2)
counter += 1

cv2.imshow("image", img) # 显示图像
cv2.imshow("image", img)


# path = "img/ag-3.png"
Expand All @@ -116,18 +123,18 @@ def discern(img, counter):
# counter += 1
# # cv2.imshow("img", houghCircles("img/temp.png"))

path = "img/ag-3.png"
path = "img/ag.png"
img = cv2.imread(path)
discern(img, counter)

cap = cv2.VideoCapture(0)
while (1):
ret, frame = cap.read()
# cap = cv2.VideoCapture(0)
# while (1):
# ret, frame = cap.read()

# cv2.imshow('frame', gray)
discern(frame, counter)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# # cv2.imshow('frame', gray)
# discern(frame, counter)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break

#

Expand Down Expand Up @@ -177,5 +184,6 @@ def discern(img, counter):
# time.sleep(1)

# cv2.imshow('detected circles', cimg)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

cv2.waitKey(0)
cv2.destroyAllWindows()
88 changes: 88 additions & 0 deletions faceai/eye2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import cv2
import dlib
import numpy as np
import time

classifier = cv2.CascadeClassifier(
"C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_eye_tree_eyeglasses.xml" # haarcascade_eye
)


#彩色直方图均衡化
def hist(img):
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
channels = cv2.split(ycrcb)
cv2.equalizeHist(channels[0], channels[0]) #输入通道、输出通道矩阵
cv2.merge(channels, ycrcb) #合并结果通道
cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR, img)
return img


def discern(img):
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faceRects = classifier.detectMultiScale(
grayImg, scaleFactor=1.2, minNeighbors=3, minSize=(30, 30))
if len(faceRects):
for faceRect in faceRects:
x, y, w, h = faceRect
rightEyeImg = img[(y):(y + h), (x):(x + w)]
# cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
# rightEyeImg = hist(rightEyeImg)
rightEyeImg = cv2.GaussianBlur(rightEyeImg, (5, 5), 1)

cv2.imwrite("img/temp.png", rightEyeImg)
# cv2.imshow("img", rightEyeImg)
# print(len(faceRects))


# discern(cv2.imread("img/ag-2.png"))

img = cv2.imread("img/temp.png", 0)

# img = cv2.GaussianBlur(img, (3, 3), 0)
# img = cv2.Canny(img, 50, 150)
img = cv2.bilateralFilter(img, 7, 50, 50)

# x = cv2.Sobel(img, -1, 1, 0, ksize=3)
# y = cv2.Sobel(img, -1, 0, 1, ksize=3)
# absx = cv2.convertScaleAbs(x)
# absy = cv2.convertScaleAbs(y)
# dist = cv2.addWeighted(absx, 0.5, absy, 0.5, 0)

# img = cv2.GaussianBlur(img, (5, 5), 1)

# laplacian = cv2.Laplacian(img, -1, ksize=3)

# laplacian = cv2.GaussianBlur(laplacian, (3, 3), 1)
# laplacian = cv2.medianBlur(laplacian, 3)

# img = dist

# img = cv2.cvtColor(dist, cv2.COLOR_BGR2GRAY)

cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # cv2.imread("img/temp.png") #

# cv2.imshow("img2", img)
# cv2.imshow("grayimg", grayimg)

circles = cv2.HoughCircles(
img,
cv2.HOUGH_GRADIENT,
1,
100,
param1=50,
param2=10,
minRadius=2,
maxRadius=0)

circles = np.uint16(np.around(circles))

for i in circles[0, :]:
# draw the outer circle
# cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 1)
# draw the center of the circle
cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 2)
cv2.imshow("img", cimg)

cv2.waitKey(0)
cv2.destroyAllWindows()
22 changes: 22 additions & 0 deletions faceai/grabCut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#coding=utf-8
#抠图

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('img/face_recognition/Gates.png')
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (0, 0, 505, 448) #划定区域
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5,
cv2.GC_INIT_WITH_RECT) #函数返回值为mask,bgdModel,fgdModel
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') #0和2做背景

img = img * mask2[:, :, np.newaxis] #使用蒙板来获取前景区域

cv2.imshow('p', img)

cv2.waitKey(0)
cv2.destroyAllWindows()
Binary file added faceai/img/ag-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified faceai/img/eye-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added faceai/img/temp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit abe00a6

Please sign in to comment.