File size: 9,609 Bytes
002bd9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
example from gradio: https://www.gradio.app/guides/running-background-tasks
"""
import gradio as gr
from dataclasses import dataclass
import sqlite3
import random
from utils.git_utils.tsv_io import TSVFile
import json
from PIL import Image
import io
import base64
import pandas as pd
import datetime
import os

# Connect to the SQLite database or create a new one if it doesn't exist
db_path = "tmp/annotations.db"
# Load the images and captions from the TSV files
images_tsv_path = "/home/v-xiaokhuang/sa1b_cropper/sa1b_data/sa1b-sub_image_w_bg-tsv/sa_000000.tar.tsv"
captions_tsv_path = "/home/v-xiaokhuang/sa1b_cropper/sa1b_data/sa1b-git_caption-tsv/model_iter_0007189.pt.TaxXiaokeV2.test0.crop384.crpPct1.fp16.gen.lenP0.6.beam4.predict.tsv"
annots_tsv_path = "/home/v-xiaokhuang/sa1b_cropper/sa1b_data/sa1b-annot-tsv/sa_000000.tar.tsv"
clip_scores_tsv_path = "/home/v-xiaokhuang/segment-caption-anything/out/sa1b-cap-0.clip-truncation.tsv"

images = TSVFile(images_tsv_path)
captions = TSVFile(captions_tsv_path)
if len(images) != len(captions):
    raise ValueError("Number of images and captions do not match.")
num_rows = len(images)


def b64_to_bin(base64_bin_str: str) -> bytes:
    """
    Decodes a base64 binary string to a binary string.
    """
    return io.BytesIO(base64.b64decode(base64_bin_str))


# Function to display a random image and its caption
def display_image_and_caption(gr_vars):
    images = gr_vars.images_tsv
    captions = gr_vars.captions_tsv
    annots = gr_vars.annots_tsv
    clip_scores = gr_vars.clip_scores_tsv
    num_rows = gr_vars.num_rows

    random_index = random.randint(0, num_rows - 1)
    identifier, base64_bytes = images[random_index]
    _identifier, caption_json_string = captions[random_index]
    if identifier != _identifier:
        raise ValueError(f"Image and caption identifiers do not match, {identifier} != {_identifier}")
    _identifier, annot_json_string = annots[random_index]
    if identifier != _identifier:
        raise ValueError(f"Image and annotation identifiers do not match, {identifier} != {_identifier}")
    _identifier, clip_scores_json_string = clip_scores[random_index]
    if identifier != _identifier:
        raise ValueError(f"Image and clip scores identifiers do not match, {identifier} != {_identifier}")

    image = Image.open(b64_to_bin(base64_bytes))

    caption_json = json.loads(caption_json_string)
    caption = caption_json[0]["caption"]  # TODO(xiaoke): now only use the first caption
    conf = caption_json[0].get("conf", -1)

    annot_json = json.loads(annot_json_string)
    area = annot_json["area"]
    image_size = annot_json["image_size"]
    image_area = image_size[0] * image_size[1]

    clip_scores_json = json.loads(clip_scores_json_string)
    clip_score = clip_scores_json[0]["clip_score"]  # TODO(xiaoke): now only use the first clip score
    # TODO(xiaoke): add aspect ratio

    image_id, region_cnt, region_id = list(map(int, identifier.split("-")))
    return (
        image,
        caption,
        dict(
            caption=caption,
            conf=conf,
            area=area,
            image_area=image_area,
            clip_score=clip_score,
            image_id=image_id,
            region_cnt=region_cnt,
            region_id=region_id,
        ),
    )


def get_latest_reviews(db: sqlite3.Connection):
    reviews = db.execute("SELECT * FROM annotations ORDER BY created_at DESC LIMIT 10").fetchall()
    num_reviews = db.execute("Select COUNT(image_id) from annotations").fetchone()[0]
    # caption, conf, area, image_area, clip_score, image_id, region_cnt, region_id, is_acceptable, created_at
    reviews = pd.DataFrame(
        reviews,
        columns=[
            "caption",
            "conf",
            "area",
            "image_area",
            "clip_score",
            "image_id",
            "region_cnt",
            "region_id",
            "is_acceptable",
            "created_at",
        ],
    )
    return reviews, num_reviews


def load_tables(gt_vars):
    db_path = gt_vars.db_path
    conn = sqlite3.connect(db_path)
    cur = conn.cursor()
    reviews, num_reviews = get_latest_reviews(conn)
    conn.close()
    return reviews, num_reviews


# Function to handle user annotation
def _annotate(gr_vars, sample_dict, is_acceptable):
    db_path = gr_vars.db_path
    conn = sqlite3.connect(db_path)
    cur = conn.cursor()
    ks = list(sample_dict.keys()) + ["is_acceptable"]
    vs = list(sample_dict.values()) + [int(is_acceptable)]
    cur.execute(
        f"INSERT INTO annotations ({', '.join(ks)}) VALUES ({', '.join(['?'] * len(ks))})",
        vs,
    )
    conn.commit()
    reviews, num_reviews = get_latest_reviews(conn)
    conn.close()
    return f"[{datetime.datetime.now()}] Annotation saved.", reviews, num_reviews


def annotate_yes(gr_vars, sample_dict):
    return _annotate(gr_vars, sample_dict, True)


def annotate_no(gr_vars, sample_dict):
    return _annotate(gr_vars, sample_dict, False)


@dataclass
class GradioVariables:
    db_path: str
    images_tsv: TSVFile = None
    captions_tsv: TSVFile = None
    annots_tsv: TSVFile = None
    clip_scores_tsv: TSVFile = None
    num_rows: int = None


def files_setup(
    gr_vars: GradioVariables,
    db_path,
    image_tsv_path,
    caption_tsv_path,
    annot_tsv_path,
    clip_scores_tsv_path,
):
    # Connect to the SQLite database or create a new one if it doesn't exist
    gr_vars.db_path = db_path
    conn = sqlite3.connect(db_path)
    cur = conn.cursor()

    # Create the annotations table if it doesn't exist
    # caption, conf, area, image_area, clip_score, image_id, region_cnt, region_id, is_acceptable, created_at
    print("Creating annotations table...")
    cur.execute(
        """
