Skip to content

Commit

Permalink
Merge pull request #19 from rootvisionai/fix/labelImg_eksen
Browse files Browse the repository at this point in the history
fix/labelImg_eksen
  • Loading branch information
rootvisionai authored Jul 9, 2023
2 parents c423e67 + 817152f commit 6b24fd8
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
*.onnx
*.drawio
/dev_gitignored/
/frontend_python/
!frontend_python/make_request_local.py

2 changes: 1 addition & 1 deletion backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def generate(gen_type):
if len(pts_) >= 3:
print(f"---> Finding bounding box: {cnt_p}/{len(points_)}")
pts = np.array([np.array(pt) for pt in pts_])
y0, y1, x0, x1 = pts[:, 0].min(), pts[:, 0].max(), pts[:, 1].min(), pts[:, 1].max()
x0, x1 ,y0, y1= pts[:, 0].min(), pts[:, 0].max(), pts[:, 1].min(), pts[:, 1].max()
bboxes.append({
"coordinates": [int(x0), int(y0), int(x1), int(y1)],
"format": "xyxy",
Expand Down
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ labeling:
bounding_box: 1
polygon_resolution: 0.3

window_size: [1024, 1024]
window_size: [512, 512]
model: vit_h # Faster <<< vit_b, vit_l, vit_h >>> More accurate
threshold: 0.9
device: "cuda"
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 6b24fd8

Please sign in to comment.