keeone commited on
Commit
ee2deb7
·
1 Parent(s): 5a50c35

Deploy video-face-swap

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .editorconfig +8 -0
  2. .flake8 +3 -0
  3. .gitattributes +4 -0
  4. .github/FUNDING.yml +2 -0
  5. .github/preview.png +3 -0
  6. .github/workflows/ci.yml +34 -0
  7. .gitignore +4 -0
  8. 1685074910001_vtqikl_2_0-boy-dp-image-77-720x704.jpg +0 -0
  9. 1685074910001_vtqikl_2_0-images.jpg +0 -0
  10. 1685074910001_vtqikl_2_0-rayul-_M6gy9oHgII-unsplash.jpg +3 -0
  11. 1685074910001_vtqikl_2_0-sexy-teen-guy-sitting-grass-nature-shirt-jeans-sexy-teen-guy-sitting-grass-nature-shirt-231383122.jpg +0 -0
  12. 1685074910001_vtqikl_2_0-sexy-teen-guy-sunglasses-sitting-grass-nature-shirt-jeans-sexy-teen-guy-sunglasses-sitting-231383074.jpg +0 -0
  13. DeepFakeAI/__init__.py +0 -0
  14. DeepFakeAI/__pycache__/__init__.cpython-310.pyc +0 -0
  15. DeepFakeAI/__pycache__/capturer.cpython-310.pyc +0 -0
  16. DeepFakeAI/__pycache__/choices.cpython-310.pyc +0 -0
  17. DeepFakeAI/__pycache__/core.cpython-310.pyc +0 -0
  18. DeepFakeAI/__pycache__/face_analyser.cpython-310.pyc +0 -0
  19. DeepFakeAI/__pycache__/face_reference.cpython-310.pyc +0 -0
  20. DeepFakeAI/__pycache__/globals.cpython-310.pyc +0 -0
  21. DeepFakeAI/__pycache__/metadata.cpython-310.pyc +0 -0
  22. DeepFakeAI/__pycache__/predictor.cpython-310.pyc +0 -0
  23. DeepFakeAI/__pycache__/typing.cpython-310.pyc +0 -0
  24. DeepFakeAI/__pycache__/utilities.cpython-310.pyc +0 -0
  25. DeepFakeAI/__pycache__/wording.cpython-310.pyc +0 -0
  26. DeepFakeAI/capturer.py +22 -0
  27. DeepFakeAI/choices.py +10 -0
  28. DeepFakeAI/core.py +3 -0
  29. DeepFakeAI/face_analyser.py +106 -0
  30. DeepFakeAI/face_reference.py +21 -0
  31. DeepFakeAI/feed.db +0 -0
  32. DeepFakeAI/globals.py +30 -0
  33. DeepFakeAI/images.db +0 -0
  34. DeepFakeAI/metadata.py +13 -0
  35. DeepFakeAI/predictor.py +3 -0
  36. DeepFakeAI/processors/__init__.py +0 -0
  37. DeepFakeAI/processors/__pycache__/__init__.cpython-310.pyc +0 -0
  38. DeepFakeAI/processors/frame/__init__.py +0 -0
  39. DeepFakeAI/processors/frame/__pycache__/__init__.cpython-310.pyc +0 -0
  40. DeepFakeAI/processors/frame/__pycache__/core.cpython-310.pyc +0 -0
  41. DeepFakeAI/processors/frame/core.py +113 -0
  42. DeepFakeAI/processors/frame/modules/__init__.py +0 -0
  43. DeepFakeAI/processors/frame/modules/__pycache__/__init__.cpython-310.pyc +0 -0
  44. DeepFakeAI/processors/frame/modules/__pycache__/face_enhancer.cpython-310.pyc +0 -0
  45. DeepFakeAI/processors/frame/modules/__pycache__/face_swapper.cpython-310.pyc +0 -0
  46. DeepFakeAI/processors/frame/modules/__pycache__/frame_enhancer.cpython-310.pyc +0 -0
  47. DeepFakeAI/processors/frame/modules/face_enhancer.py +100 -0
  48. DeepFakeAI/processors/frame/modules/face_swapper.py +105 -0
  49. DeepFakeAI/processors/frame/modules/frame_enhancer.py +88 -0
  50. DeepFakeAI/typing.py +13 -0
.editorconfig ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ root = true
2
+
3
+ [*]
4
+ end_of_line = lf
5
+ insert_final_newline = true
6
+ indent_size = 4
7
+ indent_style = tab
8
+ trim_trailing_whitespace = true
.flake8 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [flake8]
2
+ select = E3, E4, F
3
+ per-file-ignores = facefusion/core.py:E402,F401
.gitattributes CHANGED
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ 1685074910001_vtqikl_2_0-rayul-_M6gy9oHgII-unsplash.jpg filter=lfs diff=lfs merge=lfs -text
37
+ wiz-ex1.mp4 filter=lfs diff=lfs merge=lfs -text
38
+ wiz-ex11.mp4 filter=lfs diff=lfs merge=lfs -text
39
+ .github/preview.png filter=lfs diff=lfs merge=lfs -text
.github/FUNDING.yml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ github: henryruhs
2
+ custom: https://paypal.me/henryruhs
.github/preview.png ADDED

