Spaces:
Running
Running
File size: 4,696 Bytes
8ff3c52 |
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 145 146 147 148 |
import argparse
import pickle
import time
from typing import Dict
import numpy as np
import requests
from loguru import logger
API_URL_MATCH = "http://127.0.0.1:8001/v1/match"
API_URL_EXTRACT = "http://127.0.0.1:8001/v1/extract"
API_URL_EXTRACT_V2 = "http://127.0.0.1:8001/v2/extract"
def send_generate_request(path0: str, path1: str) -> Dict[str, np.ndarray]:
"""
Send a request to the API to generate a match between two images.
Args:
path0 (str): The path to the first image.
path1 (str): The path to the second image.
Returns:
Dict[str, np.ndarray]: A dictionary containing the generated matches.
The keys are "keypoints0", "keypoints1", "matches0", and "matches1",
and the values are ndarrays of shape (N, 2), (N, 2), (N, 2), and
(N, 2), respectively.
"""
files = {"image0": open(path0, "rb"), "image1": open(path1, "rb")}
try:
response = requests.post(API_URL_MATCH, files=files)
pred = {}
if response.status_code == 200:
pred = response.json()
for key in list(pred.keys()):
pred[key] = np.array(pred[key])
else:
print(
f"Error: Response code {response.status_code} - {response.text}"
)
finally:
files["image0"].close()
files["image1"].close()
return pred
def send_generate_request1(path0: str) -> Dict[str, np.ndarray]:
"""
Send a request to the API to extract features from an image.
Args:
path0 (str): The path to the image.
Returns:
Dict[str, np.ndarray]: A dictionary containing the extracted features.
The keys are "keypoints", "descriptors", and "scores", and the
values are ndarrays of shape (N, 2), (N, 128), and (N,),
respectively.
"""
files = {"image": open(path0, "rb")}
try:
response = requests.post(API_URL_EXTRACT, files=files)
pred: Dict[str, np.ndarray] = {}
if response.status_code == 200:
pred = response.json()
for key in list(pred.keys()):
pred[key] = np.array(pred[key])
else:
print(
f"Error: Response code {response.status_code} - {response.text}"
)
finally:
files["image"].close()
return pred
def send_generate_request2(image_path: str) -> Dict[str, np.ndarray]:
"""
Send a request to the API to extract features from an image.
Args:
image_path (str): The path to the image.
Returns:
Dict[str, np.ndarray]: A dictionary containing the extracted features.
The keys are "keypoints", "descriptors", and "scores", and the
values are ndarrays of shape (N, 2), (N, 128), and (N,), respectively.
"""
data = {
"image_path": image_path,
"max_keypoints": 1024,
"reference_points": [[0.0, 0.0], [1.0, 1.0]],
}
pred = {}
try:
response = requests.post(API_URL_EXTRACT_V2, json=data)
pred: Dict[str, np.ndarray] = {}
if response.status_code == 200:
pred = response.json()
for key in list(pred.keys()):
pred[key] = np.array(pred[key])
else:
print(
f"Error: Response code {response.status_code} - {response.text}"
)
except Exception as e:
print(f"An error occurred: {e}")
return pred
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Send text to stable audio server and receive generated audio."
)
parser.add_argument(
"--image0",
required=False,
help="Path for the file's melody",
default="../datasets/sacre_coeur/mapping_rot/02928139_3448003521_rot45.jpg",
)
parser.add_argument(
"--image1",
required=False,
help="Path for the file's melody",
default="../datasets/sacre_coeur/mapping_rot/02928139_3448003521_rot90.jpg",
)
args = parser.parse_args()
for i in range(10):
t1 = time.time()
preds = send_generate_request(args.image0, args.image1)
t2 = time.time()
logger.info(f"Time cost1: {(t2 - t1)} seconds")
for i in range(10):
t1 = time.time()
preds = send_generate_request1(args.image0)
t2 = time.time()
logger.info(f"Time cost2: {(t2 - t1)} seconds")
for i in range(10):
t1 = time.time()
preds = send_generate_request2(args.image0)
t2 = time.time()
logger.info(f"Time cost2: {(t2 - t1)} seconds")
with open("preds.pkl", "wb") as f:
pickle.dump(preds, f)
|