-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
134 lines (114 loc) · 4.87 KB
/
Copy pathapp.py
File metadata and controls
134 lines (114 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import sys
import telebot
import cv2 as cv
import numpy as np
from math import atan
from math import degrees
from telebot import types
from flask import Flask, request
sys.path.append('config/')
import config
import os
bot = telebot.TeleBot(os.environ.get('TOKEN', 'placeholder'))
if "HEROKU" in list(os.environ.keys()):
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)
server = Flask(__name__)
@server.route("/bot", methods=['POST'])
def getMessage():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "!", 200
@server.route("/")
def webhook():
bot.remove_webhook()
bot.set_webhook(url="https://memeser.herokuapp.com/bot") # этот url нужно заменить на url вашего Хероку приложения
return "?", 200
server.run(host="0.0.0.0", port=os.environ.get('PORT', 80))
else:
bot.remove_webhook()
bot.polling(none_stop=True)
face_cascade = cv.CascadeClassifier('config/haarcascade_frontalface_default.xml')
eye_cascade = cv.CascadeClassifier('config/haarcascade_eye.xml')
@bot.message_handler(commands=['help'])
def send_help_message(message):
#print(message)
bot.send_message(message.chat.id, 'Send me a picture with your face, and choose memes!')
@bot.message_handler(content_types=['photo'])
def handle_photo(message):
file_info = bot.get_file(message.photo[len(message.photo) - 1].file_id)
users_photo = bot.download_file(file_info.file_path)
path = 'pictures/user_pictures/' + str(message.from_user.id) + '.jpg' #change extention by regex
f = open(path, 'w')
f.write(users_photo)
f.close()
markup = types.ReplyKeyboardMarkup()
markup.row('Deal With It', 'Pepe Frog')
markup.row('test2', 'test3')
bot.send_message(message.chat.id, "Choose one memes:", reply_markup=markup)
@bot.message_handler(content_types=['text'])
def handle_memes(message):
if message.text == 'Deal With It':
deal_with_it(message)
if message.text == 'Pepe Frog':
pepe_frog(message)
if message.text == 'test2':
#test2()
pass
if message.text == 'test3':
#test3()
pass
def deal_with_it(message):
path = 'pictures/user_pictures/' + str(message.from_user.id) + '.jpg' #change extention by regex
result = 'pictures/result/' + str(message.from_user.id) + '.jpg' #change extention by regex
image = cv.imread(path, 1)
gray = cv.imread(path, 0)
glass = cv.imread('pictures/parts/glass.jpg')
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
#(ex,ey,ew,eh)
'''
first_middle_x = (eyes[0][0]*2+eyes[0][2])/2
first_middle_y = (eyes[0][1]*2+eyes[0][3])/2
second_middle_x = (eyes[1][0]*2+eyes[1][2])/2
second_middle_y = (eyes[1][1]*2+eyes[1][3])/2
angle = int(degrees(atan((second_middle_x-first_middle_x)/(second_middle_y-first_middle_y))))
rot_mat = cv.getRotationMatrix2D((glass.shape[0]/2,glass.shape[1]/2),angle,1)
glass = cv.warpAffine(glass, rot_mat, glass.shape)
'''
glass = cv.resize(glass, (eyes[1][0]+eyes[1][2]-eyes[0][0], eyes[0][3]))
y1, y2 = y, y + glass.shape[0]
x1, x2 = x, x + glass.shape[1]
alpha_glass = glass[:, :, 1] / 255.0 + glass[:, :, 2] / 255.0
alpha_image = 1.0 - alpha_glass
for c in range(0, 3):
image[y1:y2, x1:x2, c] = (alpha_glass * glass[:, :, c] + alpha_image * image[y1:y2, x1:x2, c])
cv.imwrite(result,image)
bot.send_photo(message.chat.id, open(result, 'r'))
def pepe_frog(message):
path = 'pictures/user_pictures/' + str(message.from_user.id) + '.jpg' #change extention by regex
result = 'pictures/result/' + str(message.from_user.id) + '.jpg' #change extention by regex
image = cv.imread(path, 1)
gray = cv.imread(path, 0)
pepe = cv.imread('pictures/parts/pepe.jpg')
alpha_pepe = cv.imread('pictures/parts/pepe_alpha.jpg')
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
pepe = cv.resize(pepe, (w, h))
alpha_pepe = cv.resize(alpha_pepe, (w, h))
y1, y2 = y, y + pepe.shape[0]
x1, x2 = x, x + pepe.shape[1]
frame = image[y1:y2, x1:x2]
pepe = pepe.astype(float)
frame = frame.astype(float)
alpha_pepe = alpha_pepe.astype(float)/255
pepe = cv.multiply(alpha_pepe, pepe)
frame = cv.multiply(1.0 - alpha_pepe, frame)
image[y1:y2, x1:x2] = cv.add(pepe, frame)
cv.imwrite(result,image)
bot.send_photo(message.chat.id, open(result, 'r'))