Josh Brown Kramer commited on
Commit
d6c0ea5
·
1 Parent(s): 21ff0ae

Have a locally working gradio demo

Browse files
Files changed (4) hide show
  1. .gitignore +179 -0
  2. align.py +26 -2
  3. app.py +5 -2
  4. requirements.txt +0 -2
.gitignore ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by https://www.toptal.com/developers/gitignore/api/python
2
+ # Edit at https://www.toptal.com/developers/gitignore?templates=python
3
+
4
+ ### Python ###
5
+ # Byte-compiled / optimized / DLL files
6
+ __pycache__/
7
+ *.py[cod]
8
+ *$py.class
9
+
10
+ # C extensions
11
+ *.so
12
+
13
+ # Distribution / packaging
14
+ .Python
15
+ build/
16
+ develop-eggs/
17
+ dist/
18
+ downloads/
19
+ eggs/
20
+ .eggs/
21
+ lib/
22
+ lib64/
23
+ parts/
24
+ sdist/
25
+ var/
26
+ wheels/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # PyInstaller
34
+ # Usually these files are written by a python script from a template
35
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
36
+ *.manifest
37
+ *.spec
38
+
39
+ # Installer logs
40
+ pip-log.txt
41
+ pip-delete-this-directory.txt
42
+
43
+ # Unit test / coverage reports
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ .coverage
48
+ .coverage.*
49
+ .cache
50
+ nosetests.xml
51
+ coverage.xml
52
+ *.cover
53
+ *.py,cover
54
+ .hypothesis/
55
+ .pytest_cache/
56
+ cover/
57
+
58
+ # Translations
59
+ *.mo
60
+ *.pot
61
+
62
+ # Django stuff:
63
+ *.log
64
+ local_settings.py
65
+ db.sqlite3
66
+ db.sqlite3-journal
67
+
68
+ # Flask stuff:
69
+ instance/
70
+ .webassets-cache
71
+
72
+ # Scrapy stuff:
73
+ .scrapy
74
+
75
+ # Sphinx documentation
76
+ docs/_build/
77
+
78
+ # PyBuilder
79
+ .pybuilder/
80
+ target/
81
+
82
+ # Jupyter Notebook
83
+ .ipynb_checkpoints
84
+
85
+ # IPython
86
+ profile_default/
87
+ ipython_config.py
88
+
89
+ # pyenv
90
+ # For a library or package, you might want to ignore these files since the code is
91
+ # intended to run in multiple environments; otherwise, check them in:
92
+ # .python-version
93
+
94
+ # pipenv
95
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
97
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
98
+ # install all needed dependencies.
99
+ #Pipfile.lock
100
+
101
+ # poetry
102
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
103
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
104
+ # commonly ignored for libraries.
105
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
106
+ #poetry.lock
107
+
108
+ # pdm
109
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
110
+ #pdm.lock
111
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
112
+ # in version control.
113
+ # https://pdm.fming.dev/#use-with-ide
114
+ .pdm.toml
115
+
116
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
117
+ __pypackages__/
118
+
119
+ # Celery stuff
120
+ celerybeat-schedule
121
+ celerybeat.pid
122
+
123
+ # SageMath parsed files
124
+ *.sage.py
125
+
126
+ # Environments
127
+ .env
128
+ .venv
129
+ env/
130
+ venv/
131
+ ENV/
132
+ env.bak/
133
+ venv.bak/
134
+
135
+ # Spyder project settings
136
+ .spyderproject
137
+ .spyproject
138
+
139
+ # Rope project settings
140
+ .ropeproject
141
+
142
+ # mkdocs documentation
143
+ /site
144
+
145
+ # mypy
146
+ .mypy_cache/
147
+ .dmypy.json
148
+ dmypy.json
149
+
150
+ # Pyre type checker
151
+ .pyre/
152
+
153
+ # pytype static type analyzer
154
+ .pytype/
155
+
156
+ # Cython debug symbols
157
+ cython_debug/
158
+
159
+ # PyCharm
160
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
161
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
162
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
163
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
164
+ #.idea/
165
+
166
+ ### Python Patch ###
167
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
168
+ poetry.toml
169
+
170
+ # ruff
171
+ .ruff_cache/
172
+
173
+ # LSP config files
174
+ pyrightconfig.json
175
+
176
+ # VSCode
177
+ .vscode/
178
+
179
+ # End of https://www.toptal.com/developers/gitignore/api/python
align.py CHANGED
@@ -1,9 +1,34 @@
1
  import numpy as np
2
  import mediapipe as mp
 
 
3
 
4
  mp_face_detection = mp.solutions.face_detection
