import cv2 as cvimport numpy as npmodel_bin = "D:/projects/opencv_tutorial/data/models/face_detector/opencv_face_detector_uint8.pb";config_text = "D:/projects/opencv_tutorial/data/models/face_detector/opencv_face_detector.pbtxt";def get_face(image, detect=True):if detect is not True:return image# 定义人脸ROIx = 0y = 0width = 0height = 0# 加载网络net = cv.dnn.readNetFromTensorflow(model_bin, config=config_text)h = image.shape[0]w = image.shape[1]# 人脸检测blobImage = cv.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False);net.setInput(blobImage)cvOut = net.forward()# Put efficiency information.t, _ = net.getPerfProfile()label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())cv.putText(image, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))# 绘制检测矩形count = 0for detection in cvOut[0, 0, :, :]:score = float(detection[2])if score > 0.5:left = detection[3] * wtop = detection[4] * hright = detection[5] * wbottom = detection[6] * hcount += 1x = np.int32(left - 100)y = np.int32(top - 100)height = np.int32((bottom - top) + 200)width = np.int32((right - left) + 200)if x < 0:x = 0if y < 0:y = 0if x+width > w:width = w - xif y+height > h:height = h - yif count == 1:return image[y:y + height, x:x + width, :]else:return imagedef generate_new_profile(flag_icon, avatar):mask = cv.inRange(icon, (210, 210, 210), (225, 225, 225))se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))mask = cv.morphologyEx(mask, cv.MORPH_CLOSE, se)cv.imwrite("D:/mask1.png", mask)# mask with gaussianmask = cv.GaussianBlur(mask, (5, 5), 0)cv.imshow("mask", mask)cv.imwrite("D:/mask2.png", mask)# blendh, w = mask.shape[:2]avatar = cv.resize(avatar, (w, h), interpolation=cv.INTER_CUBIC)cv.imshow("profile", avatar)result = np.zeros_like(avatar)for row in range(h):for col in range(w):pv = mask[row, col]w1 = pv / 255.0w2 = 1.0 - w1b1, g1, r1 = avatar[row, col]b2, g2, r2 = icon[row, col]b1 = b1 * w1 + b2 * w2g1 = g1 * w1 + g2 * w2r1 = r1 * w1 + r2 * w2result[row, col] = [np.int32(b1), np.int32(g1), np.int32(r1)]return resultif __name__ == "__main__":icon = cv.imread("D:/images/flag.png")src = cv.imread("D:/images/zhigang.png")cv.imshow("input", icon)cv.imshow("profile", src)avatar = get_face(src, False)result = generate_new_profile(icon, avatar)cv.imshow("result", result)cv.imwrite("D:/result.png", result)cv.waitKey(0)cv.destroyAllWindows()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。