File size: 5,175 Bytes
8dc9718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
# @Time    : 2024/9/14 8:50
# @Project : FasterLivePortrait
# @FileName: test_api.py
import os
import requests
import zipfile
from io import BytesIO
import datetime
import json


def test_with_pickle_animal():
    try:
        data = {
            'flag_is_animal': True,
            'flag_pickle': True,
            'flag_relative_input': True,
            'flag_do_crop_input': True,
            'flag_remap_input': True,
            'driving_multiplier': 1.0,
            'flag_stitching': True,
            'flag_crop_driving_video_input': True,
            'flag_video_editing_head_rotation': False,
            'scale': 2.3,
            'vx_ratio': 0.0,
            'vy_ratio': -0.125,
            'scale_crop_driving_video': 2.2,
            'vx_ratio_crop_driving_video': 0.0,
            'vy_ratio_crop_driving_video': -0.1,
            'driving_smooth_observation_variance': 1e-7
        }
        source_image_path = "./assets/examples/source/s39.jpg"
        driving_pickle_path = "./assets/examples/driving/d8.pkl"

        # 打开文件
        files = {
            'source_image': open(source_image_path, 'rb'),
            'driving_pickle': open(driving_pickle_path, 'rb')
        }

        # 发送 POST 请求
        response = requests.post("http://127.0.0.1:9871/predict/", files=files, data=data)
        response.raise_for_status()
        with zipfile.ZipFile(BytesIO(response.content), "r") as zip_ref:
            # save files for each request in a different folder
            dt = datetime.datetime.now()
            ts = int(dt.timestamp())
            tgt = f"./results/api_{ts}/"
            os.makedirs(tgt, exist_ok=True)
            zip_ref.extractall(tgt)
            print("Extracted files into", tgt)

    except requests.exceptions.RequestException as e:
        print(f"Request Error: {e}")


def test_with_video_animal():
    try:
        data = {
            'flag_is_animal': True,
            'flag_pickle': False,
            'flag_relative_input': True,
            'flag_do_crop_input': True,
            'flag_remap_input': True,
            'driving_multiplier': 1.0,
            'flag_stitching': True,
            'flag_crop_driving_video_input': True,
            'flag_video_editing_head_rotation': False,
            'scale': 2.3,
            'vx_ratio': 0.0,
            'vy_ratio': -0.125,
            'scale_crop_driving_video': 2.2,
            'vx_ratio_crop_driving_video': 0.0,
            'vy_ratio_crop_driving_video': -0.1,
            'driving_smooth_observation_variance': 1e-7
        }
        source_image_path = "./assets/examples/source/s39.jpg"
        driving_video_path = "./assets/examples/driving/d0.mp4"
        files = {
            'source_image': open(source_image_path, 'rb'),
            'driving_video': open(driving_video_path, 'rb')
        }
        response = requests.post("http://127.0.0.1:9871/predict/", files=files, data=data)
        response.raise_for_status()
        with zipfile.ZipFile(BytesIO(response.content), "r") as zip_ref:
            # save files for each request in a different folder
            dt = datetime.datetime.now()
            ts = int(dt.timestamp())
            tgt = f"./results/api_{ts}/"
            os.makedirs(tgt, exist_ok=True)
            zip_ref.extractall(tgt)
            print("Extracted files into", tgt)

    except requests.exceptions.RequestException as e:
        print(f"Request Error: {e}")


def test_with_video_human():
    try:
        data = {
            'flag_is_animal': False,
            'flag_pickle': False,
            'flag_relative_input': True,
            'flag_do_crop_input': True,
            'flag_remap_input': True,
            'driving_multiplier': 1.0,
            'flag_stitching': True,
            'flag_crop_driving_video_input': True,
            'flag_video_editing_head_rotation': False,
            'scale': 2.3,
            'vx_ratio': 0.0,
            'vy_ratio': -0.125,
            'scale_crop_driving_video': 2.2,
            'vx_ratio_crop_driving_video': 0.0,
            'vy_ratio_crop_driving_video': -0.1,
            'driving_smooth_observation_variance': 1e-7
        }
        source_image_path = "./assets/examples/source/s11.jpg"
        driving_video_path = "./assets/examples/driving/d0.mp4"
        files = {
            'source_image': open(source_image_path, 'rb'),
            'driving_video': open(driving_video_path, 'rb')
        }
        response = requests.post("http://127.0.0.1:9871/predict/", files=files, data=data)
        response.raise_for_status()
        with zipfile.ZipFile(BytesIO(response.content), "r") as zip_ref:
            # save files for each request in a different folder
            dt = datetime.datetime.now()
            ts = int(dt.timestamp())
            tgt = f"./results/api_{ts}/"
            os.makedirs(tgt, exist_ok=True)
            zip_ref.extractall(tgt)
            print("Extracted files into", tgt)

    except requests.exceptions.RequestException as e:
        print(f"Request Error: {e}")


if __name__ == '__main__':
    test_with_video_animal()
    # test_with_pickle_animal()
    # test_with_video_human()