Skip to content

Commit ecbaf9f

Browse files
visionipc: replace numpy with memoryview (#688)
1 parent 3eb9a5a commit ecbaf9f

4 files changed

Lines changed: 12 additions & 13 deletions

File tree

SConstruct

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import os
22
import platform
33
import subprocess
44
import sysconfig
5-
import numpy as np
65
import catch2
76

87
arch = subprocess.check_output(["uname", "-m"], encoding='utf8').rstrip()
@@ -71,7 +70,6 @@ env = Environment(
7170
Export('env', 'arch', 'common')
7271

7372
envCython = env.Clone(LIBS=[])
74-
envCython["CPPPATH"] += [np.get_include()]
7573
envCython["CCFLAGS"] += ["-Wno-#warnings", "-Wno-cpp", "-Wno-shadow", "-Wno-deprecated-declarations"]
7674
envCython["CCFLAGS"].remove('-Werror')
7775
if arch == "Darwin":

msgq/visionipc/tests/test_visionipc.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import struct
12
import unittest
23
from typing import Optional
3-
import numpy as np
44
from msgq.visionipc import VisionIpcServer, VisionIpcClient, VisionStreamType
55

66

@@ -55,13 +55,17 @@ def test_send_single_buffer(self):
5555
assert self.server is not None
5656
assert self.client is not None
5757
assert self.client.buffer_len is not None
58-
buf = np.zeros(self.client.buffer_len, dtype=np.uint8)
59-
buf.view('<u8')[0] = 1234
58+
buf = bytearray(self.client.buffer_len)
59+
struct.pack_into("<Q", buf, 0, 1234)
6060
self.server.send(VisionStreamType.VISION_STREAM_ROAD, buf, frame_id=1337)
6161

6262
recv_buf = self.client.recv()
6363
assert recv_buf is not None
64-
assert recv_buf.data.view('<u8')[0] == 1234
64+
data = recv_buf.data
65+
assert isinstance(data, memoryview)
66+
assert struct.unpack_from("<Q", data, 0)[0] == 1234
67+
assert len(data) == self.client.buffer_len
68+
assert data[8:].nbytes == self.client.buffer_len - 8
6569
assert self.client.frame_id == 1337
6670
assert recv_buf.frame_id == 1337
6771

@@ -70,7 +74,7 @@ def test_no_conflate(self):
7074
assert self.server is not None
7175
assert self.client is not None
7276
assert self.client.buffer_len is not None
73-
buf = np.zeros(self.client.buffer_len, dtype=np.uint8)
77+
buf = bytearray(self.client.buffer_len)
7478
self.server.send(VisionStreamType.VISION_STREAM_ROAD, buf, frame_id=1)
7579
self.server.send(VisionStreamType.VISION_STREAM_ROAD, buf, frame_id=2)
7680

@@ -87,7 +91,7 @@ def test_conflate(self):
8791
assert self.server is not None
8892
assert self.client is not None
8993
assert self.client.buffer_len is not None
90-
buf = np.zeros(self.client.buffer_len, dtype=np.uint8)
94+
buf = bytearray(self.client.buffer_len)
9195
self.server.send(VisionStreamType.VISION_STREAM_ROAD, buf, frame_id=1)
9296
self.server.send(VisionStreamType.VISION_STREAM_ROAD, buf, frame_id=2)
9397

msgq/visionipc/visionipc_pyx.pyx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
# cython: c_string_encoding=ascii, language_level=3
33

44
import sys
5-
import numpy as np
6-
cimport numpy as cnp
7-
from cython.view cimport array
85
from libc.string cimport memcpy
96
from libc.stdint cimport uint32_t, uint64_t
107
from libcpp cimport bool
@@ -37,7 +34,8 @@ cdef class VisionBuf:
3734

3835
@property
3936
def data(self):
40-
return np.asarray(<cnp.uint8_t[:self.buf.len]> self.buf.addr)
37+
cdef unsigned char[:] data = <unsigned char[:self.buf.len]> self.buf.addr
38+
return memoryview(data)
4139

4240
@property
4341
def width(self):

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dependencies = [
1919
"ruff",
2020
"parameterized",
2121
"coverage",
22-
"numpy",
2322
"cppcheck",
2423
"cpplint",
2524
"codespell",

0 commit comments

Comments
 (0)