图片处理

#!/usr/bin/env python
# coding: utf-8import torch
from PIL import Imageimport os
import dlib
import collections
from typing import Union, List
import numpy as np
from PIL import Image
import matplotlib.pyplot as pltimport PIL.Image
import PIL.ImageFile
import numpy as np
import scipy.ndimagedef get_dlib_face_detector(predictor_path: str = "shape_predictor_68_face_landmarks.dat"):detector = dlib.get_frontal_face_detector()shape_predictor = dlib.shape_predictor(predictor_path)def detect_face_landmarks(img: Union[Image.Image, np.ndarray]):if isinstance(img, Image.Image):img = np.array(img)faces = []dets = detector(img)for d in dets:shape = shape_predictor(img, d)faces.append(np.array([[v.x, v.y] for v in shape.parts()]))return facesreturn detect_face_landmarksdef align_and_crop_face(img: Image.Image,landmarks: np.ndarray,expand: float = 1.0,output_size: int = 1024, transform_size: int = 4096,enable_padding: bool = True,):# Parse landmarks.# pylint: disable=unused-variablelm = landmarkslm_chin          = lm[0  : 17]  # left-rightlm_eyebrow_left  = lm[17 : 22]  # left-rightlm_eyebrow_right = lm[22 : 27]  # left-rightlm_nose          = lm[27 : 31]  # top-downlm_nostrils      = lm[31 : 36]  # top-downlm_eye_left      = lm[36 : 42]  # left-clockwiselm_eye_right     = lm[42 : 48]  # left-clockwiselm_mouth_outer   = lm[48 : 60]  # left-clockwiselm_mouth_inner   = lm[60 : 68]  # left-clockwise# Calculate auxiliary vectors.eye_left     = np.mean(lm_eye_left, axis=0)eye_right    = np.mean(lm_eye_right, axis=0)eye_avg      = (eye_left + eye_right) * 0.5eye_to_eye   = eye_right - eye_leftmouth_left   = lm_mouth_outer[0]mouth_right  = lm_mouth_outer[6]mouth_avg    = (mouth_left + mouth_right) * 0.5eye_to_mouth = mouth_avg - eye_avg# Choose oriented crop rectangle.x = eye_to_eye - np.flipud(eye_to_mouth) * [-1, 1]x /= np.hypot(*x)x *= max(np.hypot(*eye_to_eye) * 2.0, np.hypot(*eye_to_mouth) * 1.8)x *= expandy = np.flipud(x) * [-1, 1]c = eye_avg + eye_to_mouth * 0.1quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y])qsize = np.hypot(*x) * 2# Shrink.shrink = int(np.floor(qsize / output_size * 0.5))if shrink > 1:rsize = (int(np.rint(float(img.size[0]) / shrink)), int(np.rint(float(img.size[1]) / shrink)))img = img.resize(rsize, PIL.Image.ANTIALIAS)quad /= shrinkqsize /= shrink# Crop.border = max(int(np.rint(qsize * 0.1)), 3)crop = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1]))))crop = (max(crop[0] - border, 0), max(crop[1] - border, 0), min(crop[2] + border, img.size[0]), min(crop[3] + border, img.size[1]))if crop[2] - crop[0] < img.size[0] or crop[3] - crop[1] < img.size[1]:img = img.crop(crop)quad -= crop[0:2]# Pad.pad = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1]))))pad = (max(-pad[0] + border, 0), max(-pad[1] + border, 0), max(pad[2] - img.size[0] + border, 0), max(pad[3] - img.size[1] + border, 0))if enable_padding and max(pad) > border - 4:pad = np.maximum(pad, int(np.rint(qsize * 0.3)))img = np.pad(np.float32(img), ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect')h, w, _ = img.shapey, x, _ = np.ogrid[:h, :w, :1]mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w-1-x) / pad[2]), 1.0 - np.minimum(np.float32(y) / pad[1], np.float32(h-1-y) / pad[3]))blur = qsize * 0.02img += (scipy.ndimage.gaussian_filter(img, [blur, blur, 0]) - img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0)img += (np.median(img, axis=(0,1)) - img) * np.clip(mask, 0.0, 1.0)img = PIL.Image.fromarray(np.uint8(np.clip(np.rint(img), 0, 255)), 'RGB')quad += pad[:2]# Transform.img = img.transform((transform_size, transform_size), PIL.Image.QUAD, (quad + 0.5).flatten(), PIL.Image.BILINEAR)if output_size < transform_size:img = img.resize((output_size, output_size), PIL.Image.ANTIALIAS)return img

图片测试

# 加载模型和面部检测
device = "cuda" if torch.cuda.is_available() else "cpu"
model = torch.hub.load("bryandlee/animegan2-pytorch:main", "generator", device=device).eval()
face2paint = torch.hub.load("bryandlee/animegan2-pytorch:main", "face2paint", device=device, side_by_side=True)
face_detector = get_dlib_face_detector()# 加载图片并动漫化
img = Image.open("1.jpeg").convert("RGB")
landmarks = face_detector(img)
for landmark in landmarks:face = align_and_crop_face(img, landmark, expand=1.3)plt.imshow(face2paint(model=model, img=face, size=512))plt.axis('off')plt.show()