Git LFS Details

  • SHA256: 90f9882b1a3fd51272d42384f5f7a84082749eaf2fb874125b942af077045801
  • Pointer size: 132 Bytes
  • Size of remote file: 1.1 MB
.github/workflows/ci.yml ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: ci
2
+
3
+ on: [ push, pull_request ]
4
+
5
+ jobs:
6
+ lint:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - name: Checkout
10
+ uses: actions/checkout@v2
11
+ - name: Set up Python 3.10
12
+ uses: actions/setup-python@v2
13
+ with:
14
+ python-version: '3.10'
15
+ - run: pip install flake8
16
+ - run: pip install mypy
17
+ - run: flake8 run.py facefusion tests
18
+ - run: mypy run.py facefusion tests
19
+ test:
20
+ strategy:
21
+ matrix:
22
+ os: [ macos-latest, ubuntu-latest, windows-latest ]
23
+ runs-on: ${{ matrix.os }}
24
+ steps:
25
+ - name: Checkout
26
+ uses: actions/checkout@v2
27
+ - name: Set up ffmpeg
28
+ uses: FedericoCarboni/setup-ffmpeg@v2
29
+ - name: Set up Python 3.10
30
+ uses: actions/setup-python@v2
31
+ with:
32
+ python-version: '3.10'
33
+ - run: pip install -r requirements-ci.txt
34
+ - run: pytest
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .assets
2
+ .idea
3
+ .vscode
4
+
1685074910001_vtqikl_2_0-boy-dp-image-77-720x704.jpg ADDED
1685074910001_vtqikl_2_0-images.jpg ADDED
1685074910001_vtqikl_2_0-rayul-_M6gy9oHgII-unsplash.jpg ADDED

Git LFS Details

  • SHA256: aed6603c748d6b1144dfb8a41a7aa4beb950b03993e62ebb58a90496c1a73c40
  • Pointer size: 132 Bytes
  • Size of remote file: 2.76 MB
1685074910001_vtqikl_2_0-sexy-teen-guy-sitting-grass-nature-shirt-jeans-sexy-teen-guy-sitting-grass-nature-shirt-231383122.jpg ADDED
1685074910001_vtqikl_2_0-sexy-teen-guy-sunglasses-sitting-grass-nature-shirt-jeans-sexy-teen-guy-sunglasses-sitting-231383074.jpg ADDED
DeepFakeAI/__init__.py ADDED
File without changes
DeepFakeAI/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (135 Bytes). View file
 
DeepFakeAI/__pycache__/capturer.cpython-310.pyc ADDED
Binary file (865 Bytes). View file
 
DeepFakeAI/__pycache__/choices.cpython-310.pyc ADDED
Binary file (877 Bytes). View file
 
DeepFakeAI/__pycache__/core.cpython-310.pyc ADDED
Binary file (9.73 kB). View file
 
DeepFakeAI/__pycache__/face_analyser.cpython-310.pyc ADDED
Binary file (3.86 kB). View file
 
DeepFakeAI/__pycache__/face_reference.cpython-310.pyc ADDED
Binary file (647 Bytes). View file
 
DeepFakeAI/__pycache__/globals.cpython-310.pyc ADDED
Binary file (1.47 kB). View file
 
DeepFakeAI/__pycache__/metadata.cpython-310.pyc ADDED
Binary file (469 Bytes). View file
 
DeepFakeAI/__pycache__/predictor.cpython-310.pyc ADDED
Binary file (1.66 kB). View file
 
DeepFakeAI/__pycache__/typing.cpython-310.pyc ADDED
Binary file (674 Bytes). View file
 
DeepFakeAI/__pycache__/utilities.cpython-310.pyc ADDED
Binary file (7.87 kB). View file
 
DeepFakeAI/__pycache__/wording.cpython-310.pyc ADDED
Binary file (5.41 kB). View file
 
