File size: 9,907 Bytes
7134ce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright (c) Kangan Qian. All rights reserved.
# Authors: Kangan Qian (Tsinghua University, Xiaomi Corporation)
# Description: Function agent for autonomous driving visual processing

from pathlib import Path
import pickle
import numpy as np
import math
import matplotlib.pyplot as plt
from PIL import Image
from skimage.draw import polygon
from third_party.yoloworld_demo import get_2dbox_open_vocabulary_detector
from third_party.depth_demo import get_3d_location


class FuncAgent:
    def __init__(self, data_dict=None, json_data_dict=None) -> None:
        """

        Initialize function agent for visual processing tasks

        

        Args:

            data_dict: Dictionary containing scene data

            json_data_dict: Dictionary containing JSON metadata

        """
        self.data = data_dict
        self.json_data_dict = json_data_dict
        self.short_trajectory_description = False

        # Define available visual functions
        self.visual_func_infos = [
            get_open_world_vocabulary_detection_info,
            get_3d_loc_in_cam_info,
            resize_image_info,
            crop_image_info,
        ]

    def get_open_world_vocabulary_detection(self, object_names: list, cam_type: str):
        """

        Detect objects in an image using open vocabulary detection

        

        Args:

            object_names: List of objects to detect

            cam_type: Camera type to process

            

        Returns:

            Tuple of prompts and detected bounding boxes

        """
        cam_path_info_list = self.json_data_dict['image']
        for cam_path_info in cam_path_info_list:
            if cam_type == cam_path_info.split('/')[1]:
                cur_cam_type_index = cam_path_info_list.index(cam_path_info)
        
        choosed_image_path = cam_path_info_list[cur_cam_type_index]
        prompts, detected_2d_boxs = get_2dbox_open_vocabulary_detector(
            text=object_names, 
            image_path=choosed_image_path
        )

        return prompts, detected_2d_boxs 

    def get_open_world_vocabulary_detection_info(self, object_names: list, image_path: str):
        """

        Detect objects in an image using open vocabulary detection

        

        Args:

            object_names: List of objects to detect

            image_path: Path to the image file

            

        Returns:

            Tuple of prompts and detected bounding boxes

        """
        prompts, detected_2d_boxs = get_2dbox_open_vocabulary_detector(
            text=object_names, 
            image_path=image_path
        )
        return prompts, detected_2d_boxs 

    def get_3d_loc_in_cam_info(self, object_names: list, image_path: str):
        """

        Get 3D locations of objects in camera coordinates

        

        Args:

            object_names: List of objects to locate

            image_path: Path to the image file

            

        Returns:

            Tuple of prompts and 3D locations

        """
        prompts, detected_loc_3d = get_3d_location(
            text=object_names, 
            image_path=image_path
        )
        return prompts, detected_loc_3d

    def get_ego_states(self):
        """Get ego vehicle state information"""
        return get_ego_prompts(self.data)


# Image processing functions and their metadata
resize_image_info = {
    "name": "resize_image",
    "description": "Resizes an image to specified dimensions with interpolation support",
    "parameters": {
        "type": "object",
        "properties": {
            "input_path": {"type": "string", "description": "Input image file path"},
            "output_path": {"type": "string", "description": "Output path for resized image"},
            "target_size": {
                "type": "array",
                "items": {"type": "integer"},
                "minItems": 2,
                "maxItems": 2,
                "description": "Target dimensions [width, height]"
            },
            "interpolation": {
                "type": "integer",
                "description": "Interpolation method (e.g., Image.BILINEAR for bilinear interpolation)"
            }
        },
        "required": ["input_path", "output_path", "target_size"]
    }
}


def resize_image(input_path, output_path, target_size, interpolation=Image.BILINEAR):
    """

    Resize an image to specified dimensions

    

    Args:

        input_path: Path to input image file

        output_path: Path to save resized image

        target_size: Target dimensions (width, height)

        interpolation: Interpolation method (default: bilinear)

    """
    with Image.open(input_path) as img:
        resized_img = img.resize(target_size, interpolation)
        resized_img.save(output_path)


