Пример #1
0
    def run(self):
        from bge import texture

        if self.file_map != None:
            self.file_map.seek(0)

            w = 0
            h = 0

            if self.file_map.size() == 640*480*4:
                w = 640
                h = 480
            elif self.file_map.size() == 320*240*4:
                w = 320
                h = 240
            elif self.file_map.size() == 160*120*4:
                w = 160
                h = 120

            if w > 0 and h > 0:
                if self.img_w != w or self.img_h != h:
                    self.image_texture.source = texture.ImageBuff()
                    self.image_texture.source.filter = texture.FilterRGBA32()

                buf = self.file_map.read(w*h*4)
                self.image_texture.source.load(buf, w, h)
                self.image_texture.refresh(True)
Пример #2
0
    def __init__(self, image_name, feed_2):
        import sys
        import bge
        from bge import texture
        import tempfile

        cont = bge.logic.getCurrentController()
        own = cont.owner

        try:
            matID = texture.materialID(own, image_name)
        except:
            print("Delicode NI mate Tools Error: No texture with name: " +
                  image_name)
            pass

        try:
            self.image_texture = texture.Texture(own, matID)

            if feed_2:
                filename = "NI_mate_shared_map2.data"
            else:
                filename = "NI_mate_shared_map1.data"

            file = open(tempfile.gettempdir() + "/" + filename, "rb")
            if sys.platform.startswith('darwin') or sys.platform.startswith(
                    'Linux'):
                self.file_map = mmap.mmap(file.fileno(), 0, mmap.PROT_READ,
                                          mmap.ACCESS_READ)
            else:
                self.file_map = mmap.mmap(file.fileno(), 0, None,
                                          mmap.ACCESS_READ)
            file.close()

            self.file_map.seek(0)
            buf = self.file_map.read(640 * 480 * 4)

            self.image_texture.source = texture.ImageBuff()
            self.image_texture.source.filter = texture.FilterRGBA32()
            self.image_texture.source.load(buf, 640, 480)

            if feed_2:
                print("Delicode NI mate Tools replacing " + image_name +
                      " with live feed 2")
            else:
                print("Delicode NI mate Tools replacing " + image_name +
                      " with live feed 1")
        except Exception as e:
            print("Delicode NI mate Tools Error: Couldn't open NI mate feed " +
                  tempfile.gettempdir() + "/" + filename)
            print("Reason: %s" % e)
            self.file_map = None
            pass
Пример #3
0
def run(controller):
    """
    Run, run once every frames
    """
    if controller.sensors[0].positive:
        try:
            buff_size = 4096
            msg_raw = logic.socket.recv(buff_size)

            # check for header msg indicating the number of packet that'll follow
            # containing a unique frame (image)
            if len(msg_raw) == 4:
                nb_of_packet_per_frame = msg_raw[0]

                # loop over next packets to reconstitute image
                frame_raw = b''
                for i in range(nb_of_packet_per_frame):
                    frame_raw = frame_raw + logic.socket.recv(buff_size)

                # frame = cv2.imdecode(numpy.fromstring(frame_raw, dtype=numpy.uint8), cv2.IMREAD_COLOR)
                frame = cv2.imdecode(
                    numpy.fromstring(frame_raw, dtype=numpy.uint8),
                    cv2.IMREAD_GRAYSCALE)

                if not frame is None:

                    width = frame.shape[1]
                    height = frame.shape[0]

                    l = frame.tolist()
                    lll = list(itertools.chain(*l))

                    # image_buffer = bgl.Buffer(bgl.GL_INT, [width*height*3], lll)
                    image_buffer = bgl.Buffer(bgl.GL_BYTE, width * height, lll)

                    source = texture.ImageBuff()

                    # Apply a filter, that way source.load does not except a 3(RGB) pixel image
                    source.filter = texture.FilterBlueScreen()
                    source.load(image_buffer, width, height)

                    logic.texture.source = source
                    logic.texture.refresh(False)

        except socket.timeout:
            pass
Пример #4
0
    def __init__(self, image_name, sensor_num, feed_2):
        import sys
        import bge
        from bge import texture
        import tempfile

        cont = bge.logic.getCurrentController()
        own = cont.owner
        img_w = 0
        img_h = 0

        try:
            matID = texture.materialID(own, image_name)
        except:
            print("Delicode NI mate Tools Error: No texture with name: " + image_name)
            pass

        try:
            self.image_texture = texture.Texture(own, matID)

            filename = "NI_mate_shared_map"

            if feed_2:
                filename = filename + str(2*(sensor_num-1)+2)
            else:
                filename = filename + str(2*(sensor_num-1)+1)

            filename = filename + ".data"

            file = open(tempfile.gettempdir() + "/" + filename, "rb")
            if sys.platform.startswith('darwin') or sys.platform.startswith('Linux'):
                self.file_map = mmap.mmap(file.fileno(), 0, mmap.PROT_READ, mmap.ACCESS_READ)
            else:
                self.file_map = mmap.mmap(file.fileno(), 0, None, mmap.ACCESS_READ)
            file.close()

            self.file_map.seek(0)

            w = 0
            h = 0

            if self.file_map.size() == 640*480*4:
                w = 640
                h = 480
            elif self.file_map.size() == 320*240*4:
                w = 320
                h = 240
            elif self.file_map.size() == 160*120*4:
                w = 160
                h = 120

            if w > 0 and h > 0:
                buf = self.file_map.read(w*h*4)

                self.image_texture.source = texture.ImageBuff()
                self.image_texture.source.filter = texture.FilterRGBA32()
                self.image_texture.source.load(buf, w, h)

                self.img_w = w
                self.img_h = h

            if feed_2:
                print("Delicode NI mate Tools replacing " + image_name + " with sensor " + str(sensor_num) + " live feed 2")
            else:
                print("Delicode NI mate Tools replacing " + image_name + " with sensor " + str(sensor_num) + " live feed 1")
        except Exception as e:
            print("Delicode NI mate Tools Error: Couldn't open NI mate feed " + tempfile.gettempdir() + "/" + filename)
            print("Reason: %s" % e)
            self.file_map = None
            pass