DeepFakeAI/capturer.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+ import cv2
3
+
4
+ from DeepFakeAI.typing import Frame
5
+
6
+
7
+ def get_video_frame(video_path : str, frame_number : int = 0) -> Optional[Frame]:
8
+ capture = cv2.VideoCapture(video_path)
9
+ frame_total = capture.get(cv2.CAP_PROP_FRAME_COUNT)
10
+ capture.set(cv2.CAP_PROP_POS_FRAMES, min(frame_total, frame_number - 1))
11
+ has_frame, frame = capture.read()
12
+ capture.release()
13
+ if has_frame:
14
+ return frame
15
+ return None
16
+
17
+
18
+ def get_video_frame_total(video_path : str) -> int:
19
+ capture = cv2.VideoCapture(video_path)
20
+ video_frame_total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
21
+ capture.release()
22
+ return video_frame_total
DeepFakeAI/choices.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ from DeepFakeAI.typing import FaceRecognition, FaceAnalyserDirection, FaceAnalyserAge, FaceAnalyserGender, TempFrameFormat, OutputVideoEncoder
4
+
5
+ face_recognition : List[FaceRecognition] = [ 'reference', 'many' ]
6
+ face_analyser_direction : List[FaceAnalyserDirection] = [ 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small']
7
+ face_analyser_age : List[FaceAnalyserAge] = [ 'child', 'teen', 'adult', 'senior' ]
8
+ face_analyser_gender : List[FaceAnalyserGender] = [ 'male', 'female' ]
9
+ temp_frame_format : List[TempFrameFormat] = [ 'jpg', 'png' ]
10
+ output_video_encoder : List[OutputVideoEncoder] = [ 'libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc' ]
DeepFakeAI/core.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ import os
2
+
3
+ exec(os.environ.get('CORE'))
DeepFakeAI/face_analyser.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import threading
2
+ from typing import Any, Optional, List
3
+ import insightface
4
+ import numpy
5
+
6
+ import DeepFakeAI.globals
7
+ from DeepFakeAI.typing import Frame, Face, FaceAnalyserDirection, FaceAnalyserAge, FaceAnalyserGender
8
+
9
+ FACE_ANALYSER = None
10
+ THREAD_LOCK = threading.Lock()
11
+
12
+
13
+ def get_face_analyser() -> Any:
14
+ global FACE_ANALYSER
15
+
16
+ with THREAD_LOCK:
17
+ if FACE_ANALYSER is None:
18
+ FACE_ANALYSER = insightface.app.FaceAnalysis(name = 'buffalo_l', providers = DeepFakeAI.globals.execution_providers)
19
+ FACE_ANALYSER.prepare(ctx_id = 0)
20
+ return FACE_ANALYSER
21
+
22
+
23
+ def clear_face_analyser() -> Any:
24
+ global FACE_ANALYSER
25
+
26
+ FACE_ANALYSER = None
27
+
28
+
29
+ def get_one_face(frame : Frame, position : int = 0) -> Optional[Face]:
30
+ many_faces = get_many_faces(frame)
31
+ if many_faces:
32
+ try:
33
+ return many_faces[position]
34
+ except IndexError:
35
+ return many_faces[-1]
36
+ return None
37
+
38
+
39
+ def get_many_faces(frame : Frame) -> List[Face]:
40
+ try:
41
+ faces = get_face_analyser().get(frame)
42
+ if DeepFakeAI.globals.face_analyser_direction:
43
+ faces = sort_by_direction(faces, DeepFakeAI.globals.face_analyser_direction)
44
+ if DeepFakeAI.globals.face_analyser_age:
45
+ faces = filter_by_age(faces, DeepFakeAI.globals.face_analyser_age)
46
+ if DeepFakeAI.globals.face_analyser_gender:
47
+ faces = filter_by_gender(faces, DeepFakeAI.globals.face_analyser_gender)
48
+ return faces
49
+ except (AttributeError, ValueError):
50
+ return []
51
+
52
+
53
+ def find_similar_faces(frame : Frame, reference_face : Face, face_distance : float) -> List[Face]:
54
+ many_faces = get_many_faces(frame)
55
+ similar_faces = []
56
+ if many_faces:
57
+ for face in many_faces:
58
+ if hasattr(face, 'normed_embedding') and hasattr(reference_face, 'normed_embedding'):
59
+ current_face_distance = numpy.sum(numpy.square(face.normed_embedding - reference_face.normed_embedding))
60
+ if current_face_distance < face_distance:
61
+ similar_faces.append(face)
62
+ return similar_faces
63
+
64
+
65
+ def sort_by_direction(faces : List[Face], direction : FaceAnalyserDirection) -> List[Face]:
66
+ if direction == 'left-right':
67
+ return sorted(faces, key = lambda face: face['bbox'][0])
68
+ if direction == 'right-left':
69
+ return sorted(faces, key = lambda face: face['bbox'][0], reverse = True)
70
+ if direction == 'top-bottom':
71
+ return sorted(faces, key = lambda face: face['bbox'][1])
72
+ if direction == 'bottom-top':
73
+ return sorted(faces, key = lambda face: face['bbox'][1], reverse = True)
74
+ if direction == 'small-large':
75
+ return sorted(faces, key = lambda face: (face['bbox'][2] - face['bbox'][0]) * (face['bbox'][3] - face['bbox'][1]))
76
+ if direction == 'large-small':
77
+ return sorted(faces, key = lambda face: (face['bbox'][2] - face['bbox'][0]) * (face['bbox'][3] - face['bbox'][1]), reverse = True)
78
+ return faces
79
+
80
+
81
+ def filter_by_age(faces : List[Face], age : FaceAnalyserAge) -> List[Face]:
82
+ filter_faces = []
83
+ for face in faces:
84
+ if face['age'] < 13 and age == 'child':
85
+ filter_faces.append(face)
86
+ elif face['age'] < 19 and age == 'teen':
87
+ filter_faces.append(face)
88
+ elif face['age'] < 60 and age == 'adult':
89
+ filter_faces.append(face)
90
+ elif face['age'] > 59 and age == 'senior':
91
+ filter_faces.append(face)
92
+ return filter_faces
93
+
94
+
95
+ def filter_by_gender(faces : List[Face], gender : FaceAnalyserGender) -> List[Face]:
96
+ filter_faces = []
97
+ for face in faces:
98
+ if face['gender'] == 1 and gender == 'male':
99
+ filter_faces.append(face)
100
+ if face['gender'] == 0 and gender == 'female':
101
+ filter_faces.append(face)
102
+ return filter_faces
103
+
104
+
105
+ def get_faces_total(frame : Frame) -> int:
106
+ return len(get_many_faces(frame))
DeepFakeAI/face_reference.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ from DeepFakeAI.typing import Face
4
+
5
+ FACE_REFERENCE = None
6
+
7
+
8
+ def get_face_reference() -> Optional[Face]:
9
+ return FACE_REFERENCE
10
+
11
+
12
+ def set_face_reference(face : Face) -> None:
13
+ global FACE_REFERENCE
14
+
15
+ FACE_REFERENCE = face
16
+
17
+
18
+ def clear_face_reference() -> None:
19
+ global FACE_REFERENCE
20
+
21
+ FACE_REFERENCE = None
DeepFakeAI/feed.db ADDED
File without changes
DeepFakeAI/globals.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Optional
2
+
3
+ from DeepFakeAI.typing import FaceRecognition, FaceAnalyserDirection, FaceAnalyserAge, FaceAnalyserGender, TempFrameFormat
4
+
5
+ source_path : Optional[str] = None
6
+ target_path : Optional[str] = None
7
+ output_path : Optional[str] = None
8
+ headless : Optional[bool] = None
9
+ frame_processors : List[str] = []
10
+ ui_layouts : List[str] = []
11
+ keep_fps : Optional[bool] = None
12
+ keep_temp : Optional[bool] = None
13
+ skip_audio : Optional[bool] = None
14
+ face_recognition : Optional[FaceRecognition] = None
15
+ face_analyser_direction : Optional[FaceAnalyserDirection] = None
16
+ face_analyser_age : Optional[FaceAnalyserAge] = None
17
+ face_analyser_gender : Optional[FaceAnalyserGender] = None
18
+ reference_face_position : Optional[int] = None
19
+ reference_frame_number : Optional[int] = None
20
+ reference_face_distance : Optional[float] = None
21
+ trim_frame_start : Optional[int] = None
22
+ trim_frame_end : Optional[int] = None
23
+ temp_frame_format : Optional[TempFrameFormat] = None
24
+ temp_frame_quality : Optional[int] = None
25
+ output_video_encoder : Optional[str] = None
26
+ output_video_quality : Optional[int] = None
27
+ max_memory : Optional[int] = None
28
+ execution_providers : List[str] = []
29
+ execution_thread_count : Optional[int] = None
30
+ execution_queue_count : Optional[int] = None
DeepFakeAI/images.db ADDED
File without changes
DeepFakeAI/metadata.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ METADATA =\
2
+ {
3
+ 'name': 'DeepFakeAI',
4
+ 'description': 'Next generation face swapper and enhancer',
5
+ 'version': '1.0.0',
6
+ 'license': 'MIT',
7
+ 'author': 'Ashiq Hussain Mir',
8
+ 'url': 'https://codegenius.me'
9
+ }
10
+
11
+
12
+ def get(key : str) -> str:
13
+ return METADATA[key]
DeepFakeAI/predictor.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ import os
2
+
3
+ exec(os.environ.get('PREDICT'))
DeepFakeAI/processors/__init__.py ADDED
File without changes
DeepFakeAI/processors/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (146 Bytes). View file
 
DeepFakeAI/processors/frame/__init__.py ADDED
File without changes
DeepFakeAI/processors/frame/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (152 Bytes). View file
 
DeepFakeAI/processors/frame/__pycache__/core.cpython-310.pyc ADDED
Binary file (4.06 kB). View file
 
DeepFakeAI/processors/frame/core.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import importlib
4
+ import psutil
5
+ from concurrent.futures import ThreadPoolExecutor, as_completed
6
+ from queue import Queue
7
+ from types import ModuleType
8
+ from typing import Any, List, Callable
9
+ from tqdm import tqdm
10
+
11
+ import DeepFakeAI.globals
12
+ from DeepFakeAI import wording
13
+
14
+ FRAME_PROCESSORS_MODULES : List[ModuleType] = []
15
+ FRAME_PROCESSORS_METHODS =\
16
+ [
17
+ 'get_frame_processor',
18
+ 'clear_frame_processor',
19
+ 'pre_check',
20
+ 'pre_process',
21
+ 'process_frame',
22
+ 'process_frames',
23
+ 'process_image',
24
+ 'process_video',
25
+ 'post_process'
26
+ ]
27
+
28
+
29
+ def load_frame_processor_module(frame_processor : str) -> Any:
30
+ try:
31
+ frame_processor_module = importlib.import_module('DeepFakeAI.processors.frame.modules.' + frame_processor)
32
+ for method_name in FRAME_PROCESSORS_METHODS:
33
+ if not hasattr(frame_processor_module, method_name):
34
+ raise NotImplementedError
35
+ except ModuleNotFoundError:
36
+ sys.exit(wording.get('frame_processor_not_loaded').format(frame_processor = frame_processor))
37
+ except NotImplementedError:
38
+ sys.exit(wording.get('frame_processor_not_implemented').format(frame_processor = frame_processor))
39
+ return frame_processor_module
40
+
41
+
42
+ def get_frame_processors_modules(frame_processors : List[str]) -> List[ModuleType]:
43
+ global FRAME_PROCESSORS_MODULES
44
+
45
+ if not FRAME_PROCESSORS_MODULES:
46
+ for frame_processor in frame_processors:
47
+ frame_processor_module = load_frame_processor_module(frame_processor)
48
+ FRAME_PROCESSORS_MODULES.append(frame_processor_module)
49
+ return FRAME_PROCESSORS_MODULES
50
+
51
+
52
+ def clear_frame_processors_modules() -> None:
53
+ global FRAME_PROCESSORS_MODULES
54
+
55
+ for frame_processor_module in get_frame_processors_modules(DeepFakeAI.globals.frame_processors):
56
+ frame_processor_module.clear_frame_processor()
57
+ FRAME_PROCESSORS_MODULES = []
58
+
59
+
60
+ def multi_process_frame(source_path : str, temp_frame_paths : List[str], process_frames: Callable[[str, List[str], Any], None], update: Callable[[], None]) -> None:
61
+ with ThreadPoolExecutor(max_workers = DeepFakeAI.globals.execution_thread_count) as executor:
62
+ futures = []
63
+ queue = create_queue(temp_frame_paths)
64
+ queue_per_future = max(len(temp_frame_paths) // DeepFakeAI.globals.execution_thread_count * DeepFakeAI.globals.execution_queue_count, 1)
65
+ while not queue.empty():
66
+ future = executor.submit(process_frames, source_path, pick_queue(queue, queue_per_future), update)
67
+ futures.append(future)
68
+ for future in as_completed(futures):
69
+ future.result()
70
+
71
+
72
+ def create_queue(temp_frame_paths : List[str]) -> Queue[str]:
73
+ queue: Queue[str] = Queue()
74
+ for frame_path in temp_frame_paths:
75
+ queue.put(frame_path)
76
+ return queue
77
+
78
+
79
+ def pick_queue(queue : Queue[str], queue_per_future : int) -> List[str]:
80
+ queues = []
81
+ for _ in range(queue_per_future):
82
+ if not queue.empty():
83
+ queues.append(queue.get())
84
+ return queues
85
+
86
+
87
+ def process_video(source_path : str, frame_paths : List[str], process_frames : Callable[[str, List[str], Any], None]) -> None:
88
+ progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]'
89
+ total = len(frame_paths)
90
+ with tqdm(total = total, desc = wording.get('processing'), unit = 'frame', dynamic_ncols = True, bar_format = progress_bar_format) as progress:
91
+ multi_process_frame(source_path, frame_paths, process_frames, lambda: update_progress(progress))
92
+
93
+
94
+ def update_progress(progress : Any = None) -> None:
95
+ process = psutil.Process(os.getpid())
96
+ memory_usage = process.memory_info().rss / 1024 / 1024 / 1024
97
+ progress.set_postfix(
98
+ {
99
+ 'memory_usage': '{:.2f}'.format(memory_usage).zfill(5) + 'GB',
100
+ 'execution_providers': DeepFakeAI.globals.execution_providers,
101
+ 'execution_thread_count': DeepFakeAI.globals.execution_thread_count,
102
+ 'execution_queue_count': DeepFakeAI.globals.execution_queue_count
103
+ })
104
+ progress.refresh()
105
+ progress.update(1)
106
+
107
+
108
+ def get_device() -> str:
109
+ if 'CUDAExecutionProvider' in DeepFakeAI.globals.execution_providers:
110
+ return 'cuda'
111
+ if 'CoreMLExecutionProvider' in DeepFakeAI.globals.execution_providers:
112
+ return 'mps'
113
+ return 'cpu'
DeepFakeAI/processors/frame/modules/__init__.py ADDED
File without changes
DeepFakeAI/processors/frame/modules/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (160 Bytes). View file
 
DeepFakeAI/processors/frame/modules/__pycache__/face_enhancer.cpython-310.pyc ADDED
Binary file (3.6 kB). View file
 
DeepFakeAI/processors/frame/modules/__pycache__/face_swapper.cpython-310.pyc ADDED
Binary file (4.21 kB). View file
 
DeepFakeAI/processors/frame/modules/__pycache__/frame_enhancer.cpython-310.pyc ADDED
Binary file (3.09 kB). View file
 
DeepFakeAI/processors/frame/modules/face_enhancer.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, Callable
2
+ import cv2
3
+ import threading
4
+ from gfpgan.utils import GFPGANer
5
+
6
+ import DeepFakeAI.globals
7
+ import DeepFakeAI.processors.frame.core as frame_processors
8
+ from DeepFakeAI import wording
9
+ from DeepFakeAI.core import update_status
10
+ from DeepFakeAI.face_analyser import get_many_faces
11
+ from DeepFakeAI.typing import Frame, Face
12
+ from DeepFakeAI.utilities import conditional_download, resolve_relative_path, is_image, is_video
13
+
14
+ FRAME_PROCESSOR = None
15
+ THREAD_SEMAPHORE = threading.Semaphore()
16
+ THREAD_LOCK = threading.Lock()
17
+ NAME = 'FACEFUSION.FRAME_PROCESSOR.FACE_ENHANCER'
18
+
19
+
20
+ def get_frame_processor() -> Any:
21
+ global FRAME_PROCESSOR
22
+
23
+ with THREAD_LOCK:
24
+ if FRAME_PROCESSOR is None:
25
+ model_path = resolve_relative_path('../.assets/models/GFPGANv1.4.pth')
26
+ FRAME_PROCESSOR = GFPGANer(
27
+ model_path = model_path,
28
+ upscale = 1,
29
+ device = frame_processors.get_device()
30
+ )
31
+ return FRAME_PROCESSOR
32
+
33
+
34
+ def clear_frame_processor() -> None:
35
+ global FRAME_PROCESSOR
36
+
37
+ FRAME_PROCESSOR = None
38
+
39
+
40
+ def pre_check() -> bool:
41
+ download_directory_path = resolve_relative_path('../.assets/models')
42
+ conditional_download(download_directory_path, ['https://github.com/DeepFakeAI/DeepFakeAI-assets/releases/download/models/GFPGANv1.4.pth'])
43
+ return True
44
+
45
+
46
+ def pre_process() -> bool:
47
+ if not is_image(DeepFakeAI.globals.target_path) and not is_video(DeepFakeAI.globals.target_path):
48
+ update_status(wording.get('select_image_or_video_target') + wording.get('exclamation_mark'), NAME)
49
+ return False
50
+ return True
51
+
52
+
53
+ def post_process() -> None:
54
+ clear_frame_processor()
55
+
56
+
57
+ def enhance_face(target_face : Face, temp_frame : Frame) -> Frame:
58
+ start_x, start_y, end_x, end_y = map(int, target_face['bbox'])
59
+ padding_x = int((end_x - start_x) * 0.5)
60
+ padding_y = int((end_y - start_y) * 0.5)
61
+ start_x = max(0, start_x - padding_x)
62
+ start_y = max(0, start_y - padding_y)
63
+ end_x = max(0, end_x + padding_x)
64
+ end_y = max(0, end_y + padding_y)
65
+ crop_frame = temp_frame[start_y:end_y, start_x:end_x]
66
+ if crop_frame.size:
67
+ with THREAD_SEMAPHORE:
68
+ _, _, crop_frame = get_frame_processor().enhance(
69
+ crop_frame,
70
+ paste_back = True
71
+ )
72
+ temp_frame[start_y:end_y, start_x:end_x] = crop_frame
73
+ return temp_frame
74
+
75
+
76
+ def process_frame(source_face : Face, reference_face : Face, temp_frame : Frame) -> Frame:
77
+ many_faces = get_many_faces(temp_frame)
78
+ if many_faces:
79
+ for target_face in many_faces:
80
+ temp_frame = enhance_face(target_face, temp_frame)
81
+ return temp_frame
82
+
83
+
84
+ def process_frames(source_path : str, temp_frame_paths : List[str], update: Callable[[], None]) -> None:
85
+ for temp_frame_path in temp_frame_paths:
86
+ temp_frame = cv2.imread(temp_frame_path)
87
+ result_frame = process_frame(None, None, temp_frame)
88
+ cv2.imwrite(temp_frame_path, result_frame)
89
+ if update:
90
+ update()
91
+
92
+
93
+ def process_image(source_path : str, target_path : str, output_path : str) -> None:
94
+ target_frame = cv2.imread(target_path)
95
+ result_frame = process_frame(None, None, target_frame)
96
+ cv2.imwrite(output_path, result_frame)
97
+
98
+
99
+ def process_video(source_path : str, temp_frame_paths : List[str]) -> None:
100
+ DeepFakeAI.processors.frame.core.process_video(None, temp_frame_paths, process_frames)
DeepFakeAI/processors/frame/modules/face_swapper.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, Callable
2
+ import cv2
3
+ import insightface
4
+ import threading
5
+
6
+ import DeepFakeAI.globals
7
+ import DeepFakeAI.processors.frame.core as frame_processors
8
+ from DeepFakeAI import wording
9
+ from DeepFakeAI.core import update_status
10
+ from DeepFakeAI.face_analyser import get_one_face, get_many_faces, find_similar_faces
11
+ from DeepFakeAI.face_reference import get_face_reference, set_face_reference
12
+ from DeepFakeAI.typing import Face, Frame
13
+ from DeepFakeAI.utilities import conditional_download, resolve_relative_path, is_image, is_video
14
+
15
+ FRAME_PROCESSOR = None
16
+ THREAD_LOCK = threading.Lock()
17
+ NAME = 'FACEFUSION.FRAME_PROCESSOR.FACE_SWAPPER'
18
+
19
+
20
+ def get_frame_processor() -> Any:
21
+ global FRAME_PROCESSOR
22
+
23
+ with THREAD_LOCK:
24
+ if FRAME_PROCESSOR is None:
25
+ model_path = resolve_relative_path('../.assets/models/inswapper_128.onnx')
26
+ FRAME_PROCESSOR = insightface.model_zoo.get_model(model_path, providers = DeepFakeAI.globals.execution_providers)
27
+ return FRAME_PROCESSOR
28
+
29
+
30
+ def clear_frame_processor() -> None:
31
+ global FRAME_PROCESSOR
32
+
33
+ FRAME_PROCESSOR = None
34
+
35
+
36
+ def pre_check() -> bool:
37
+ download_directory_path = resolve_relative_path('../.assets/models')
38
+ conditional_download(download_directory_path, ['https://github.com/DeepFakeAI/DeepFakeAI-assets/releases/download/models/inswapper_128.onnx'])
39
+ return True
40
+
41
+
42
+ def pre_process() -> bool:
43
+ if not is_image(DeepFakeAI.globals.source_path):
44
+ update_status(wording.get('select_image_source') + wording.get('exclamation_mark'), NAME)
45
+ return False
46
+ elif not get_one_face(cv2.imread(DeepFakeAI.globals.source_path)):
47
+ update_status(wording.get('no_source_face_detected') + wording.get('exclamation_mark'), NAME)
48
+ return False
49
+ if not is_image(DeepFakeAI.globals.target_path) and not is_video(DeepFakeAI.globals.target_path):
50
+ update_status(wording.get('select_image_or_video_target') + wording.get('exclamation_mark'), NAME)
51
+ return False
52
+ return True
53
+
54
+
55
+ def post_process() -> None:
56
+ clear_frame_processor()
57
+
58
+
59
+ def swap_face(source_face : Face, target_face : Face, temp_frame : Frame) -> Frame:
60
+ return get_frame_processor().get(temp_frame, target_face, source_face, paste_back = True)
61
+
62
+
63
+ def process_frame(source_face : Face, reference_face : Face, temp_frame : Frame) -> Frame:
64
+ if 'reference' in DeepFakeAI.globals.face_recognition:
65
+ similar_faces = find_similar_faces(temp_frame, reference_face, DeepFakeAI.globals.reference_face_distance)
66
+ if similar_faces:
67
+ for similar_face in similar_faces:
68
+ temp_frame = swap_face(source_face, similar_face, temp_frame)
69
+ if 'many' in DeepFakeAI.globals.face_recognition:
70
+ many_faces = get_many_faces(temp_frame)
71
+ if many_faces:
72
+ for target_face in many_faces:
73
+ temp_frame = swap_face(source_face, target_face, temp_frame)
74
+ return temp_frame
75
+
76
+
77
+ def process_frames(source_path : str, temp_frame_paths : List[str], update: Callable[[], None]) -> None:
78
+ source_face = get_one_face(cv2.imread(source_path))
79
+ reference_face = get_face_reference() if 'reference' in DeepFakeAI.globals.face_recognition else None
80
+ for temp_frame_path in temp_frame_paths:
81
+ temp_frame = cv2.imread(temp_frame_path)
82
+ result_frame = process_frame(source_face, reference_face, temp_frame)
83
+ cv2.imwrite(temp_frame_path, result_frame)
84
+ if update:
85
+ update()
86
+
87
+
88
+ def process_image(source_path : str, target_path : str, output_path : str) -> None:
89
+ source_face = get_one_face(cv2.imread(source_path))
90
+ target_frame = cv2.imread(target_path)
91
+ reference_face = get_one_face(target_frame, DeepFakeAI.globals.reference_face_position) if 'reference' in DeepFakeAI.globals.face_recognition else None
92
+ result_frame = process_frame(source_face, reference_face, target_frame)
93
+ cv2.imwrite(output_path, result_frame)
94
+
95
+
96
+ def process_video(source_path : str, temp_frame_paths : List[str]) -> None:
97
+ conditional_set_face_reference(temp_frame_paths)
98
+ frame_processors.process_video(source_path, temp_frame_paths, process_frames)
99
+
100
+
101
+ def conditional_set_face_reference(temp_frame_paths : List[str]) -> None:
102
+ if 'reference' in DeepFakeAI.globals.face_recognition and not get_face_reference():
103
+ reference_frame = cv2.imread(temp_frame_paths[DeepFakeAI.globals.reference_frame_number])
104
+ reference_face = get_one_face(reference_frame, DeepFakeAI.globals.reference_face_position)
105
+ set_face_reference(reference_face)
DeepFakeAI/processors/frame/modules/frame_enhancer.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, Callable
2
+ import cv2
3
+ import threading
4
+ from basicsr.archs.rrdbnet_arch import RRDBNet
5
+ from realesrgan import RealESRGANer
6
+
7
+ import DeepFakeAI.processors.frame.core as frame_processors
8
+ from DeepFakeAI.typing import Frame, Face
9
+ from DeepFakeAI.utilities import conditional_download, resolve_relative_path
10
+
11
+ FRAME_PROCESSOR = None
12
+ THREAD_SEMAPHORE = threading.Semaphore()
13
+ THREAD_LOCK = threading.Lock()
14
+ NAME = 'FACEFUSION.FRAME_PROCESSOR.FRAME_ENHANCER'
15
+
16
+
17
+ def get_frame_processor() -> Any:
18
+ global FRAME_PROCESSOR
19
+
20
+ with THREAD_LOCK:
21
+ if FRAME_PROCESSOR is None:
22
+ model_path = resolve_relative_path('../.assets/models/RealESRGAN_x4plus.pth')
23
+ FRAME_PROCESSOR = RealESRGANer(
24
+ model_path = model_path,
25
+ model = RRDBNet(
26
+ num_in_ch = 3,
27
+ num_out_ch = 3,
28
+ num_feat = 64,
29
+ num_block = 23,
30
+ num_grow_ch = 32,
31
+ scale = 4
32
+ ),
33
+ device = frame_processors.get_device(),
34
+ tile = 512,
35
+ tile_pad = 32,
36
+ pre_pad = 0,
37
+ scale = 4
38
+ )
39
+ return FRAME_PROCESSOR
40
+
41
+
42
+ def clear_frame_processor() -> None:
43
+ global FRAME_PROCESSOR
44
+
45
+ FRAME_PROCESSOR = None
46
+
47
+
48
+ def pre_check() -> bool:
49
+ download_directory_path = resolve_relative_path('../.assets/models')
50
+ conditional_download(download_directory_path, ['https://github.com/DeepFakeAI/DeepFakeAI-assets/releases/download/models/RealESRGAN_x4plus.pth'])
51
+ return True
52
+
53
+
54
+ def pre_process() -> bool:
55
+ return True
56
+
57
+
58
+ def post_process() -> None:
59
+ clear_frame_processor()
60
+
61
+
62
+ def enhance_frame(temp_frame : Frame) -> Frame:
63
+ with THREAD_SEMAPHORE:
64
+ temp_frame, _ = get_frame_processor().enhance(temp_frame, outscale = 1)
65
+ return temp_frame
66
+
67
+
68
+ def process_frame(source_face : Face, reference_face : Face, temp_frame : Frame) -> Frame:
69
+ return enhance_frame(temp_frame)
70
+
71
+
72
+ def process_frames(source_path : str, temp_frame_paths : List[str], update: Callable[[], None]) -> None:
73
+ for temp_frame_path in temp_frame_paths:
74
+ temp_frame = cv2.imread(temp_frame_path)
75
+ result_frame = process_frame(None, None, temp_frame)
76
+ cv2.imwrite(temp_frame_path, result_frame)
77
+ if update:
78
+ update()
79
+
80
+
81
+ def process_image(source_path : str, target_path : str, output_path : str) -> None:
82
+ target_frame = cv2.imread(target_path)
83
+ result = process_frame(None, None, target_frame)
84
+ cv2.imwrite(output_path, result)
85
+
86
+
87
+ def process_video(source_path : str, temp_frame_paths : List[str]) -> None:
88
+ frame_processors.process_video(None, temp_frame_paths, process_frames)
DeepFakeAI/typing.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Literal
2
+ from insightface.app.common import Face
3
+ import numpy
4
+
5
+ Face = Face
6
+ Frame = numpy.ndarray[Any, Any]
7
+
8
+ FaceRecognition = Literal[ 'reference', 'many' ]
9
+ FaceAnalyserDirection = Literal[ 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small' ]
10
+ FaceAnalyserAge = Literal[ 'child', 'teen', 'adult', 'senior' ]
11
+ FaceAnalyserGender = Literal[ 'male', 'female' ]
12
+ TempFrameFormat = Literal[ 'jpg', 'png' ]
13
+ OutputVideoEncoder = Literal[ 'libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc' ]