Materials for presentation at LT
I tried to make the face recognition (OpenCV) that I have done so far into a bot
The item description is as follows
** Somehow! ** **
Until now, I've already made it with Slack Bot.
But I wonder if LINE Bot is ...
That's right, ** Discord can make bots, right? ?? ?? ** **
I researched Discord Bot
It looks fun so I made it ☆
How to make a Discord bot has been updated the other day. (It's a little rough ...)
What you need is as follows Install what you need!
pip install discord #Used when you want to do various things with discord
pip install opencv-python #Required when using OpenCV functions (used with cv2 on python)
pip install os #You can use functions that depend on os
pip install requests #Used for HTTP communication
pip install shutil #High-level manipulation of files and collections of files
The directory structure is as follows
-- dis_test
-- cascade
-- haarcascade_frontalface_alt.xml
-- img
-- dis.py
Face learning data is here I used.
Discord Bot
dis_bot.py
# coding:utf-8
import discord
import time
import requests
import shutil
import cv2
import os
# from scripts.photo_processing import PhotoProcessing
IMG_PATH = "./img/"
IMG_SIZE = (128, 128)
FACE_CASCADE_PATH = "./cascade/haarcascade_frontalface_alt.xml"
FACE_CASCADE = cv2.CascadeClassifier(FACE_CASCADE_PATH)
TOKEN = "TOKEN ID"
client = discord.Client()
now_time = time.time()
times = time.gmtime(now_time)
#bot startup processing
@client.event
async def on_ready():
channel = client.get_channel(Channel ID)
await channel.send("Hi! how are you?")
#When receiving a message
@client.event
async def on_message(message):
#Ignore if sender is a bot
if message.author.bot:
return
if message.content.startswith('trim') and len(message.attachments) != 0:
channel_id = message.channel.id
url = message.attachments[0].url
file_name = url.rsplit("/", 1)[1]
#Save image
rst = requests.get(url, stream = True)
open_file = open(IMG_PATH + file_name, "wb")
shutil.copyfileobj(rst.raw, open_file)
open_file.close()
#Image scrutiny
img_file = cv2.imread(IMG_PATH + file_name, cv2.IMREAD_COLOR)
# processing
gray_file = cv2.cvtColor(img_file, cv2.COLOR_BGR2GRAY)
front_face_list = FACE_CASCADE.detectMultiScale(gray_file, minSize=(50, 50), minNeighbors=3)
if len(front_face_list) > 0:
# trimming
for (x, y, width, height) in front_face_list:
trim_file = img_file[y:y+height, x:x+width]
trim_file = cv2.resize(trim_file, IMG_SIZE)
cv2.imwrite("{0}trim_{1}".format(IMG_PATH, file_name), trim_file)
await message.channel.send('', file=discord.File("{0}trim_{1}".format(IMG_PATH, file_name)))
else:
await message.channel.send("The face was not authenticated.")
#Image deletion
os.remove(IMG_PATH + file_name)
os.remove("{0}trim_{1}".format(IMG_PATH, file_name))
client.run(TOKEN)
Output result Command line
It recognizes the face properly and trims it
Of course, multiple people can trim
It seems that the cropped image can be used as an icon! (I don't use it) That's all for Discord Bot x Face Recognition (˘ω˘)
Recommended Posts