Spaces:
Runtime error
Runtime error
Sushil Thapa
commited on
Commit
·
886e7c2
1
Parent(s):
306aad1
Add face detection
Browse files- app.py +20 -7
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -6,9 +6,9 @@ import PIL
|
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
import os
|
|
|
|
| 9 |
import uuid
|
| 10 |
import torch
|
| 11 |
-
from torch import autocast
|
| 12 |
import cv2
|
| 13 |
from matplotlib import pyplot as plt
|
| 14 |
from torchvision import transforms
|
|
@@ -20,7 +20,7 @@ auth_token = os.environ.get("API_TOKEN") or True
|
|
| 20 |
|
| 21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
|
| 23 |
-
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", dtype=torch.
|
| 24 |
|
| 25 |
transform = transforms.Compose([
|
| 26 |
transforms.ToTensor(),
|
|
@@ -38,11 +38,24 @@ def read_content(file_path: str) -> str:
|
|
| 38 |
|
| 39 |
def predict(dict, prompt=""):
|
| 40 |
init_image = dict["image"].convert("RGB").resize((512, 512))
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return output.images[0], gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 44 |
|
| 45 |
-
|
| 46 |
css = '''
|
| 47 |
.container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
|
| 48 |
#image_upload{min-height:400px}
|
|
@@ -92,7 +105,7 @@ with image_blocks as demo:
|
|
| 92 |
image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="pil", label="Upload source image here").style(height=400)
|
| 93 |
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
| 94 |
prompt = gr.Textbox(placeholder = 'Enter name here (what you want in place of what is erased)', show_label=False, elem_id="input-text")
|
| 95 |
-
btn = gr.Button("
|
| 96 |
margin=False,
|
| 97 |
rounded=(False, True, True, False),
|
| 98 |
full_width=False,
|
|
@@ -125,4 +138,4 @@ with image_blocks as demo:
|
|
| 125 |
"""
|
| 126 |
)
|
| 127 |
|
| 128 |
-
image_blocks.launch()
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
import os
|
| 9 |
+
import cvlib as cv
|
| 10 |
import uuid
|
| 11 |
import torch
|
|
|
|
| 12 |
import cv2
|
| 13 |
from matplotlib import pyplot as plt
|
| 14 |
from torchvision import transforms
|
|
|
|
| 20 |
|
| 21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
|
| 23 |
+
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", dtype=torch.float32, revision="fp16", use_auth_token=auth_token).to(device)
|
| 24 |
|
| 25 |
transform = transforms.Compose([
|
| 26 |
transforms.ToTensor(),
|
|
|
|
| 38 |
|
| 39 |
def predict(dict, prompt=""):
|
| 40 |
init_image = dict["image"].convert("RGB").resize((512, 512))
|
| 41 |
+
_init_image = cv2.cvtColor(np.array(init_image), cv2.COLOR_RGB2BGR)
|
| 42 |
+
faces, confidences = cv.detect_face(_init_image)
|
| 43 |
+
cv2.imwrite('data/init_image.jpg',_init_image)
|
| 44 |
+
for (x,y,p,q) in faces:
|
| 45 |
+
cv2.rectangle(_init_image,(x,y),(p,q),(255,0,0),2)
|
| 46 |
+
cv2.imwrite('data/face_detected_image.jpg',_init_image)
|
| 47 |
+
|
| 48 |
+
(x, y, x2, y2) = faces[0]
|
| 49 |
+
|
| 50 |
+
face_mask = np.zeros((512, 512))
|
| 51 |
+
face_mask[y:y2, x:x2] = 255
|
| 52 |
+
cv2.imwrite('data/face_mask.jpg',face_mask)
|
| 53 |
+
mask = Image.fromarray(face_mask).convert("RGB")
|
| 54 |
+
|
| 55 |
+
# mask = dict["mask"].convert("RGB").resize((512, 512))
|
| 56 |
+
output = pipe(prompt = prompt, image=init_image, mask_image=mask, guidance_scale=8) #7.5
|
| 57 |
return output.images[0], gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 58 |
|
|
|
|
| 59 |
css = '''
|
| 60 |
.container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
|
| 61 |
#image_upload{min-height:400px}
|
|
|
|
| 105 |
image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="pil", label="Upload source image here").style(height=400)
|
| 106 |
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
| 107 |
prompt = gr.Textbox(placeholder = 'Enter name here (what you want in place of what is erased)', show_label=False, elem_id="input-text")
|
| 108 |
+
btn = gr.Button("Generate!").style(
|
| 109 |
margin=False,
|
| 110 |
rounded=(False, True, True, False),
|
| 111 |
full_width=False,
|
|
|
|
| 138 |
"""
|
| 139 |
)
|
| 140 |
|
| 141 |
+
image_blocks.launch(share=True)
|
requirements.txt
CHANGED
|
@@ -8,4 +8,6 @@ numpy
|
|
| 8 |
matplotlib
|
| 9 |
uuid
|
| 10 |
opencv-python
|
| 11 |
-
|
|
|
|
|
|
|
|
|
| 8 |
matplotlib
|
| 9 |
uuid
|
| 10 |
opencv-python
|
| 11 |
+
tensorflow
|
| 12 |
+
cvlib
|
| 13 |
+
git+https://github.com/openai/CLIP.git
|