Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,63 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio
|
| 3 |
import base64
|
| 4 |
-
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
from sentence_transformers import SentenceTransformer, util
|
| 7 |
|
| 8 |
-
|
| 9 |
backgroundPipe = pipeline("image-segmentation", model="facebook/maskformer-swin-large-coco")
|
| 10 |
PersonPipe = pipeline("image-segmentation", model="mattmdjaga/segformer_b2_clothes")
|
| 11 |
sentenceModal = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def getImageDetails(image) -> dict:
|
|
|
|
| 14 |
person = PersonPipe(image)
|
| 15 |
bg = backgroundPipe(image)
|
| 16 |
-
ret = {}
|
| 17 |
-
labs = []
|
| 18 |
for imask in bg:
|
| 19 |
-
ret[imask["label"]] = imask["mask"] # Apply base64 image converter here if needed
|
| 20 |
-
labs.append(imask["label"])
|
| 21 |
for mask in person:
|
| 22 |
-
ret[mask["label"]] = mask["mask"] # Apply base64 image converter here if needed
|
| 23 |
-
|
| 24 |
-
return ret, labs
|
| 25 |
|
| 26 |
def processSentence(sentence: str, semilist: list):
|
| 27 |
query_embedding = sentenceModal.encode(sentence)
|
|
@@ -51,9 +87,12 @@ def process_image(image):
|
|
| 51 |
return processed_image
|
| 52 |
|
| 53 |
def processAndGetMask(image: str, text: str):
|
| 54 |
-
datas
|
|
|
|
|
|
|
| 55 |
selector = processSentence(text, labs)
|
| 56 |
imageout = datas[selector]
|
|
|
|
| 57 |
return process_image(imageout)
|
| 58 |
|
| 59 |
gr = gradio.Interface(
|
|
@@ -61,4 +100,4 @@ gr = gradio.Interface(
|
|
| 61 |
[gradio.Image(type="pil"), gradio.Text()],
|
| 62 |
gradio.Image(type="pil")
|
| 63 |
)
|
| 64 |
-
gr.launch()
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio
|
| 3 |
import base64
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
from io import BytesIO
|
| 6 |
from sentence_transformers import SentenceTransformer, util
|
| 7 |
|
|
|
|
| 8 |
backgroundPipe = pipeline("image-segmentation", model="facebook/maskformer-swin-large-coco")
|
| 9 |
PersonPipe = pipeline("image-segmentation", model="mattmdjaga/segformer_b2_clothes")
|
| 10 |
sentenceModal = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 11 |
+
personDetailsPipe = pipeline("image-segmentation", model="yolo12138/segformer-b2-human-parse-24")
|
| 12 |
+
faceModal = pipeline("image-segmentation", model="jonathandinu/face-parsing")
|
| 13 |
+
faceDetectionModal = pipeline("object-detection", model="aditmohan96/detr-finetuned-face")
|
| 14 |
+
PersonDetectionpipe = pipeline("object-detection", model="hustvl/yolos-tiny")
|
| 15 |
+
|
| 16 |
+
def getPersonDetail(image):
|
| 17 |
+
data = PersonDetectionpipe(image)
|
| 18 |
+
persn = []
|
| 19 |
+
for per in data:
|
| 20 |
+
if per["label"].lower() == "person":
|
| 21 |
+
persn.append(per["box"])
|
| 22 |
+
n = 1
|
| 23 |
+
ret = {}
|
| 24 |
+
for cord in persn:
|
| 25 |
+
crop_box = (cord['xmin'], cord['ymin'], cord['xmax'], cord['ymax'])
|
| 26 |
+
cropped_image = image.crop(crop_box)
|
| 27 |
+
personData = personDetailsPipe(cropped_image)
|
| 28 |
+
for dt in personData:
|
| 29 |
+
if len(persn) > 1:
|
| 30 |
+
ret[(f'Person {n} {dt["label"]}').lower()] = cbiwm(image, dt["mask"], cord)
|
| 31 |
+
else:
|
| 32 |
+
ret[dt["label"].lower()] = cbiwm(image, dt["mask"], cord)
|
| 33 |
+
n = n + 1
|
| 34 |
+
return ret
|
| 35 |
+
|
| 36 |
+
def cbiwm(image, mask, coordinates):
|
| 37 |
+
black_image = Image.new("RGBA", image.size, (0, 0, 0, 255))
|
| 38 |
+
black_image.paste(mask, (coordinates['xmin'], coordinates['ymin']), mask)
|
| 39 |
+
return black_image
|
| 40 |
+
|
| 41 |
+
def processFaceDetails(image):
|
| 42 |
+
ret = getPersonDetail(image)
|
| 43 |
+
data = faceDetectionModal(image)
|
| 44 |
+
cordinates = data[1]["box"]
|
| 45 |
+
crop_box = (data[1]["box"]['xmin'], data[1]["box"]['ymin'], data[1]["box"]['xmax'], data[1]["box"]['ymax'])
|
| 46 |
+
cropped_image = image.crop(crop_box)
|
| 47 |
+
facedata = faceModal(cropped_image)
|
| 48 |
+
for imask in facedata:
|
| 49 |
+
ret[imask["label"].replace(".png", "").lower()] = cbiwm(image, imask["mask"], cordinates)
|
| 50 |
+
return ret
|
| 51 |
|
| 52 |
def getImageDetails(image) -> dict:
|
| 53 |
+
ret = processFaceDetails(image)
|
| 54 |
person = PersonPipe(image)
|
| 55 |
bg = backgroundPipe(image)
|
|
|
|
|
|
|
| 56 |
for imask in bg:
|
| 57 |
+
ret[imask["label"].lower()] = imask["mask"] # Apply base64 image converter here if needed
|
|
|
|
| 58 |
for mask in person:
|
| 59 |
+
ret[mask["label"].lower()] = mask["mask"] # Apply base64 image converter here if needed
|
| 60 |
+
return ret
|
|
|
|
| 61 |
|
| 62 |
def processSentence(sentence: str, semilist: list):
|
| 63 |
query_embedding = sentenceModal.encode(sentence)
|
|
|
|
| 87 |
return processed_image
|
| 88 |
|
| 89 |
def processAndGetMask(image: str, text: str):
|
| 90 |
+
datas = getImageDetails(image)
|
| 91 |
+
labs = list(datas.keys())
|
| 92 |
+
print(labs)
|
| 93 |
selector = processSentence(text, labs)
|
| 94 |
imageout = datas[selector]
|
| 95 |
+
print(f"Selected : {selector}")
|
| 96 |
return process_image(imageout)
|
| 97 |
|
| 98 |
gr = gradio.Interface(
|
|
|
|
| 100 |
[gradio.Image(type="pil"), gradio.Text()],
|
| 101 |
gradio.Image(type="pil")
|
| 102 |
)
|
| 103 |
+
gr.launch(debug=True)
|