| !pip install insightface |
| ------------------------------- |
|
|
|
|
| from huggingface_hub import loggin |
|
|
| loggin(token="") |
| ----------------------------------------------- |
|
|
|
|
| from huggingface_hub import hf_hub_download |
|
|
| repo_id = "AshanGimhana/NewFWModelV1" |
| model_filename = "inswapperFTV1Model.onnx" |
|
|
| -------------------------------------------------- |
|
|
|
|
| model_path = hf_hub_download(repo_id=repo_id, filename=model_filename) |
|
|
| ---------------------------------------------------------------------------- |
|
|
|
|
| model = "copy path and paste" |
|
|
| ---------------------------------------------------------------------------- |
|
|
| from matplotlib import pyplot as plt |
| import cv2 |
| import insightface |
| import numpy as np |
| ----------------------------------------------------------------------------- |
|
|
| providers = ["CPUExecutionProvider"] |
| ----------------------------------------------------------------------------- |
|
|
| target_frame = cv2.imread('Enter reference image path') |
| src_frame = cv2.imread("Enter user image path") |
| fig = plt.figure() |
| ax1 = fig.add_subplot(1,2,1) |
| ax1.imshow(cv2.cvtColor(src_frame, cv2.COLOR_BGR2RGB)) |
| ax2 = fig.add_subplot(1,2,2) |
| ax2.imshow(cv2.cvtColor(target_frame, cv2.COLOR_BGR2RGB)) |
| plt.show() |
| ------------------------------------------------------------------------------ |
|
|
| FACE_ANALYSER = insightface.app.FaceAnalysis( |
| name="buffalo_l", |
| root=".", providers=providers,allowed_modules=["landmark_3d_68", "landmark_2d_106","detection","recognition"] |
| ) |
| FACE_ANALYSER.prepare( |
| ctx_id=0, |
| det_size=(640, 640), |
| ) |
| ---------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| src_faces = FACE_ANALYSER.get(src_frame) |
| target_faces = FACE_ANALYSER.get(target_frame) |
| fig = plt.figure() |
| ax1 = fig.add_subplot(1,2,1) |
| rimg = FACE_ANALYSER.draw_on(src_frame,src_faces) |
| ax1.imshow(cv2.cvtColor(rimg, cv2.COLOR_BGR2RGB)) |
| ax1 = fig.add_subplot(1,2,2) |
| rimg = FACE_ANALYSER.draw_on(target_frame,target_faces) |
| ax1.imshow(cv2.cvtColor(rimg, cv2.COLOR_BGR2RGB)) |
| plt.show() |
|
|
| ---------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| model_swap_insightface = insightface.model_zoo.get_model(model, providers=providers) |
|
|
| ---------------------------------------------------------------------------------------------------- |
|
|
| img_fake = model_swap_insightface.get(img = target_frame, target_face=FACE_ANALYSER.get(target_frame)[0], source_face=src_faces[0], paste_back=True) |
|
|
| ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| plt.imshow(cv2.cvtColor(img_fake, cv2.COLOR_BGR2RGB)) |
|
|
| ------------------------------------------------------------------------ |