File size: 11,824 Bytes
2df809d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python3

# --------------------------------------------------------
# Script to pre-process the COP3D dataset.
# Usage:
#   python3 preprocess_cop3d.py --cop3d_dir /path/to/cop3d \
#       --output_dir /path/to/processed_cop3d
# --------------------------------------------------------

import argparse
import random
import gzip
import json
import os
import os.path as osp

import torch
import PIL.Image
import numpy as np
import cv2

from tqdm.auto import tqdm
import matplotlib.pyplot as plt

import src.dust3r.datasets.utils.cropping as cropping

# Define the object categories. (These are used for seeding.)
CATEGORIES = ["cat", "dog"]
CATEGORIES_IDX = {cat: i for i, cat in enumerate(CATEGORIES)}


def get_parser():
    """Set up the argument parser."""
    parser = argparse.ArgumentParser(
        description="Preprocess the CO3D dataset and output processed images, masks, and metadata."
    )
    parser.add_argument(
        "--output_dir",
        type=str,
        default="",
        help="Output directory for processed CO3D data.",
    )
    parser.add_argument(
        "--cop3d_dir",
        type=str,
        default="",
        help="Directory containing the raw CO3D data.",
    )
    parser.add_argument(
        "--seed", type=int, default=42, help="Random seed for reproducibility."
    )
    parser.add_argument(
        "--min_quality",
        type=float,
        default=0.5,
        help="Minimum viewpoint quality score.",
    )
    parser.add_argument(
        "--img_size",
        type=int,
        default=512,
        help=(
            "Lower dimension will be >= img_size * 3/4, and max dimension will be >= img_size"
        ),
    )
    return parser


def convert_ndc_to_pinhole(focal_length, principal_point, image_size):
    """Convert normalized device coordinates to a pinhole camera intrinsic matrix."""
    focal_length = np.array(focal_length)
    principal_point = np.array(principal_point)
    image_size_wh = np.array([image_size[1], image_size[0]])
    half_image_size = image_size_wh / 2
    rescale = half_image_size.min()
    principal_point_px = half_image_size - principal_point * rescale
    focal_length_px = focal_length * rescale
    fx, fy = focal_length_px[0], focal_length_px[1]
    cx, cy = principal_point_px[0], principal_point_px[1]
    K = np.array([[fx, 0.0, cx], [0.0, fy, cy], [0.0, 0.0, 1.0]], dtype=np.float32)
    return K


def opencv_from_cameras_projection(R, T, focal, p0, image_size):
    """
    Convert camera projection parameters from CO3D (NDC) to OpenCV coordinates.

    Returns:
        R, tvec, camera_matrix: OpenCV-style rotation matrix, translation vector, and intrinsic matrix.
    """
    R = torch.from_numpy(R)[None, :, :]
    T = torch.from_numpy(T)[None, :]
    focal = torch.from_numpy(focal)[None, :]
    p0 = torch.from_numpy(p0)[None, :]
    image_size = torch.from_numpy(image_size)[None, :]

    # Convert to PyTorch3D convention.
    R_pytorch3d = R.clone()
    T_pytorch3d = T.clone()
    focal_pytorch3d = focal
    p0_pytorch3d = p0
    T_pytorch3d[:, :2] *= -1
    R_pytorch3d[:, :, :2] *= -1
    tvec = T_pytorch3d
    R = R_pytorch3d.permute(0, 2, 1)

    # Retype image_size (flip to width, height).
    image_size_wh = image_size.to(R).flip(dims=(1,))

    # Compute scale and principal point.
    scale = image_size_wh.to(R).min(dim=1, keepdim=True)[0] / 2.0
    scale = scale.expand(-1, 2)
    c0 = image_size_wh / 2.0
    principal_point = -p0_pytorch3d * scale + c0
    focal_length = focal_pytorch3d * scale

    camera_matrix = torch.zeros_like(R)
    camera_matrix[:, :2, 2] = principal_point
    camera_matrix[:, 2, 2] = 1.0
    camera_matrix[:, 0, 0] = focal_length[:, 0]
    camera_matrix[:, 1, 1] = focal_length[:, 1]
    return R[0], tvec[0], camera_matrix[0]


