Skip to content

Commit

Permalink
Update semantic localization
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyuan1996 committed Sep 30, 2021
1 parent fb761ee commit 110b871
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/data/retrieval_system_data/tmp/
/data/retrieval_system_data/semantic_localization_data/demo/
/code/models/checkpoint/
/data/retrieval_system_data/semantic_localization_data/
/data/retrieval_system_data/semantic_localization_data/8e9yjvt4/
15 changes: 12 additions & 3 deletions code/api_controlers/semantic_localization_ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def split_image(img_path, steps):

def generate_heatmap(img_path, text):
subimages_dir = os.path.join(cfg['data_paths']['temp_path'], os.path.basename(img_path).split(".")[0]) +'_subimages'
heatmap_dir = os.path.join(cfg['data_paths']['semantic_localization_path'], os.path.basename(img_path).split(".")[0])

heatmap_subdir = utils.create_random_dirs_name(cfg['data_paths']['temp_path'])
heatmap_dir = os.path.join(cfg['data_paths']['semantic_localization_path'], heatmap_subdir)

# 清除缓存
if os.path.exists(heatmap_dir):
utils.delete_dire(heatmap_dir)
Expand Down Expand Up @@ -123,6 +126,12 @@ def generate_heatmap(img_path, text):
cv2.imwrite(os.path.join(heatmap_dir, "heatmap_add.png"),img_add)
logger.info("Saved ok.")

# clear temp
utils.delete_dire(subimages_dir)
os.rmdir(subimages_dir)

return heatmap_dir


def semantic_localization(request_data):
logger.info("Request json: {}".format(request_data))
Expand All @@ -147,5 +156,5 @@ def semantic_localization(request_data):
return utils.get_stand_return(False, "File format is uncorrect: only support .tif, .tiff, .jpg, and .png .")
else:
split_image(image_path, steps)
generate_heatmap(image_path, text)
return utils.get_stand_return(True, "Generate successfully.")
heatmap_dir = generate_heatmap(image_path, text)
return utils.get_stand_return(True, "Generate successfully in {}".format(heatmap_dir))
12 changes: 10 additions & 2 deletions code/api_controlers/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import yaml
import numpy as np
import pickle
import os
import os,random

# 加载参数
def get_config(path_opt="common/config.yaml"):
Expand Down Expand Up @@ -120,4 +120,12 @@ def log_to_txt( contexts=None,filename="save.txt", mark=False,encoding='UTF-8',a
def load_from_txt(filename, encoding="utf-8"):
f = open(filename,'r' ,encoding=encoding)
contexts = f.readlines()
return contexts
return contexts

# 创建随机文件夹
def create_random_dirs_name(dir_path):
dirs = os.listdir(dir_path)
new_dir = ""
while (new_dir == "") or (new_dir in dirs):
new_dir = "".join(random.sample('1234567890qwertyuiopasdfghjklzxcvbnm', 8))
return new_dir
1 change: 1 addition & 0 deletions code/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
logger.info("Create init variables")
globalvar.set_value("unembeded_images", value={})
globalvar.set_value("rsd", value=utils.init_rsd(cfg['data_paths']['rsd_path']))
utils.create_dirs(cfg['data_paths']['rsd_path'])
utils.create_dirs(cfg['data_paths']['semantic_localization_path'])
utils.create_dirs(cfg['data_paths']['temp_path'])

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def post_semantic_localization():
# # semantic localization
data = {
'image_path': "../data/test_data/images/demo1.tif",
'text': "there are two tennis c ourts beside the playground",
'text': "there are two tennis courts beside the playground",
'params': {
'steps': [128,256,512]
},
Expand Down

0 comments on commit 110b871

Please sign in to comment.