File size: 9,338 Bytes
2c58401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from datasets import build_dataset
import argparse
import opts
import time

import numpy as np
import matplotlib.pyplot as plt
import cv2
from io import BytesIO
import base64
from PIL import Image
import json

from openai import OpenAI

def mark_object_and_encode(frame, mask, instance_id, text_query, color_mask=False, label_number=False):
    #마스크 색칠할지
    if color_mask == True:
        alpha = 0.1

        colored_mask = np.zeros_like(frame)
        colored_mask[mask == 1] = [255, 0, 0]
        frame[mask == 1] = (
            (1 - alpha) * frame[mask == 1] +
            alpha * colored_mask[mask == 1]
        )

    #마스크 아웃라인 그리기
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(frame, contours, -1, [255, 0, 0], 2)

    #instance_id 적을지
    if label_number == True:
        if len(contours) > 0:
            largest_contour = max(contours, key=cv2.contourArea)
            M = cv2.moments(largest_contour)
            if M["m00"] != 0:  
                center_x = int(M["m10"] / M["m00"])
                center_y = int(M["m01"] / M["m00"])
            else:
                center_x, center_y = 0, 0

            font = cv2.FONT_HERSHEY_SIMPLEX
            text = str(instance_id)
            font_scale = 0.6  
            text_size = cv2.getTextSize(text, font, font_scale, 2)[0]                        
            text_x = center_x - text_size[0] // 1  # 텍스트의 가로 중심
            text_y = center_y
            # text_y = center_y + text_size[1] // 2 # 텍스트의 세로 중심
            
            # 텍스트 배경 사각형 좌표 계산
            rect_start = (text_x - 5, text_y - text_size[1] - 5) # 배경 사각형 좌상단
            # rect_end = (text_x + text_size[0] + 5, text_y + 5) 
            rect_end = (text_x + text_size[0] + 5, text_y)
            
            cv2.rectangle(frame, rect_start, rect_end, (0, 0, 0), -1)
            cv2.putText(frame, text, (text_x, text_y), font, font_scale, (255, 255, 255), 2)

    # plt.figure(figsize=(6, 10))
    # plt.imshow(frame)
    # plt.title(text_query)
    # plt.tight_layout()
    # plt.axis('off')
    # plt.show()

    buffer = BytesIO()
    frame = Image.fromarray(frame)
    frame.save(buffer, format='jpeg')
    buffer.seek(0)
    encoded_frame = base64.b64encode(buffer.read()).decode("utf-8")

    return encoded_frame

