From a05cb83e996d4675e6174ca79241a41fd5a08a2c Mon Sep 17 00:00:00 2001 From: davey Date: Fri, 15 Jul 2016 16:31:57 +0100 Subject: [PATCH 1/2] Changed bound type of frame pixels. --- Malmo/src/PythonWrapper/python_module.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Malmo/src/PythonWrapper/python_module.cpp b/Malmo/src/PythonWrapper/python_module.cpp index 2cbb56abc..79afbea7e 100755 --- a/Malmo/src/PythonWrapper/python_module.cpp +++ b/Malmo/src/PythonWrapper/python_module.cpp @@ -87,6 +87,15 @@ struct ptime_to_python_datetime } }; +struct unsigned_char_vec_to_python_array +{ + static PyObject* convert(std::vector const& vec) + { + const char* buffer = reinterpret_cast(vec.data()); + return PyByteArray_FromStringAndSize(buffer, vec.size()); + } +}; + // Defines the API available to Python. BOOST_PYTHON_MODULE(MalmoPython) { @@ -95,6 +104,7 @@ BOOST_PYTHON_MODULE(MalmoPython) // Bind the converter for posix_time to python DateTime PyDateTime_IMPORT; to_python_converter(); + to_python_converter, unsigned_char_vec_to_python_array>(); class_< ArgumentParser, boost::noncopyable >("ArgumentParser", init< const std::string& >()) .def( "parse", &parsePythonList ) @@ -253,7 +263,7 @@ BOOST_PYTHON_MODULE(MalmoPython) .def_readonly( "width", &TimestampedVideoFrame::width ) .def_readonly( "height", &TimestampedVideoFrame::height ) .def_readonly( "channels", &TimestampedVideoFrame::channels ) - .def_readonly( "pixels", &TimestampedVideoFrame::pixels ) + .add_property( "pixels", make_getter(&TimestampedVideoFrame::pixels, return_value_policy())) .def(self_ns::str(self_ns::self)) ; class_< std::vector< boost::shared_ptr< TimestampedString > > >( "TimestampedStringVector" ) @@ -265,8 +275,5 @@ BOOST_PYTHON_MODULE(MalmoPython) class_< std::vector< boost::shared_ptr< TimestampedVideoFrame > > >( "TimestampedVideoFrameVector" ) .def( vector_indexing_suite< std::vector< boost::shared_ptr< TimestampedVideoFrame > >, true >() ) ; - class_< std::vector< unsigned char > >( "UnsignedCharVector") - .def( vector_indexing_suite< std::vector< unsigned char > >() ) - ; register_exception_translator(&translateXMLSchemaException); } From effbc2981d14493e8d0078aca047e08387292741 Mon Sep 17 00:00:00 2001 From: davey Date: Fri, 15 Jul 2016 17:48:35 +0100 Subject: [PATCH 2/2] Fixed ALE_HAC to work with python pixel array. --- Malmo/samples/Python_examples/ALE_HAC.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Malmo/samples/Python_examples/ALE_HAC.py b/Malmo/samples/Python_examples/ALE_HAC.py index f98c0a01c..3ac0a2899 100755 --- a/Malmo/samples/Python_examples/ALE_HAC.py +++ b/Malmo/samples/Python_examples/ALE_HAC.py @@ -24,7 +24,7 @@ import time import errno import Tkinter as tk -#from PIL import Image, ImageTk - we need these for displaying our own gui, but it's a nightmare installing the dependencies correctly. +from PIL import Image, ImageTk from array import array from struct import pack @@ -192,14 +192,12 @@ def sendCommand(): print "Error:",error.text if world_state.number_of_video_frames_since_last_state > 0 and want_own_display: # Turn the frame into an image to display on our canvas. - # On my system creating buff was too slow to be usable, whichever of these three apporaches I tried: - buff = str(bytearray(world_state.video_frames[-1].pixels)) - # Or buff = pack('100800B', *(world_state.video_frames[-1].pixels)) - # Or buff = array('B', world_state.video_frames[-1].pixels) - image = Image.frombytes('RGB', (320,420), buff) + frame = world_state.video_frames[-1] + buff = buffer(frame.pixels, 0, frame.width * frame.height * frame.channels) + image = Image.frombytes('RGB', (frame.width,frame.height), buff) photo = ImageTk.PhotoImage(image) canvas.delete("all") - canvas.create_image(80,105, image=photo) + canvas.create_image(frame.width/2, frame.height/2, image=photo) root.update() if world_state.is_mission_running: @@ -237,7 +235,7 @@ def sendCommand(): iterations = agent_host.getIntArgument('goes') my_mission = MalmoPython.MissionSpec() -my_mission.requestVideo( 210, 160 ) +my_mission.requestVideo( 160, 210 ) recordingsDirectory = rom_file.rpartition('/')[-1]+'_recordings' try: