Project import generated by Copybara.
GitOrigin-RevId: 1e13be30e2c6838d4a2ff768a39c414bc80534bb
This commit is contained in:
committed by
Sebastian Schmidt
parent
63e679d99c
commit
4dc4b19ddb
+87
-1
@@ -12,10 +12,12 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
|
||||
load("@org_tensorflow//tensorflow:tensorflow.bzl", "pybind_extension")
|
||||
|
||||
licenses(["notice"]) # Apache 2.0
|
||||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
pybind_extension(
|
||||
name = "_framework_bindings",
|
||||
srcs = ["framework_bindings.cc"],
|
||||
@@ -31,8 +33,10 @@ pybind_extension(
|
||||
"-lopencv_imgcodecs",
|
||||
],
|
||||
}),
|
||||
module_name = "_framework_bindings",
|
||||
deps = [
|
||||
":builtin_calculators",
|
||||
":builtin_task_graphs",
|
||||
"//mediapipe/python/pybind:calculator_graph",
|
||||
"//mediapipe/python/pybind:image",
|
||||
"//mediapipe/python/pybind:image_frame",
|
||||
@@ -43,6 +47,7 @@ pybind_extension(
|
||||
"//mediapipe/python/pybind:resource_util",
|
||||
"//mediapipe/python/pybind:timestamp",
|
||||
"//mediapipe/python/pybind:validated_graph_config",
|
||||
"//mediapipe/tasks/python/core/pybind:task_runner",
|
||||
# Type registration.
|
||||
"//mediapipe/framework:basic_types_registration",
|
||||
"//mediapipe/framework/formats:classification_registration",
|
||||
@@ -76,3 +81,84 @@ cc_library(
|
||||
"//mediapipe/modules/selfie_segmentation:selfie_segmentation_cpu",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "builtin_task_graphs",
|
||||
deps = [
|
||||
"//mediapipe/tasks/cc/vision/object_detector:object_detector_graph",
|
||||
],
|
||||
)
|
||||
|
||||
py_library(
|
||||
name = "packet_creator",
|
||||
srcs = ["packet_creator.py"],
|
||||
srcs_version = "PY3",
|
||||
deps = [
|
||||
":_framework_bindings",
|
||||
],
|
||||
)
|
||||
|
||||
py_library(
|
||||
name = "packet_getter",
|
||||
srcs = ["packet_getter.py"],
|
||||
srcs_version = "PY3",
|
||||
deps = [
|
||||
":_framework_bindings",
|
||||
],
|
||||
)
|
||||
|
||||
py_test(
|
||||
name = "calculator_graph_test",
|
||||
srcs = ["calculator_graph_test.py"],
|
||||
python_version = "PY3",
|
||||
srcs_version = "PY3",
|
||||
deps = [
|
||||
":_framework_bindings",
|
||||
":packet_creator",
|
||||
":packet_getter",
|
||||
"//mediapipe/framework:calculator_py_pb2",
|
||||
],
|
||||
)
|
||||
|
||||
py_test(
|
||||
name = "image_test",
|
||||
srcs = ["image_test.py"],
|
||||
python_version = "PY3",
|
||||
srcs_version = "PY3",
|
||||
deps = [
|
||||
":_framework_bindings",
|
||||
],
|
||||
)
|
||||
|
||||
py_test(
|
||||
name = "image_frame_test",
|
||||
srcs = ["image_frame_test.py"],
|
||||
python_version = "PY3",
|
||||
srcs_version = "PY3",
|
||||
deps = [
|
||||
":_framework_bindings",
|
||||
],
|
||||
)
|
||||
|
||||
py_test(
|
||||
name = "packet_test",
|
||||
srcs = ["packet_test.py"],
|
||||
python_version = "PY3",
|
||||
srcs_version = "PY3",
|
||||
deps = [
|
||||
":_framework_bindings",
|
||||
":packet_creator",
|
||||
":packet_getter",
|
||||
"//mediapipe/framework/formats:detection_py_pb2",
|
||||
],
|
||||
)
|
||||
|
||||
py_test(
|
||||
name = "timestamp_test",
|
||||
srcs = ["timestamp_test.py"],
|
||||
python_version = "PY3",
|
||||
srcs_version = "PY3",
|
||||
deps = [
|
||||
":_framework_bindings",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -14,12 +14,17 @@
|
||||
|
||||
"""Tests for mediapipe.python._framework_bindings.calculator_graph."""
|
||||
|
||||
# Dependency imports
|
||||
|
||||
from absl.testing import absltest
|
||||
import mediapipe as mp
|
||||
|
||||
from google.protobuf import text_format
|
||||
from mediapipe.framework import calculator_pb2
|
||||
from mediapipe.python import packet_creator
|
||||
from mediapipe.python import packet_getter
|
||||
from mediapipe.python._framework_bindings import calculator_graph
|
||||
from mediapipe.python._framework_bindings import validated_graph_config
|
||||
|
||||
CalculatorGraph = calculator_graph.CalculatorGraph
|
||||
ValidatedGraphConfig = validated_graph_config.ValidatedGraphConfig
|
||||
|
||||
|
||||
class GraphTest(absltest.TestCase):
|
||||
@@ -28,7 +33,7 @@ class GraphTest(absltest.TestCase):
|
||||
with self.assertRaisesRegex(
|
||||
FileNotFoundError,
|
||||
'(No such file or directory|The path does not exist)'):
|
||||
mp.CalculatorGraph(binary_graph_path='/tmp/abc.binarypb')
|
||||
CalculatorGraph(binary_graph_path='/tmp/abc.binarypb')
|
||||
|
||||
def test_invalid_node_config(self):
|
||||
text_config = """
|
||||
@@ -45,7 +50,7 @@ class GraphTest(absltest.TestCase):
|
||||
ValueError,
|
||||
'Input and output streams to PassThroughCalculator must use matching tags and indexes.'
|
||||
):
|
||||
mp.CalculatorGraph(graph_config=config_proto)
|
||||
CalculatorGraph(graph_config=config_proto)
|
||||
|
||||
def test_invalid_calculator_type(self):
|
||||
text_config = """
|
||||
@@ -59,7 +64,7 @@ class GraphTest(absltest.TestCase):
|
||||
text_format.Parse(text_config, config_proto)
|
||||
with self.assertRaisesRegex(
|
||||
RuntimeError, 'Unable to find Calculator \"SomeUnknownCalculator\"'):
|
||||
mp.CalculatorGraph(graph_config=config_proto)
|
||||
CalculatorGraph(graph_config=config_proto)
|
||||
|
||||
def test_graph_initialized_with_proto_config(self):
|
||||
text_config = """
|
||||
@@ -74,11 +79,11 @@ class GraphTest(absltest.TestCase):
|
||||
"""
|
||||
config_proto = calculator_pb2.CalculatorGraphConfig()
|
||||
text_format.Parse(text_config, config_proto)
|
||||
graph = mp.CalculatorGraph(graph_config=config_proto)
|
||||
graph = CalculatorGraph(graph_config=config_proto)
|
||||
|
||||
hello_world_packet = mp.packet_creator.create_string('hello world')
|
||||
hello_world_packet = packet_creator.create_string('hello world')
|
||||
out = []
|
||||
graph = mp.CalculatorGraph(graph_config=config_proto)
|
||||
graph = CalculatorGraph(graph_config=config_proto)
|
||||
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
|
||||
graph.start_run()
|
||||
graph.add_packet_to_input_stream(
|
||||
@@ -86,15 +91,16 @@ class GraphTest(absltest.TestCase):
|
||||
graph.add_packet_to_input_stream(
|
||||
stream='in', packet=hello_world_packet.at(1))
|
||||
graph.close()
|
||||
self.assertEqual(graph.graph_input_stream_add_mode,
|
||||
mp.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
|
||||
self.assertEqual(
|
||||
graph.graph_input_stream_add_mode,
|
||||
calculator_graph.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
|
||||
self.assertEqual(graph.max_queue_size, 1)
|
||||
self.assertFalse(graph.has_error())
|
||||
self.assertLen(out, 2)
|
||||
self.assertEqual(out[0].timestamp, 0)
|
||||
self.assertEqual(out[1].timestamp, 1)
|
||||
self.assertEqual(mp.packet_getter.get_str(out[0]), 'hello world')
|
||||
self.assertEqual(mp.packet_getter.get_str(out[1]), 'hello world')
|
||||
self.assertEqual(packet_getter.get_str(out[0]), 'hello world')
|
||||
self.assertEqual(packet_getter.get_str(out[1]), 'hello world')
|
||||
|
||||
def test_graph_initialized_with_text_config(self):
|
||||
text_config = """
|
||||
@@ -108,9 +114,9 @@ class GraphTest(absltest.TestCase):
|
||||
}
|
||||
"""
|
||||
|
||||
hello_world_packet = mp.packet_creator.create_string('hello world')
|
||||
hello_world_packet = packet_creator.create_string('hello world')
|
||||
out = []
|
||||
graph = mp.CalculatorGraph(graph_config=text_config)
|
||||
graph = CalculatorGraph(graph_config=text_config)
|
||||
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
|
||||
graph.start_run()
|
||||
graph.add_packet_to_input_stream(
|
||||
@@ -118,15 +124,16 @@ class GraphTest(absltest.TestCase):
|
||||
graph.add_packet_to_input_stream(
|
||||
stream='in', packet=hello_world_packet, timestamp=1)
|
||||
graph.close()
|
||||
self.assertEqual(graph.graph_input_stream_add_mode,
|
||||
mp.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
|
||||
self.assertEqual(
|
||||
graph.graph_input_stream_add_mode,
|
||||
calculator_graph.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
|
||||
self.assertEqual(graph.max_queue_size, 1)
|
||||
self.assertFalse(graph.has_error())
|
||||
self.assertLen(out, 2)
|
||||
self.assertEqual(out[0].timestamp, 0)
|
||||
self.assertEqual(out[1].timestamp, 1)
|
||||
self.assertEqual(mp.packet_getter.get_str(out[0]), 'hello world')
|
||||
self.assertEqual(mp.packet_getter.get_str(out[1]), 'hello world')
|
||||
self.assertEqual(packet_getter.get_str(out[0]), 'hello world')
|
||||
self.assertEqual(packet_getter.get_str(out[1]), 'hello world')
|
||||
|
||||
def test_graph_validation_and_initialization(self):
|
||||
text_config = """
|
||||
@@ -140,14 +147,14 @@ class GraphTest(absltest.TestCase):
|
||||
}
|
||||
"""
|
||||
|
||||
hello_world_packet = mp.packet_creator.create_string('hello world')
|
||||
hello_world_packet = packet_creator.create_string('hello world')
|
||||
out = []
|
||||
validated_graph_config = mp.ValidatedGraphConfig()
|
||||
self.assertFalse(validated_graph_config.initialized())
|
||||
validated_graph_config.initialize(graph_config=text_config)
|
||||
self.assertTrue(validated_graph_config.initialized())
|
||||
validated_graph = ValidatedGraphConfig()
|
||||
self.assertFalse(validated_graph.initialized())
|
||||
validated_graph.initialize(graph_config=text_config)
|
||||
self.assertTrue(validated_graph.initialized())
|
||||
|
||||
graph = mp.CalculatorGraph(validated_graph_config=validated_graph_config)
|
||||
graph = CalculatorGraph(validated_graph_config=validated_graph)
|
||||
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
|
||||
graph.start_run()
|
||||
graph.add_packet_to_input_stream(
|
||||
@@ -155,15 +162,16 @@ class GraphTest(absltest.TestCase):
|
||||
graph.add_packet_to_input_stream(
|
||||
stream='in', packet=hello_world_packet, timestamp=1)
|
||||
graph.close()
|
||||
self.assertEqual(graph.graph_input_stream_add_mode,
|
||||
mp.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
|
||||
self.assertEqual(
|
||||
graph.graph_input_stream_add_mode,
|
||||
calculator_graph.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
|
||||
self.assertEqual(graph.max_queue_size, 1)
|
||||
self.assertFalse(graph.has_error())
|
||||
self.assertLen(out, 2)
|
||||
self.assertEqual(out[0].timestamp, 0)
|
||||
self.assertEqual(out[1].timestamp, 1)
|
||||
self.assertEqual(mp.packet_getter.get_str(out[0]), 'hello world')
|
||||
self.assertEqual(mp.packet_getter.get_str(out[1]), 'hello world')
|
||||
self.assertEqual(packet_getter.get_str(out[0]), 'hello world')
|
||||
self.assertEqual(packet_getter.get_str(out[1]), 'hello world')
|
||||
|
||||
def test_insert_packets_with_same_timestamp(self):
|
||||
text_config = """
|
||||
@@ -179,9 +187,9 @@ class GraphTest(absltest.TestCase):
|
||||
config_proto = calculator_pb2.CalculatorGraphConfig()
|
||||
text_format.Parse(text_config, config_proto)
|
||||
|
||||
hello_world_packet = mp.packet_creator.create_string('hello world')
|
||||
hello_world_packet = packet_creator.create_string('hello world')
|
||||
out = []
|
||||
graph = mp.CalculatorGraph(graph_config=config_proto)
|
||||
graph = CalculatorGraph(graph_config=config_proto)
|
||||
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
|
||||
graph.start_run()
|
||||
graph.add_packet_to_input_stream(
|
||||
@@ -203,13 +211,13 @@ class GraphTest(absltest.TestCase):
|
||||
"""
|
||||
config_proto = calculator_pb2.CalculatorGraphConfig()
|
||||
text_format.Parse(text_config, config_proto)
|
||||
graph = mp.CalculatorGraph(graph_config=config_proto)
|
||||
graph = CalculatorGraph(graph_config=config_proto)
|
||||
graph.start_run(
|
||||
input_side_packets={'string': mp.packet_creator.create_string('42')})
|
||||
input_side_packets={'string': packet_creator.create_string('42')})
|
||||
graph.wait_until_done()
|
||||
self.assertFalse(graph.has_error())
|
||||
self.assertEqual(
|
||||
mp.packet_getter.get_uint(graph.get_output_side_packet('number')), 42)
|
||||
packet_getter.get_uint(graph.get_output_side_packet('number')), 42)
|
||||
|
||||
def test_sequence_input(self):
|
||||
text_config = """
|
||||
@@ -222,9 +230,9 @@ class GraphTest(absltest.TestCase):
|
||||
output_stream: 'out'
|
||||
}
|
||||
"""
|
||||
hello_world_packet = mp.packet_creator.create_string('hello world')
|
||||
hello_world_packet = packet_creator.create_string('hello world')
|
||||
out = []
|
||||
graph = mp.CalculatorGraph(graph_config=text_config)
|
||||
graph = CalculatorGraph(graph_config=text_config)
|
||||
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
|
||||
graph.start_run()
|
||||
|
||||
@@ -236,7 +244,7 @@ class GraphTest(absltest.TestCase):
|
||||
self.assertLen(out, sequence_size)
|
||||
for i in range(sequence_size):
|
||||
self.assertEqual(out[i].timestamp, i)
|
||||
self.assertEqual(mp.packet_getter.get_str(out[i]), 'hello world')
|
||||
self.assertEqual(packet_getter.get_str(out[i]), 'hello world')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "mediapipe/python/pybind/resource_util.h"
|
||||
#include "mediapipe/python/pybind/timestamp.h"
|
||||
#include "mediapipe/python/pybind/validated_graph_config.h"
|
||||
#include "mediapipe/tasks/python/core/pybind/task_runner.h"
|
||||
|
||||
namespace mediapipe {
|
||||
namespace python {
|
||||
@@ -37,6 +38,10 @@ PYBIND11_MODULE(_framework_bindings, m) {
|
||||
PacketGetterSubmodule(&m);
|
||||
CalculatorGraphSubmodule(&m);
|
||||
ValidatedGraphConfigSubmodule(&m);
|
||||
// As all MediaPipe calculators and Python bindings need to go into a single
|
||||
// .so file, having MediaPipe Tasks' task runner module in _framework_bindings
|
||||
// as well.
|
||||
tasks::python::TaskRunnerSubmodule(&m);
|
||||
}
|
||||
|
||||
} // namespace python
|
||||
|
||||
@@ -17,12 +17,17 @@
|
||||
import gc
|
||||
import random
|
||||
import sys
|
||||
|
||||
from absl.testing import absltest
|
||||
import cv2
|
||||
import mediapipe as mp
|
||||
import numpy as np
|
||||
import PIL.Image
|
||||
|
||||
from mediapipe.python._framework_bindings import image_frame
|
||||
|
||||
ImageFormat = image_frame.ImageFormat
|
||||
ImageFrame = image_frame.ImageFrame
|
||||
|
||||
|
||||
# TODO: Add unit tests specifically for memory management.
|
||||
class ImageFrameTest(absltest.TestCase):
|
||||
@@ -33,13 +38,13 @@ class ImageFrameTest(absltest.TestCase):
|
||||
np.random.randint(2**8 - 1, size=(h, w, 3), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2GRAY)
|
||||
mat[2, 2] = 42
|
||||
image_frame = mp.ImageFrame(image_format=mp.ImageFormat.GRAY8, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, image_frame.numpy_view()))
|
||||
gray8_image_frame = ImageFrame(image_format=ImageFormat.GRAY8, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, gray8_image_frame.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'index dimension mismatch'):
|
||||
print(image_frame[w, h, 1])
|
||||
print(gray8_image_frame[w, h, 1])
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image_frame[w, h])
|
||||
self.assertEqual(42, image_frame[2, 2])
|
||||
print(gray8_image_frame[w, h])
|
||||
self.assertEqual(42, gray8_image_frame[2, 2])
|
||||
|
||||
def test_create_image_frame_from_rgb_cv_mat(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3
|
||||
@@ -47,11 +52,11 @@ class ImageFrameTest(absltest.TestCase):
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2BGR)
|
||||
mat[2, 2, 1] = 42
|
||||
image_frame = mp.ImageFrame(image_format=mp.ImageFormat.SRGB, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, image_frame.numpy_view()))
|
||||
rgb_image_frame = ImageFrame(image_format=ImageFormat.SRGB, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, rgb_image_frame.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image_frame[w, h, channels])
|
||||
self.assertEqual(42, image_frame[2, 2, 1])
|
||||
print(rgb_image_frame[w, h, channels])
|
||||
self.assertEqual(42, rgb_image_frame[2, 2, 1])
|
||||
|
||||
def test_create_image_frame_from_rgb48_cv_mat(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3
|
||||
@@ -59,55 +64,58 @@ class ImageFrameTest(absltest.TestCase):
|
||||
np.random.randint(2**16 - 1, size=(h, w, channels), dtype=np.uint16),
|
||||
cv2.COLOR_RGB2BGR)
|
||||
mat[2, 2, 1] = 42
|
||||
image_frame = mp.ImageFrame(image_format=mp.ImageFormat.SRGB48, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, image_frame.numpy_view()))
|
||||
rgb48_image_frame = ImageFrame(image_format=ImageFormat.SRGB48, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, rgb48_image_frame.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image_frame[w, h, channels])
|
||||
self.assertEqual(42, image_frame[2, 2, 1])
|
||||
print(rgb48_image_frame[w, h, channels])
|
||||
self.assertEqual(42, rgb48_image_frame[2, 2, 1])
|
||||
|
||||
def test_create_image_frame_from_gray_pil_image(self):
|
||||
w, h = random.randrange(3, 100), random.randrange(3, 100)
|
||||
img = PIL.Image.fromarray(
|
||||
np.random.randint(2**8 - 1, size=(h, w), dtype=np.uint8), 'L')
|
||||
image_frame = mp.ImageFrame(
|
||||
image_format=mp.ImageFormat.GRAY8, data=np.asarray(img))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), image_frame.numpy_view()))
|
||||
gray8_image_frame = ImageFrame(
|
||||
image_format=ImageFormat.GRAY8, data=np.asarray(img))
|
||||
self.assertTrue(
|
||||
np.array_equal(np.asarray(img), gray8_image_frame.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'index dimension mismatch'):
|
||||
print(image_frame[w, h, 1])
|
||||
print(gray8_image_frame[w, h, 1])
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image_frame[w, h])
|
||||
print(gray8_image_frame[w, h])
|
||||
|
||||
def test_create_image_frame_from_rgb_pil_image(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3
|
||||
img = PIL.Image.fromarray(
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
'RGB')
|
||||
image_frame = mp.ImageFrame(
|
||||
image_format=mp.ImageFormat.SRGB, data=np.asarray(img))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), image_frame.numpy_view()))
|
||||
rgb_image_frame = ImageFrame(
|
||||
image_format=ImageFormat.SRGB, data=np.asarray(img))
|
||||
self.assertTrue(
|
||||
np.array_equal(np.asarray(img), rgb_image_frame.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image_frame[w, h, channels])
|
||||
print(rgb_image_frame[w, h, channels])
|
||||
|
||||
def test_create_image_frame_from_rgba64_pil_image(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 4
|
||||
img = PIL.Image.fromarray(
|
||||
np.random.randint(2**16 - 1, size=(h, w, channels), dtype=np.uint16),
|
||||
'RGBA')
|
||||
image_frame = mp.ImageFrame(
|
||||
image_format=mp.ImageFormat.SRGBA64,
|
||||
rgba_image_frame = ImageFrame(
|
||||
image_format=ImageFormat.SRGBA64,
|
||||
data=np.asarray(img).astype(np.uint16))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), image_frame.numpy_view()))
|
||||
self.assertTrue(
|
||||
np.array_equal(np.asarray(img), rgba_image_frame.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image_frame[1000, 1000, 1000])
|
||||
print(rgba_image_frame[1000, 1000, 1000])
|
||||
|
||||
def test_image_frame_numby_view(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3
|
||||
mat = cv2.cvtColor(
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2BGR)
|
||||
image_frame = mp.ImageFrame(image_format=mp.ImageFormat.SRGB, data=mat)
|
||||
output_ndarray = image_frame.numpy_view()
|
||||
self.assertTrue(np.array_equal(mat, image_frame.numpy_view()))
|
||||
rgb_image_frame = ImageFrame(image_format=ImageFormat.SRGB, data=mat)
|
||||
output_ndarray = rgb_image_frame.numpy_view()
|
||||
self.assertTrue(np.array_equal(mat, rgb_image_frame.numpy_view()))
|
||||
# The output of numpy_view() is a reference to the internal data and it's
|
||||
# unwritable after creation.
|
||||
with self.assertRaisesRegex(ValueError,
|
||||
@@ -122,12 +130,12 @@ class ImageFrameTest(absltest.TestCase):
|
||||
mat = cv2.cvtColor(
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2GRAY)
|
||||
image_frame = mp.ImageFrame(
|
||||
image_format=mp.ImageFormat.GRAY8,
|
||||
gray8_image_frame = ImageFrame(
|
||||
image_format=ImageFormat.GRAY8,
|
||||
data=np.ascontiguousarray(mat[offset:-offset, offset:-offset]))
|
||||
self.assertTrue(
|
||||
np.array_equal(mat[offset:-offset, offset:-offset],
|
||||
image_frame.numpy_view()))
|
||||
gray8_image_frame.numpy_view()))
|
||||
|
||||
def test_cropped_rgb_image(self):
|
||||
w, h = random.randrange(20, 100), random.randrange(20, 100)
|
||||
@@ -135,12 +143,12 @@ class ImageFrameTest(absltest.TestCase):
|
||||
mat = cv2.cvtColor(
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2BGR)
|
||||
image_frame = mp.ImageFrame(
|
||||
image_format=mp.ImageFormat.SRGB,
|
||||
rgb_image_frame = ImageFrame(
|
||||
image_format=ImageFormat.SRGB,
|
||||
data=np.ascontiguousarray(mat[offset:-offset, offset:-offset, :]))
|
||||
self.assertTrue(
|
||||
np.array_equal(mat[offset:-offset, offset:-offset, :],
|
||||
image_frame.numpy_view()))
|
||||
rgb_image_frame.numpy_view()))
|
||||
|
||||
# For image frames that store contiguous data, the output of numpy_view()
|
||||
# points to the pixel data of the original image frame object. The life cycle
|
||||
@@ -148,22 +156,22 @@ class ImageFrameTest(absltest.TestCase):
|
||||
def test_image_frame_numpy_view_with_contiguous_data(self):
|
||||
w, h = 640, 480
|
||||
mat = np.random.randint(2**8 - 1, size=(h, w, 3), dtype=np.uint8)
|
||||
image_frame = mp.ImageFrame(image_format=mp.ImageFormat.SRGB, data=mat)
|
||||
self.assertTrue(image_frame.is_contiguous())
|
||||
initial_ref_count = sys.getrefcount(image_frame)
|
||||
self.assertTrue(np.array_equal(mat, image_frame.numpy_view()))
|
||||
rgb_image_frame = ImageFrame(image_format=ImageFormat.SRGB, data=mat)
|
||||
self.assertTrue(rgb_image_frame.is_contiguous())
|
||||
initial_ref_count = sys.getrefcount(rgb_image_frame)
|
||||
self.assertTrue(np.array_equal(mat, rgb_image_frame.numpy_view()))
|
||||
# Get 2 data array objects and verify that the image frame's ref count is
|
||||
# increased by 2.
|
||||
np_view = image_frame.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(image_frame), initial_ref_count + 1)
|
||||
np_view2 = image_frame.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(image_frame), initial_ref_count + 2)
|
||||
np_view = rgb_image_frame.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(rgb_image_frame), initial_ref_count + 1)
|
||||
np_view2 = rgb_image_frame.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(rgb_image_frame), initial_ref_count + 2)
|
||||
del np_view
|
||||
del np_view2
|
||||
gc.collect()
|
||||
# After the two data array objects getting destroyed, the current ref count
|
||||
# should euqal to the initial ref count.
|
||||
self.assertEqual(sys.getrefcount(image_frame), initial_ref_count)
|
||||
self.assertEqual(sys.getrefcount(rgb_image_frame), initial_ref_count)
|
||||
|
||||
# For image frames that store non contiguous data, the output of numpy_view()
|
||||
# stores a copy of the pixel data of the image frame object. The life cycle of
|
||||
@@ -171,15 +179,15 @@ class ImageFrameTest(absltest.TestCase):
|
||||
def test_image_frame_numpy_view_with_non_contiguous_data(self):
|
||||
w, h = 641, 481
|
||||
mat = np.random.randint(2**8 - 1, size=(h, w, 3), dtype=np.uint8)
|
||||
image_frame = mp.ImageFrame(image_format=mp.ImageFormat.SRGB, data=mat)
|
||||
self.assertFalse(image_frame.is_contiguous())
|
||||
initial_ref_count = sys.getrefcount(image_frame)
|
||||
self.assertTrue(np.array_equal(mat, image_frame.numpy_view()))
|
||||
np_view = image_frame.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(image_frame), initial_ref_count)
|
||||
rgb_image_frame = ImageFrame(image_format=ImageFormat.SRGB, data=mat)
|
||||
self.assertFalse(rgb_image_frame.is_contiguous())
|
||||
initial_ref_count = sys.getrefcount(rgb_image_frame)
|
||||
self.assertTrue(np.array_equal(mat, rgb_image_frame.numpy_view()))
|
||||
np_view = rgb_image_frame.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(rgb_image_frame), initial_ref_count)
|
||||
del np_view
|
||||
gc.collect()
|
||||
self.assertEqual(sys.getrefcount(image_frame), initial_ref_count)
|
||||
self.assertEqual(sys.getrefcount(rgb_image_frame), initial_ref_count)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -17,12 +17,18 @@
|
||||
import gc
|
||||
import random
|
||||
import sys
|
||||
|
||||
from absl.testing import absltest
|
||||
import cv2
|
||||
import mediapipe as mp
|
||||
import numpy as np
|
||||
import PIL.Image
|
||||
|
||||
from mediapipe.python._framework_bindings import image
|
||||
from mediapipe.python._framework_bindings import image_frame
|
||||
|
||||
Image = image.Image
|
||||
ImageFormat = image_frame.ImageFormat
|
||||
|
||||
|
||||
# TODO: Add unit tests specifically for memory management.
|
||||
class ImageTest(absltest.TestCase):
|
||||
@@ -33,13 +39,13 @@ class ImageTest(absltest.TestCase):
|
||||
np.random.randint(2**8 - 1, size=(h, w, 3), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2GRAY)
|
||||
mat[2, 2] = 42
|
||||
image = mp.Image(image_format=mp.ImageFormat.GRAY8, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, image.numpy_view()))
|
||||
gray8_image = Image(image_format=ImageFormat.GRAY8, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, gray8_image.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'index dimension mismatch'):
|
||||
print(image[w, h, 1])
|
||||
print(gray8_image[w, h, 1])
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image[w, h])
|
||||
self.assertEqual(42, image[2, 2])
|
||||
print(gray8_image[w, h])
|
||||
self.assertEqual(42, gray8_image[2, 2])
|
||||
|
||||
def test_create_image_from_rgb_cv_mat(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3
|
||||
@@ -47,11 +53,11 @@ class ImageTest(absltest.TestCase):
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2BGR)
|
||||
mat[2, 2, 1] = 42
|
||||
image = mp.Image(image_format=mp.ImageFormat.SRGB, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, image.numpy_view()))
|
||||
rgb_image = Image(image_format=ImageFormat.SRGB, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, rgb_image.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image[w, h, channels])
|
||||
self.assertEqual(42, image[2, 2, 1])
|
||||
print(rgb_image[w, h, channels])
|
||||
self.assertEqual(42, rgb_image[2, 2, 1])
|
||||
|
||||
def test_create_image_from_rgb48_cv_mat(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3
|
||||
@@ -59,53 +65,53 @@ class ImageTest(absltest.TestCase):
|
||||
np.random.randint(2**16 - 1, size=(h, w, channels), dtype=np.uint16),
|
||||
cv2.COLOR_RGB2BGR)
|
||||
mat[2, 2, 1] = 42
|
||||
image = mp.Image(image_format=mp.ImageFormat.SRGB48, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, image.numpy_view()))
|
||||
rgb48_image = Image(image_format=ImageFormat.SRGB48, data=mat)
|
||||
self.assertTrue(np.array_equal(mat, rgb48_image.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image[w, h, channels])
|
||||
self.assertEqual(42, image[2, 2, 1])
|
||||
print(rgb48_image[w, h, channels])
|
||||
self.assertEqual(42, rgb48_image[2, 2, 1])
|
||||
|
||||
def test_create_image_from_gray_pil_image(self):
|
||||
w, h = random.randrange(3, 100), random.randrange(3, 100)
|
||||
img = PIL.Image.fromarray(
|
||||
np.random.randint(2**8 - 1, size=(h, w), dtype=np.uint8), 'L')
|
||||
image = mp.Image(image_format=mp.ImageFormat.GRAY8, data=np.asarray(img))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), image.numpy_view()))
|
||||
gray8_image = Image(image_format=ImageFormat.GRAY8, data=np.asarray(img))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), gray8_image.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'index dimension mismatch'):
|
||||
print(image[w, h, 1])
|
||||
print(gray8_image[w, h, 1])
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image[w, h])
|
||||
print(gray8_image[w, h])
|
||||
|
||||
def test_create_image_from_rgb_pil_image(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3
|
||||
img = PIL.Image.fromarray(
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
'RGB')
|
||||
image = mp.Image(image_format=mp.ImageFormat.SRGB, data=np.asarray(img))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), image.numpy_view()))
|
||||
rgb_image = Image(image_format=ImageFormat.SRGB, data=np.asarray(img))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), rgb_image.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image[w, h, channels])
|
||||
print(rgb_image[w, h, channels])
|
||||
|
||||
def test_create_image_from_rgba64_pil_image(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 4
|
||||
img = PIL.Image.fromarray(
|
||||
np.random.randint(2**16 - 1, size=(h, w, channels), dtype=np.uint16),
|
||||
'RGBA')
|
||||
image = mp.Image(
|
||||
image_format=mp.ImageFormat.SRGBA64,
|
||||
rgba_image = Image(
|
||||
image_format=ImageFormat.SRGBA64,
|
||||
data=np.asarray(img).astype(np.uint16))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), image.numpy_view()))
|
||||
self.assertTrue(np.array_equal(np.asarray(img), rgba_image.numpy_view()))
|
||||
with self.assertRaisesRegex(IndexError, 'out of bounds'):
|
||||
print(image[1000, 1000, 1000])
|
||||
print(rgba_image[1000, 1000, 1000])
|
||||
|
||||
def test_image_numby_view(self):
|
||||
w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3
|
||||
mat = cv2.cvtColor(
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2BGR)
|
||||
image = mp.Image(image_format=mp.ImageFormat.SRGB, data=mat)
|
||||
output_ndarray = image.numpy_view()
|
||||
self.assertTrue(np.array_equal(mat, image.numpy_view()))
|
||||
rgb_image = Image(image_format=ImageFormat.SRGB, data=mat)
|
||||
output_ndarray = rgb_image.numpy_view()
|
||||
self.assertTrue(np.array_equal(mat, rgb_image.numpy_view()))
|
||||
# The output of numpy_view() is a reference to the internal data and it's
|
||||
# unwritable after creation.
|
||||
with self.assertRaisesRegex(ValueError,
|
||||
@@ -120,11 +126,12 @@ class ImageTest(absltest.TestCase):
|
||||
mat = cv2.cvtColor(
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2GRAY)
|
||||
image = mp.Image(
|
||||
image_format=mp.ImageFormat.GRAY8,
|
||||
gray8_image = Image(
|
||||
image_format=ImageFormat.GRAY8,
|
||||
data=np.ascontiguousarray(mat[offset:-offset, offset:-offset]))
|
||||
self.assertTrue(
|
||||
np.array_equal(mat[offset:-offset, offset:-offset], image.numpy_view()))
|
||||
np.array_equal(mat[offset:-offset, offset:-offset],
|
||||
gray8_image.numpy_view()))
|
||||
|
||||
def test_cropped_rgb_image(self):
|
||||
w, h = random.randrange(20, 100), random.randrange(20, 100)
|
||||
@@ -132,12 +139,12 @@ class ImageTest(absltest.TestCase):
|
||||
mat = cv2.cvtColor(
|
||||
np.random.randint(2**8 - 1, size=(h, w, channels), dtype=np.uint8),
|
||||
cv2.COLOR_RGB2BGR)
|
||||
image = mp.Image(
|
||||
image_format=mp.ImageFormat.SRGB,
|
||||
rgb_image = Image(
|
||||
image_format=ImageFormat.SRGB,
|
||||
data=np.ascontiguousarray(mat[offset:-offset, offset:-offset, :]))
|
||||
self.assertTrue(
|
||||
np.array_equal(mat[offset:-offset, offset:-offset, :],
|
||||
image.numpy_view()))
|
||||
rgb_image.numpy_view()))
|
||||
|
||||
# For image frames that store contiguous data, the output of numpy_view()
|
||||
# points to the pixel data of the original image frame object. The life cycle
|
||||
@@ -145,22 +152,22 @@ class ImageTest(absltest.TestCase):
|
||||
def test_image_numpy_view_with_contiguous_data(self):
|
||||
w, h = 640, 480
|
||||
mat = np.random.randint(2**8 - 1, size=(h, w, 3), dtype=np.uint8)
|
||||
image = mp.Image(image_format=mp.ImageFormat.SRGB, data=mat)
|
||||
self.assertTrue(image.is_contiguous())
|
||||
initial_ref_count = sys.getrefcount(image)
|
||||
self.assertTrue(np.array_equal(mat, image.numpy_view()))
|
||||
rgb_image = Image(image_format=ImageFormat.SRGB, data=mat)
|
||||
self.assertTrue(rgb_image.is_contiguous())
|
||||
initial_ref_count = sys.getrefcount(rgb_image)
|
||||
self.assertTrue(np.array_equal(mat, rgb_image.numpy_view()))
|
||||
# Get 2 data array objects and verify that the image frame's ref count is
|
||||
# increased by 2.
|
||||
np_view = image.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(image), initial_ref_count + 1)
|
||||
np_view2 = image.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(image), initial_ref_count + 2)
|
||||
np_view = rgb_image.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(rgb_image), initial_ref_count + 1)
|
||||
np_view2 = rgb_image.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(rgb_image), initial_ref_count + 2)
|
||||
del np_view
|
||||
del np_view2
|
||||
gc.collect()
|
||||
# After the two data array objects getting destroyed, the current ref count
|
||||
# should euqal to the initial ref count.
|
||||
self.assertEqual(sys.getrefcount(image), initial_ref_count)
|
||||
self.assertEqual(sys.getrefcount(rgb_image), initial_ref_count)
|
||||
|
||||
# For image frames that store non contiguous data, the output of numpy_view()
|
||||
# stores a copy of the pixel data of the image frame object. The life cycle of
|
||||
@@ -168,15 +175,15 @@ class ImageTest(absltest.TestCase):
|
||||
def test_image_numpy_view_with_non_contiguous_data(self):
|
||||
w, h = 641, 481
|
||||
mat = np.random.randint(2**8 - 1, size=(h, w, 3), dtype=np.uint8)
|
||||
image = mp.Image(image_format=mp.ImageFormat.SRGB, data=mat)
|
||||
self.assertFalse(image.is_contiguous())
|
||||
initial_ref_count = sys.getrefcount(image)
|
||||
self.assertTrue(np.array_equal(mat, image.numpy_view()))
|
||||
np_view = image.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(image), initial_ref_count)
|
||||
rgb_image = Image(image_format=ImageFormat.SRGB, data=mat)
|
||||
self.assertFalse(rgb_image.is_contiguous())
|
||||
initial_ref_count = sys.getrefcount(rgb_image)
|
||||
self.assertTrue(np.array_equal(mat, rgb_image.numpy_view()))
|
||||
np_view = rgb_image.numpy_view()
|
||||
self.assertEqual(sys.getrefcount(rgb_image), initial_ref_count)
|
||||
del np_view
|
||||
gc.collect()
|
||||
self.assertEqual(sys.getrefcount(image), initial_ref_count)
|
||||
self.assertEqual(sys.getrefcount(rgb_image), initial_ref_count)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -45,6 +45,7 @@ create_int_vector = _packet_creator.create_int_vector
|
||||
create_bool_vector = _packet_creator.create_bool_vector
|
||||
create_float_vector = _packet_creator.create_float_vector
|
||||
create_string_vector = _packet_creator.create_string_vector
|
||||
create_image_vector = _packet_creator.create_image_vector
|
||||
create_packet_vector = _packet_creator.create_packet_vector
|
||||
create_string_to_packet_map = _packet_creator.create_string_to_packet_map
|
||||
create_matrix = _packet_creator.create_matrix
|
||||
|
||||
@@ -31,6 +31,7 @@ get_int_list = _packet_getter.get_int_list
|
||||
get_bool_list = _packet_getter.get_bool_list
|
||||
get_float_list = _packet_getter.get_float_list
|
||||
get_str_list = _packet_getter.get_str_list
|
||||
get_image_list = _packet_getter.get_image_list
|
||||
get_packet_list = _packet_getter.get_packet_list
|
||||
get_str_to_packet_dict = _packet_getter.get_str_to_packet_dict
|
||||
get_image = _packet_getter.get_image
|
||||
|
||||
+163
-136
@@ -18,218 +18,245 @@ import gc
|
||||
import random
|
||||
import sys
|
||||
from absl.testing import absltest
|
||||
import mediapipe as mp
|
||||
import numpy as np
|
||||
|
||||
from google.protobuf import text_format
|
||||
from mediapipe.framework.formats import detection_pb2
|
||||
from mediapipe.python import packet_creator
|
||||
from mediapipe.python import packet_getter
|
||||
from mediapipe.python._framework_bindings import calculator_graph
|
||||
from mediapipe.python._framework_bindings import image
|
||||
from mediapipe.python._framework_bindings import image_frame
|
||||
from mediapipe.python._framework_bindings import packet
|
||||
|
||||
CalculatorGraph = calculator_graph.CalculatorGraph
|
||||
Image = image.Image
|
||||
ImageFormat = image_frame.ImageFormat
|
||||
ImageFrame = image_frame.ImageFrame
|
||||
|
||||
|
||||
class PacketTest(absltest.TestCase):
|
||||
|
||||
def test_empty_packet(self):
|
||||
p = mp.Packet()
|
||||
p = packet.Packet()
|
||||
self.assertTrue(p.is_empty())
|
||||
|
||||
def test_boolean_packet(self):
|
||||
p = mp.packet_creator.create_bool(True)
|
||||
p = packet_creator.create_bool(True)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_bool(p), True)
|
||||
self.assertEqual(packet_getter.get_bool(p), True)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
|
||||
def test_int_packet(self):
|
||||
with self.assertRaisesRegex(OverflowError, 'execeeds the maximum value'):
|
||||
p = mp.packet_creator.create_int(2**32)
|
||||
p = mp.packet_creator.create_int(42)
|
||||
p = packet_creator.create_int(2**32)
|
||||
p = packet_creator.create_int(42)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p), 42)
|
||||
self.assertEqual(packet_getter.get_int(p), 42)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_int(np.intc(1))
|
||||
p2 = packet_creator.create_int(np.intc(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_int8_packet(self):
|
||||
with self.assertRaisesRegex(OverflowError, 'execeeds the maximum value'):
|
||||
p = mp.packet_creator.create_int8(2**7)
|
||||
p = mp.packet_creator.create_int8(2**7 - 1)
|
||||
p = packet_creator.create_int8(2**7)
|
||||
p = packet_creator.create_int8(2**7 - 1)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p), 2**7 - 1)
|
||||
self.assertEqual(packet_getter.get_int(p), 2**7 - 1)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_int8(np.int8(1))
|
||||
p2 = packet_creator.create_int8(np.int8(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_int16_packet(self):
|
||||
with self.assertRaisesRegex(OverflowError, 'execeeds the maximum value'):
|
||||
p = mp.packet_creator.create_int16(2**15)
|
||||
p = mp.packet_creator.create_int16(2**15 - 1)
|
||||
p = packet_creator.create_int16(2**15)
|
||||
p = packet_creator.create_int16(2**15 - 1)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p), 2**15 - 1)
|
||||
self.assertEqual(packet_getter.get_int(p), 2**15 - 1)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_int16(np.int16(1))
|
||||
p2 = packet_creator.create_int16(np.int16(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_int32_packet(self):
|
||||
with self.assertRaisesRegex(OverflowError, 'execeeds the maximum value'):
|
||||
p = mp.packet_creator.create_int32(2**31)
|
||||
p = packet_creator.create_int32(2**31)
|
||||
|
||||
p = mp.packet_creator.create_int32(2**31 - 1)
|
||||
p = packet_creator.create_int32(2**31 - 1)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p), 2**31 - 1)
|
||||
self.assertEqual(packet_getter.get_int(p), 2**31 - 1)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_int32(np.int32(1))
|
||||
p2 = packet_creator.create_int32(np.int32(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_int64_packet(self):
|
||||
p = mp.packet_creator.create_int64(2**63 - 1)
|
||||
p = packet_creator.create_int64(2**63 - 1)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p), 2**63 - 1)
|
||||
self.assertEqual(packet_getter.get_int(p), 2**63 - 1)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_int64(np.int64(1))
|
||||
p2 = packet_creator.create_int64(np.int64(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(packet_getter.get_int(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_uint8_packet(self):
|
||||
with self.assertRaisesRegex(OverflowError, 'execeeds the maximum value'):
|
||||
p = mp.packet_creator.create_uint8(2**8)
|
||||
p = mp.packet_creator.create_uint8(2**8 - 1)
|
||||
p = packet_creator.create_uint8(2**8)
|
||||
p = packet_creator.create_uint8(2**8 - 1)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_uint(p), 2**8 - 1)
|
||||
self.assertEqual(packet_getter.get_uint(p), 2**8 - 1)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_uint8(np.uint8(1))
|
||||
p2 = packet_creator.create_uint8(np.uint8(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_uint(p2), 1)
|
||||
self.assertEqual(packet_getter.get_uint(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_uint16_packet(self):
|
||||
with self.assertRaisesRegex(OverflowError, 'execeeds the maximum value'):
|
||||
p = mp.packet_creator.create_uint16(2**16)
|
||||
p = mp.packet_creator.create_uint16(2**16 - 1)
|
||||
p = packet_creator.create_uint16(2**16)
|
||||
p = packet_creator.create_uint16(2**16 - 1)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_uint(p), 2**16 - 1)
|
||||
self.assertEqual(packet_getter.get_uint(p), 2**16 - 1)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_uint16(np.uint16(1))
|
||||
p2 = packet_creator.create_uint16(np.uint16(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_uint(p2), 1)
|
||||
self.assertEqual(packet_getter.get_uint(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_uint32_packet(self):
|
||||
with self.assertRaisesRegex(OverflowError, 'execeeds the maximum value'):
|
||||
p = mp.packet_creator.create_uint32(2**32)
|
||||
p = mp.packet_creator.create_uint32(2**32 - 1)
|
||||
p = packet_creator.create_uint32(2**32)
|
||||
p = packet_creator.create_uint32(2**32 - 1)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_uint(p), 2**32 - 1)
|
||||
self.assertEqual(packet_getter.get_uint(p), 2**32 - 1)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_uint32(np.uint32(1))
|
||||
p2 = packet_creator.create_uint32(np.uint32(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_uint(p2), 1)
|
||||
self.assertEqual(packet_getter.get_uint(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_uint64_packet(self):
|
||||
p = mp.packet_creator.create_uint64(2**64 - 1)
|
||||
p = packet_creator.create_uint64(2**64 - 1)
|
||||
p.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_uint(p), 2**64 - 1)
|
||||
self.assertEqual(packet_getter.get_uint(p), 2**64 - 1)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_uint64(np.uint64(1))
|
||||
p2 = packet_creator.create_uint64(np.uint64(1))
|
||||
p2.timestamp = 0
|
||||
self.assertEqual(mp.packet_getter.get_uint(p2), 1)
|
||||
self.assertEqual(packet_getter.get_uint(p2), 1)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_float_packet(self):
|
||||
p = mp.packet_creator.create_float(0.42)
|
||||
p = packet_creator.create_float(0.42)
|
||||
p.timestamp = 0
|
||||
self.assertAlmostEqual(mp.packet_getter.get_float(p), 0.42)
|
||||
self.assertAlmostEqual(packet_getter.get_float(p), 0.42)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_float(np.float(0.42))
|
||||
p2 = packet_creator.create_float(np.float(0.42))
|
||||
p2.timestamp = 0
|
||||
self.assertAlmostEqual(mp.packet_getter.get_float(p2), 0.42)
|
||||
self.assertAlmostEqual(packet_getter.get_float(p2), 0.42)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_double_packet(self):
|
||||
p = mp.packet_creator.create_double(0.42)
|
||||
p = packet_creator.create_double(0.42)
|
||||
p.timestamp = 0
|
||||
self.assertAlmostEqual(mp.packet_getter.get_float(p), 0.42)
|
||||
self.assertAlmostEqual(packet_getter.get_float(p), 0.42)
|
||||
self.assertEqual(p.timestamp, 0)
|
||||
p2 = mp.packet_creator.create_double(np.double(0.42))
|
||||
p2 = packet_creator.create_double(np.double(0.42))
|
||||
p2.timestamp = 0
|
||||
self.assertAlmostEqual(mp.packet_getter.get_float(p2), 0.42)
|
||||
self.assertAlmostEqual(packet_getter.get_float(p2), 0.42)
|
||||
self.assertEqual(p2.timestamp, 0)
|
||||
|
||||
def test_detection_proto_packet(self):
|
||||
detection = detection_pb2.Detection()
|
||||
text_format.Parse('score: 0.5', detection)
|
||||
p = mp.packet_creator.create_proto(detection).at(100)
|
||||
p = packet_creator.create_proto(detection).at(100)
|
||||
|
||||
def test_string_packet(self):
|
||||
p = mp.packet_creator.create_string('abc').at(100)
|
||||
self.assertEqual(mp.packet_getter.get_str(p), 'abc')
|
||||
p = packet_creator.create_string('abc').at(100)
|
||||
self.assertEqual(packet_getter.get_str(p), 'abc')
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
p.timestamp = 200
|
||||
self.assertEqual(p.timestamp, 200)
|
||||
|
||||
def test_bytes_packet(self):
|
||||
p = mp.packet_creator.create_string(b'xd0\xba\xd0').at(300)
|
||||
self.assertEqual(mp.packet_getter.get_bytes(p), b'xd0\xba\xd0')
|
||||
p = packet_creator.create_string(b'xd0\xba\xd0').at(300)
|
||||
self.assertEqual(packet_getter.get_bytes(p), b'xd0\xba\xd0')
|
||||
self.assertEqual(p.timestamp, 300)
|
||||
|
||||
def test_int_array_packet(self):
|
||||
p = mp.packet_creator.create_int_array([1, 2, 3]).at(100)
|
||||
p = packet_creator.create_int_array([1, 2, 3]).at(100)
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
|
||||
def test_float_array_packet(self):
|
||||
p = mp.packet_creator.create_float_array([0.1, 0.2, 0.3]).at(100)
|
||||
p = packet_creator.create_float_array([0.1, 0.2, 0.3]).at(100)
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
|
||||
def test_int_vector_packet(self):
|
||||
p = mp.packet_creator.create_int_vector([1, 2, 3]).at(100)
|
||||
self.assertEqual(mp.packet_getter.get_int_list(p), [1, 2, 3])
|
||||
p = packet_creator.create_int_vector([1, 2, 3]).at(100)
|
||||
self.assertEqual(packet_getter.get_int_list(p), [1, 2, 3])
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
|
||||
def test_float_vector_packet(self):
|
||||
p = mp.packet_creator.create_float_vector([0.1, 0.2, 0.3]).at(100)
|
||||
output_list = mp.packet_getter.get_float_list(p)
|
||||
p = packet_creator.create_float_vector([0.1, 0.2, 0.3]).at(100)
|
||||
output_list = packet_getter.get_float_list(p)
|
||||
self.assertAlmostEqual(output_list[0], 0.1)
|
||||
self.assertAlmostEqual(output_list[1], 0.2)
|
||||
self.assertAlmostEqual(output_list[2], 0.3)
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
|
||||
def test_image_vector_packet(self):
|
||||
w, h, offset = 80, 40, 10
|
||||
mat = np.random.randint(2**8 - 1, size=(h, w, 3), dtype=np.uint8)
|
||||
p = packet_creator.create_image_vector([
|
||||
Image(image_format=ImageFormat.SRGB, data=mat),
|
||||
Image(
|
||||
image_format=ImageFormat.SRGB,
|
||||
data=np.ascontiguousarray(mat[offset:-offset, offset:-offset, :]))
|
||||
]).at(100)
|
||||
output_list = packet_getter.get_image_list(p)
|
||||
self.assertLen(output_list, 2)
|
||||
self.assertTrue(np.array_equal(output_list[0].numpy_view(), mat))
|
||||
self.assertTrue(
|
||||
np.array_equal(output_list[1].numpy_view(), mat[offset:-offset,
|
||||
offset:-offset, :]))
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
|
||||
def test_string_vector_packet(self):
|
||||
p = mp.packet_creator.create_string_vector(['a', 'b', 'c']).at(100)
|
||||
output_list = mp.packet_getter.get_str_list(p)
|
||||
p = packet_creator.create_string_vector(['a', 'b', 'c']).at(100)
|
||||
output_list = packet_getter.get_str_list(p)
|
||||
self.assertEqual(output_list[0], 'a')
|
||||
self.assertEqual(output_list[1], 'b')
|
||||
self.assertEqual(output_list[2], 'c')
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
|
||||
def test_packet_vector_packet(self):
|
||||
p = mp.packet_creator.create_packet_vector([
|
||||
mp.packet_creator.create_float(0.42),
|
||||
mp.packet_creator.create_int(42),
|
||||
mp.packet_creator.create_string('42')
|
||||
p = packet_creator.create_packet_vector([
|
||||
packet_creator.create_float(0.42),
|
||||
packet_creator.create_int(42),
|
||||
packet_creator.create_string('42')
|
||||
]).at(100)
|
||||
output_list = mp.packet_getter.get_packet_list(p)
|
||||
self.assertAlmostEqual(mp.packet_getter.get_float(output_list[0]), 0.42)
|
||||
self.assertEqual(mp.packet_getter.get_int(output_list[1]), 42)
|
||||
self.assertEqual(mp.packet_getter.get_str(output_list[2]), '42')
|
||||
output_list = packet_getter.get_packet_list(p)
|
||||
self.assertAlmostEqual(packet_getter.get_float(output_list[0]), 0.42)
|
||||
self.assertEqual(packet_getter.get_int(output_list[1]), 42)
|
||||
self.assertEqual(packet_getter.get_str(output_list[2]), '42')
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
|
||||
def test_string_to_packet_map_packet(self):
|
||||
p = mp.packet_creator.create_string_to_packet_map({
|
||||
'float': mp.packet_creator.create_float(0.42),
|
||||
'int': mp.packet_creator.create_int(42),
|
||||
'string': mp.packet_creator.create_string('42')
|
||||
p = packet_creator.create_string_to_packet_map({
|
||||
'float': packet_creator.create_float(0.42),
|
||||
'int': packet_creator.create_int(42),
|
||||
'string': packet_creator.create_string('42')
|
||||
}).at(100)
|
||||
output_list = mp.packet_getter.get_str_to_packet_dict(p)
|
||||
self.assertAlmostEqual(
|
||||
mp.packet_getter.get_float(output_list['float']), 0.42)
|
||||
self.assertEqual(mp.packet_getter.get_int(output_list['int']), 42)
|
||||
self.assertEqual(mp.packet_getter.get_str(output_list['string']), '42')
|
||||
output_list = packet_getter.get_str_to_packet_dict(p)
|
||||
self.assertAlmostEqual(packet_getter.get_float(output_list['float']), 0.42)
|
||||
self.assertEqual(packet_getter.get_int(output_list['int']), 42)
|
||||
self.assertEqual(packet_getter.get_str(output_list['string']), '42')
|
||||
self.assertEqual(p.timestamp, 100)
|
||||
|
||||
def test_uint8_image_packet(self):
|
||||
@@ -237,13 +264,14 @@ class PacketTest(absltest.TestCase):
|
||||
2**8 - 1,
|
||||
size=(random.randrange(3, 100), random.randrange(3, 100), 3),
|
||||
dtype=np.uint8)
|
||||
image_frame_packet = mp.packet_creator.create_image_frame(
|
||||
mp.ImageFrame(image_format=mp.ImageFormat.SRGB, data=uint8_img))
|
||||
output_image_frame = mp.packet_getter.get_image_frame(image_frame_packet)
|
||||
image_frame_packet = packet_creator.create_image_frame(
|
||||
image_frame.ImageFrame(
|
||||
image_format=image_frame.ImageFormat.SRGB, data=uint8_img))
|
||||
output_image_frame = packet_getter.get_image_frame(image_frame_packet)
|
||||
self.assertTrue(np.array_equal(output_image_frame.numpy_view(), uint8_img))
|
||||
image_packet = mp.packet_creator.create_image(
|
||||
mp.Image(image_format=mp.ImageFormat.SRGB, data=uint8_img))
|
||||
output_image = mp.packet_getter.get_image(image_packet)
|
||||
image_packet = packet_creator.create_image(
|
||||
Image(image_format=ImageFormat.SRGB, data=uint8_img))
|
||||
output_image = packet_getter.get_image(image_packet)
|
||||
self.assertTrue(np.array_equal(output_image.numpy_view(), uint8_img))
|
||||
|
||||
def test_uint16_image_packet(self):
|
||||
@@ -251,26 +279,26 @@ class PacketTest(absltest.TestCase):
|
||||
2**16 - 1,
|
||||
size=(random.randrange(3, 100), random.randrange(3, 100), 4),
|
||||
dtype=np.uint16)
|
||||
image_frame_packet = mp.packet_creator.create_image_frame(
|
||||
mp.ImageFrame(image_format=mp.ImageFormat.SRGBA64, data=uint16_img))
|
||||
output_image_frame = mp.packet_getter.get_image_frame(image_frame_packet)
|
||||
image_frame_packet = packet_creator.create_image_frame(
|
||||
ImageFrame(image_format=ImageFormat.SRGBA64, data=uint16_img))
|
||||
output_image_frame = packet_getter.get_image_frame(image_frame_packet)
|
||||
self.assertTrue(np.array_equal(output_image_frame.numpy_view(), uint16_img))
|
||||
image_packet = mp.packet_creator.create_image(
|
||||
mp.Image(image_format=mp.ImageFormat.SRGBA64, data=uint16_img))
|
||||
output_image = mp.packet_getter.get_image(image_packet)
|
||||
image_packet = packet_creator.create_image(
|
||||
Image(image_format=ImageFormat.SRGBA64, data=uint16_img))
|
||||
output_image = packet_getter.get_image(image_packet)
|
||||
self.assertTrue(np.array_equal(output_image.numpy_view(), uint16_img))
|
||||
|
||||
def test_float_image_frame_packet(self):
|
||||
float_img = np.float32(
|
||||
np.random.random_sample(
|
||||
(random.randrange(3, 100), random.randrange(3, 100), 2)))
|
||||
image_frame_packet = mp.packet_creator.create_image_frame(
|
||||
mp.ImageFrame(image_format=mp.ImageFormat.VEC32F2, data=float_img))
|
||||
output_image_frame = mp.packet_getter.get_image_frame(image_frame_packet)
|
||||
image_frame_packet = packet_creator.create_image_frame(
|
||||
ImageFrame(image_format=ImageFormat.VEC32F2, data=float_img))
|
||||
output_image_frame = packet_getter.get_image_frame(image_frame_packet)
|
||||
self.assertTrue(np.allclose(output_image_frame.numpy_view(), float_img))
|
||||
image_packet = mp.packet_creator.create_image(
|
||||
mp.Image(image_format=mp.ImageFormat.VEC32F2, data=float_img))
|
||||
output_image = mp.packet_getter.get_image(image_packet)
|
||||
image_packet = packet_creator.create_image(
|
||||
Image(image_format=ImageFormat.VEC32F2, data=float_img))
|
||||
output_image = packet_getter.get_image(image_packet)
|
||||
self.assertTrue(np.array_equal(output_image.numpy_view(), float_img))
|
||||
|
||||
def test_image_frame_packet_creation_copy_mode(self):
|
||||
@@ -279,8 +307,8 @@ class PacketTest(absltest.TestCase):
|
||||
# rgb_data is c_contiguous.
|
||||
self.assertTrue(rgb_data.flags.c_contiguous)
|
||||
initial_ref_count = sys.getrefcount(rgb_data)
|
||||
p = mp.packet_creator.create_image_frame(
|
||||
image_format=mp.ImageFormat.SRGB, data=rgb_data)
|
||||
p = packet_creator.create_image_frame(
|
||||
image_format=ImageFormat.SRGB, data=rgb_data)
|
||||
# copy mode doesn't increase the ref count of the data.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count)
|
||||
|
||||
@@ -288,12 +316,12 @@ class PacketTest(absltest.TestCase):
|
||||
# rgb_data is now not c_contiguous. But, copy mode shouldn't be affected.
|
||||
self.assertFalse(rgb_data.flags.c_contiguous)
|
||||
initial_ref_count = sys.getrefcount(rgb_data)
|
||||
p = mp.packet_creator.create_image_frame(
|
||||
image_format=mp.ImageFormat.SRGB, data=rgb_data)
|
||||
p = packet_creator.create_image_frame(
|
||||
image_format=ImageFormat.SRGB, data=rgb_data)
|
||||
# copy mode doesn't increase the ref count of the data.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count)
|
||||
|
||||
output_frame = mp.packet_getter.get_image_frame(p)
|
||||
output_frame = packet_getter.get_image_frame(p)
|
||||
self.assertEqual(output_frame.height, h)
|
||||
self.assertEqual(output_frame.width, w)
|
||||
self.assertEqual(output_frame.channels, channels)
|
||||
@@ -311,8 +339,8 @@ class PacketTest(absltest.TestCase):
|
||||
rgb_data = np.random.randint(255, size=(h, w, channels), dtype=np.uint8)
|
||||
rgb_data.flags.writeable = False
|
||||
initial_ref_count = sys.getrefcount(rgb_data)
|
||||
image_frame_packet = mp.packet_creator.create_image_frame(
|
||||
image_format=mp.ImageFormat.SRGB, data=rgb_data)
|
||||
image_frame_packet = packet_creator.create_image_frame(
|
||||
image_format=ImageFormat.SRGB, data=rgb_data)
|
||||
# Reference mode increase the ref count of the rgb_data by 1.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count + 1)
|
||||
del image_frame_packet
|
||||
@@ -329,12 +357,12 @@ class PacketTest(absltest.TestCase):
|
||||
output_side_packet: "out"
|
||||
}
|
||||
"""
|
||||
graph = mp.CalculatorGraph(graph_config=text_config)
|
||||
graph = CalculatorGraph(graph_config=text_config)
|
||||
graph.start_run(
|
||||
input_side_packets={
|
||||
'in':
|
||||
mp.packet_creator.create_image_frame(
|
||||
image_format=mp.ImageFormat.SRGB, data=rgb_data)
|
||||
packet_creator.create_image_frame(
|
||||
image_format=ImageFormat.SRGB, data=rgb_data)
|
||||
})
|
||||
# reference mode increase the ref count of the rgb_data by 1.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count + 1)
|
||||
@@ -347,7 +375,7 @@ class PacketTest(absltest.TestCase):
|
||||
# after the graph and the original rgb_data data are deleted.
|
||||
self.assertTrue(
|
||||
np.array_equal(
|
||||
mp.packet_getter.get_image_frame(output_packet).numpy_view(),
|
||||
packet_getter.get_image_frame(output_packet).numpy_view(),
|
||||
rgb_data_copy))
|
||||
|
||||
def test_image_frame_packet_copy_creation_with_cropping(self):
|
||||
@@ -355,12 +383,12 @@ class PacketTest(absltest.TestCase):
|
||||
channels, offset = 3, 10
|
||||
rgb_data = np.random.randint(255, size=(h, w, channels), dtype=np.uint8)
|
||||
initial_ref_count = sys.getrefcount(rgb_data)
|
||||
p = mp.packet_creator.create_image_frame(
|
||||
image_format=mp.ImageFormat.SRGB,
|
||||
p = packet_creator.create_image_frame(
|
||||
image_format=ImageFormat.SRGB,
|
||||
data=rgb_data[offset:-offset, offset:-offset, :])
|
||||
# copy mode doesn't increase the ref count of the data.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count)
|
||||
output_frame = mp.packet_getter.get_image_frame(p)
|
||||
output_frame = packet_getter.get_image_frame(p)
|
||||
self.assertEqual(output_frame.height, h - 2 * offset)
|
||||
self.assertEqual(output_frame.width, w - 2 * offset)
|
||||
self.assertEqual(output_frame.channels, channels)
|
||||
@@ -380,8 +408,8 @@ class PacketTest(absltest.TestCase):
|
||||
# rgb_data is c_contiguous.
|
||||
self.assertTrue(rgb_data.flags.c_contiguous)
|
||||
initial_ref_count = sys.getrefcount(rgb_data)
|
||||
p = mp.packet_creator.create_image(
|
||||
image_format=mp.ImageFormat.SRGB, data=rgb_data)
|
||||
p = packet_creator.create_image(
|
||||
image_format=ImageFormat.SRGB, data=rgb_data)
|
||||
# copy mode doesn't increase the ref count of the data.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count)
|
||||
|
||||
@@ -389,12 +417,12 @@ class PacketTest(absltest.TestCase):
|
||||
# rgb_data is now not c_contiguous. But, copy mode shouldn't be affected.
|
||||
self.assertFalse(rgb_data.flags.c_contiguous)
|
||||
initial_ref_count = sys.getrefcount(rgb_data)
|
||||
p = mp.packet_creator.create_image(
|
||||
image_format=mp.ImageFormat.SRGB, data=rgb_data)
|
||||
p = packet_creator.create_image(
|
||||
image_format=ImageFormat.SRGB, data=rgb_data)
|
||||
# copy mode doesn't increase the ref count of the data.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count)
|
||||
|
||||
output_image = mp.packet_getter.get_image(p)
|
||||
output_image = packet_getter.get_image(p)
|
||||
self.assertEqual(output_image.height, h)
|
||||
self.assertEqual(output_image.width, w)
|
||||
self.assertEqual(output_image.channels, channels)
|
||||
@@ -412,8 +440,8 @@ class PacketTest(absltest.TestCase):
|
||||
rgb_data = np.random.randint(255, size=(h, w, channels), dtype=np.uint8)
|
||||
rgb_data.flags.writeable = False
|
||||
initial_ref_count = sys.getrefcount(rgb_data)
|
||||
image_packet = mp.packet_creator.create_image(
|
||||
image_format=mp.ImageFormat.SRGB, data=rgb_data)
|
||||
image_packet = packet_creator.create_image(
|
||||
image_format=ImageFormat.SRGB, data=rgb_data)
|
||||
# Reference mode increase the ref count of the rgb_data by 1.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count + 1)
|
||||
del image_packet
|
||||
@@ -430,12 +458,12 @@ class PacketTest(absltest.TestCase):
|
||||
output_side_packet: "out"
|
||||
}
|
||||
"""
|
||||
graph = mp.CalculatorGraph(graph_config=text_config)
|
||||
graph = CalculatorGraph(graph_config=text_config)
|
||||
graph.start_run(
|
||||
input_side_packets={
|
||||
'in':
|
||||
mp.packet_creator.create_image(
|
||||
image_format=mp.ImageFormat.SRGB, data=rgb_data)
|
||||
packet_creator.create_image(
|
||||
image_format=ImageFormat.SRGB, data=rgb_data)
|
||||
})
|
||||
# reference mode increase the ref count of the rgb_data by 1.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count + 1)
|
||||
@@ -448,20 +476,19 @@ class PacketTest(absltest.TestCase):
|
||||
# after the graph and the original rgb_data data are deleted.
|
||||
self.assertTrue(
|
||||
np.array_equal(
|
||||
mp.packet_getter.get_image(output_packet).numpy_view(),
|
||||
rgb_data_copy))
|
||||
packet_getter.get_image(output_packet).numpy_view(), rgb_data_copy))
|
||||
|
||||
def test_image_packet_copy_creation_with_cropping(self):
|
||||
w, h, channels = random.randrange(40, 100), random.randrange(40, 100), 3
|
||||
channels, offset = 3, 10
|
||||
rgb_data = np.random.randint(255, size=(h, w, channels), dtype=np.uint8)
|
||||
initial_ref_count = sys.getrefcount(rgb_data)
|
||||
p = mp.packet_creator.create_image(
|
||||
image_format=mp.ImageFormat.SRGB,
|
||||
p = packet_creator.create_image(
|
||||
image_format=ImageFormat.SRGB,
|
||||
data=rgb_data[offset:-offset, offset:-offset, :])
|
||||
# copy mode doesn't increase the ref count of the data.
|
||||
self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count)
|
||||
output_image = mp.packet_getter.get_image(p)
|
||||
output_image = packet_getter.get_image(p)
|
||||
self.assertEqual(output_image.height, h - 2 * offset)
|
||||
self.assertEqual(output_image.width, w - 2 * offset)
|
||||
self.assertEqual(output_image.channels, channels)
|
||||
@@ -478,10 +505,10 @@ class PacketTest(absltest.TestCase):
|
||||
def test_matrix_packet(self):
|
||||
np_matrix = np.array([[.1, .2, .3], [.4, .5, .6]])
|
||||
initial_ref_count = sys.getrefcount(np_matrix)
|
||||
p = mp.packet_creator.create_matrix(np_matrix)
|
||||
p = packet_creator.create_matrix(np_matrix)
|
||||
# Copy mode should not increase the ref count of np_matrix.
|
||||
self.assertEqual(initial_ref_count, sys.getrefcount(np_matrix))
|
||||
output_matrix = mp.packet_getter.get_matrix(p)
|
||||
output_matrix = packet_getter.get_matrix(p)
|
||||
del np_matrix
|
||||
gc.collect()
|
||||
self.assertTrue(
|
||||
@@ -491,11 +518,11 @@ class PacketTest(absltest.TestCase):
|
||||
np_matrix = np.array([[.1, .2, .3], [.4, .5, .6]])[:, ::-1]
|
||||
# np_matrix is not c_contiguous.
|
||||
self.assertFalse(np_matrix.flags.c_contiguous)
|
||||
p = mp.packet_creator.create_matrix(np_matrix)
|
||||
p = packet_creator.create_matrix(np_matrix)
|
||||
initial_ref_count = sys.getrefcount(np_matrix)
|
||||
# Copy mode should not increase the ref count of np_matrix.
|
||||
self.assertEqual(initial_ref_count, sys.getrefcount(np_matrix))
|
||||
output_matrix = mp.packet_getter.get_matrix(p)
|
||||
output_matrix = packet_getter.get_matrix(p)
|
||||
del np_matrix
|
||||
gc.collect()
|
||||
self.assertTrue(
|
||||
|
||||
@@ -16,7 +16,7 @@ load("@pybind11_bazel//:build_defs.bzl", "pybind_library")
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
package(default_visibility = ["//mediapipe/python:__subpackages__"])
|
||||
package(default_visibility = ["//mediapipe:__subpackages__"])
|
||||
|
||||
pybind_library(
|
||||
name = "calculator_graph",
|
||||
|
||||
@@ -76,6 +76,7 @@ Packet CreateImagePacket(mediapipe::ImageFormat::Format format,
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
// The packet creator methods that can be accessed directly by the users.
|
||||
void PublicPacketCreators(pybind11::module* m) {
|
||||
m->def(
|
||||
"create_string",
|
||||
@@ -515,18 +516,41 @@ void PublicPacketCreators(pybind11::module* m) {
|
||||
)doc",
|
||||
py::arg().noconvert(), py::return_value_policy::move);
|
||||
|
||||
m->def(
|
||||
"create_image_vector",
|
||||
[](const std::vector<Image>& data) {
|
||||
return MakePacket<std::vector<Image>>(data);
|
||||
},
|
||||
R"doc(Create a MediaPipe Packet holding a vector of MediaPipe Images.
|
||||
|
||||
Args:
|
||||
data: A list of MediaPipe Images.
|
||||
|
||||
Returns:
|
||||
A MediaPipe Packet holding a vector of MediaPipe Images.
|
||||
|
||||
Raises:
|
||||
TypeError: If the input is not a list of MediaPipe Images.
|
||||
|
||||
Examples:
|
||||
packet = mp.packet_creator.create_image_vector([
|
||||
image1, image2, image3])
|
||||
data = mp.packet_getter.get_image_list(packet)
|
||||
)doc",
|
||||
py::arg().noconvert(), py::return_value_policy::move);
|
||||
|
||||
m->def(
|
||||
"create_packet_vector",
|
||||
[](const std::vector<Packet>& data) {
|
||||
return MakePacket<std::vector<Packet>>(data);
|
||||
},
|
||||
R"doc(Create a MediaPipe Packet holds a vector of packets.
|
||||
R"doc(Create a MediaPipe Packet holding a vector of packets.
|
||||
|
||||
Args:
|
||||
data: A list of packets.
|
||||
|
||||
Returns:
|
||||
A MediaPipe Packet holds a vector of packets.
|
||||
A MediaPipe Packet holding a vector of packets.
|
||||
|
||||
Raises:
|
||||
TypeError: If the input is not a list of packets.
|
||||
@@ -552,7 +576,7 @@ void PublicPacketCreators(pybind11::module* m) {
|
||||
data: A dictionary that has (str, Packet) pairs.
|
||||
|
||||
Returns:
|
||||
A MediaPipe Packet holds std::map<std::string, Packet>.
|
||||
A MediaPipe Packet holding std::map<std::string, Packet>.
|
||||
|
||||
Raises:
|
||||
TypeError: If the input is not a dictionary from str to packet.
|
||||
@@ -602,8 +626,9 @@ void PublicPacketCreators(pybind11::module* m) {
|
||||
matrix = mp.packet_getter.get_matrix(packet)
|
||||
)doc",
|
||||
py::return_value_policy::move);
|
||||
}
|
||||
} // NOLINT(readability/fn_size)
|
||||
|
||||
// The packet creator methods that should be used by MediaPipe Python itself.
|
||||
void InternalPacketCreators(pybind11::module* m) {
|
||||
m->def("_create_image_frame_from_pixel_data", &CreateImageFramePacket,
|
||||
py::arg("format"), py::arg("data").noconvert(), py::arg("copy"),
|
||||
|
||||
@@ -297,6 +297,25 @@ void PublicPacketGetters(pybind11::module* m) {
|
||||
data = mp.packet_getter.get_str_list(packet)
|
||||
)doc");
|
||||
|
||||
m->def(
|
||||
"get_image_list", &GetContent<std::vector<Image>>,
|
||||
R"doc(Get the content of a MediaPipe Packet of image vector as a list of MediaPipe Images.
|
||||
|
||||
Args:
|
||||
packet: A MediaPipe Packet that holds std:vector<mediapipe::Image>.
|
||||
|
||||
Returns:
|
||||
A list of MediaPipe Images.
|
||||
|
||||
Raises:
|
||||
ValueError: If the Packet doesn't contain std:vector<mediapipe::Image>.
|
||||
|
||||
Examples:
|
||||
packet = mp.packet_creator.create_image_vector([
|
||||
image1, image2, image3])
|
||||
image_list = mp.packet_getter.get_image_list(packet)
|
||||
)doc");
|
||||
|
||||
m->def(
|
||||
"get_packet_list", &GetContent<std::vector<Packet>>,
|
||||
R"doc(Get the content of a MediaPipe Packet of Packet vector as a Packet list.
|
||||
|
||||
@@ -33,29 +33,28 @@ from google.protobuf import descriptor
|
||||
from google.protobuf import message
|
||||
# resources dependency
|
||||
# pylint: disable=unused-import
|
||||
# pylint: enable=unused-import
|
||||
from mediapipe.framework import calculator_pb2
|
||||
# pylint: disable=unused-import
|
||||
from mediapipe.framework.formats import detection_pb2
|
||||
from mediapipe.calculators.core import constant_side_packet_calculator_pb2
|
||||
from mediapipe.calculators.image import image_transformation_calculator_pb2
|
||||
from mediapipe.calculators.tensor import tensors_to_detections_calculator_pb2
|
||||
from mediapipe.calculators.util import landmarks_smoothing_calculator_pb2
|
||||
from mediapipe.calculators.util import logic_calculator_pb2
|
||||
from mediapipe.calculators.util import thresholding_calculator_pb2
|
||||
from mediapipe.framework import calculator_pb2
|
||||
from mediapipe.framework.formats import body_rig_pb2
|
||||
from mediapipe.framework.formats import classification_pb2
|
||||
from mediapipe.framework.formats import detection_pb2
|
||||
from mediapipe.framework.formats import landmark_pb2
|
||||
from mediapipe.framework.formats import rect_pb2
|
||||
from mediapipe.modules.objectron.calculators import annotation_data_pb2
|
||||
from mediapipe.modules.objectron.calculators import lift_2d_frame_annotation_to_3d_calculator_pb2
|
||||
# pylint: enable=unused-import
|
||||
from mediapipe.python import packet_creator
|
||||
from mediapipe.python import packet_getter
|
||||
from mediapipe.python._framework_bindings import calculator_graph
|
||||
from mediapipe.python._framework_bindings import image_frame
|
||||
from mediapipe.python._framework_bindings import packet
|
||||
from mediapipe.python._framework_bindings import resource_util
|
||||
from mediapipe.python._framework_bindings import validated_graph_config
|
||||
import mediapipe.python.packet_creator as packet_creator
|
||||
import mediapipe.python.packet_getter as packet_getter
|
||||
|
||||
RGB_CHANNELS = 3
|
||||
# TODO: Enable calculator options modification for more calculators.
|
||||
@@ -100,6 +99,7 @@ class PacketDataType(enum.Enum):
|
||||
FLOAT_LIST = 'float_list'
|
||||
AUDIO = 'matrix'
|
||||
IMAGE = 'image'
|
||||
IMAGE_LIST = 'image_list'
|
||||
IMAGE_FRAME = 'image_frame'
|
||||
PROTO = 'proto'
|
||||
PROTO_LIST = 'proto_list'
|
||||
@@ -171,6 +171,8 @@ NAME_TO_TYPE: Mapping[str, 'PacketDataType'] = {
|
||||
PacketDataType.PROTO,
|
||||
'::mediapipe::Image':
|
||||
PacketDataType.IMAGE,
|
||||
'::std::vector<::mediapipe::Image>':
|
||||
PacketDataType.IMAGE_LIST,
|
||||
'::std::vector<::mediapipe::Classification>':
|
||||
PacketDataType.PROTO_LIST,
|
||||
'::std::vector<::mediapipe::ClassificationList>':
|
||||
|
||||
@@ -17,48 +17,51 @@
|
||||
import time
|
||||
|
||||
from absl.testing import absltest
|
||||
import mediapipe as mp
|
||||
|
||||
from mediapipe.python._framework_bindings import timestamp
|
||||
|
||||
Timestamp = timestamp.Timestamp
|
||||
|
||||
|
||||
class TimestampTest(absltest.TestCase):
|
||||
|
||||
def test_timestamp(self):
|
||||
t = mp.Timestamp(100)
|
||||
t = Timestamp(100)
|
||||
self.assertEqual(t.value, 100)
|
||||
self.assertEqual(t, 100)
|
||||
self.assertEqual(str(t), '<mediapipe.Timestamp with value: 100>')
|
||||
|
||||
def test_timestamp_copy_constructor(self):
|
||||
ts1 = mp.Timestamp(100)
|
||||
ts2 = mp.Timestamp(ts1)
|
||||
ts1 = Timestamp(100)
|
||||
ts2 = Timestamp(ts1)
|
||||
self.assertEqual(ts1, ts2)
|
||||
|
||||
def test_timestamp_comparsion(self):
|
||||
ts1 = mp.Timestamp(100)
|
||||
ts2 = mp.Timestamp(100)
|
||||
ts1 = Timestamp(100)
|
||||
ts2 = Timestamp(100)
|
||||
self.assertEqual(ts1, ts2)
|
||||
ts3 = mp.Timestamp(200)
|
||||
ts3 = Timestamp(200)
|
||||
self.assertNotEqual(ts1, ts3)
|
||||
|
||||
def test_timestamp_special_values(self):
|
||||
t1 = mp.Timestamp.UNSET
|
||||
t1 = Timestamp.UNSET
|
||||
self.assertEqual(str(t1), '<mediapipe.Timestamp with value: UNSET>')
|
||||
t2 = mp.Timestamp.UNSTARTED
|
||||
t2 = Timestamp.UNSTARTED
|
||||
self.assertEqual(str(t2), '<mediapipe.Timestamp with value: UNSTARTED>')
|
||||
t3 = mp.Timestamp.PRESTREAM
|
||||
t3 = Timestamp.PRESTREAM
|
||||
self.assertEqual(str(t3), '<mediapipe.Timestamp with value: PRESTREAM>')
|
||||
t4 = mp.Timestamp.MIN
|
||||
t4 = Timestamp.MIN
|
||||
self.assertEqual(str(t4), '<mediapipe.Timestamp with value: MIN>')
|
||||
t5 = mp.Timestamp.MAX
|
||||
t5 = Timestamp.MAX
|
||||
self.assertEqual(str(t5), '<mediapipe.Timestamp with value: MAX>')
|
||||
t6 = mp.Timestamp.POSTSTREAM
|
||||
t6 = Timestamp.POSTSTREAM
|
||||
self.assertEqual(str(t6), '<mediapipe.Timestamp with value: POSTSTREAM>')
|
||||
t7 = mp.Timestamp.DONE
|
||||
t7 = Timestamp.DONE
|
||||
self.assertEqual(str(t7), '<mediapipe.Timestamp with value: DONE>')
|
||||
|
||||
def test_timestamp_comparisons(self):
|
||||
ts1 = mp.Timestamp(100)
|
||||
ts2 = mp.Timestamp(101)
|
||||
ts1 = Timestamp(100)
|
||||
ts2 = Timestamp(101)
|
||||
self.assertGreater(ts2, ts1)
|
||||
self.assertGreaterEqual(ts2, ts1)
|
||||
self.assertLess(ts1, ts2)
|
||||
@@ -67,7 +70,7 @@ class TimestampTest(absltest.TestCase):
|
||||
|
||||
def test_from_seconds(self):
|
||||
now = time.time()
|
||||
ts = mp.Timestamp.from_seconds(now)
|
||||
ts = Timestamp.from_seconds(now)
|
||||
self.assertAlmostEqual(now, ts.seconds(), delta=1)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user