5
  face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5, model_selection=0)
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def get_locations(numpy_array,model_type="dlib"):
8
  '''
9
  model_type can be "dlib" or "mediapipe"
@@ -46,7 +71,6 @@ def align(pil_image,enable_padding=True,output_size=512,model_type="dlib"):
46
  return None
47
  areas = [(l[2] - l[0])*(l[1] - l[3]) for l in locations]
48
  i = np.argmax(areas)
49
- t0 = time.time()
50
  face_landmarks_list = get_landmarks(numpy_im,[locations[i]],context,model_type)#face_recognition.face_landmarks(numpy_im,[locations[i]])
51
  im,quad = image_align(Image.fromarray(numpy_im),face_landmarks_list[0],enable_padding=enable_padding,output_size=output_size,transform_size=4*output_size)
52
  return im,quad*scale
@@ -127,7 +151,7 @@ def image_align(img, lm, output_size=1024, transform_size=4096, enable_padding=T
127
  # Transform.
128
  img = img.transform((transform_size, transform_size), PIL.Image.QUAD, (quad + 0.5).flatten(), PIL.Image.BILINEAR)
129
  if output_size < transform_size:
130
- img = img.resize((output_size, output_size), PIL.Image.ANTIALIAS)
131
 
132
  # Save aligned image.
133
  return img,original_quad
 
1
  import numpy as np
2
  import mediapipe as mp
3
+ from PIL import Image
4
+ import PIL
5
 
6
  mp_face_detection = mp.solutions.face_detection
7
  face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5, model_selection=0)
8
 
9
+ def get_landmarks(numpy_array,locations,context,model_type="dlib"):
10
+ '''
11
+ model_type can be "dlib" or "mediapipe"
12
+ context is the second result from get_locations
13
+ '''
14
+ assert(model_type in ["dlib","mediapipe"])
15
+
16
+ if model_type == "dlib":
17
+ return face_recognition.face_landmarks(numpy_array,locations)
18
+ else:
19
+ return [context[tuple(l)] for l in locations]
20
+
21
+ def landmarks_from_result(result,np_array):
22
+ keypoint_names = ["left_eye", "right_eye", "nose" ,"mouth", "left_ear", "right_ear"]
23
+
24
+ landmarks = {}
25
+ for i,k in enumerate(result.location_data.relative_keypoints):
26
+ x = round(k.x * np_array.shape[1])
27
+ y = round(k.y * np_array.shape[0])
28
+ landmarks[keypoint_names[i]] = np.array([x,y])
29
+
30
+ return landmarks
31
+
32
  def get_locations(numpy_array,model_type="dlib"):
33
  '''
34
  model_type can be "dlib" or "mediapipe"
 
71
  return None
72
  areas = [(l[2] - l[0])*(l[1] - l[3]) for l in locations]
73
  i = np.argmax(areas)
 
74
  face_landmarks_list = get_landmarks(numpy_im,[locations[i]],context,model_type)#face_recognition.face_landmarks(numpy_im,[locations[i]])
75
  im,quad = image_align(Image.fromarray(numpy_im),face_landmarks_list[0],enable_padding=enable_padding,output_size=output_size,transform_size=4*output_size)
76
  return im,quad*scale
 
151
  # Transform.
152
  img = img.transform((transform_size, transform_size), PIL.Image.QUAD, (quad + 0.5).flatten(), PIL.Image.BILINEAR)
153
  if output_size < transform_size:
154
+ img = img.resize((output_size, output_size), PIL.Image.LANCZOS)
155
 
156
  # Save aligned image.
157
  return img,original_quad
app.py CHANGED
@@ -43,11 +43,14 @@ title = "pix2pixHD Image-to-Image Translation"
43
  description = "Upload an image to see the pix2pixHD model in action."
44
  article = "<p style='text-align: center'>Model based on the <a href='https://github.com/NVIDIA/pix2pixHD' target='_blank'>pix2pixHD repository</a>.</p>"
45
 
46
- gr.Interface(
47
  fn=predict,
48
  inputs=gr.Image(type="pil", label="Input Image"),
49
  outputs=gr.Image(type="pil", label="Output Image"),
50
  title=title,
51
  description=description,
52
  article=article,
53
- ).launch()
 
 
 
 
43
  description = "Upload an image to see the pix2pixHD model in action."
44
  article = "<p style='text-align: center'>Model based on the <a href='https://github.com/NVIDIA/pix2pixHD' target='_blank'>pix2pixHD repository</a>.</p>"
45
 
46
+ demo = gr.Interface(
47
  fn=predict,
48
  inputs=gr.Image(type="pil", label="Input Image"),
49
  outputs=gr.Image(type="pil", label="Output Image"),
50
  title=title,
51
  description=description,
52
  article=article,
53
+ )
54
+
55
+ demo.launch()
56
+ #demo.launch(debug=True)
requirements.txt CHANGED
@@ -2,6 +2,4 @@ gradio
2
  onnxruntime-gpu
3
  opencv-python
4
  numpy
5
- #face_recognition
6
- #dlib
7
  mediapipe
 
2
  onnxruntime-gpu
3
  opencv-python
4
  numpy
 
 
5
  mediapipe