Skip to content

Commit fff7b02

Browse files
committed
update all python examples that used to use BGR->RGB conversion to use BGR directly
(tested all scripts touched here)
1 parent 47edbf6 commit fff7b02

File tree

10 files changed

+21
-37
lines changed

10 files changed

+21
-37
lines changed

docs/snippets/all/archetypes/image_advanced.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@
3333
# Read with OpenCV
3434
image = cv2.imread(file_path)
3535

36-
# OpenCV uses BGR ordering, so we need to convert to RGB.
37-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
38-
rr.log("from_opencv", rr.Image(image))
36+
# OpenCV uses BGR ordering, we need to make this known to Rerun.
37+
rr.log("from_opencv", rr.Image(image, color_model="BGR"))

examples/python/arkit_scenes/arkit_scenes/__main__.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ def log_arkit(recording_path: Path, include_highres: bool) -> None:
225225
rr.set_time_seconds("time", float(frame_timestamp))
226226
# load the lowres image and depth
227227
bgr = cv2.imread(f"{lowres_image_dir}/{video_id}_{frame_timestamp}.png")
228-
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
229228
depth = cv2.imread(f"{lowres_depth_dir}/{video_id}_{frame_timestamp}.png", cv2.IMREAD_ANYDEPTH)
230229

231230
high_res_exists: bool = (image_dir / f"{video_id}_{frame_timestamp}.png").exists() and include_highres
@@ -240,7 +239,7 @@ def log_arkit(recording_path: Path, include_highres: bool) -> None:
240239
LOWRES_POSED_ENTITY_PATH,
241240
)
242241

243-
rr.log(f"{LOWRES_POSED_ENTITY_PATH}/rgb", rr.Image(rgb).compress(jpeg_quality=95))
242+
rr.log(f"{LOWRES_POSED_ENTITY_PATH}/bgr", rr.Image(bgr, color_model="BGR").compress(jpeg_quality=95))
244243
rr.log(f"{LOWRES_POSED_ENTITY_PATH}/depth", rr.DepthImage(depth, meter=1000))
245244

246245
# log the high res camera
@@ -260,9 +259,7 @@ def log_arkit(recording_path: Path, include_highres: bool) -> None:
260259
highres_bgr = cv2.imread(f"{image_dir}/{video_id}_{frame_timestamp}.png")
261260
highres_depth = cv2.imread(f"{depth_dir}/{video_id}_{frame_timestamp}.png", cv2.IMREAD_ANYDEPTH)
262261

263-
highres_rgb = cv2.cvtColor(highres_bgr, cv2.COLOR_BGR2RGB)
264-
265-
rr.log(f"{HIGHRES_ENTITY_PATH}/rgb", rr.Image(highres_rgb).compress(jpeg_quality=75))
262+
rr.log(f"{HIGHRES_ENTITY_PATH}/bgr", rr.Image(highres_bgr, color_model="BGR").compress(jpeg_quality=75))
266263
rr.log(f"{HIGHRES_ENTITY_PATH}/depth", rr.DepthImage(highres_depth, meter=1000))
267264

268265

