Commit
•
1dfbcf3
1
Parent(s):
6fa98d4
added script to test sending post request to the deployed API
Browse files- test_post_request.py +66 -0
test_post_request.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import base64
|
4 |
+
import logging
|
5 |
+
import argparse
|
6 |
+
import requests
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
from skimage.io import imread, imsave
|
10 |
+
|
11 |
+
|
12 |
+
def send_image_post_request(ARGS: argparse.Namespace) -> None:
|
13 |
+
logging.basicConfig(level=logging.INFO)
|
14 |
+
list_test_images = os.listdir(ARGS.dir_test_images)
|
15 |
+
|
16 |
+
for file_image in list_test_images:
|
17 |
+
target_file_image = file_image
|
18 |
+
file_image = os.path.join(ARGS.dir_test_images, file_image)
|
19 |
+
files = {"image_file": (file_image, open(file_image, "rb"))}
|
20 |
+
|
21 |
+
# if the deployment is on local machine
|
22 |
+
# response = requests.post(
|
23 |
+
# "http://127.0.0.1:7860/predict",
|
24 |
+
# files=files,
|
25 |
+
# )
|
26 |
+
|
27 |
+
# if the deployment is on hugging face
|
28 |
+
response = requests.post(
|
29 |
+
"https://abhishekrs4-image-colourization-cgan.hf.space/predict",
|
30 |
+
files=files,
|
31 |
+
)
|
32 |
+
|
33 |
+
# logging.info(response.json())
|
34 |
+
encoded_image = response.json()["encoded_image"]
|
35 |
+
image_str = base64.b64decode(encoded_image)
|
36 |
+
image_dec = cv2.imdecode(np.frombuffer(image_str, np.uint8), cv2.IMREAD_COLOR)
|
37 |
+
logging.info(f"image_sum: {np.sum(image_dec)}")
|
38 |
+
imsave(os.path.join(ARGS.dir_gen_images, target_file_image), image_dec)
|
39 |
+
return
|
40 |
+
|
41 |
+
|
42 |
+
def main() -> None:
|
43 |
+
dir_test_images = "./sample_test_images/"
|
44 |
+
dir_gen_images = "./generated_color_images/"
|
45 |
+
parser = argparse.ArgumentParser(
|
46 |
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
47 |
+
)
|
48 |
+
parser.add_argument(
|
49 |
+
"--dir_test_images",
|
50 |
+
default=dir_test_images,
|
51 |
+
type=str,
|
52 |
+
help="full directory path to dataset containing test images",
|
53 |
+
)
|
54 |
+
parser.add_argument(
|
55 |
+
"--dir_gen_images",
|
56 |
+
default=dir_gen_images,
|
57 |
+
type=str,
|
58 |
+
help="full directory path to directory where generated color images needs to be saved",
|
59 |
+
)
|
60 |
+
ARGS, unparsed = parser.parse_known_args()
|
61 |
+
send_image_post_request(ARGS)
|
62 |
+
return
|
63 |
+
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
main()
|