crop_image_info = {
    "name": "crop_image",
    "description": "Crops a rectangular region from an image",
    "parameters": {
        "type": "object",
        "properties": {
            "input_path": {"type": "string", "description": "Input image file path"},
            "output_path": {"type": "string", "description": "Output path for cropped image"},
            "box": {
                "type": "array",
                "items": {"type": "integer"},
                "minItems": 4,
                "maxItems": 4,
                "description": "Crop region coordinates [left, upper, right, lower]"
            }
        },
        "required": ["input_path", "output_path", "box"]
    }
}


def crop_image(input_path, output_path, box):
    """

    Crop a region from an image

    

    Args:

        input_path: Path to input image file

        output_path: Path to save cropped image

        box: Crop region coordinates (left, upper, right, lower)

    """
    with Image.open(input_path) as img:
        cropped_img = img.crop(box)
        cropped_img.save(output_path)


rotate_image_info = {
    "name": "rotate_image",
    "description": "Rotates an image by specified degrees with canvas expansion support",
    "parameters": {
        "type": "object",
        "properties": {
            "input_path": {"type": "string", "description": "Input image file path"},
            "output_path": {"type": "string", "description": "Output path for rotated image"},
            "degrees": {"type": "number", "description": "Rotation angle in degrees (clockwise)"},
            "expand": {
                "type": "boolean",
                "description": "Whether to expand canvas to fit rotation (default: False)"
            },
            "fill_color": {
                "type": "array",
                "items": {"type": "integer"},
                "minItems": 3,
                "maxItems": 3,
                "description": "RGB fill color for expanded areas (default: [255,255,255])"
            }
        },
        "required": ["input_path", "output_path", "degrees"]
    }
}


def rotate_image(input_path, output_path, degrees, expand=False, fill_color=(255, 255, 255)):
    """

    Rotate an image by specified degrees

    

    Args:

        input_path: Path to input image file

        output_path: Path to save rotated image

        degrees: Rotation angle in degrees

        expand: Whether to expand canvas to fit rotation

        fill_color: Fill color for expanded areas

    """
    with Image.open(input_path) as img:
        rotated_img = img.rotate(degrees, expand=expand, fillcolor=fill_color)
        rotated_img.save(output_path)


adjust_brightness_info = {
    "name": "adjust_brightness",
    "description": "Adjusts image brightness using enhancement factor",
    "parameters": {
        "type": "object",
        "properties": {
            "input_path": {"type": "string", "description": "Input image file path"},
            "output_path": {"type": "string", "description": "Output path for adjusted image"},
            "factor": {
                "type": "number",
                "description": "Brightness multiplier (1.0=original, >1.0=brighter, <1.0=darker)"
            }
        },
        "required": ["input_path", "output_path", "factor"]
    }
}


def adjust_brightness(input_path, output_path, factor):
    """

    Adjust image brightness

    

    Args:

        input_path: Path to input image file

        output_path: Path to save adjusted image

        factor: Brightness multiplier (1.0=original, >1.0=brighter, <1.0=darker)

    """
    with Image.open(input_path) as img:
        enhancer = ImageEnhance.Brightness(img)
        bright_img = enhancer.enhance(factor)
        bright_img.save(output_path)


get_open_world_vocabulary_detection_info = {
    "name": "get_open_world_vocabulary_detection",
    "description": "Detects objects in an image using open vocabulary detection",
    "parameters": {
        "type": "object",
        "properties": {
            "text": {
                "type": "list",
                "description": "List of objects to detect",
            },
            "image_path": {
                "type": "str",
                "description": "Path to the image file"
            }
        },
        "required": ["text", "image_path"],
    },
}


get_3d_loc_in_cam_info = {
    "name": "get_3d_loc_in_cam",
    "description": "Calculates 3D locations of objects in camera coordinates",
    "parameters": {
        "type": "object",
        "properties": {
            "text": {
                "type": "list",
                "description": "List of objects to locate",
            },
            "image_path": {
                "type": "str",
                "description": "Path to the image file"
            }
        },
        "required": ["text", "image_path"],
    },
}