Spaces:
Running
Running
Josh Brown Kramer commited on
Commit ·
a56695f
1
Parent(s): d6c0ea5
Don't include gradio
Browse filesFiles changed (4)
- .gitignore+3
- app.py+31-12
- faceparsing.pyAdded+54
- requirements.txt+3-1
175 unmodified lines176177178179175 unmodified lines# VSCode.vscode/# End of https://www.toptal.com/developers/gitignore/api/python175 unmodified lines176177178179180181182175 unmodified lines# VSCode.vscode/# Gradio.gradio/# End of https://www.toptal.com/developers/gitignore/api/python
1 unmodified line23456722 unmodified lines3031323334353637383940414243444546474849505152535455561 unmodified lineimport zombiefrom huggingface_hub import hf_hub_downloadimport onnxruntime as ort# import torch# from your_pix2pixhd_code import YourPix2PixHDModel, load_image, tensor2im # Adapt these imports22 unmodified lines# # return output_imagedef predict(input_image):zombie_image = zombie.transition_onnx(input_image,ort_session)if zombie_image is None:return "No face found"return zombie_image# --- 3. Create the Gradio Interface ---title = "pix2pixHD Image-to-Image Translation"description = "Upload an image to see the pix2pixHD model in action."article = "<p style='text-align: center'>Model based on the <a href='https://github.com/NVIDIA/pix2pixHD' target='_blank'>pix2pixHD repository</a>.</p>"demo = gr.Interface(fn=predict,inputs=gr.Image(type="pil", label="Input Image"),outputs=gr.Image(type="pil", label="Output Image"),title=title,description=description,article=article,)demo.launch()#demo.launch(debug=True)1 unmodified line23456789101122 unmodified lines3435363738394041424344454647484950515253545556575859606162636465666768697071727374751 unmodified lineimport zombiefrom huggingface_hub import hf_hub_downloadimport onnxruntime as ortimport numpy as npfrom PIL import Imagefrom faceparsing import get_face_mask# import torch# from your_pix2pixhd_code import YourPix2PixHDModel, load_image, tensor2im # Adapt these imports22 unmodified lines# # return output_imagedef predict(input_image, mode):if mode == "Classic":# Use the transition_onnx function for side-by-side comparisonzombie_image = zombie.transition_onnx(input_image, ort_session)if zombie_image is None:return "No face found"return zombie_imageelif mode == "In Place":# Use the make_faces_zombie_from_array function for in-place transformation#zombie_image = zombie.make_faces_zombie_from_array(im_array, None, ort_session)#if zombie_image is None:# return "No face found"#return zombie_imageface_mask = get_face_mask(input_image)return face_maskelse:return "Invalid mode selected"# --- 3. Create the Gradio Interface ---title = "Make Me A Zombie"description = "Upload an image to see the pix2pixHD model in action."article = """<p style='text-align: center'>Model based on the <a href='https://github.com/NVIDIA/pix2pixHD' target='_blank'>pix2pixHD repository</a>.More details at <a href='https://makemeazombie.com' target='_blank'>makemeazombie.com</a>.</p>"""demo = gr.Interface(fn=predict,inputs=[gr.Image(type="pil", label="Input Image"),gr.Dropdown(choices=["Classic", "In Place"], value="Classic", label="Mode")],outputs=gr.Image(type="pil", label="Output Image"),title=title,description=description,article=article,)#demo.launch()demo.launch(debug=True)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import torchfrom torch import nnfrom transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
from PIL import Imageimport matplotlib.pyplot as plt
# convenience expression for automatically determining devicedevice = ( "cuda" # Device for NVIDIA or AMD GPUs if torch.cuda.is_available() else "mps" # Device for Apple Silicon (Metal Performance Shaders) if torch.backends.mps.is_available() else "cpu")
# load modelsimage_processor = SegformerImageProcessor.from_pretrained("jonathandinu/face-parsing")model = SegformerForSemanticSegmentation.from_pretrained("jonathandinu/face-parsing")model.to(device)
def get_face_mask(image): # run inference on image inputs = image_processor(images=image, return_tensors="pt").to(device) outputs = model(**inputs) logits = outputs.logits # shape (batch_size, num_labels, ~height/4, ~width/4)
# resize output to match input image dimensions upsampled_logits = nn.functional.interpolate(logits, size=image.size[::-1], # H x W mode='bilinear', align_corners=False)
# get label masks labels = upsampled_logits.argmax(dim=1)[0]
# move to CPU to visualize in matplotlib labels_viz = labels.cpu().numpy()
#Map to something more colorful. Use a color map to map the labels to a color. #Create a color map for colors 0 through 18 color_map = plt.get_cmap('tab20') #Map the labels to a color colors = color_map(labels_viz)
#Convert to PIL Image colors_pil = Image.fromarray((colors * 255).astype(np.uint8))
return labels_viz
1 unmodified line23451 unmodified lineonnxruntime-gpuopencv-pythonnumpymediapipe1 unmodified line2345671 unmodified lineonnxruntime-gpuopencv-pythonnumpymediapipetorchtransformers
.gitignore+3
app.py+31-12
faceparsing.pyAdded+54
requirements.txt+3-1