summaryrefslogtreecommitdiff
path: root/ATRI/plugins/AIchangeFace.py
diff options
context:
space:
mode:
Diffstat (limited to 'ATRI/plugins/AIchangeFace.py')
1 files changed, 76 insertions, 11 deletions
diff --git a/ATRI/plugins/AIchangeFace.py b/ATRI/plugins/AIchangeFace.py
index 1d82e12..3b72997 100644
--- a/ATRI/plugins/AIchangeFace.py
+++ b/ATRI/plugins/AIchangeFace.py
@@ -1,9 +1,11 @@
import os
+import cv2
import json
import requests
import base64
import nonebot
import time
+from pathlib import Path
from nonebot import on_command, CommandSession
import config # type: ignore
@@ -13,6 +15,7 @@ bot = nonebot.get_bot()
master = config.MASTER()
key = config.FACE_KEY()
secret = config.FACE_SECRET()
+SCALE_FACTOR = 1 # 图像的放缩比
#获取图片的人脸特征参数
@@ -27,7 +30,7 @@ def find_face(imgpath):
#换脸,函数传参中number表示两张脸的相似度为99%
-def change_face(image_1, image_2, user, number=99):
+def change_face(image_1, image_2, user, kyaru, number=99):
url = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
find_p1 = find_face(image_1)
find_p2 = find_face(image_2)
@@ -47,7 +50,13 @@ def change_face(image_1, image_2, user, number=99):
response = requests.post(url,data=data).json()
results = response['result']
image = base64.b64decode(results)
- files = f'ATRI/data/temp/face/{user}'
+ files = 'test'
+ if int(kyaru) == 1:
+ files = f'ATRI/data/temp/face/{user}'
+ elif int(kyaru) == 2:
+ files = f'ATRI/data/temp/head/{user}'
+ print(files)
+
if not os.path.exists(files):
os.mkdir(files)
with open(files + '/img3.jpg','wb') as file:
@@ -61,11 +70,11 @@ def change_face(image_1, image_2, user, number=99):
@on_command('ai_ch_face', aliases = ['AI换脸', 'ai换脸'], only_to_me = False)
async def _(session: CommandSession):
user = session.event.user_id
- with open("ATRI/plugins/switch/switch.json", 'r') as f:
+ with open(Path('.') / 'ATRI' / 'plugins' / 'switch' / 'switch.json', 'r') as f:
data = json.load(f)
if data["change_face"] == 0:
- with open('ATRI/plugins/noobList/noobList.json', 'r') as f:
+ with open(Path('.') / 'ATRI' / 'plugins' / 'noobList' / 'noobList.json', 'r') as f:
data0 = json.load(f)
if str(user) in data0.keys():
@@ -102,22 +111,78 @@ async def _(session: CommandSession):
except:
session.finish('请求数据貌似失败了...')
- img1File = f'ATRI/data/temp/face/{user}/img1.jpg'
- img2File = f'ATRI/data/temp/face/{user}/img2.jpg'
+ img1File = Path('.') / 'ATRI' / 'data' / 'temp' / 'face' / f'{user}' / 'img1.jpg'
+ img2File = Path('.') / 'ATRI' / 'data' / 'temp' / 'face' / f'{user}' / 'img2.jpg'
try:
- change_face(img1File, img2File, user)
+ change_face(img1File, img2File, user, 1)
except:
session.finish('emm...貌似失败了呢......')
time.sleep(0.5)
- doneIMG = f'ATRI/data/temp/face/{user}/img3.jpg'
+ doneIMG = Path('.') / 'ATRI' / 'data' / 'temp' / 'face' / f'{user}' / 'img3.jpg'
img = os.path.abspath(doneIMG)
await session.send(f'[CQ:image,file=file:///{img}]')
files = f'ATRI/data/temp/face/{user}'
os.remove(files)
-@on_command('change_u_head', aliases = ['接头霸王'], only_to_me = False)
-async def _(session: CommandSession):
- pass # 明天做 \ No newline at end of file
+# def f_1(x, A, B):
+# return A*x + B
+
+# @on_command('change_u_head', aliases = ['接头霸王'], only_to_me = False)
+# async def _(session: CommandSession):
+# user = session.event.user_id
+# with open("ATRI/plugins/switch/switch.json", 'r') as f:
+# data = json.load(f)
+
+# if data["change_face"] == 0:
+# with open('ATRI/plugins/noobList/noobList.json', 'r') as f:
+# data0 = json.load(f)
+
+# if str(user) in data0.keys():
+# pass
+# else:
+# img1 = session.get('img1', prompt = '请发送需要换头的图片')
+# a = img1.split(',')
+# a = a[2].replace(']', '')
+# a = a.replace('url=', '')
+# print(a)
+# try:
+# imgres1 = requests.get(a)
+# file1 = f'ATRI/data/temp/head/{user}'
+# if not os.path.exists(file1):
+# os.mkdir(file1)
+# with open(file1 + '/img1.jpg', 'wb') as f:
+# f.write(imgres1.content)
+# except:
+# session.finish('获取数据貌似失败了呢...')
+# img1File = Path('.') / 'ATRI' / 'data' / 'temp' / 'head' / f'{user}' / 'img1.jpg'
+
+# imgN = Path('.') / 'ATRI' / 'data' / 'img' / 'kyaru' / 'idk.png '
+# img = os.path.abspath(imgN)
+# await session.send(f'[CQ:image,file=:///{img}]')
+# head = session.get('head', prompt = '请输入头的序号,例如选择:1,发送:1')
+# if head.isdigit():
+# pass
+# else:
+# await session.send('请输入阿拉伯数字!')
+# return
+# headIMG = Path('.') / 'ATRI' / 'data' / 'img' / 'kyaru' / f'{int(head)}.png'
+
+# try:
+# img = cv2.imread(img1File)
+# face_cascade = cv2.CascadeClassifier(cv2.haarcascades + r'haarcascade_frontalface_default.xml')
+# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
+# faces = face_cascade.detectMultiScale(gray, scaleFactor = 1.15, minNeighbors = 5, minSize = (5, 5))
+# await session.send(faces)
+
+# except:
+# session.finish('emm...貌似焊接失败了呢......')
+
+# time.sleep(0.5)
+# doneIMG = Path('.') / 'ATRI' / 'data' / 'temp' / 'head' / f'{user}' / 'img3.jpg'
+# img = os.path.abspath(doneIMG)
+# await session.send(f'[CQ:image,file=file:///{img}]')
+# files = f'ATRI/data/temp/head/{user}'
+# os.remove(files) \ No newline at end of file