结果

1.jpeg

输出

图片动漫化 - 面部 - AnimeGAN相关推荐

  1. Opencv实战【4】——图片动漫化处理

    博主联系方式: QQ:1540984562 微信:wxid_nz49532kbh9u22 QQ交流群:750313950 目录 动漫化风格的特点 处理手段 代码 实现效果 总结 动漫化风格的特点 (1 ...

  2. 百度API实现图片动漫化

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.基于百度api实现图像动漫化和人脸处理 二.所有使用到的库 1.引入库 2.完整代码 注意:图片目录不能在属性里直 ...

  3. 某音爆火,人物头像动漫化,为女朋友打造独一无二的头像【python实战:人物图片动漫化】

    大家好,我是辣条. 今天是参加CSDN新星计划的最后一天了,感谢梦想橡皮擦[擦姐]的一些指导以及擦哥直播时的建议,虽然没有直接的交流,但对于我这个写作小白来说还是很有指导意义的,新星计划只是我在CSD ...

  4. python将图片转动漫_如何把照片变成手绘动漫化?

    看了各位答主的回答,大都是利用 ps 或者是相应的 app 来实现动漫效果的. 其中的原理大同小异,大家看我这篇文章就足够了! 1.普通照片和动漫图片的区别 要想搞清楚怎么变成动漫化风格,首先就要明白 ...

  5. AnimeGANv2:照片动漫化

    简介 本文介绍一个很有趣的基于GAN来将现实图片动漫化的项目,为AnimeGANv2,它可以将现实场景的图片处理为动漫画分,目前支持宫崎骏.新海诚和今敏的三种风格.由于原作者使用的是TensorFlo ...

  6. 利用python+百度智能云为人物头像动漫化(附API代码及SDK代码)

    文章目录 前言 1.2.打开第一个搜索结果 1.3.点击立即使用 1.4.创建应用 1.5.获取APPID等参数 二.API与SDK的使用 1.API代码 2.SDK使用 2.1.首先下载python ...

  7. 视频图片 超分与动漫化+补帧

    转自AI Studio,原文链接:视频&图片 超分与动漫化+补帧 - 飞桨AI Studio 1.安装依赖 由于视频不便展示,均放到项目根目录下,以便于查看 需要自行建立piece目录和pie ...

  8. 人脸动漫化——AnimeGAN快速上手

    最近看到Github上有个项目很火,于是尝试clone下玩玩. 项目名:animegan2-pytorch 项目地址:https://github.com/bryandlee/animegan2-py ...

  9. 人脸动漫化AnimeGAN V2的具体实施步骤

    本文提供应对课设,毕设的小伙伴们一些参考:项目是参照网络上比较火的一个项目AnimeGAN V2,然后当时的一个课题也是跟这个相关,然后我是直接调用这个的,但是中间遇到过很多的问题,现在想说明一些问题 ...

最新文章

  1. spring boot 2.0 源码分析(二)
  2. ubuntu安装redis的方法以及PHP安装redis扩展、CI框架sess使用redis的方法
  3. zend studio让编译器导入项目为utf-8如何设置
  4. Documentum中关于日期时间类型字段的特殊处理
  5. Oracle 实例恢复时 前滚(roll forward) 后滚(roll back) 问题
  6. IntelliJ IDEA的几种常见的快捷键
  7. 连接端口 配置hive_Zeppelin带有Kerberos认证的Hive解释器的配置
  8. java.lang包—枚举类Enum
  9. (LeetCode 83)Remove Duplicates from Sorted Lists
  10. java 异或_脚本语言系列之Java | Java中的运算符
  11. Windows CE.0002.ERROR:Image is too large for current RAM and RAMIMAGE settings.
  12. RabbitMQ单机瞎玩(2)
  13. KDD2021|小红书在推荐多样化的实践——SSD
  14. 快速使用redis保存session信息
  15. 计算机组成原理在线实验,计算机组成原理编程实验
  16. linux cpu load命令,Linux性能检测常用的10个基本命令
  17. DevOps团队如何选择监控工具
  18. Codeforces Round #439 (Div. 2) E. The Untended Antiquity (hash+数状数组)
  19. 参考文献格式字号字体_字体字号格式要求
  20. 人工智能概念类知识点总结

热门文章

  1. 图形算法:圆形生成算法
  2. 在word2016中对图片插入题注
  3. css3自定义字体、字体图标
  4. MacBook Pro设置外接显示器竖屏显示,2k 字体小
  5. hex颜色透明度对比表
  6. 基于W5500的实时远程温湿度监控系统
  7. wps手机版ppt动画效果_你不知道的WPS|仅这2个制作PPT的动画技能,就足够让你的PPT逼格瞬间提升...
  8. 浅析商业银行保理业务风险类型
  9. 第88章、系统服务之NOTIFICATION_SERVICE服务(从零开始学Android)
  10. 二维码生成器怎么在线制作?制作二维码其实很简单