@@ -293,9 +290,9 @@ def main() -> None:
293290
# For this to work, the origin of the 2D views has to be a pinhole camera,
294291
# this way the viewer knows how to project the 3D annotations into the 2D views.
295292
rrb.Spatial2DView(
296-
name="RGB",
293+
name="BGR",
297294
origin=primary_camera_entity,
298-
contents=["$origin/rgb", "/world/annotations/**"],
295+
contents=["$origin/bgr", "/world/annotations/**"],
299296
),
300297
rrb.Spatial2DView(
301298
name="Depth",

examples/python/face_tracking/face_tracking.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,12 @@ def run_from_video_capture(vid: int | str, max_dim: int | None, max_frame_count:
357357
# On some platforms it always returns zero, so we compute from the frame counter and fps
358358
frame_time_nano = int(frame_idx * 1000 / fps * 1e6)
359359

360-
# convert to rgb
361-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
362-
363360
# log data
364361
rr.set_time_sequence("frame_nr", frame_idx)
365362
rr.set_time_nanos("frame_time", frame_time_nano)
366363
detector.detect_and_log(frame, frame_time_nano)
367364
landmarker.detect_and_log(frame, frame_time_nano)
368-
rr.log("video/image", rr.Image(frame))
365+
rr.log("video/image", rr.Image(frame, color_model="BGR"))
369366

370367
except KeyboardInterrupt:
371368
pass
@@ -379,12 +376,11 @@ def run_from_sample_image(path: Path, max_dim: int | None, num_faces: int) -> No
379376
"""Run the face detector on a single image."""
380377
image = cv2.imread(str(path))
381378
image = resize_image(image, max_dim)
382-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
383379
logger = FaceDetectorLogger(video_mode=False)
384380
landmarker = FaceLandmarkerLogger(video_mode=False, num_faces=num_faces)
385381
logger.detect_and_log(image, 0)
386382
landmarker.detect_and_log(image, 0)
387-
rr.log("video/image", rr.Image(image))
383+
rr.log("video/image", rr.Image(image, color_model="BGR"))
388384

389385

390386
def main() -> None:

examples/python/gesture_detection/gesture_detection.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ def run_from_sample_image(path: Path | str) -> None:
192192
"""Run the gesture recognition on a single image."""
193193
image = cv2.imread(str(path))
194194
# image = resize_image(image, max_dim)
195-
show_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
196-
rr.log("media/image", rr.Image(show_image))
195+
rr.log("media/image", rr.Image(image, color_model="BGR"))
197196
logger = GestureDetectorLogger(video_mode=False)
198197
logger.detect_and_log(show_image, 0)
199198

@@ -236,14 +235,11 @@ def run_from_video_capture(vid: int | str, max_frame_count: int | None) -> None:
236235
# On some platforms it always returns zero, so we compute from the frame counter and fps
237236
frame_time_nano = int(frame_idx * 1000 / fps * 1e6)
238237

239-
# convert to rgb
240-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
241-
242238
# log data
243239
rr.set_time_sequence("frame_nr", frame_idx)
244240
rr.set_time_nanos("frame_time", frame_time_nano)
245241
detector.detect_and_log(frame, frame_time_nano)
246-
rr.log("media/video", rr.Image(frame).compress(jpeg_quality=75))
242+
rr.log("media/video", rr.Image(frame, color_model="BGR").compress(jpeg_quality=75))
247243

248244
except KeyboardInterrupt:
249245
pass

examples/python/human_pose_tracking/human_pose_tracking.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,14 @@ def track_pose(video_path: str, model_path: str, *, segment: bool, max_frame_cou
7777
break
7878

7979
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=bgr_frame.data)
80-
rgb = cv2.cvtColor(bgr_frame.data, cv2.COLOR_BGR2RGB)
8180
rr.set_time_seconds("time", bgr_frame.time)
8281
rr.set_time_sequence("frame_idx", bgr_frame.idx)
8382

8483
results = pose_landmarker.detect_for_video(mp_image, int(bgr_frame.time * 1000))
85-
h, w, _ = rgb.shape
84+
h, w, _ = bgr_frame.data.shape
8685
landmark_positions_2d = read_landmark_positions_2d(results, w, h)
8786

88-
rr.log("video/rgb", rr.Image(rgb).compress(jpeg_quality=75))
87+
rr.log("video/bgr", rr.Image(bgr_frame.data, color_model="BGR").compress(jpeg_quality=75))
8988
if landmark_positions_2d is not None:
9089
rr.log(
9190
"video/pose/points",
@@ -237,7 +236,7 @@ def main() -> None:
237236
rrb.Spatial3DView(origin="person", name="3D pose"),
238237
),
239238
rrb.Vertical(
240-
rrb.Spatial2DView(origin="video/rgb", name="Raw video"),
239+
rrb.Spatial2DView(origin="video/bgr", name="Raw video"),
241240
rrb.TextDocumentView(origin="description", name="Description"),
242241
row_shares=[2, 3],
243242
),

examples/python/live_camera_edge_detection/live_camera_edge_detection.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ def run_canny(num_frames: int | None) -> None:
4242
frame_nr += 1
4343

4444
# Log the original image
45-
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
46-
rr.log("image/rgb", rr.Image(rgb))
45+
rr.log("image/rgb", rr.Image(img, color_model="BGR"))
4746

4847
# Convert to grayscale
4948
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

examples/python/ocr/ocr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def detect_and_log_layouts(file_path: str) -> None:
365365
else:
366366
# read image
367367
img = cv2.imread(file_path)
368-
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
368+
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Rerun can handle BGR as well, but `ocr_model_pp` expects RGB
369369
images.append(image_rgb.astype(np.uint8))
370370

371371
# Extracte the layout from each image

examples/python/rgbd/rgbd.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ def parse_timestamp(filename: str) -> datetime:
4444
return datetime.fromtimestamp(float(time))
4545

4646

47-
def read_image_rgb(buf: bytes) -> npt.NDArray[np.uint8]:
47+
def read_image_bgr(buf: bytes) -> npt.NDArray[np.uint8]:
4848
"""Decode an image provided in `buf`, and interpret it as RGB data."""
4949
np_buf: npt.NDArray[np.uint8] = np.ndarray(shape=(1, len(buf)), dtype=np.uint8, buffer=buf)
50-
# OpenCV reads images in BGR rather than RGB format
5150
img_bgr = cv2.imdecode(np_buf, cv2.IMREAD_COLOR)
52-
img_rgb: npt.NDArray[Any] = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
53-
return img_rgb
51+
return img_bgr
5452

5553

5654
def read_depth_image(buf: bytes) -> npt.NDArray[Any]:
@@ -85,8 +83,8 @@ def log_nyud_data(recording_path: Path, subset_idx: int, frames: int) -> None:
8583

8684
if f.filename.endswith(".ppm"):
8785
buf = archive.read(f)
88-
img_rgb = read_image_rgb(buf)
89-
rr.log("world/camera/image/rgb", rr.Image(img_rgb).compress(jpeg_quality=95))
86+
img_bgr = read_image_bgr(buf)
87+
rr.log("world/camera/image/rgb", rr.Image(img_bgr, color_model="BGR").compress(jpeg_quality=95))
9088

9189
elif f.filename.endswith(".pgm"):
9290
buf = archive.read(f)

examples/python/segment_anything_model/segment_anything_model.py

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def load_image(image_uri: str) -> cv2.typing.MatLike:
138138
else:
139139
image = cv2.imread(image_uri, cv2.IMREAD_COLOR)
140140

141+
# Rerun can handle BGR as well, but SAM requires RGB.
141142
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
142143
return image
143144

examples/python/structure_from_motion/structure_from_motion/__main__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ def read_and_log_sparse_reconstruction(dataset_path: Path, filter_output: bool,
162162
if resize:
163163
bgr = cv2.imread(str(image_file))
164164
bgr = cv2.resize(bgr, resize)
165-
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
166-
rr.log("camera/image", rr.Image(rgb).compress(jpeg_quality=75))
165+
rr.log("camera/image", rr.Image(bgr, color_model="BGR").compress(jpeg_quality=75))
167166
else:
168167
rr.log("camera/image", rr.EncodedImage(path=dataset_path / "images" / image.name))
169168

0 commit comments

Comments
 (0)