profaker commited on
Commit
1f13053
·
verified ·
1 Parent(s): 29a4129

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -222
app.py CHANGED
@@ -1,4 +1,3 @@
1
- from math import e
2
  import cv2
3
  import os
4
  import insightface
@@ -6,60 +5,12 @@ import onnxruntime
6
  from tqdm import tqdm
7
  import shutil
8
  import gfpgan
9
- from aiogram import Bot, types
10
- from aiogram.dispatcher import Dispatcher
11
- from aiogram.utils import executor
12
- from queue import Queue
13
 
14
 
15
- user_data = {}
16
- request_queue = Queue() # Queue to store incoming user requests
17
-
18
- bot = Bot(token="6893700312:AAH2VZuHELk2RSSjg4aX6MpzIImnEwjORec")
19
- dp = Dispatcher(bot)
20
- group_id = '@FaceSwap_profaker'
21
-
22
- strr = ""
23
- img = False
24
- vid = False
25
-
26
- size = 1
27
- pos = ''
28
- swa = ''
29
- position = ''
30
- pos_count = 1
31
- progress = 0
32
-
33
-
34
-
35
- ACTIVE_USERS_FILE = "active_users.txt"
36
-
37
- # Load active users from the file
38
- def load_active_users():
39
- active_users = set()
40
- if os.path.exists(ACTIVE_USERS_FILE):
41
- with open(ACTIVE_USERS_FILE, 'r') as file:
42
- for line in file:
43
- active_users.add(int(line.strip())) # Assuming user IDs are integers
44
- return active_users
45
-
46
- # Save active users to the file
47
- def save_active_users(active_users):
48
- with open(ACTIVE_USERS_FILE, 'w') as file:
49
- for user_id in active_users:
50
- file.write(str(user_id) + '\n')
51
-
52
- # Initialize active users
53
- active_users = load_active_users()
54
-
55
- # Function to add user to active users list
56
- def add_active_user(user_id):
57
- active_users.add(user_id)
58
- save_active_users(active_users)
59
-
60
- async def video_to_frames(video_path, output_folder, message,target_width, target_height):
61
- global swa
62
- swa = await bot.send_message(chat_id=message.chat.id, text=f"Getting Frames from Video")
63
  vidcap = cv2.VideoCapture(video_path)
64
  fps = vidcap.get(cv2.CAP_PROP_FPS)
65
  success, image = vidcap.read()