CREATE TABLE IF NOT EXISTS annotations (
    caption TEXT,
    conf REAL,
    area REAL,
    image_area REAL,
    clip_score REAL,
    image_id INTEGER,
    region_cnt INTEGER,
    region_id INTEGER,
    is_acceptable INTEGER,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)  
"""
    )

    # Load the images and captions from the TSV files
    for path in [image_tsv_path, caption_tsv_path, annot_tsv_path, clip_scores_tsv_path]:
        if not os.path.exists(path):
            raise ValueError(f"File {path} does not exist.")
    gr_vars.images_tsv = TSVFile(image_tsv_path)
    gr_vars.captions_tsv = TSVFile(caption_tsv_path)
    gr_vars.annots_tsv = TSVFile(annot_tsv_path)
    gr_vars.clip_scores_tsv = TSVFile(clip_scores_tsv_path)
    if len(gr_vars.images_tsv) != len(gr_vars.captions_tsv):
        raise ValueError("Number of images and captions do not match.")
    if len(gr_vars.images_tsv) != len(gr_vars.annots_tsv):
        raise ValueError("Number of images and annotations do not match.")
    if len(gr_vars.images_tsv) != len(gr_vars.clip_scores_tsv):
        raise ValueError("Number of images and CLIP scores do not match.")
    gr_vars.num_rows = len(gr_vars.images_tsv)


# Gradio UI components
with gr.Blocks() as iface:
    gr_vars = GradioVariables(db_path)
    gr_vars = gr.Variable(gr_vars)

    with gr.Accordion(label="Files Setup", open=False) as file_setup:
        db_path = gr.Textbox(label="Database Path", value=db_path)
        image_tsv_path = gr.Textbox(label="Image TSV Path", value=images_tsv_path)
        caption_tsv_path = gr.Textbox(label="Caption TSV Path", value=captions_tsv_path)
        annot_tsv_path = gr.Textbox(label="Annotation TSV Path", value=annots_tsv_path)
        clip_scores_tsv_path = gr.Textbox(label="CLIP Scores TSV Path", value=clip_scores_tsv_path)
        files_setup_button = gr.Button(value="Reload Files")
    files_setup_button.click(
        files_setup, inputs=[gr_vars, db_path, image_tsv_path, caption_tsv_path, annot_tsv_path, clip_scores_tsv_path]
    )
    iface.load(
        files_setup, inputs=[gr_vars, db_path, image_tsv_path, caption_tsv_path, annot_tsv_path, clip_scores_tsv_path]
    )

    intro = gr.Markdown(
        value="""## Dataset Annotator
This tool is used to annotate the dataset.
1. If the region and caption are related and acceptable, click "Yes".
    1. The "in the whitebackground" region is ok.
2. If the region and caption are not related or neither of them are not acceptable, click "No".

"""
    )

    with gr.Row() as image_row:
        image = gr.Image(height=500)
        with gr.Column() as caption_column:
            caption = gr.Textbox(label="Caption")
    sample_dict = gr.Variable({})

    with gr.Row() as choice_row:
        yes_button = gr.Button(value="Yes")
        no_button = gr.Button(value="No")
        # TODO(xiaoke) add a button to skip the current image
    db_output = gr.Textbox(label="Log")

    with gr.Accordion(label="Latest annotations from the database", open=True) as accordion:
        count = gr.Number(label="Total number of rows")
        data = gr.Dataframe(label="Most recently created 10 rows")

    iface_handle = iface.load(display_image_and_caption, inputs=[gr_vars], outputs=[image, caption, sample_dict])
    iface_handle.then(load_tables, inputs=[gr_vars], outputs=[data, count])

    yes_button_handle = yes_button.click(annotate_yes, inputs=[gr_vars, sample_dict], outputs=[db_output, data, count])
    yes_button_handle = yes_button_handle.then(
        display_image_and_caption, inputs=[gr_vars], outputs=[image, caption, sample_dict]
    )

    no_button_handle = no_button.click(annotate_no, inputs=[gr_vars, sample_dict], outputs=[db_output, data, count])
    no_button_handle.then(display_image_and_caption, inputs=[gr_vars], outputs=[image, caption, sample_dict])

iface.launch()