def getCaption(frame, mask, instance_id, text_query, model='gpt-4o', color_mask=False, label_number=True):
    
    base64_image = mark_object_and_encode(frame, mask, instance_id, text_query, color_mask, label_number)

    captioner = OpenAI()

    #필터링하지 않고 바로 ref exp 만들기
    dense_caption_prompt = f"""
    You are a visual assistant analyzing a single frame of a video.  
    In the given frame, I labeled 1 object by marking each with a bright numeric ID at the center and its boundary.
    I also give you a text query describing the marked object.
    I want to use your expression to create an **action-centric referring expression** dataset.  
    Based on the frame and text query, please describe the marked object using **clearly observable** and **specific** actions
    ---
    ## Guidelines:
    1. **Focus on visible, prominent actions** only (e.g., running, pushing, grasping an object).  
    2. **Avoid describing minor or ambiguous actions** (e.g., "slightly moving a paw", "slightly tilting head").  
    3. **Do not include subjective or speculative descriptions** (e.g., “it seems excited” or “it might be preparing to jump”).  
    4. **Avoid vague expressions** like "interacting with something" or "engaging with another object." Instead, specify the action (e.g., "grabbing a stick," "pressing a button").  
    5. **Use dynamic action verbs** (holding, throwing, inspecting, leaning, pressing) to highlight body movement or object/animal interaction.  
    6. If there are multiple objects, ensure the description for the marked object **differentiates** its action.  
    7. Base your description on these action definitions:
    - Avoid using term 'minimal' or 'slightly'.
    - General body movement, body position, or pattern which is prominent. (e.g. "lifting head up", "facing towards", "showing its back")
    - details such as motion and intention, facial with object manipulation
    - movements with object or other entities when they are prominent and observable. expression should be specific.
        (e.g., "pushing another person" (O), "engaging with someone" (X) "interacting with another person" (X))           
    --
    ## Output Format:
    - For each labeled object, output **exactly one line**. Your answer should contain details and follow the following format :
        object id. action-oriented description        
        (e.g. 1. the person is holding ski poles and skiing on a snow mountain, with his two legs bent forward.)
    ### Example
    If the frame has 1 labeled bear, your output should look like:
    1. the bear reaching his right arm while leaning forward to capture the prey
    ---
    **Do not include** appearance details (e.g., color, size, texture) or relative positioning (e.g., “on the left/right”).
    **Do not include object IDs** or reference them (e.g., "Person 1" or "object 2" is not allowed).
    **Do not include markdown** in the output.
    Keep in mind that you should not group the object, e.g., 2-5. people: xxx, be sure to describe each object separately (one by one). 
    For each labeled object, output referring expressions for each object id.
    """
    prompt_with_text_query = f"prompt: {dense_caption_prompt}\n text query: {text_query}"

    MAX_RETRIES = 2
    retry_count = 0
    
    while retry_count < MAX_RETRIES:
        response = captioner.chat.completions.create(
            model=model,
            messages=[
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": prompt_with_text_query,
                        },
                        {
                            "type": "image_url",
                            "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
                        },
                    ],
                }
            ],
        )    
            

        caption = response.choices[0].message.content.strip() 
        caption_lower = caption.lower().lstrip()
        if caption_lower.startswith("1.") and not any(
            phrase in caption_lower for phrase in ["i'm sorry", "please", "can't help"]
        ):
            break
        print(f"Retrying caption generation... ({retry_count + 1}/{MAX_RETRIES})")
        retry_count += 1
        time.sleep(2)
        
        if retry_count == MAX_RETRIES:
            caption = None
            print("Max retries reached. Caption generation failed.")
            
    else:
        caption = None

    return caption

if __name__ == "__main__":
    parser = argparse.ArgumentParser('ReferFormer training and evaluation script', parents=[opts.get_args_parser()])
    parser.add_argument('--save_caption_path', type=str, default='mbench_a2d/numbered_captions.json')
    args = parser.parse_args()

    train_dataset = build_dataset('a2d', image_set = 'train', args = args)
    text_annotations = train_dataset.text_annotations

    all_captions = {}

    #os.environ['OPENAI_API_KEY'] = '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'
    os.environ['OPENAI_API_KEY'] = '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'
    
    first_text_query = ""
    for idx in range(300):
        imgs, target = train_dataset[idx]
        frames_idx = target['frames_idx'].tolist()
        text_query, vid_id, frame_id, instance_id = text_annotations[idx]

        if text_query == first_text_query:
            continue

        print(f"------------vid id: {vid_id}, frame id: {frame_id}, instance id: {instance_id}", flush=True)
        
        frame_id = frame_id - 1
        frame_order = frames_idx.index(frame_id)

        frame = imgs[frame_order, :, :, :].permute(1, 2, 0).numpy()
        mask = target['masks'].numpy().astype(np.uint8).squeeze()
              
        caption = getCaption(frame, mask, instance_id, text_query, model='gpt-4o-mini')
        
        if vid_id in all_captions:
            if frame_id in all_captions[vid_id]:
                all_captions[vid_id][frame_id][instance_id] = caption
            else:
                all_captions[vid_id][frame_id] = {instance_id : caption}
        else:
            all_captions[vid_id] = {frame_id : {instance_id: caption}}

        if idx % 50 == 0:
            with open(args.save_caption_path, 'w') as file:
                json.dump(all_captions, file, indent=4)
    
    print("Finished!", flush=True)

    with open(args.save_caption_path, 'w') as file:
                json.dump(all_captions, file, indent=4)