# python环境搭建
# 安装依赖
# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ openssl-devel xorg-x11-xauth zlib* libffi-devel wget
# 下载安装包
# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
# 移动安装包
# mv Python-3.6.6.tar.xz /usr/lib/
# 解压安装包
# xz -d Python-3.6.6.tar.xz
# tar -xvf Python-3.6.6.tar
# 添加配置
# ./configure --prefix=/usr/lib/python3
# 编译安装
# make && make install
# 创建软连接
# ln -s /usr/lib/python3/bin/python3 /usr/bin/python3
# ln -s /usr/lib/python3/bin/pip3 /usr/bin/pip3
# 安装 opencv
# pip3 install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
# yum install libXrender.x86_64
# 安装 face-recognition
# yum install cmake
# yum install boost
# wget https://github.com/xianyi/OpenBLAS/archive/v0.3.7.tar.gz
# tar -zxvf v0.3.7.tar.gz
# cd OpenBLAS
# make && make install
# pip3 install dlib -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
# pip3 install face-recognition -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
# 搭建 web 服务器
# 安装 Django
# pip3 install django==2.1.8 -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
# 创建软连接
# ln -s /usr/lib/python3/bin/django-admin /usr/bin/django-admin
# 创建项目
# django-admin startproject beautymakeup
# 创建App
# django-admin startapp app
import os
import urllib
import cv2
import base64
import face_recognition
from django.shortcuts import render
from django.http.response import JsonResponse
from PIL import Image, ImageDraw
def facecosmetic(request):
# 获取请求参数
url = request.GET['path']
cosmetic = int(request.GET['cosmetic'])
# 获取文件后缀
filename = os.path.basename(url)
suffix = '.'+filename.split('.')[1]
# 保存临时文件
f = urllib.request.urlopen(url)
data = f.read()
with open('./temp'+suffix, "wb") as f:
f.write(data)
# opencv 美颜
image = cv2.imread('./temp'+suffix)
image = cv2.bilateralFilter(image, 28, 28 * 2, 28 / 2)
cv2.imwrite('./temp'+suffix, image)
# face_Recognition 口红
image = face_recognition.load_image_file("./temp"+suffix)
face_landmarks_list = face_recognition.face_landmarks(image)
for face_landmarks in face_landmarks_list:
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, cosmetic))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, cosmetic))
pil_image.show()
pil_image.save('./temp'+suffix)
# 返回数据
with open("./temp"+suffix, 'rb') as f:
base64_data = base64.b64encode(f.read())
return JsonResponse(
{
'data': base64_data.decode()
}
)