-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_desktop.py
More file actions
403 lines (276 loc) · 8.92 KB
/
app_desktop.py
File metadata and controls
403 lines (276 loc) · 8.92 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""
FACESWAP KILAT DESKTOP PRO
Versi 6.0 Professional UI Edition
----------------------------------------------------
Realtime FaceSwap Desktop Native dengan UI profesional.
Fitur:
• Professional sidebar UI
• Thumbnail wajah sumber
• Drag & drop source image
• Switch camera realtime
• Record video
• Virtual camera output
• ROI swap ultra fast
• 30–45 FPS CPU
• Clean exit
Coder : Deddy Ratnanto
https://github.com/drat
"""
# ================= IMPORT =================
import sys,cv2,pickle,hashlib,time
import numpy as np
import pyvirtualcam
from pathlib import Path
from PyQt5.QtWidgets import (
QApplication,QMainWindow,QLabel,QPushButton,
QVBoxLayout,QHBoxLayout,QWidget,QComboBox,
QFileDialog,QFrame)
from PyQt5.QtCore import QTimer,Qt
from PyQt5.QtGui import QImage,QPixmap,QFont
from core.face_processor import FaceProcessor
# ================= CONFIG =================
CACHE_DIR=Path("embedding_cache")
CACHE_DIR.mkdir(exist_ok=True)
DETECT_INTERVAL=20
DETECT_WIDTH=320
DISPLAY_WIDTH=720
# ================= LOAD MODEL =================
print("Loading model...")
processor=FaceProcessor()
print("Model ready")
# ================= HASH =================
def hash_image(img):
"""hash image untuk cache"""
return hashlib.md5(img.tobytes()).hexdigest()
# ================= SAVE EMBEDDING =================
def save_embed(h,face):
data={
"embedding":face.embedding,
"normed_embedding":face.normed_embedding,
"bbox":face.bbox,
"kps":face.kps}
pickle.dump(data,open(CACHE_DIR/f"{h}.pkl","wb"))
# ================= LOAD EMBEDDING =================
def load_embed(h):
path=CACHE_DIR/f"{h}.pkl"
if not path.exists():
return None
try:
data=pickle.load(open(path,"rb"))
except:
return None
class Face:pass
face=Face()
face.embedding=data["embedding"]
face.normed_embedding=data["normed_embedding"]
face.bbox=data["bbox"]
face.kps=data["kps"]
return face
# ================= GET SOURCE =================
def get_source_face(img):
h=hash_image(img)
cached=load_embed(h)
if cached:
return cached
faces=processor.get_faces(img)
if not faces:
return None
face=faces[0]
save_embed(h,face)
return face
# ================= DETECT =================
def detect_face(frame):
h,w,_=frame.shape
scale=DETECT_WIDTH/w
small=cv2.resize(frame,(DETECT_WIDTH,int(h*scale)))
faces=processor.get_faces(small)
if not faces:
return None
face=faces[0]
face.bbox/=scale
face.kps/=scale
return face
# ================= ROI SWAP =================
def swap_roi(frame,target,source):
x1,y1,x2,y2=map(int,target.bbox)
pad=80
x1=max(0,x1-pad)
y1=max(0,y1-pad)
x2=min(frame.shape[1],x2+pad)
y2=min(frame.shape[0],y2+pad)
roi=frame[y1:y2,x1:x2]
faces=processor.get_faces(roi)
if faces:
roi=processor.swapper.get(roi,faces[0],source,paste_back=True)
frame[y1:y2,x1:x2]=roi
return frame
# ================= MAIN APP =================
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("FaceSwap Kilat Desktop Pro")
self.resize(1100,700)
self.source=None
self.target=None
self.frame_count=0
self.record=False
self.writer=None
self.prev_time=time.time()
self.init_camera()
self.init_ui()
self.timer=QTimer()
self.timer.timeout.connect(self.loop)
self.timer.start(16)
# ===== INIT CAMERA =====
def init_camera(self,index=0):
self.cap=cv2.VideoCapture(index)
self.cap.set(3,1280)
self.cap.set(4,720)
w=int(self.cap.get(3))
h=int(self.cap.get(4))
self.virtual=pyvirtualcam.Camera(w,h,30)
# ===== UI =====
def init_ui(self):
font=QFont("Arial",10)
QApplication.instance().setFont(font)
container=QHBoxLayout()
# SIDEBAR
sidebar=QVBoxLayout()
logo=QLabel("FaceSwap Kilat")
logo.setStyleSheet("color:white;font-size:18px;font-weight:bold")
sidebar.addWidget(logo)
load=QPushButton("Load Source")
load.clicked.connect(self.load_source)
sidebar.addWidget(load)
self.source_preview=QLabel()
self.source_preview.setFixedSize(160,160)
self.source_preview.setStyleSheet(
"background:#020617;border:2px solid #1e293b;border-radius:10px")
sidebar.addWidget(self.source_preview)
self.source_status=QLabel("No source")
self.source_status.setStyleSheet("color:#ef4444")
sidebar.addWidget(self.source_status)
self.cam=QComboBox()
self.cam.addItems(["Camera 0","Camera 1","Camera 2"])
self.cam.currentIndexChanged.connect(self.switch_cam)
sidebar.addWidget(self.cam)
self.rec=QPushButton("Record")
self.rec.clicked.connect(self.toggle_record)
sidebar.addWidget(self.rec)
exit_btn=QPushButton("Exit")
exit_btn.clicked.connect(self.close)
sidebar.addWidget(exit_btn)
sidebar.addStretch()
# PREVIEW AREA
preview_layout=QVBoxLayout()
self.preview=QLabel()
self.preview.setAlignment(Qt.AlignCenter)
self.preview.setStyleSheet(
"background:black;border-radius:12px")
preview_layout.addWidget(self.preview)
container.addLayout(sidebar,1)
container.addLayout(preview_layout,4)
widget=QWidget()
widget.setLayout(container)
self.setCentralWidget(widget)
self.setStyleSheet("""
QMainWindow{background:#0f172a;}
QPushButton{
background:#2563eb;
color:white;
padding:6px;
border-radius:6px;}
QPushButton:hover{background:#3b82f6;}
QComboBox{
background:#1e293b;
color:white;
padding:4px;}
QLabel{color:white;}
""")
# ===== DRAG DROP =====
def dragEnterEvent(self,event):
if event.mimeData().hasUrls():
event.accept()
def dropEvent(self,event):
path=event.mimeData().urls()[0].toLocalFile()
self.load_source_file(path)
# ===== LOAD SOURCE =====
def load_source(self):
path,_=QFileDialog.getOpenFileName()
if path:
self.load_source_file(path)
def load_source_file(self,path):
img=cv2.imread(path)
face=get_source_face(img)
if face:
self.source=face
x1,y1,x2,y2=map(int,face.bbox)
face_img=img[y1:y2,x1:x2]
thumb=cv2.resize(face_img,(160,160))
thumb=cv2.cvtColor(thumb,cv2.COLOR_BGR2RGB)
h,w,ch=thumb.shape
qt=QImage(thumb.data,w,h,ch*w,QImage.Format_RGB888)
self.source_preview.setPixmap(QPixmap.fromImage(qt))
self.source_status.setText("Source loaded")
self.source_status.setStyleSheet("color:#22c55e")
# ===== SWITCH CAMERA =====
def switch_cam(self,i):
self.cap.release()
self.virtual.close()
self.init_camera(i)
# ===== RECORD =====
def toggle_record(self):
if not self.record:
fourcc=cv2.VideoWriter_fourcc(*"mp4v")
self.writer=cv2.VideoWriter(
"record.mp4",fourcc,30,
(int(self.cap.get(3)),int(self.cap.get(4))))
self.record=True
self.rec.setText("Stop")
else:
self.record=False
self.writer.release()
self.rec.setText("Record")
# ===== LOOP =====
def loop(self):
ret,frame=self.cap.read()
if not ret:
return
self.frame_count+=1
if self.frame_count%DETECT_INTERVAL==0 or self.target is None:
face=detect_face(frame)
if face:
self.target=face
if self.source and self.target:
frame=swap_roi(frame,self.target,self.source)
if self.record:
self.writer.write(frame)
self.virtual.send(frame)
self.virtual.sleep_until_next_frame()
now=time.time()
fps=1/(now-self.prev_time)
self.prev_time=now
cv2.putText(frame,f"{int(fps)} FPS",(20,40),
cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
display=cv2.resize(frame,(DISPLAY_WIDTH,
int(frame.shape[0]*DISPLAY_WIDTH/frame.shape[1])))
rgb=cv2.cvtColor(display,cv2.COLOR_BGR2RGB)
h,w,ch=rgb.shape
qt=QImage(rgb.data,w,h,ch*w,QImage.Format_RGB888)
self.preview.setPixmap(QPixmap.fromImage(qt))
# ===== CLEAN EXIT =====
def closeEvent(self,event):
try:
self.timer.stop()
self.cap.release()
self.virtual.close()
if self.writer:
self.writer.release()
except:
pass
event.accept()
# ================= RUN =================
app=QApplication(sys.argv)
win=App()
win.show()
sys.exit(app.exec())