-
Notifications
You must be signed in to change notification settings - Fork 5
/
CoreImage.py
191 lines (165 loc) · 5.07 KB
/
CoreImage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
# @Author: haodaquan
# @Date: 2016-12-25 10:13:57
# @Last Modified by: haodaquan
# @Last Modified time: 2016-12-30 16:50:36
import sys
import copy
import cv2
import math
import datetime
#----------------------------------------------------------------------
# CoreImage: 核心图片类,获取核心图片
#----------------------------------------------------------------------
class CoreImage(object):
#imageInfo = [imageName,imagePath,newImagePath]
def __init__(self,*arg):
imgInfo = arg[0]
cropInfo = arg[1]
#原图信息
self.imgPath = imgInfo.get('imgPath','./originalImage/')
self.imgName = imgInfo.get('imgName','image.jpg')
#新图信息
self.newImgPath = cropInfo.get('newImgPath','./newImage/')
self.isBgPure = cropInfo.get('isBgPure',0)#是否纯色背景,0-不是,1-是
self.newWidth = cropInfo.get('newWidth',380)
self.newHeight = cropInfo.get('newHeight',380)
self.line = cropInfo.get('line',1) #每隔几行扫描一次,建议1,4,8
#获取图片边缘
def getImgEdge(self):
img = cv2.imread(self.imgPath+self.imgName)
edge = cv2.Canny(img,100,300) #可以根据实际业务进行调整
# cv2.imwrite(self.newImgPath+'edge.jpg',edge)
return [edge,img]
#获取核心图片
def getCoreImg(self):
if(self.isBgPure==1):
return self.getPureBgCoreImg()
else:
return self.getDefaultBgCoreImg()
#获取纯色背景核心图片
def getPureBgCoreImg(self):
begin = datetime.datetime.now()
edgeImg = self.getImgEdge()
img_width = edgeImg[1].shape[1]
img_height = edgeImg[1].shape[0]
x0 = 0 # 左上角x
y0 = 0 # 左上角y
x1 = img_width-1
y1 = img_height-1
#左上角X轴
i = j = 0
for i in range(0,img_width,self.line):
if(x0!=0):
break;
for j in range(0,img_height):
if(edgeImg[0][j,i]!=0):
x0=i-self.line
break
#左上角Y轴
i = j = 0
for i in range(0,img_height,self.line):
if(y0!=0):
break;
for j in range(0,img_width):
if(edgeImg[0][i,j]!=0):
y0=i-self.line
break
#右下角X轴
i = j = 0
for i in range(0,img_width,self.line)[::-1]:
if(x1!=img_width-1):
break;
for j in range(0,img_height)[::-1]:
if(edgeImg[0][j,i]!=0):
x1=i+self.line
break
#右下角Y轴
i = j = 0
for i in range(0,img_height,self.line)[::-1]:
if(y1!=img_height-1):
break;
for j in range(0,img_width)[::-1]:
if(edgeImg[0][i,j]!=0):
y1=i+self.line
break
crop_img = edgeImg[1][y0:y1,x0:x1]
# image_name = 'new_image'+str(self.line)+'.jpg'
image_name = 'new_image.jpg'
cv2.imwrite(self.newImgPath+image_name,crop_img)
end = datetime.datetime.now()
print(end-begin);
return crop_img
#获取默认的核心图片 含人脸识别
def getDefaultBgCoreImg(self):
# 加载训练好的脸、眼分类器
face_cascade = cv2.CascadeClassifier(
'/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier(
'/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml')
width = self.newWidth
height = self.newHeight
img = cv2.imread(self.imgPath+self.imgName)
# 返回 img 的浅拷贝
original = copy.copy(img)
# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE)
imHeight, imWidth = img.shape[:2]
maxFaceCenter = 0
maxFaceRight = 0
maxFaceMidlle = 0
for (x, y, w, h) in faces:
# 人脸部分用实心矩形框选
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), -1)
# cropBias = cropBias - x
# 找出最大人脸
if (x + w) > maxFaceRight:
maxFaceCenter = (x + w) - (w // 2)
maxFaceRight = x + w
maxFaceMidlle = y + h // 2
# 截出人脸
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
# 检测眼睛
eyes = eye_cascade.detectMultiScale(roi_gray)
# 框选出人眼
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0),2)
#左上角位置定位
leftPosX = maxFaceCenter - width // 2
leftPosY = maxFaceMidlle - height // 2
if(leftPosX < 0):
leftPosX = 0
if(leftPosY < 0):
leftPosY = 0
# print(leftPosY,height, leftPosX,width)
copyX = width + leftPosX
copyY = height + leftPosY
if(copyX < maxFaceRight):
copyX = maxFaceRight
# print(leftPosY,copyY, leftPosX,copyX)
croppedData = original[leftPosY:copyY, leftPosX:copyX]
croppedName = self.newImgPath+"new_face.jpg"
cv2.imwrite(croppedName, croppedData)
if __name__ == '__main__':
imgPath = './originalImage/'
newImgPath = './newImage/'
#人脸识别
imgName = 'face.jpg'
imgInfo = {'imgName':imgName,'imgPath':imgPath}
newImgInfo = {'isBgPure':0,'newWidth':280,'newHeight':280};
coeImg = CoreImage(imgInfo,newImgInfo)
coeImg.getCoreImg();
#背景纯色图
imgName = 'image.jpg'
imgInfo = {'imgName':imgName,'imgPath':imgPath}
newImgInfo = {'isBgPure':1,'line':8};
coeImg = CoreImage(imgInfo,newImgInfo)
coeImg.getCoreImg();