Skip to content

Commit

Permalink
Merge branch 'main' into feature/faster_processing
Browse files Browse the repository at this point in the history
  • Loading branch information
rootvisionai authored Jul 23, 2023
2 parents ba925c6 + 6b24fd8 commit 8da80b1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
*.json
*.onnx
*.drawio
/dev/
/frontend_python/
/dev_gitignored/
!frontend_python/make_request_local.py
3 changes: 1 addition & 2 deletions backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,12 @@ def generate(gen_type):
)

for pt in points_:

bboxes.append({
"coordinates": [int(pt[:, 0].min()), int(pt[:, 1].min()), int(pt[:, 0].max()), int(pt[:, 1].max())],
"format": "xyxy",
"label": label_str
})

masks.append(mask_)

labels_str.append(label_str)
Expand Down
9 changes: 5 additions & 4 deletions frontend_python/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ def handle_click(event, x_click, y_click, flags, param):

if __name__ == "__main__":


cfg = utils.load_config("./config.yml")
annotations = []
encoded_images = []
image_paths = []
for image_id, image_path in enumerate(glob.glob(os.path.join("..", "support_images", "*.jpg"))):
for image_id, image_path in enumerate(glob.glob(os.path.join(".", "support_images", "*.jpg"))):
init_image = utils.import_image(image_path)
image_shape = init_image.shape
window_size = (512, 512)
image = cv2.resize(copy.deepcopy(init_image), (window_size[0], window_size[1]))
ref_width = cfg.window_size[0]
new_height = int((image_shape[0] / image_shape[1]) * ref_width)
image = cv2.resize(copy.deepcopy(init_image), (ref_width,new_height))
while True:
print("CLICK ON POSITIVE POINTS")
points = click_on_point(img=image)
Expand Down
13 changes: 13 additions & 0 deletions frontend_python/interface_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import numpy as np
import base64
import io
import yaml
import json
import types

def import_image(path):
image_ = Image.open(path)
Expand Down Expand Up @@ -42,3 +45,13 @@ def get_image(image_data):
image = Image.new("RGB", image_.size)
image.paste(image_)
return image

def load_config(path_to_config_yaml="./config.yaml"):
with open(path_to_config_yaml) as f:
dct = yaml.safe_load(f)

def load_object(dct):
return types.SimpleNamespace(**dct)

cfg = json.loads(json.dumps(dct), object_hook=load_object)
return cfg
82 changes: 82 additions & 0 deletions frontend_python/make_request_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import requests
import json
import interface_utils as utils
import time, os
import glob


# Define the URL of the endpoint: http://fewshotsam.rootvisionai.net
base = "localhost:8080"
url = f"http://{base}/extract_features" # replace with your actual endpoint

# make get request
if "http" in url:
response = os.system(f"curl --request GET http://{base}/health")
print(response)

# Convert json to dict
with open("request_content.json", "r") as fp:
data_json = json.load(fp)

# Set the headers for the request
headers = {
"Content-Type": "application/json"
}

# Send the POST request
t0 = time.time()
response = requests.post(url, data=json.dumps(data_json), headers=headers)

t1 = time.time()
print(response.text)
data_json = json.loads(response.text)

# Query Images: Load, extract, match
cfg_data_result_dir = "./results/"
cfg_data_query_dir = "./query_images/"
cfg_data_format = "jpg"
q_image_paths = glob.glob(os.path.join(cfg_data_query_dir, f"*.{cfg_data_format}"))
q_image_paths += glob.glob(os.path.join(cfg_data_query_dir, "**", f"*.{cfg_data_format}"))

for cnt, qip in enumerate(q_image_paths):
image_path = qip
image_filename = os.path.basename(image_path).split(".")[0]
data_json["image_path"] = image_path
init_image = utils.import_image(image_path)
data_json["image"] = utils.numpy_to_base64(init_image)

# Print the response
try:
print(data_json["error"] if "error" in data_json.keys() else data_json.keys())
except:
print(data_json)

# Define the URL of the endpoint
url = f"http://{base}/generate/all"

# Send the POST request
t1 = time.time()
response = requests.post(url, data=json.dumps(data_json), headers=headers)

t2 = time.time()
data_json = json.loads(response.text)
masks = utils.get_image(data_json["masks"])
masks.save(cfg_data_result_dir+f"masks_{image_filename}.png")

# Print the response
try:
print(data_json["error"] if "error" in data_json.keys() else data_json.keys())
except:
print(data_json)

# save annotation
with open(cfg_data_query_dir+f"{image_filename}.json", "w") as fp:
json.dump(data_json["coco_json"], fp, indent=4)


# save pascal_xml
with open(cfg_data_query_dir+f"{image_filename}.xml", "w", encoding="utf-8") as fp:
fp.write(str(data_json["pascal_xml"]))

print(f"Request.1 Time: {t1-t0} | Request.2 Time: {t2-t1}")

2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3
0.0.4

0 comments on commit 8da80b1

Please sign in to comment.