Arrcttacsrks commited on
Commit
56f924b
·
verified ·
1 Parent(s): 0931b5b

Update requirements.txt

Browse files
Files changed (1) hide show
  1. requirements.txt +5 -82
requirements.txt CHANGED
@@ -1,82 +1,5 @@
1
- import os
2
- import cv2
3
- import numpy as np
4
- import onnxruntime as ort
5
- import gradio as gr
6
- from huggingface_hub import hf_hub_download
7
-
8
- # Ensure the Hugging Face token is retrieved from environment variables
9
- huggingface_token = os.getenv("HF_TOKEN")
10
- if huggingface_token is None:
11
- raise ValueError("HUGGINGFACE_TOKEN environment variable not set.")
12
-
13
- # Download the model file from Hugging Face using the token
14
- model_id = "Arrcttacsrks/netrunner-exe_Insight-Swap-models-onnx"
15
- model_file = hf_hub_download(repo_id=model_id, filename="simswap_512_unoff.onnx", token=huggingface_token)
16
-
17
- def load_and_preprocess_image(image):
18
- if image is None or not isinstance(image, np.ndarray):
19
- raise ValueError("Input image is not valid.")
20
-
21
- img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
22
- img = cv2.resize(img, (512, 512))
23
- img = img / 255.0 # Normalize to [0, 1]
24
- return img.astype(np.float32)
25
-
26
- def swap_faces(source_image, target_image):
27
- try:
28
- # Load the ONNX model
29
- session = ort.InferenceSession(model_file)
30
-
31
- # Get input names
32
- input_names = [input.name for input in session.get_inputs()]
33
-
34
- # Print input shapes for debugging
35
- for input in session.get_inputs():
36
- print(f"Input '{input.name}' expects shape: {input.shape}")
37
-
38
- # Preprocess the images
39
- source_img = load_and_preprocess_image(source_image)
40
- target_img = load_and_preprocess_image(target_image)
41
-
42
- # Reshape inputs according to model requirements
43
- # For the first input (assuming it's the image input)
44
- source_input = source_img.transpose(2, 0, 1)[np.newaxis, ...] # Shape: (1, 3, 512, 512)
45
-
46
- # For the second input (onnx::Gemm_1), reshape to rank 2 as required by the error message
47
- target_features = target_img.transpose(2, 0, 1).reshape(-1, 512) # Reshape to 2D array
48
-
49
- # Create input dictionary
50
- input_dict = {
51
- input_names[0]: source_input.astype(np.float32),
52
- input_names[1]: target_features.astype(np.float32)
53
- }
54
-
55
- # Run inference
56
- result = session.run(None, input_dict)[0]
57
-
58
- # Post-process the result
59
- result = result[0].transpose(1, 2, 0) # Convert from NCHW to HWC format
60
- result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB) # Convert back to RGB
61
- return np.clip(result * 255, 0, 255).astype(np.uint8)
62
-
63
- except Exception as e:
64
- print(f"Error during face swapping: {str(e)}")
65
- raise
66
-
67
- # Create Gradio interface
68
- interface = gr.Interface(
69
- fn=swap_faces,
70
- inputs=[
71
- gr.Image(label="Source Face", type="numpy"),
72
- gr.Image(label="Target Image", type="numpy")
73
- ],
74
- outputs=gr.Image(label="Result"),
75
- title="Face Swap using SimSwap",
76
- description="Upload a source face and a target image to swap faces. The source face will be transferred onto the target image.",
77
- allow_flagging="never"
78
- )
79
-
80
- # Launch the interface
81
- if __name__ == "__main__":
82
- interface.launch()
 
1
+ onnxruntime
2
+ gradio
3
+ opencv-python
4
+ huggingface-hub
5
+ numpy