File size: 4,562 Bytes
9ee6f68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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