def get_set_list(category_dir, split):
    """Obtain a list of sequences for a given category and split."""
    listfiles = os.listdir(osp.join(category_dir, "set_lists"))
    subset_list_files = [f for f in listfiles if "manyview" in f]
    if len(subset_list_files) <= 0:
        subset_list_files = [f for f in listfiles if "fewview" in f]

    sequences_all = []
    for subset_list_file in subset_list_files:
        with open(osp.join(category_dir, "set_lists", subset_list_file)) as f:
            subset_lists_data = json.load(f)
            sequences_all.extend(subset_lists_data[split])
    return sequences_all


def prepare_sequences(
    category, cop3d_dir, output_dir, img_size, split, min_quality, seed
):
    """
    Process sequences for a given category and split.

    This function loads per-frame and per-sequence annotations,
    filters sequences based on quality, crops and rescales images,
    and saves metadata for each frame.

    Returns a dictionary mapping sequence names to lists of selected frame indices.
    """
    random.seed(seed)
    category_dir = osp.join(cop3d_dir, category)
    category_output_dir = osp.join(output_dir, category)
    sequences_all = get_set_list(category_dir, split)

    # Get unique sequence names.
    sequences_numbers = sorted(set(seq_name for seq_name, _, _ in sequences_all))

    # Load frame and sequence annotation files.
    frame_file = osp.join(category_dir, "frame_annotations.jgz")
    sequence_file = osp.join(category_dir, "sequence_annotations.jgz")

    with gzip.open(frame_file, "r") as fin:
        frame_data = json.loads(fin.read())
    with gzip.open(sequence_file, "r") as fin:
        sequence_data = json.loads(fin.read())

    # Organize frame annotations per sequence.
    frame_data_processed = {}
    for f_data in frame_data:
        sequence_name = f_data["sequence_name"]
        frame_data_processed.setdefault(sequence_name, {})[
            f_data["frame_number"]
        ] = f_data

    # Select sequences with quality above the threshold.
    good_quality_sequences = set()
    for seq_data in sequence_data:
        if seq_data["viewpoint_quality_score"] > min_quality:
            good_quality_sequences.add(seq_data["sequence_name"])
    sequences_numbers = [
        seq_name for seq_name in sequences_numbers if seq_name in good_quality_sequences
    ]
    selected_sequences_numbers = sequences_numbers
    selected_sequences_numbers_dict = {
        seq_name: [] for seq_name in selected_sequences_numbers
    }

    # Filter frames to only those from selected sequences.
    sequences_all = [
        (seq_name, frame_number, filepath)
        for seq_name, frame_number, filepath in sequences_all
        if seq_name in selected_sequences_numbers_dict
    ]

    # Process each frame.
    for seq_name, frame_number, filepath in tqdm(
        sequences_all, desc="Processing frames"
    ):
        frame_idx = int(filepath.split("/")[-1][5:-4])
        selected_sequences_numbers_dict[seq_name].append(frame_idx)
        mask_path = filepath.replace("images", "masks").replace(".jpg", ".png")
        frame_data_entry = frame_data_processed[seq_name][frame_number]
        focal_length = frame_data_entry["viewpoint"]["focal_length"]
        principal_point = frame_data_entry["viewpoint"]["principal_point"]
        image_size = frame_data_entry["image"]["size"]
        K = convert_ndc_to_pinhole(focal_length, principal_point, image_size)
        R, tvec, camera_intrinsics = opencv_from_cameras_projection(
            np.array(frame_data_entry["viewpoint"]["R"]),
            np.array(frame_data_entry["viewpoint"]["T"]),
            np.array(focal_length),
            np.array(principal_point),
            np.array(image_size),
        )

        # Load input image and mask.
        image_path = osp.join(cop3d_dir, filepath)
        mask_path_full = osp.join(cop3d_dir, mask_path)
        input_rgb_image = PIL.Image.open(image_path).convert("RGB")
        input_mask = plt.imread(mask_path_full)
        H, W = input_mask.shape

        camera_intrinsics = camera_intrinsics.numpy()
        cx, cy = camera_intrinsics[:2, 2].round().astype(int)
        min_margin_x = min(cx, W - cx)
        min_margin_y = min(cy, H - cy)
        l, t = cx - min_margin_x, cy - min_margin_y
        r, b = cx + min_margin_x, cy + min_margin_y
        crop_bbox = (l, t, r, b)

        # Crop the image, mask, and adjust intrinsics.
        input_rgb_image, input_mask, input_camera_intrinsics = (
            cropping.crop_image_depthmap(
                input_rgb_image, input_mask, camera_intrinsics, crop_bbox
            )
        )
        scale_final = ((img_size * 3 // 4) / min(H, W)) + 1e-8
        output_resolution = np.floor(np.array([W, H]) * scale_final).astype(int)
        if max(output_resolution) < img_size:
            scale_final = (img_size / max(H, W)) + 1e-8
            output_resolution = np.floor(np.array([W, H]) * scale_final).astype(int)
        input_rgb_image, input_mask, input_camera_intrinsics = (
            cropping.rescale_image_depthmap(
                input_rgb_image, input_mask, input_camera_intrinsics, output_resolution
            )
        )

        # Generate and adjust camera pose.
        camera_pose = np.eye(4, dtype=np.float32)
        camera_pose[:3, :3] = R
        camera_pose[:3, 3] = tvec
        camera_pose = np.linalg.inv(camera_pose)

        # Save processed image and mask.
        save_img_path = osp.join(output_dir, filepath)
        save_mask_path = osp.join(output_dir, mask_path)
        os.makedirs(osp.split(save_img_path)[0], exist_ok=True)
        os.makedirs(osp.split(save_mask_path)[0], exist_ok=True)
        input_rgb_image.save(save_img_path)
        cv2.imwrite(save_mask_path, (input_mask * 255).astype(np.uint8))

        # Save metadata (intrinsics and pose).
        save_meta_path = save_img_path.replace("jpg", "npz")
        np.savez(
            save_meta_path,
            camera_intrinsics=input_camera_intrinsics,
            camera_pose=camera_pose,
        )

    return selected_sequences_numbers_dict


def main():
    parser = get_parser()
    args = parser.parse_args()
    assert (
        args.cop3d_dir != args.output_dir
    ), "Input and output directories must differ."
    categories = CATEGORIES
    os.makedirs(args.output_dir, exist_ok=True)

    # Process each split separately.
    for split in ["train", "test"]:
        selected_sequences_path = osp.join(
            args.output_dir, f"selected_seqs_{split}.json"
        )
        if os.path.isfile(selected_sequences_path):
            continue

        all_selected_sequences = {}
        for category in categories:
            category_output_dir = osp.join(args.output_dir, category)
            os.makedirs(category_output_dir, exist_ok=True)
            category_selected_sequences_path = osp.join(
                category_output_dir, f"selected_seqs_{split}.json"
            )
            if os.path.isfile(category_selected_sequences_path):
                with open(category_selected_sequences_path, "r") as fid:
                    category_selected_sequences = json.load(fid)
            else:
                print(f"Processing {split} - category = {category}")
                category_selected_sequences = prepare_sequences(
                    category=category,
                    cop3d_dir=args.cop3d_dir,
                    output_dir=args.output_dir,
                    img_size=args.img_size,
                    split=split,
                    min_quality=args.min_quality,
                    seed=args.seed + CATEGORIES_IDX[category],
                )
                with open(category_selected_sequences_path, "w") as file:
                    json.dump(category_selected_sequences, file)

            all_selected_sequences[category] = category_selected_sequences

        with open(selected_sequences_path, "w") as file:
            json.dump(all_selected_sequences, file)


if __name__ == "__main__":
    main()