def main(self, src, dst): graph = Image.open(src) resultjson = facep.detect(src) if resultjson != -1: land_mark = resultjson # Operate area size = graph.size width = size[0] height = size[1] contour_left3 = int(land_mark['result'][0]['landmark']['contour_left3']['x'] * width / 100) / 2 * 2 left_eyebrow_upper_middle = int(land_mark['result'][0]['landmark']['left_eyebrow_upper_middle']['y'] * height / 100 * 0.97) / 2 * 2 contour_right3 = int(land_mark['result'][0]['landmark']['contour_right3']['x'] * width / 100) / 2 * 2 contour_right7 = int(land_mark['result'][0]['landmark']['contour_right7']['y'] * height / 100) / 2 * 2 box = (contour_left3, left_eyebrow_upper_middle, contour_right3, contour_right7) self.graph = graph.crop(box) self.size = self.graph.size self.width = self.size[0] self.height = self.size[1] self.photometricDec = 10.0 self.spatialDecay = 1.0 self.gain_mu = (1.0 - self.spatialDecay) / (1.0 + self.spatialDecay) self.rho = 1.0 + self.spatialDecay self.c = -0.5/(self.photometricDec * self.photometricDec) self.hv_graph = ny.double(ny.zeros([self.height, self.width, 3])) self.vh_graph = ny.double(ny.zeros([self.height, self.width, 3])) self.result = ny.double(ny.zeros([self.height, self.width, 3])) self.graph = ny.double(self.graph) for d in range(3): self.hv_graph = self.graph[:, :, d] self.vh_graph = self.graph[:, :, d] self.beeps_hv() self.beeps_vh() for row in range(self.height): for col in range(self.width): self.result[row, col, d] = (self.vh_graph[row, col] + self.hv_graph[row, col]) / 2 self.result = self.result[0:self.height-2, 0:self.width-2, :] self.result = Image.fromarray(ny.uint8(self.result)).convert('RGB') box = (contour_left3, left_eyebrow_upper_middle, contour_right3-2, contour_right7-2) graph.paste(self.result, box) graph.save(dst) return 0 else: graph.save(dst) return -1
def baozou(src, dst): graph = Image.open(src) resultjson = facep.detect(src) if resultjson != -1: land_mark = resultjson # Operate area size = graph.size width = size[0] height = size[1] left_eyebrow_left_corner = int(land_mark['result'][0]['landmark']['left_eyebrow_left_corner']['x'] * width / 100) / 2 * 2 left_eyebrow_upper_middle = int(land_mark['result'][0]['landmark']['left_eyebrow_upper_middle']['y'] * height / 100 * 0.97) / 2 * 2 contour_right7x = int(land_mark['result'][0]['landmark']['contour_right7']['x'] * width / 100) / 2 * 2 contour_right7y = int(land_mark['result'][0]['landmark']['contour_right7']['y'] * height / 100) / 2 * 2 box = (left_eyebrow_left_corner, left_eyebrow_upper_middle, contour_right7x, contour_right7y) result = graph.crop(box) result = ny.array(result) result = ny.matrix(0.3 * result[:, :, 1] + 0.59 * result[:, :, 2] + 0.11 * result[:, :, 2]) R = Image.fromarray(ny.uint8(result)).convert('RGB') size = R.size width = size[0] height = size[1] # npercent = 2 # average = 128 brightness = sum(sum(result).T) / width / height / 128 / 2.8 # result += brightness * 0.9 result /= brightness # result = average + (result - average) * npercent for x in range(height): for y in range(width): if result[x, y] > 220: result[x, y] *= (result[x, y] * 2.0 / 255) result[x, y] = max(0, min(255, result[x, y])) result = Image.fromarray(ny.uint8(result)) template = Image.open('PhotoManager/Library/template.jpg') temp = (width + height) * 2 template = template.resize([temp, temp]) per = 0.30 x1 = int(temp / 2 - width / 2) / 2 * 2 y1 = int(temp * per - height / 2) / 2 * 2 x2 = int(temp / 2 + width / 2) / 2 * 2 y2 = int(temp * per + height / 2) / 2 * 2 tbox = [x1, y1, x2, y2] template.paste(result, tbox) result = Image.fromarray(ny.uint8(template)).convert('RGB') result.save(dst) return 0 else: graph.save(dst) return -1
def enlarge(src, dst): # -*- coding: utf-8 -*- graph = Image.open(src) resultjson = facep.detect(src) if resultjson != -1: land_mark = resultjson # Operate area size = graph.size width = size[0] height = size[1] pIn = ny.array(graph) ptemp = pIn cenX = int(land_mark['result'][0]['landmark']['left_eye_bottom']['y'] * height / 100) cenY = int(land_mark['result'][0]['landmark']['left_eye_bottom']['x'] * width / 100) left_eye_left_corner = int( land_mark['result'][0]['landmark']['left_eye_left_corner']['x'] * width / 100) left_eye_right_corner = int( land_mark['result'][0]['landmark']['left_eye_right_corner']['x'] * width / 100) left_eye_top = int( land_mark['result'][0]['landmark']['left_eye_top']['y'] * height / 100) left_eye_bottom = int( land_mark['result'][0]['landmark']['left_eye_bottom']['y'] * height / 100) R = left_eye_right_corner - left_eye_left_corner # R = left_eye_bottom - left_eye_top R *= 1.1 theta = math.pi for k in range(3): for x in range(1 - cenX, left_eye_bottom - cenX): for y in range(1 - cenY, width - cenY): disX = int(1.2 * x) disY = int(1.2 * y) dis = disX * disX + disY * disY r = math.sqrt(dis) if r <= R and dis != 0: xx = 2 * R * disX * math.acos( math.sqrt(R * R - dis) / R) / (theta * r) + cenX yy = 2 * R * disY * math.acos( math.sqrt(R * R - dis) / R) / (theta * r) + cenY xx = ny.around(ny.array(xx)) yy = ny.around(ny.array(yy)) if width >= yy >= 1 and height >= xx >= 1: ptemp[x + cenX, y + cenY, k] = pIn[xx, yy, k] else: ptemp[x + cenX, y + cenY, k] = pIn[x + cenX, y + cenY, k] pOut = ptemp bias = height / 64 cenX = int(land_mark['result'][0]['landmark']['right_eye_bottom']['y'] * height / 100) + bias cenY = int( land_mark['result'][0]['landmark']['right_eye_bottom']['x'] * width / 100) right_eye_left_corner = int( land_mark['result'][0]['landmark']['right_eye_left_corner']['x'] * width / 100) right_eye_right_corner = int( land_mark['result'][0]['landmark']['right_eye_right_corner']['x'] * width / 100) right_eye_top = int( land_mark['result'][0]['landmark']['right_eye_top']['y'] * height / 100) right_eye_bottom = int( land_mark['result'][0]['landmark']['right_eye_bottom']['y'] * height / 100) R = right_eye_right_corner - right_eye_left_corner # R = left_eye_bottom - left_eye_top R *= 1.1 for k in range(3): for x in range(1 - cenX, right_eye_bottom + bias - cenX): for y in range(1 - cenY, width - cenY): disX = int(1.2 * x) disY = int(1.2 * y) dis = disX * disX + disY * disY r = math.sqrt(dis) if r <= R and dis != 0: xx = 2 * R * disX * math.acos( math.sqrt(R * R - dis) / R) / (theta * r) + cenX yy = 2 * R * disY * math.acos( math.sqrt(R * R - dis) / R) / (theta * r) + cenY xx = ny.around(ny.array(xx)) yy = ny.around(ny.array(yy)) if width >= yy >= 1 and height >= xx >= 1: pOut[x + cenX, y + cenY, k] = pIn[xx, yy, k] else: pOut[x + cenX, y + cenY, k] = pIn[x + cenX, y + cenY, k] result = Image.fromarray(ny.uint8(pOut)).convert('RGB') result.save(dst) return 0 else: graph.save(dst) return -1
def baozou(src, dst): graph = Image.open(src) resultjson = facep.detect(src) if resultjson != -1: land_mark = resultjson # Operate area size = graph.size width = size[0] height = size[1] left_eyebrow_left_corner = int( land_mark['result'][0]['landmark']['left_eyebrow_left_corner']['x'] * width / 100) / 2 * 2 left_eyebrow_upper_middle = int( land_mark['result'][0]['landmark']['left_eyebrow_upper_middle'] ['y'] * height / 100 * 0.97) / 2 * 2 contour_right7x = int( land_mark['result'][0]['landmark']['contour_right7']['x'] * width / 100) / 2 * 2 contour_right7y = int( land_mark['result'][0]['landmark']['contour_right7']['y'] * height / 100) / 2 * 2 box = (left_eyebrow_left_corner, left_eyebrow_upper_middle, contour_right7x, contour_right7y) result = graph.crop(box) result = ny.array(result) result = ny.matrix(0.3 * result[:, :, 1] + 0.59 * result[:, :, 2] + 0.11 * result[:, :, 2]) R = Image.fromarray(ny.uint8(result)).convert('RGB') size = R.size width = size[0] height = size[1] # npercent = 2 # average = 128 brightness = sum(sum(result).T) / width / height / 128 / 2.8 # result += brightness * 0.9 result /= brightness # result = average + (result - average) * npercent for x in range(height): for y in range(width): if result[x, y] > 220: result[x, y] *= (result[x, y] * 2.0 / 255) result[x, y] = max(0, min(255, result[x, y])) result = Image.fromarray(ny.uint8(result)) template = Image.open('PhotoManager/Library/template.jpg') temp = (width + height) * 2 template = template.resize([temp, temp]) per = 0.30 x1 = int(temp / 2 - width / 2) / 2 * 2 y1 = int(temp * per - height / 2) / 2 * 2 x2 = int(temp / 2 + width / 2) / 2 * 2 y2 = int(temp * per + height / 2) / 2 * 2 tbox = [x1, y1, x2, y2] template.paste(result, tbox) result = Image.fromarray(ny.uint8(template)).convert('RGB') result.save(dst) return 0 else: graph.save(dst) return -1
def main(self, src, dst): graph = Image.open(src) resultjson = facep.detect(src) if resultjson != -1: land_mark = resultjson # Operate area size = graph.size width = size[0] height = size[1] contour_left3 = int( land_mark['result'][0]['landmark']['contour_left3']['x'] * width / 100) / 2 * 2 left_eyebrow_upper_middle = int( land_mark['result'][0]['landmark']['left_eyebrow_upper_middle'] ['y'] * height / 100 * 0.97) / 2 * 2 contour_right3 = int( land_mark['result'][0]['landmark']['contour_right3']['x'] * width / 100) / 2 * 2 contour_right7 = int( land_mark['result'][0]['landmark']['contour_right7']['y'] * height / 100) / 2 * 2 box = (contour_left3, left_eyebrow_upper_middle, contour_right3, contour_right7) self.graph = graph.crop(box) self.size = self.graph.size self.width = self.size[0] self.height = self.size[1] self.photometricDec = 10.0 self.spatialDecay = 1.0 self.gain_mu = (1.0 - self.spatialDecay) / (1.0 + self.spatialDecay) self.rho = 1.0 + self.spatialDecay self.c = -0.5 / (self.photometricDec * self.photometricDec) self.hv_graph = ny.double(ny.zeros([self.height, self.width, 3])) self.vh_graph = ny.double(ny.zeros([self.height, self.width, 3])) self.result = ny.double(ny.zeros([self.height, self.width, 3])) self.graph = ny.double(self.graph) for d in range(3): self.hv_graph = self.graph[:, :, d] self.vh_graph = self.graph[:, :, d] self.beeps_hv() self.beeps_vh() for row in range(self.height): for col in range(self.width): self.result[row, col, d] = (self.vh_graph[row, col] + self.hv_graph[row, col]) / 2 self.result = self.result[0:self.height - 2, 0:self.width - 2, :] self.result = Image.fromarray(ny.uint8(self.result)).convert('RGB') box = (contour_left3, left_eyebrow_upper_middle, contour_right3 - 2, contour_right7 - 2) graph.paste(self.result, box) graph.save(dst) return 0 else: graph.save(dst) return -1
def enlarge(src, dst): # -*- coding: utf-8 -*- graph = Image.open(src) resultjson = facep.detect(src) if resultjson != -1: land_mark = resultjson # Operate area size = graph.size width = size[0] height = size[1] pIn = ny.array(graph) ptemp = pIn cenX = int(land_mark['result'][0]['landmark']['left_eye_bottom']['y'] * height / 100) cenY = int(land_mark['result'][0]['landmark']['left_eye_bottom']['x'] * width / 100) left_eye_left_corner = int(land_mark['result'][0]['landmark']['left_eye_left_corner']['x'] * width / 100) left_eye_right_corner = int(land_mark['result'][0]['landmark']['left_eye_right_corner']['x'] * width / 100) left_eye_top = int(land_mark['result'][0]['landmark']['left_eye_top']['y'] * height / 100) left_eye_bottom = int(land_mark['result'][0]['landmark']['left_eye_bottom']['y'] * height / 100) R = left_eye_right_corner - left_eye_left_corner # R = left_eye_bottom - left_eye_top R *= 1.1 theta = math.pi for k in range(3): for x in range(1 - cenX, left_eye_bottom - cenX): for y in range(1 - cenY, width - cenY): disX = int(1.2 * x) disY = int(1.2 * y) dis = disX * disX + disY * disY r = math.sqrt(dis) if r <= R and dis != 0: xx = 2 * R * disX * math.acos(math.sqrt(R * R - dis) / R) / (theta * r) + cenX yy = 2 * R * disY * math.acos(math.sqrt(R * R - dis) / R) / (theta * r) + cenY xx = ny.around(ny.array(xx)) yy = ny.around(ny.array(yy)) if width >= yy >= 1 and height >= xx >= 1: ptemp[x + cenX, y + cenY, k] = pIn[xx, yy, k] else: ptemp[x + cenX, y + cenY, k] = pIn[x + cenX, y + cenY, k] pOut = ptemp bias = height / 64 cenX = int(land_mark['result'][0]['landmark']['right_eye_bottom']['y'] * height / 100) + bias cenY = int(land_mark['result'][0]['landmark']['right_eye_bottom']['x'] * width / 100) right_eye_left_corner = int(land_mark['result'][0]['landmark']['right_eye_left_corner']['x'] * width / 100) right_eye_right_corner = int(land_mark['result'][0]['landmark']['right_eye_right_corner']['x'] * width / 100) right_eye_top = int(land_mark['result'][0]['landmark']['right_eye_top']['y'] * height / 100) right_eye_bottom = int(land_mark['result'][0]['landmark']['right_eye_bottom']['y'] * height / 100) R = right_eye_right_corner - right_eye_left_corner # R = left_eye_bottom - left_eye_top R *= 1.1 for k in range(3): for x in range(1 - cenX, right_eye_bottom + bias - cenX): for y in range(1 - cenY, width - cenY): disX = int(1.2 * x) disY = int(1.2 * y) dis = disX * disX + disY * disY r = math.sqrt(dis) if r <= R and dis != 0: xx = 2 * R * disX * math.acos(math.sqrt(R * R - dis) / R) / (theta * r) + cenX yy = 2 * R * disY * math.acos(math.sqrt(R * R - dis) / R) / (theta * r) + cenY xx = ny.around(ny.array(xx)) yy = ny.around(ny.array(yy)) if width >= yy >= 1 and height >= xx >= 1: pOut[x + cenX, y + cenY, k] = pIn[xx, yy, k] else: pOut[x + cenX, y + cenY, k] = pIn[x + cenX, y + cenY, k] result = Image.fromarray(ny.uint8(pOut)).convert('RGB') result.save(dst) return 0 else: graph.save(dst) return -1