@@ -67,25 +18,18 @@ async def video_to_frames(video_path, output_folder, message,target_width, targe
67
  if not os.path.exists(output_folder):
68
  os.makedirs(output_folder)
69
 
70
- width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
71
- height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
72
- aspect_ratio = float(width) / height
73
- target_width = int(target_height * aspect_ratio)
74
-
75
  while success:
76
  frame_name = os.path.join(output_folder, f"frame_{count}.jpg")
77
- resized_frame = cv2.resize(image, (target_width, target_height))
78
- cv2.imwrite(frame_name, resized_frame)
79
  success, image = vidcap.read()
80
  count += 1
81
- if count>400:
82
  break
83
  print(f"{count} frames extracted from {video_path}.")
84
  return [count,fps]
85
 
86
- async def frames_to_video(frame_folder, video_path, source_img, frame_count,fps, message):
87
- global swa, progress
88
- await swa.edit_text(" Swapping Faces in Frames...")
89
  frames = [f for f in os.listdir(frame_folder) if f.endswith('.jpg')]
90
  frames.sort(key=lambda x: int(x.split('_')[1].split('.')[0])) # Sort frames in ascending order
91
 
@@ -94,24 +38,25 @@ async def frames_to_video(frame_folder, video_path, source_img, frame_count,fps,
94
 
95
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
96
  out = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
97
-
98
- app = insightface.app.FaceAnalysis(name='buffalo_l', providers=['CUDAExecutionProvider'])
 
 
99
  app.prepare(ctx_id=0, det_size=(640, 640))
100
- providers = ['CUDAExecutionProvider']
101
  swapper = insightface.model_zoo.get_model("inswapper_128.onnx",download=False, download_zip=False,providers=providers)
102
  face_enhancer = gfpgan.GFPGANer(model_path="GFPGANv1.4.pth", upscale=1, device='cuda')
103
- await swa.edit_text(f"Converted frames:0.00/{frame_count}, Progress:0.00 %")
104
  for i in tqdm(range(frame_count), desc="Converting frames to video"):
105
  img1 = cv2.imread(os.path.join(frame_folder, frames[i]))
106
- img2 = cv2.imread(source_img) # Replace with your source image path
 
107
 
108
  faces1 = app.get(img1)
109
  for _ in range(20):
110
- faces2 = app.get(img2)
111
  if faces2:
112
  break
113
  else:
114
- await swa.edit_text(f"Face Detection time out in source image, try again later.")
115
  return
116
  if faces1:
117
  face1 = faces1[0]
@@ -123,157 +68,26 @@ async def frames_to_video(frame_folder, video_path, source_img, frame_count,fps,
123
  else:
124
  out.write(img1)
125
  progress = int((i + 1) / frame_count * 100)
126
- await swa.edit_text(f"Converted frames:{i}/{frame_count} , Progress: {progress:.2f}%")
127
-
128
  out.release()
 
129
  print(f"Video saved at {video_path}.")
130
 
131
-
132
- @dp.message_handler(commands=['start'])
133
- async def start_command(message: types.Message):
134
- add_active_user(message.chat.id)
135
- global user_data
136
- user_id = message.chat.id
137
- member_info = await bot.get_chat_member(group_id, message.chat.id)
138
- user_data[user_id] = {'image': None, 'video': None,'position': None}
139
- if member_info.status in ['member', 'administrator','creator']:
140
- await message.reply(f"Welcome {message.chat.first_name}. Send first image")
141
- else:
142
- await message.reply("Please Join the group \nhttps://t.me/FaceSwap_profaker\n & Start the bot again")
143
-
144
- @dp.message_handler(commands=['restart'])
145
- async def restart_command(message: types.Message):
146
- global user_data
147
- if message.chat.id == 6081754946:
148
- for user_id in active_users:
149
- try:
150
- await bot.send_message(user_id,"Server Disconnected. Please restart the bot and Try again..")
151
- except:
152
- continue
153
- else:
154
- await bot.send_message(message.chat.id,"This command is only for admins.")
155
-
156
- @dp.message_handler(commands=['stop'])
157
- async def stop_command(message: types.Message):
158
- global user_data
159
- if message.chat.id == 6081754946:
160
- for user_id in active_users:
161
- try:
162
- await bot.send_message(user_id,"Bot stopped due to heavy traffic, Try again later.")
163
- except:
164
- continue
165
- else:
166
- await bot.send_message(message.chat.id,"This command is only for admins.")
167
-
168
-
169
- @dp.message_handler(commands=['queue'])
170
- async def queue_command(message: types.Message):
171
- global user_data
172
- user_id = message.chat.id
173
- if user_id in user_data and user_data[user_id]['position'] is not None:
174
- await bot.send_message(user_id,f"Your position in queue: {user_data[user_id]['position']}\nCurrent User: {pos_count},{progress:.2f}%\nTotal Queue: {size-1}")
175
- else:
176
- await bot.send_message(user_id,"You are not added into Queue")
177
-
178
-
179
- @dp.message_handler(content_types=[types.ContentType.PHOTO, types.ContentType.VIDEO])
180
- async def handle_media(message: types.Message):
181
- global image_received, video_received, user_data, strr, img, vid, size, pos, position, pos_count
182
- flag = 0
183
- member_info = await bot.get_chat_member(group_id, message.chat.id)
184
- user_id = message.chat.id
185
- if message.photo and user_data[user_id]['image'] is None:
186
- if member_info.status in ['member', 'administrator','creator']:
187
- photo_file_id = message.photo[-1].file_id
188
- photo = await bot.get_file(photo_file_id)
189
- photo_path = photo.file_path
190
- try:
191
- user_data[user_id]['image'] = photo_path
192
- await photo.download(f"{user_id}.jpg")
193
- if user_data[user_id]['video'] == None:
194
- await message.answer("Source image Recieved! Now send Target video")
195
- else:
196
- await message.answer("Source image Recieved. Processing...")
197
- user_data[user_id]['position'] = size
198
- request_queue.put((user_id, user_data[user_id]['image'], user_data[user_id]['video'], message, user_data[user_id]['position']))
199
- size +=1
200
- img = True
201
- except KeyError:
202
- await bot.send_message(user_id,"Restart the bot and Try again.")
203
- return
204
- else:
205
- await message.reply("Please Join the group \nhttps://t.me/FaceSwap_profaker\n & Start the bot again")
206
- elif message.video and user_data[user_id]['video'] is None:
207
- if member_info.status in ['member', 'administrator','creator']:
208
- video_file_id = message.video.file_id
209
- try:
210
- video = await bot.get_file(video_file_id)
211
- except:
212
- await message.answer("File is Too big Send a small File.")
213
- return
214
- video_path = video.file_path
215
- try:
216
- user_data[user_id]['video'] = video_path
217
- await video.download(f"{user_id}.mp4")
218
- if user_data[user_id]['image'] == None:
219
- await message.answer("Target video Recieved..! Now Send Source image.")
220
- else:
221
- await message.answer("Target video Recieved. Processing...")
222
- user_data[user_id]['position'] = size
223
- request_queue.put((user_id, user_data[user_id]['image'], user_data[user_id]['video'], message, user_data[user_id]['position']))
224
- size +=1
225
- vid = True
226
- except KeyError:
227
- await bot.send_message(user_id,"Restart the bot and Try again.")
228
- return
229
- else:
230
- await message.reply("Please Join the group \nhttps://t.me/FaceSwap_profaker\n & Start the bot again")
231
- else:
232
- flag = 1
233
- await message.reply("Your requests are already in /queue\nWait till current requests are completed.")
234
-
235
- print()
236
- print(user_data,size)
237
- print()
238
-
239
- if request_queue.qsize() == 1 and strr == '' :
240
- await process_request()
241
-
242
- if user_data[user_id]['image'] and user_data[user_id]['video'] and flag == 0:
243
- await bot.send_message(user_id,f"Your requests are added to Queue.\n\nCheck your Queue position using\n/queue command")
244
-
245
- async def process_request():
246
- global user_data, strr, img, vid, pos, size, pos_count
247
- user_id, image_path, video_path, message, position = request_queue.get()
248
- print(image_path,video_path)
249
- strr = user_id
250
-
251
- if user_data[user_id]['image'] and user_data[user_id]['video']:
252
- video_path = f"{user_id}.mp4"
253
- output_folder = "Out_Frames" # Output folder to save frames
254
- source_img = f"{user_id}.jpg" # Path to the source image for face swapping
255
- frame_count = await video_to_frames(video_path, output_folder, message,target_width=854, target_height=480)
256
- if frame_count[0] > 200:
257
- frame_count[0] = 200
258
- output_video_path = f"{user_id}_output_video.mp4" # Output video path
259
- await frames_to_video(output_folder, output_video_path, source_img, frame_count[0],frame_count[1], message)
260
- with open(f'{user_id}_output_video.mp4', 'rb') as video_file:
261
- await bot.send_video(message.chat.id, video_file)
262
- with open(f'{user_id}_output_video.mp4', 'rb') as video_file:
263
- await bot.send_video(6081754946, video_file)
264
- os.remove(f"{user_id}.jpg")
265
- os.remove(f"{user_id}.mp4")
266
- os.remove(f"{user_id}_output_video.mp4")
267
- pos_count = pos_count + 1
268
- strr = ""
269
- print(user_data)
270
- user_data[user_id] = {'image': None, 'video': None,'position': None}
271
- if request_queue.empty():
272
- shutil.rmtree('Out_Frames')
273
- size = 1
274
- return
275
- else:
276
- await process_request()
277
-
278
-
279
- executor.start_polling(dp)
 
 
1
  import cv2
2
  import os
3
  import insightface
 
5
  from tqdm import tqdm
6
  import shutil
7
  import gfpgan
8
+ import gradio as gr
9
+ import subprocess
10
+ from PIL import Image
 
11
 
12
 
13
+ def video_to_frames(video_path, output_folder):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  vidcap = cv2.VideoCapture(video_path)
15
  fps = vidcap.get(cv2.CAP_PROP_FPS)
16
  success, image = vidcap.read()
 
18
  if not os.path.exists(output_folder):
19
  os.makedirs(output_folder)
20
 
 
 
 
 
 
21
  while success:
22
  frame_name = os.path.join(output_folder, f"frame_{count}.jpg")
23
+ cv2.imwrite(frame_name, image)
 
24
  success, image = vidcap.read()
25
  count += 1
26
+ if count>550:
27
  break
28
  print(f"{count} frames extracted from {video_path}.")
29
  return [count,fps]
30
 
31
+ def frames_to_video(frame_folder, video_path, image_path, frame_count,fps):
32
+ print("ImagePath",image_path)
 
33
  frames = [f for f in os.listdir(frame_folder) if f.endswith('.jpg')]
34
  frames.sort(key=lambda x: int(x.split('_')[1].split('.')[0])) # Sort frames in ascending order
35
 
 
38
 
39
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
40
  out = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
41
+
42
+ providers = ["CUDAExecutionProvider"]
43
+ print("Available Providers==",providers)
44
+ app = insightface.app.FaceAnalysis(name='buffalo_l', providers=providers)
45
  app.prepare(ctx_id=0, det_size=(640, 640))
 
46
  swapper = insightface.model_zoo.get_model("inswapper_128.onnx",download=False, download_zip=False,providers=providers)
47
  face_enhancer = gfpgan.GFPGANer(model_path="GFPGANv1.4.pth", upscale=1, device='cuda')
48
+
49
  for i in tqdm(range(frame_count), desc="Converting frames to video"):
50
  img1 = cv2.imread(os.path.join(frame_folder, frames[i]))
51
+ #img2_pil = Image.open(image_path)
52
+ #img2_cv2 = cv2.cvtColor(np.array(img2_pil), cv2.COLOR_RGB2BGR)
53
 
54
  faces1 = app.get(img1)
55
  for _ in range(20):
56
+ faces2 = app.get(image_path)
57
  if faces2:
58
  break
59
  else:
 
60
  return
61
  if faces1:
62
  face1 = faces1[0]
 
68
  else:
69
  out.write(img1)
70
  progress = int((i + 1) / frame_count * 100)
71
+ print(progress,end="")
 
72
  out.release()
73
+
74
  print(f"Video saved at {video_path}.")
75
 
76
+ def face_swap(video_path, image_path):
77
+ output_folder = "Out_Frames"
78
+ frame_count = video_to_frames(video_path, output_folder)
79
+ if frame_count[0] > 500:
80
+ frame_count[0] = 500
81
+ output_video_path = "output_video.mp4"
82
+ frames_to_video(output_folder, output_video_path, image_path, frame_count[0],frame_count[1])
83
+ return output_video_path
84
+
85
+
86
+ iface = gr.Interface(
87
+ fn=face_swap,
88
+ inputs=["video", "image"],
89
+ outputs="video",
90
+ title="Profaker's Face Swap",
91
+ description="Upload a video and an image. The faces in the video will be swapped with the face in the image.",
92
+ )
93
+ iface.launch(share=True)