-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
179 lines (127 loc) · 5.54 KB
/
bot.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import argparse
import numpy as np
from scipy.special import softmax
import cv2 as cv
import telebot
import io
from threading import Lock
import time
parser = argparse.ArgumentParser()
parser.add_argument('--token', help='Telegram bot token', required=True)
parser.add_argument('--max_frames', help='Limit maximum number of frames in video per request', default=300)
parser.add_argument('--delay', help='Limit time in seconds for interactions', type=int, default=0)
args = parser.parse_args()
bot = telebot.TeleBot(args.token)
mutex = Lock()
class HairSegmentation(object):
def __init__(self):
self.net = cv.dnn.readNet("hair_segmentation.tflite")
def _mix_prev_mask(self, prev_mask, new_mask):
combine_with_prev_ratio = 0.9
eps = 1e-3
uncertainty_alpha = 1.0 + (new_mask * np.log(new_mask + eps) + (1.0 - new_mask) * np.log(1.0 - new_mask + eps)) / np.log(2.0)
uncertainty_alpha = np.clip(uncertainty_alpha, 0, 1)
uncertainty_alpha *= 2.0 - uncertainty_alpha
mixed_mask = new_mask * uncertainty_alpha + prev_mask * (1.0 - uncertainty_alpha)
return mixed_mask * combine_with_prev_ratio + (1.0 - combine_with_prev_ratio) * new_mask
def process_image(self, frame, color, num_runs=2):
prev_mask = np.zeros((512, 512), dtype=np.float32)
color = np.ones(frame.shape, dtype=np.uint8) * color
# Prepare input
blob = cv.dnn.blobFromImage(frame, 1.0 / 255, (512, 512), swapRB=True)
blob = np.concatenate((blob, prev_mask.reshape(1, 1, 512, 512)), axis=1)
for i in range(num_runs):
# Copy previous frame mask to a new tensor
blob[0, 3] = prev_mask
# Run network
self.net.setInput(blob)
out = self.net.forward()
out = softmax(out, axis=1)
mask = out[0, 1]
prev_mask = self._mix_prev_mask(prev_mask, mask)
mask = cv.resize(prev_mask, (frame.shape[1], frame.shape[0]))
lum = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) / 255
mask *= lum
mask = np.repeat(np.expand_dims(mask, axis=-1), 3, axis=-1)
result = (mask * (color.astype(np.float32) - frame) + frame).astype(np.uint8)
return result
# def process_video(self, cap, out_cap):
# prev_mask = np.zeros((512, 512), dtype=np.float32)
# color = np.zeros((384, 384, 3), dtype=np.uint8)
# color[:, :, 0] = 255
# num_frames = 0
# while cap.isOpened() and num_frames < args.max_frames:
# has_frame, frame = cap.read()
# if not has_frame:
# break
# num_frames += 1
# # Prepare input
# blob = cv.dnn.blobFromImage(frame, 1.0 / 255, (512, 512), swapRB=True)
# # Copy previous frame mask to a new tensor
# blob = np.concatenate((blob, prev_mask.reshape(1, 1, 512, 512)), axis=1)
# # Run network
# self.net.setInput(blob)
# out = self.net.forward()
# out = softmax(out, axis=1)
# mask = out[0, 1]
# prev_mask = self._mix_prev_mask(prev_mask, mask)
# mask = cv.resize(prev_mask, (frame.shape[1], frame.shape[0]))
# lum = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) / 255
# mask *= lum
# mask = np.repeat(np.expand_dims(mask, axis=-1), 3, axis=-1)
# frame = (mask * (color.astype(np.float32) - frame) + frame).astype(np.uint8)
# out_cap.write(frame)
model = HairSegmentation()
colors = {}
timestamps = {}
# def process_video(inp_file_name, out_file_name):
# cap = cv.VideoCapture(inp_file_name)
# out = cv.VideoWriter(out_file_name, cv.VideoWriter_fourcc(*'mp4v'), 30, (384, 384))
# model.process(cap, out)
# def send_video(message, video_path):
# with open(video_path, 'rb') as f:
# bot.send_video(chat_id=message.chat.id, video=f)
def get_image(message):
fileID = message.photo[-1].file_id
file = bot.get_file(fileID)
data = bot.download_file(file.file_path)
buf = np.frombuffer(data, dtype=np.uint8)
return cv.imdecode(buf, cv.IMREAD_COLOR)
def send_image(message, img):
_, buf = cv.imencode(".jpg", img, [cv.IMWRITE_JPEG_QUALITY, 90])
outputbuf = io.BytesIO(buf)
bot.send_photo(message.chat.id, outputbuf)
# @bot.message_handler(content_types=['video_note'])
# def process_image(message):
# f = bot.get_file(message.video_note.file_id)
# data = bot.download_file(f.file_path)
# inp_file_name = 'tmp_in.mp4'
# out_file_name = 'tmp_out.mp4'
# with open(inp_file_name, 'wb') as f:
# f.write(data)
# process_video(inp_file_name, out_file_name)
# send_video(message, out_file_name)
@bot.message_handler(content_types=['photo'])
def process_image(message):
chat_id = message.chat.id
now = time.time()
timestamp = timestamps.get(chat_id, 0)
if now - timestamp < args.delay:
bot.send_message(chat_id, f"Try again after {args.delay - int(now - timestamp)} seconds")
return
timestamps[chat_id] = now
mutex.acquire()
color = colors.get(chat_id, [255, 0, 0])
img = get_image(message)
stylized = model.process_image(img, color)
send_image(message, stylized)
mutex.release()
@bot.message_handler(commands=['color'])
def process_image(message):
color_hex = message.text.split(' ')[1]
if not color_hex.startswith('#') or len(color_hex) != 7:
return
color_hex = color_hex[1:]
color_bgr = [int(color_hex[i:i + 2], 16) for i in (4, 2, 0)]
colors[message.chat.id] = color_bgr
bot.polling()