ManiTaskGen / src /preprocessing /renaming_engine.py
ManiacWallnut's picture
Deploy ManiTaskGen VLM-free Gradio demo
9ee6f68 verified
Raw History Blame Contribute Delete
4.56 kB
import json
import os
import glog
import sapien
import time
from src.vlm_interaction import vlm_interactor
from src.utils.config_manager import get_config
from src.utils.config_manager import get_rename_engine_config
import json
class RenamingEngine:
def __init__(self):
self.rename_engine_config = get_rename_engine_config()
self.model = self.rename_engine_config.model
self.interactor = vlm_interactor.VLMInteractor(mode="online", model=self.model)
self.interactor.initcount()
self.interactor.chkcount()
self.config = get_config()
self.prompts = self.config.rename_engine_prompt_template_path
if self.prompts is None or not os.path.exists(self.prompts):
glog.warning("Prompt template file not found.")
with open(self.prompts, "r") as f:
self.prompts = json.load(f)
def classify(self, img_path_folder, msg=None):
image_files = [
f
for f in os.listdir(img_path_folder)
if f.endswith((".jpg", ".png", ".jpeg"))
]
new_name_dict = {}
img_path_list = [os.path.join(img_path_folder, f) for f in image_files]
for i in range(0, len(img_path_list), 1):
self.interactor.add_content(
content=self.prompts["task_config"]["system_prompt"],
role="system",
content_type="text",
)
self.interactor.add_content(
content=self.prompts["task_config"]["user_prompt"],
role="user",
content_type="text",
)
self.interactor.add_content(
content=f"previously you have classified these items: {list(new_name_dict.values())}, please DO NOT repeat them",
role="user",
content_type="text",
)
# glog.info(f'previously you have classified these items{new_name_dict.values()}, please do not repeat them')
for j in range(i, min(i + 1, len(img_path_list))):
self.interactor.add_content(
content=img_path_list[j], role="user", content_type="image"
)
self.interactor.add_content(
content=self.prompts["task_config"]["per_pic_prompt"],
role="user",
content_type="text",
)
status_code, answer1 = self.interactor.send_content_n_request()
if status_code == vlm_interactor.InteractStatusCode.SUCCESS:
img_file = image_files[j]
print(answer1, img_file[: img_file.rfind(".")])
new_name_dict[img_file[: img_file.rfind(".")]] = answer1
self.interactor.add_content(
content=answer1, role="assistant", content_type="text"
)
else:
glog.info("VLM Interactor send failed")
new_name_dict[img_file[: img_file.rfind(".")]] = (
"VLM Interactor send failed"
)
pass
self.interactor.clear_history()
return new_name_dict
def rename_objects_with_engine(self, scene_graph, image_folder_path, device="cuda"):
"""
Rename objects in the scene graph using the renaming engine.
"""
glog.info("Renaming objects with the renaming engine...")
import numpy as np
for node_name, node in scene_graph.nodes.items():
if node.depth > 1:
node.auto_take_non_ground_object_picture(
scene=scene_graph.corresponding_scene,
view="human_focus",
width=1280,
height=720,
focus_ratio=0.5,
fovy_range=[np.deg2rad(40), np.deg2rad(60)],
save_path=os.path.join(image_folder_path, f"{node_name}.jpg"),
)
rename_dict = self.classify(
img_path_folder=image_folder_path,
)
glog.info("Renaming completed.")
return rename_dict
def main():
# Example usage
classifier = RenamingEngine()
img_path_folder = "./image4classify"
msg = "Classify these items"
ts = time.perf_counter()
new_names = classifier.classify(img_path_folder, msg)
print(new_names)
glog.info(f"Time taken: {time.perf_counter() - ts} seconds")
if __name__ == "__main__":
main()
# Example usage