Skip to content

Commit 4b6c27c

Browse files
committed
Virtual devices should encode frames as PNG
Because, the virtual world is too perfect to JPEG. Lossy is obvious in the virtual world. Lossless is expected and actually encodes really well for this type of frame, so no down-side.
1 parent 1ea3527 commit 4b6c27c

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

auto/camera.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
This is a **synchronous** interface.
1616
"""
1717

18+
import os
1819
import cv2
1920
import base64
2021
import numpy as np
@@ -23,6 +24,9 @@
2324
from auto.capabilities import list_caps, acquire, release
2425

2526

27+
IS_VIRTUAL = os.environ.get('MAI_IS_VIRTUAL', 'False').lower() in ['true', 't', '1', 'yes', 'y']
28+
29+
2630
class _CameraRGB:
2731
def __init__(self, camera):
2832
self._camera = camera
@@ -141,7 +145,11 @@ def base64_encode_image(frame):
141145
raise Exception("invalid number of channels")
142146
else:
143147
raise Exception("invalid frame ndarray ndim")
144-
jpg_img = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 50])[1].tobytes()
145-
base64_img = 'data:image/jpeg;base64,' + base64.b64encode(jpg_img).decode('ascii')
148+
if IS_VIRTUAL:
149+
png_img = cv2.imencode('.png', frame, [cv2.IMWRITE_PNG_COMPRESSION, 6])[1].tobytes()
150+
base64_img = 'data:image/png;base64,' + base64.b64encode(png_img).decode('ascii')
151+
else:
152+
jpg_img = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 50])[1].tobytes()
153+
base64_img = 'data:image/jpeg;base64,' + base64.b64encode(jpg_img).decode('ascii')
146154
return base64_img
147155

auto/frame_streamer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def stream(frame, to_console=True, to_labs=False, verbose=False):
175175
rect = [0, 0, 0, 0]
176176
console.stream_image(rect, shape, final_frame.tobytes())
177177

178-
# Convert the frame to a JPG buffer and publish to the network connection.
178+
# Encode the frame and publish to the network connection.
179179
if to_labs:
180180
base64_img = base64_encode_image(frame)
181181
send_message_to_labs({'base64_img': base64_img})

0 commit comments

Comments
 (0)