Exemple #1
0
    def createSender(self,
                     name='output',
                     type=GL_RGB,
                     dataType=GL_UNSIGNED_BYTE,
                     id=0):
        """
        Initialize spout sender
        Args:
            name: receiver name, default = 'output'
            type: texture type, default = GL_RGB, available = GL_RGBA, GL_RGB, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA
            dataType: texture data type, default = GL_UNSIGNED_BYTE, available = GL_UNSIGNED_BYTE, GL_FLOAT
            id: id of sender if want multiple, default = 0
        """

        self.senderName[id] = name
        self.senderWidth[id] = 0
        self.senderHeight[id] = 0
        self.senderType[id] = type
        self.senderDataType[id] = dataType
        # init spout sender

        self.spoutSender[id] = SpoutSDK.SpoutSender()
        # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
        self.spoutSender[id].CreateSender(self.senderName[id], self.width,
                                          self.height, 0)
        # create textures for spout receiver and spout sender
        self.textureSendID[id] = glGenTextures(1)
Exemple #2
0
 def _init_Spout(self):
     '''
     Initalize a Spout receiver using Python Bindings for Spout C++ SDK. 
     Source code from: https://github.com/spiraltechnica/Spout-for-Python
     '''
     # create spout receiver
     self._spout_receiver = SpoutSDK.SpoutReceiver()
 
     # Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
     self._spout_receiver.pyCreateReceiver(self._spout_name,
                                           self._spout_size[0],
                                           self._spout_size[1],
                                           False
     )
Exemple #3
0
 def createChannel(self, width, height, name):
     self.bufferCnt += 1
     self.width[name], self.height[name] = width, height
     self.spoutSender[name] = SpoutSDK.SpoutSender()  # init spout sender
     self.spoutSender[name].CreateSender(
         name, self.width[name], self.height[name], 0
     )  # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
     self.senderTextureID[name] = glGenTextures(
         1)  # init spout sender texture ID
     try:
         self.senderTextureID[name] = self.senderTextureID[name][-1]
     except:
         pass
     # initialise texture
     glBindTexture(GL_TEXTURE_2D, self.senderTextureID[name])
Exemple #4
0
    def createReceiver(self,
                       name='input',
                       type=GL_RGB,
                       dataType=GL_UNSIGNED_BYTE,
                       id=0):
        """
        Initialize spout receiver
        Args:
            name: receiver name, default = 'input'
            type: texture type, default = GL_RGB, available = GL_RGBA, GL_RGB, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA
            dataType: texture data type, default = GL_UNSIGNED_BYTE, available = GL_UNSIGNED_BYTE, GL_FLOAT
            id: id of receiver if want multiple, default = 0
        """

        self.receiverName[id] = name
        self.receiverType[id] = type
        self.receiverDataType[id] = dataType

        # init spout receiver
        self.spoutReceiver[id] = SpoutSDK.SpoutReceiver()

        self.receiverWidth[id] = self.spoutReceiver[id].GetWidth(
            self.receiverName[id])
        self.receiverHeight[id] = self.spoutReceiver[id].GetHeight(
            self.receiverName[id])

        # create spout receiver
        # Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
        self.spoutReceiver[id].pyCreateReceiver(self.receiverName[id],
                                                self.receiverWidth[id],
                                                self.receiverHeight[id], False)
        # create textures for spout receiver and spout sender
        self.textureReceiveID[id] = glGenTextures(1)

        # initalise receiver texture
        glBindTexture(GL_TEXTURE_2D, self.textureReceiveID[id])
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

        # copy data into texture
        glTexImage2D(GL_TEXTURE_2D, 0, self.receiverType[id],
                     self.receiverWidth[id], self.receiverHeight[id], 0,
                     self.receiverType[id], self.receiverDataType[id], None)
        glBindTexture(GL_TEXTURE_2D, 0)

        return True
def texshare_main(self, context):
    global db_drawHandle
    global db_spoutInstances

    # my database ID
    dbID = context.camera.texshare.dbID

    # if streaming has been enabled and no id has yet been stored in the db
    if context.camera.texshare.enable == 1 and dbID not in db_drawHandle:
        # first we create a unique identifier for the reference db dicts
        dbID = str(uuid.uuid1())

        dWIDTH = context.camera.texshare.capture_width
        dHEIGHT = context.camera.texshare.capture_height

        # create a new spout sender instance
        spoutSender = SpoutSDK.SpoutSender()
        spoutSender.CreateSender(context.camera.name, dWIDTH, dHEIGHT, 0)

        # create a off screen renderer
        offscreen = gpu.types.GPUOffScreen(dWIDTH, dHEIGHT)

        # collect all the arguments to pass to the draw handler
        args = (self, context, context.camera, context.object, offscreen,
                spoutSender)

        # instantiate the draw handler,
        # using the texshare_capture function defined above
        drawhandle = bpy.types.SpaceView3D.draw_handler_add(
            texshare_capture, args, 'WINDOW', 'POST_PIXEL')

        # store the references inside the db-dicts
        db_drawHandle[dbID] = drawhandle
        db_spoutInstances[dbID] = spoutSender

    # if streaming has been disabled and my ID is still stored in the db
    if context.camera.texshare.enable == 0 and dbID in db_drawHandle:
        bpy.types.SpaceView3D.draw_handler_remove(db_drawHandle[dbID],
                                                  'WINDOW')
        db_spoutInstances[dbID].ReleaseSender(0)
        #removing my ID
        db_drawHandle.pop(dbID, None)
        dbID == "off"

    # store the database ID again inside the settings
    context.camera.texshare.dbID = dbID
Exemple #6
0
def main():
    # display = (width, height) # window details
    # pygame.init() # window setup
    # pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    glutInit()
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
    glutInitWindowSize(width, height)
    glutCreateWindow(b"OpenGL Offscreen")
    glutHideWindow()

    spoutSender = SpoutSDK.SpoutSender()  # init spout sender
    spoutSender.CreateSender(
        'Spout Python Sender', spoutSenderWidth, spoutSenderHeight, 0
    )  # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
    senderTextureID = glGenTextures(1)  # init spout sender texture ID

    # initialise texture
    glBindTexture(GL_TEXTURE_2D, senderTextureID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    # fill texture with blank data
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, spoutSenderWidth,
                     spoutSenderHeight, 0)
    glBindTexture(GL_TEXTURE_2D, 0)

    while True:
        frame = cap.read()[1]
        # Copy the frame from the opencv into the sender texture
        glBindTexture(GL_TEXTURE_2D, senderTextureID)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, spoutSenderWidth,
                     spoutSenderHeight, 0, GL_BGR, GL_UNSIGNED_BYTE,
                     cv2.flip(frame, 0))

        # send texture to Spout
        # Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
        spoutSender.SendTexture(np.int(senderTextureID), GL_TEXTURE_2D,
                                spoutSenderWidth, spoutSenderHeight, True, 0)
Exemple #7
0
def main():

    # parse arguments
    args = parse_args()
    # window details
    width = args.spout_size[0] 
    height = args.spout_size[1] 
    display = (width,height)
    
    req_type = args.type
    receiverName = args.spout_input_name 
    senderName = args.spout_output_name
    silent = args.silent
    
    # window setup
    pygame.init() 
    pygame.display.set_caption(senderName)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,width,height,0,1,-1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0,0.0,0.0,0.0)
    glEnable(GL_TEXTURE_2D)

    if req_type == 'input' or req_type == 'input-output':
        # init spout receiver
        spoutReceiverWidth = width
        spoutReceiverHeight = height
        # create spout receiver
        spoutReceiver = SpoutSDK.SpoutReceiver()
	    # Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
        spoutReceiver.pyCreateReceiver(receiverName,spoutReceiverWidth,spoutReceiverHeight, False)
        # create textures for spout receiver and spout sender 
        textureReceiveID = glGenTextures(1)
        
        # initalise receiver texture
        glBindTexture(GL_TEXTURE_2D, textureReceiveID)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

        # copy data into texture
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spoutReceiverWidth, spoutReceiverHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, None ) 
        glBindTexture(GL_TEXTURE_2D, 0)

    if req_type == 'output' or req_type == 'input-output':
        # init spout sender
        spoutSender = SpoutSDK.SpoutSender()
        spoutSenderWidth = width
        spoutSenderHeight = height
	    # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
        spoutSender.CreateSender(senderName, spoutSenderWidth, spoutSenderHeight, 0)
        # create textures for spout receiver and spout sender 
    textureSendID = glGenTextures(1)

    # loop for graph frame by frame
    while(True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                spoutReceiver.ReleaseReceiver()
                pygame.quit()
                quit()
        
        if req_type == 'input' or req_type == 'input-output':
            # receive texture
            # Its signature in c++ looks like this: bool pyReceiveTexture(const char* theName, unsigned int theWidth, unsigned int theHeight, GLuint TextureID, GLuint TextureTarget, bool bInvert, GLuint HostFBO);
            spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth, spoutReceiverHeight, textureReceiveID, GL_TEXTURE_2D, False, 0)
        
            glBindTexture(GL_TEXTURE_2D, textureReceiveID)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
            # copy pixel byte array from received texture   
            data = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, outputType=None)  #Using GL_RGB can use GL_RGBA 
            glBindTexture(GL_TEXTURE_2D, 0)
            # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
            data.shape = (data.shape[1], data.shape[0], data.shape[2])
        else:
            data = np.ones((width,height,3))*255
        
        # call our main function
        output = main_pipeline(data)
        
        # setup the texture so we can load the output into it
        glBindTexture(GL_TEXTURE_2D, textureSendID);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        # copy output into texture
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, output )
            
        # setup window to draw to screen
        glActiveTexture(GL_TEXTURE0)
        # clean start
        glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT )
        # reset drawing perspective
        glLoadIdentity()
        # draw texture on screen
        glBegin(GL_QUADS)

        glTexCoord(0,0)        
        glVertex2f(0,0)

        glTexCoord(1,0)
        glVertex2f(width,0)

        glTexCoord(1,1)
        glVertex2f(width,height)

        glTexCoord(0,1)
        glVertex2f(0,height)

        glEnd()
        
        if silent:
            pygame.display.iconify()
                
        # update window
        pygame.display.flip()        

        if req_type == 'output' or req_type == 'input-output':
            # Send texture to spout...
            # Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
            spoutSender.SendTexture(textureSendID, GL_TEXTURE_2D, spoutSenderWidth, spoutSenderHeight, False, 0)
Exemple #8
0
def main():

    # กำหนด Callback ของ MQTT
    print("Init MQTT")
    CLIENT.on_connect	= MyMqttOnConnect
    CLIENT.on_disconnect	= MyMqttOnDisconnect
    CLIENT.username_pw_set(THINGSBOARD_TOKEN)
    CLIENT.loop_start()

    # เริ่มเชื่อมต่อ Thingsboard ด้วย MQTT
    print("Connecting to thingsboard ...")
    CLIENT.connect_async(THINGSBOARD_HOST, THINGSBOARD_MQTT_PORT)
    while IsMqttConnect == False:
        time.sleep(0.5)

    # เปิดใช้งาน Serial Port
    print("Init Serial Port ...")
    port = serial.Serial(port='COM2', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=0, xonxoff=0, rtscts=0, dsrdtr=0)

    # window details
    display = (width, height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout Receiver (OpenGL)')
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,width,height,0,1,-1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0,0.0,0.0,0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout receiver
    receiverName = "Station.Vision"
    spoutReceiverWidth = width
    spoutReceiverHeight = height
    # create spout receiver
    spoutReceiver = SpoutSDK.SpoutReceiver()

	# Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
    spoutReceiver.pyCreateReceiver(receiverName,spoutReceiverWidth,spoutReceiverHeight, False)

    # create texture for spout receiver
    textureReceiveID = glGenTextures(1)

    # initalise receiver texture
    glBindTexture(GL_TEXTURE_2D, textureReceiveID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

    # copy data into texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spoutReceiverWidth, spoutReceiverHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, None )
    glBindTexture(GL_TEXTURE_2D, 0)


    # สร้าง GUI สำหรับปรับค่าการตรวจจับสี
    # settings
    cv2.namedWindow("Settings", cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
    cv2.createTrackbar("lower", "Settings", 50, 180, MyNothing)
    cv2.createTrackbar("upper", "Settings", 88, 180, MyNothing)

    # loop for graph frame by frame
    while(True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                CLIENT.loop_stop()
                CLIENT.disconnect()
                spoutReceiver.ReleaseReceiver()
                pygame.quit()
                quit(0)

        # receive texture
        # Its signature in c++ looks like this: bool pyReceiveTexture(const char* theName, unsigned int theWidth, unsigned int theHeight, GLuint TextureID, GLuint TextureTarget, bool bInvert, GLuint HostFBO);
        spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth, spoutReceiverHeight, int(textureReceiveID), GL_TEXTURE_2D, False, 0)

        glBindTexture(GL_TEXTURE_2D, textureReceiveID)

        # copy pixel byte array from received texture - this example doesn't use it, but may be useful for those who do want pixel info
        # แปลงภาพ Texture ใน OpenGL เป็นข้อมูลภาพของ OpenCV
        img_opencv = glGetTexImage(GL_TEXTURE_2D, 0, GL_BGR, GL_UNSIGNED_BYTE, outputType=None)  #Using GL_RGB can use GL_RGBA

        # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
        img_opencv.shape = (img_opencv.shape[1], img_opencv.shape[0], img_opencv.shape[2])

        # setup window to draw to screen
        glActiveTexture(GL_TEXTURE0)

        # clean start
        glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT )
        # reset drawing perspective
        glLoadIdentity()

        # draw texture on screen
        # glPushMatrix() use these lines if you want to scale your received texture
        # glScale(0.3, 0.3, 0.3)
        glBegin(GL_QUADS)

        glTexCoord(0,0)
        glVertex2f(0,0)

        glTexCoord(1,0)
        glVertex2f(spoutReceiverWidth,0)

        glTexCoord(1,1)
        glVertex2f(spoutReceiverWidth,spoutReceiverHeight)

        glTexCoord(0,1)
        glVertex2f(0,spoutReceiverHeight)

        glEnd()
        # glPopMatrix() make sure to pop your matrix if you're doing a scale
        # update window
        pygame.display.flip()


        ### เริ่มต้นประมวลผลภาพด้วย OpenCV ###

        # สั่งให้ Slave อ่านค่าจาก Sensor
        print("Request Reading ...")
        port.write(b'0')

        # รอการตอบกลับจาก Slave
        print("Waiting for Result from Slave ...")
        while port.inWaiting() == 0:
            time.sleep(0.001)

        # มีข้อมูลตอบกลับจาก Slave
        data    = b''
        while port.inWaiting() > 0:
            data = port.read(port.inWaiting())
            print("Result=" + str(data))

            # ตรวจสอบข้อมูลจาก Sensor ว่าตรวจพบวัตถุหรือไม่
            if data == b'1':    # พบวัตถุ

                # ตัดภาพบริเวรที่สนใจ (ตรงกลาง 50x50)
                px  = int((width / 2) - 25) - 100
                py  = int((height / 2) - 25)
                img_crop   = img_opencv[py:py + 50, px:px + 50, :]
                cv2.imshow("CROP", img_crop)

                # เฉลี่ยนค่าสีจากรูปที่ผ่านการ Filter
                color_avg_current   = MyAverageValueInHSV(img_crop)
                print("color avg={}".format(color_avg_current))

                # ตรวจสอบค่าของสีว่าตรงตามเงื่อนใขหรือไม่
                color_blue = color_avg_current >= 120-20 and color_avg_current <= 120+20
                if color_blue :
                    # ค่าสีตรงตามเงื่อนใข
                    print("Pass")
                    # ข้อมูลสำหรับส่งไปยัง Thingsboard อยู่ในรูปแบบ JSON
                    # ส่งข้อมูลไปยัง Thingsboard ด้วย MQTT
                    CLIENT.publish("v1/devices/me/telemetry", str("{detection:1, rejection:0, color:" + str(color_avg_current) + "}"))

                else:
                    # ค่าสีไม่ตรงตามเงื่อนใข
                    print("Reject")
                    time.sleep(0.1)
                    # สั่งให้ Slave ควบคุม Actuator มีสถานะ Active
                    port.write(b'2')
                    time.sleep(0.5)
                    # สั่งให้ Slave ควบคุม Actuator มีสถานะ Inactive
                    port.write(b'1')
                    time.sleep(0.5)

                    # ข้อมูลสำหรับส่งไปยัง Thingsboard อยู่ในรูปแบบ JSON
                    # ส่งข้อมูลไปยัง Thingsboard ด้วย MQTT
                    CLIENT.publish("v1/devices/me/telemetry", str("{detection:1, rejection:1, color:" + str(color_avg_current) + "}"))

            else:   # ไม่พบ
                # ข้อมูลสำหรับส่งไปยัง Thingsboard อยู่ในรูปแบบ JSON
                # ส่งข้อมูลไปยัง Thingsboard ด้วย MQTT
                CLIENT.publish("v1/devices/me/telemetry", str("{detection:0}"))

        # แสดงภาพ
        cv2.imshow("OpenCV", img_opencv)

        print("")

        if (cv2.waitKey(1) == 27):
            CLIENT.loop_stop()
            CLIENT.disconnect()
            spoutReceiver.ReleaseReceiver()
            pygame.quit()
            quit(0)
def main():

    # window details
    # ตั้งค่าขนาดของ Window ให้ตรงกับ Resolution ของ Plant Simulation
    width = 1600
    height = 900
    display = (width, height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout Receiver (OpenGL)')
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,width,height,0,1,-1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0,0.0,0.0,0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout receiver
    receiverName = "Station.Vision"
    spoutReceiverWidth = width
    spoutReceiverHeight = height
    # create spout receiver
    spoutReceiver = SpoutSDK.SpoutReceiver()

	# Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
    spoutReceiver.pyCreateReceiver(receiverName,spoutReceiverWidth,spoutReceiverHeight, False)

    # create texture for spout receiver
    textureReceiveID = glGenTextures(1)

    # initalise receiver texture
    glBindTexture(GL_TEXTURE_2D, textureReceiveID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

    # copy data into texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spoutReceiverWidth, spoutReceiverHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, None )
    glBindTexture(GL_TEXTURE_2D, 0)

    # loop for graph frame by frame
    while(True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                spoutReceiver.ReleaseReceiver()
                pygame.quit()
                quit(0)

        # receive texture
        # Its signature in c++ looks like this: bool pyReceiveTexture(const char* theName, unsigned int theWidth, unsigned int theHeight, GLuint TextureID, GLuint TextureTarget, bool bInvert, GLuint HostFBO);
        spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth, spoutReceiverHeight, int(textureReceiveID), GL_TEXTURE_2D, False, 0)

        glBindTexture(GL_TEXTURE_2D, textureReceiveID)

        # copy pixel byte array from received texture - this example doesn't use it, but may be useful for those who do want pixel info
        # แปลงภาพ Texture ใน OpenGL เป็นข้อมูลภาพของ OpenCV
        img_opencv = glGetTexImage(GL_TEXTURE_2D, 0, GL_BGR, GL_UNSIGNED_BYTE, outputType=None)  #Using GL_RGB can use GL_RGBA

        # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
        img_opencv.shape = (img_opencv.shape[1], img_opencv.shape[0], img_opencv.shape[2])

        # setup window to draw to screen
        glActiveTexture(GL_TEXTURE0)

        # clean start
        glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT )
        # reset drawing perspective
        glLoadIdentity()

        # draw texture on screen
        # glPushMatrix() use these lines if you want to scale your received texture
        # glScale(0.3, 0.3, 0.3)
        glBegin(GL_QUADS)

        glTexCoord(0,0)
        glVertex2f(0,0)

        glTexCoord(1,0)
        glVertex2f(spoutReceiverWidth,0)

        glTexCoord(1,1)
        glVertex2f(spoutReceiverWidth,spoutReceiverHeight)

        glTexCoord(0,1)
        glVertex2f(0,spoutReceiverHeight)

        glEnd()
        # glPopMatrix() make sure to pop your matrix if you're doing a scale
        # update window
        pygame.display.flip()


        ### เริ่มต้นประมวลผลภาพด้วย OpenCV ###

        # แสดงภาพ
        cv2.imshow("OpenCV", img_opencv)

        if (cv2.waitKey(1) == 27):
            spoutReceiver.ReleaseReceiver()
            pygame.quit()
            quit(0)
def main():

    # parse arguments
    args = parse_args()

    # window details
    width = args.camSize[0]
    height = args.camSize[1]
    display = (width, height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout for Python Webcam Sender Example')
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)

    # init capture & set
    '''
    cap = cv2.VideoCapture(args.camID)
    cap.set(3, width)
    cap.set(4, height)
    '''

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glOrtho(0, width, height, 0, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout sender
    spoutSender = SpoutSDK.SpoutSender()
    spoutSenderWidth = width
    spoutSenderHeight = height
    # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
    #spoutSender.CreateSender('Spout for Python Webcam Sender Example', width, height, 0)
    spoutSender.CreateSender('SpoutPy', width, height, 0)

    # create texture id for use with Spout
    senderTextureID = glGenTextures(1)

    # initalise our sender texture
    glBindTexture(GL_TEXTURE_2D, senderTextureID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glBindTexture(GL_TEXTURE_2D, 0)

    #---------------------------------
    #predict
    path = os.getcwd()
    print('------------------')
    modelPath = 'gan-generateFace01.h5'
    model = load_model(modelPath, compile=False)
    z_dim = 100
    model._make_predict_function()

    start = np.random.normal(0, 1, (1, z_dim))
    end = np.random.normal(0, 1, (1, z_dim))
    v = end - start
    gen_imgs = model.predict(start)
    #gen_imgs = 0.5 * gen_imgs + 0.5
    cnt = 0
    #---------------------------------
    from socket import socket, AF_INET, SOCK_DGRAM
    import struct

    HOST = ''
    PORT = 7000
    s = socket(AF_INET, SOCK_DGRAM)
    s.bind((HOST, PORT))
    val = 0.0

    #---------------------------------
    # loop
    while (True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        #cal-predict
        if cnt > 35:
            start = np.random.normal(0, 1, (1, z_dim))
            end = np.random.normal(0, 1, (1, z_dim))
            v = end - start
            v *= 0.01
            cnt = 0

        gen_imgs = model.predict(start)
        gen_imgs = 0.5 * gen_imgs + 0.5
        target = gen_imgs.reshape(250, 250, 3)

        #rgb_gen_imgs = target
        #rgb_gen_imgs = rgb_gen_imgs.astype('uint8')
        #rgb_gen_imgs = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
        #print('rgb imgs :  '+ str(rgb_gen_imgs.shape))

        #cv2.cvtColor(rgb_gen_imgs, target, cv2.COLOR_BGR2RGB)

        frame = np.zeros((250, 250, 3), dtype="float16")
        #print('shape : ' + str(frame.shape))
        frame = target
        cv2.imwrite("faceTest.png", frame)
        msg, address = s.recvfrom(8192)
        val = msg.decode()
        val = (float)(val.replace('\0', ''))
        start += val * 0.01
        cnt += 1
        '''
        print('shape : ' + str(frame.shape))
        print('shape : ' + str(frame[:, :, 0].size))
        print('shape : ' + str(frame[:, :, 1].size))
        print('shape : ' + str(frame[:, :, 2].size))
        '''

        #-----------------------------------------------

        #camera
        '''
        ret, frame = cap.read()
        print('frame-shape' + str(frame.shape))
        #frame = cv2.imread('../Images/2500.png')
        frame = cv2.flip(frame, 1 )
        print('shape : ' + str(frame.shape))
        print('shape : ' + str(frame[:, :, 0].size))
        print('shape : ' + str(frame[:, :, 1].size))
        print('shape : ' + str(frame[:, :, 2].size))
        '''
        #-----------------------------------------------

        # Copy the frame from the webcam into the sender texture
        glBindTexture(GL_TEXTURE_2D, senderTextureID)

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
                     GL_FLOAT, frame)

        # Send texture to Spout
        # Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
        spoutSender.SendTexture(senderTextureID, GL_TEXTURE_2D,
                                spoutSenderWidth, spoutSenderHeight, False, 0)

        # Clear screen
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        # reset the drawing perspective
        glLoadIdentity()

        # Draw texture to screen

        glBegin(GL_QUADS)

        glTexCoord(0, 0)
        glVertex2f(0.0, 0)

        glTexCoord(1, 0)
        glVertex2f(width, 0)

        glTexCoord(1, 1)
        glVertex2f(width, height)

        glTexCoord(0.0, 1)
        glVertex2f(0, height)

        glEnd()

        # update window
        pygame.display.flip()

        # unbind our sender texture
        glBindTexture(GL_TEXTURE_2D, 0)
Exemple #11
0
def main():

    # window details
    # ตั้งค่าขนาดของ Window ให้ตรงกับ Resolution ของ Plant Simulation
    width = 1600
    height = 900
    display = (width, height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout Receiver (OpenGL)')
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, width, height, 0, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout receiver
    receiverName = "Station.Vision"
    spoutReceiverWidth = width
    spoutReceiverHeight = height
    # create spout receiver
    spoutReceiver = SpoutSDK.SpoutReceiver()

    # Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
    spoutReceiver.pyCreateReceiver(receiverName, spoutReceiverWidth,
                                   spoutReceiverHeight, False)

    # create texture for spout receiver
    textureReceiveID = glGenTextures(1)

    # initalise receiver texture
    glBindTexture(GL_TEXTURE_2D, textureReceiveID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

    # copy data into texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spoutReceiverWidth,
                 spoutReceiverHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
    glBindTexture(GL_TEXTURE_2D, 0)

    # สร้าง GUI สำหรับปรับค่าการตรวจจับสี
    # settings
    cv2.namedWindow("Settings", cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
    cv2.createTrackbar("lower", "Settings", 50, 180, MyNothing)
    cv2.createTrackbar("upper", "Settings", 88, 180, MyNothing)

    # loop for graph frame by frame
    while (True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                spoutReceiver.ReleaseReceiver()
                pygame.quit()
                quit(0)

        # receive texture
        # Its signature in c++ looks like this: bool pyReceiveTexture(const char* theName, unsigned int theWidth, unsigned int theHeight, GLuint TextureID, GLuint TextureTarget, bool bInvert, GLuint HostFBO);
        spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth,
                                       spoutReceiverHeight,
                                       int(textureReceiveID), GL_TEXTURE_2D,
                                       False, 0)

        glBindTexture(GL_TEXTURE_2D, textureReceiveID)

        # copy pixel byte array from received texture - this example doesn't use it, but may be useful for those who do want pixel info
        # แปลงภาพ Texture ใน OpenGL เป็นข้อมูลภาพของ OpenCV
        img_opencv = glGetTexImage(
            GL_TEXTURE_2D, 0, GL_BGR, GL_UNSIGNED_BYTE,
            outputType=None)  #Using GL_RGB can use GL_RGBA

        # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
        img_opencv.shape = (img_opencv.shape[1], img_opencv.shape[0],
                            img_opencv.shape[2])

        # setup window to draw to screen
        glActiveTexture(GL_TEXTURE0)

        # clean start
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        # reset drawing perspective
        glLoadIdentity()

        # draw texture on screen
        # glPushMatrix() use these lines if you want to scale your received texture
        # glScale(0.3, 0.3, 0.3)
        glBegin(GL_QUADS)

        glTexCoord(0, 0)
        glVertex2f(0, 0)

        glTexCoord(1, 0)
        glVertex2f(spoutReceiverWidth, 0)

        glTexCoord(1, 1)
        glVertex2f(spoutReceiverWidth, spoutReceiverHeight)

        glTexCoord(0, 1)
        glVertex2f(0, spoutReceiverHeight)

        glEnd()
        # glPopMatrix() make sure to pop your matrix if you're doing a scale
        # update window
        pygame.display.flip()

        ### เริ่มต้นประมวลผลภาพด้วย OpenCV ###

        # ตัดภาพบริเวรที่สนใจ (ตรงกลาง 100x100)
        px = int((width / 2) - 50)
        py = int((height / 2) - 50)
        img_crop = img_opencv[py:py + 100, px:px + 100, :]

        # เฉลี่ยนค่าสีจากรูปที่ผ่านการ Filter
        color_avg_current = MyAverageValueInHSV(img_crop)
        print("color avg={}".format(color_avg_current))

        # Filter เฉพาะสีที่ต้องการ
        img_filter = MyFilterColor(img_opencv)

        # แสดงภาพ
        cv2.imshow("OpenCV", img_opencv)
        cv2.imshow("Filter", img_filter)
        cv2.imshow("CROP", img_crop)

        if (cv2.waitKey(1) == 27):
            spoutReceiver.ReleaseReceiver()
            pygame.quit()
            quit(0)
Exemple #12
0
def main():
    args = parse_args()

    width = args.windows_size[0]
    height = args.windows_size[1]
    display = (width, height)

    pygame.init()
    pygame.display.set_caption('Style transfer and Spout for Python')
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE,8)

    cap = cv2.VideoCapture(0)
    cap.set(3,width)
    cap.set(4,height)

    glMatrixMode(GL_PROJECTION)
    glOrtho(0,width,height,0,1,-1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0,0.0,0.0,0.0)
    glEnable(GL_TEXTURE_2D)
   
    spoutSender = SpoutSDK.SpoutSender()
    spoutSenderWidth = width
    spoutSenderHeight = height
    spoutSender.CreateSender('Spout for Python Webcam Sender Example', width, height, 0)
    
    senderTextureID = glGenTextures(1)

    glBindTexture(GL_TEXTURE_2D, senderTextureID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glBindTexture(GL_TEXTURE_2D, 0)

    device_t='/gpu:0'
    g = tf.Graph()
    soft_config = tf.ConfigProto(allow_soft_placement=True)
    soft_config.gpu_options.allow_growth = True

    with g.as_default(), g.device(device_t), tf.Session(config=soft_config) as sess:
        img_shape = (height, width, 3)
        batch_shape = (1,) + img_shape
        style = tf.placeholder(tf.float32, shape = batch_shape, name = 'input')

        preds = transform.net(style)
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        try:
            saver.restore(sess, args.style_model)
            #style = cv2.imread(args.style_model)
        except:
            print("checkpoint %s not loaded correctly" % args.style_model)

        while(True):
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    spoutReceiver.ReleaseReceiver()
                    pygame.quit()
                    quit()
            ret, frame = cap.read()
            frame = cv2.flip(frame, 1)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            
            glBindTexture(GL_TEXTURE_2D, senderTextureID)
            glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, frame )
            data = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, outputType = None)
            glBindTexture(GL_TEXTURE_2D, 0)        
            data.shape = (data.shape[1], data.shape[0], data.shape[2])

            X = np.zeros(batch_shape, dtype=np.float32)
            X[0] = frame 
            output = sess.run(preds, feed_dict={style:X})
            output = output[:,:,:, [2,1,0]].reshape(img_shape)
            output = np.clip(output, 0.0, 255.0)
            output = output.astype(np.uint8)
            glBindTexture(GL_TEXTURE_2D, senderTextureID)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
            # copy output into texture
            glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, spoutSenderWidth, spoutSenderHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, output)

            spoutSender.SendTexture(senderTextureID, GL_TEXTURE_2D, spoutSenderWidth, spoutSenderHeight, False, 0)
            # Clear screen
            glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT )
            """# reset the drawing perspective
            glLoadIdentity()
        
            # Draw texture to screen
            glBegin(GL_QUADS)

            glTexCoord(0,0)        
            glVertex2f(0,0)

            glTexCoord(1,0)
            glVertex2f(width,0)

            glTexCoord(1,1)
            glVertex2f(width,height)

            glTexCoord(0,1)
            glVertex2f(0,height)

            glEnd()

            # update window
            pygame.display.flip()          """   
        
            # unbind our sender texture
            glBindTexture(GL_TEXTURE_2D, 0)
def main():

    # parse arguments
    args = parse_args()

    # window details
    width = args.camSize[0]
    height = args.camSize[1]
    display = (width, height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout for Python Webcam Sender Example')
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)

    # init capture & set size
    cap = cv2.VideoCapture(args.camID)
    cap.set(3, width)
    cap.set(4, height)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glOrtho(0, width, height, 0, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout sender
    spoutSender = SpoutSDK.SpoutSender()
    spoutSenderWidth = width
    spoutSenderHeight = height
    # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
    #spoutSender.CreateSender('Spout for Python Webcam Sender Example', width, height, 0)
    spoutSender.CreateSender('SpoutPy', width, height, 0)

    # create texture id for use with Spout
    senderTextureID = glGenTextures(1)

    # initalise our sender texture
    glBindTexture(GL_TEXTURE_2D, senderTextureID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glBindTexture(GL_TEXTURE_2D, 0)

    #---------------------------------
    #predict
    path = os.getcwd()
    print('------------------')
    #print(path)
    #print(os.path.exists('gan_generator_50.h5'))
    model = load_model('gan_generator_50.h5', compile=False)
    z_dim = 50
    noise = np.random.normal(0, 1, (1, z_dim))
    model._make_predict_function()
    #gen_imgs = model.predict(noise)
    #gen_imgs = 0.5 * gen_imgs + 0.5

    start = np.random.normal(0, 1, (1, z_dim))
    end = np.random.normal(0, 1, (1, z_dim))
    v = end - start
    cnt = 0
    #---------------------------------
    from socket import socket, AF_INET, SOCK_DGRAM
    import struct

    HOST = ''
    PORT = 5002
    s = socket(AF_INET, SOCK_DGRAM)
    s.bind((HOST, PORT))
    val = 1.0

    #---------------------------------
    # loop
    while (True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        #cal-predict
        ret, frame = cap.read()

        msg, address = s.recvfrom(8192)
        #val = msg.decode()

        if cnt > 35:
            start = np.random.normal(0, 1, (1, z_dim))
            end = np.random.normal(0, 1, (1, z_dim))
            v = end - start
            v *= 0.01 * val
            cnt = 0

        gen_imgs = model.predict(start)
        gen_imgs = 0.5 * gen_imgs + 0.5
        target = gen_imgs.reshape(1, 28, 28, 1)[0]
        rgb_gen_imgs = cv2.cvtColor(target, cv2.COLOR_GRAY2RGB)
        rgb_gen_imgs = cv2.resize(rgb_gen_imgs, None, fx=10, fy=10)
        frame = rgb_gen_imgs * 255

        start += v * 10.0
        cnt += 1

        # Copy the frame from the webcam into the sender texture
        glBindTexture(GL_TEXTURE_2D, senderTextureID)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
                     GL_UNSIGNED_BYTE, frame)

        # Send texture to Spout
        # Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
        spoutSender.SendTexture(senderTextureID, GL_TEXTURE_2D,
                                spoutSenderWidth, spoutSenderHeight, False, 0)

        # Clear screen
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        # reset the drawing perspective
        glLoadIdentity()

        # Draw texture to screen

        glBegin(GL_QUADS)

        glTexCoord(0, 0)
        glVertex2f(0, 0)

        glTexCoord(1, 0)
        glVertex2f(width, 0)

        glTexCoord(1, 1)
        glVertex2f(width, height)

        glTexCoord(0, 1)
        glVertex2f(0, height)

        glEnd()

        # update window
        pygame.display.flip()

        # unbind our sender texture
        glBindTexture(GL_TEXTURE_2D, 0)
def main():

    # parse arguments
    args = parse_args()

    # window details
    width = args.window_size[0]
    height = args.window_size[1]
    display = (width, height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout Neural Style Sender/Receiver')
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, width, height, 0, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout receiver
    receiverName = args.spout_name
    spoutReceiverWidth = args.spout_size[0]
    spoutReceiverHeight = args.spout_size[1]
    # create spout receiver
    spoutReceiver = SpoutSDK.SpoutReceiver()
    # name, width, height, use active sender
    spoutReceiver.pyCreateReceiver(receiverName, spoutReceiverWidth,
                                   spoutReceiverHeight, False)

    # init spout sender
    spoutSender = SpoutSDK.SpoutSender()
    spoutSenderWidth = args.spout_size[0]
    spoutSenderHeight = args.spout_size[1]
    spoutSender.CreateSender('Neural Style Sender', spoutSenderWidth,
                             spoutSenderHeight, 0)

    # create textures for spout receiver and spout sender
    textureReceiveID = glGenTextures(1)
    textureStyleID = glGenTextures(1)

    # initalise receiver texture
    glBindTexture(GL_TEXTURE_2D, textureReceiveID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

    # copy data into texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spoutReceiverWidth,
                 spoutReceiverHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
    glBindTexture(GL_TEXTURE_2D, 0)

    # open tf session
    soft_config = tf.ConfigProto(allow_soft_placement=True)
    soft_config.gpu_options.allow_growth = True  # to deal with large image
    sess = tf.Session(config=soft_config)
    # build tf graph
    style = tf.placeholder(tf.float32,
                           shape=[spoutSenderHeight, spoutSenderWidth, 3],
                           name='input')
    styleI = tf.expand_dims(style, 0)  # add one dim for batch

    # result image from transform-net
    scaler = transform.Transform()
    y_hat = scaler.net(styleI / 255.0)
    y_hat = tf.squeeze(y_hat)  # remove one dim for batch
    y_hat = tf.clip_by_value(y_hat, 0., 255.)

    # initialize parameters
    sess.run(tf.global_variables_initializer())

    # load pre-trained model
    saver = tf.train.Saver()
    saver.restore(sess, args.style_model)

    # loop for graph frame by frame
    while (True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                spoutReceiver.ReleaseReceiver()
                pygame.quit()
                quit()

        #receive texture
        spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth,
                                       spoutReceiverHeight, textureReceiveID,
                                       GL_TEXTURE_2D, False, 0)

        glBindTexture(GL_TEXTURE_2D, textureReceiveID)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        # copy pixel byte array from received texture
        data = glGetTexImage(GL_TEXTURE_2D,
                             0,
                             GL_RGB,
                             GL_UNSIGNED_BYTE,
                             outputType=None)  #Using GL_RGB can use GL_RGBA
        glBindTexture(GL_TEXTURE_2D, 0)
        # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
        data.shape = (data.shape[1], data.shape[0], data.shape[2])

        # start time of the loop for FPS counter
        start_time = time.time()
        #run the graph
        output = sess.run(y_hat, feed_dict={style: data})
        # fiddle back to an image we can display. I *think* this is correct
        output = np.clip(output, 0.0, 255.0)
        output = output.astype(np.uint8)

        # setup the texture so we can load the stylised output into it
        glBindTexture(GL_TEXTURE_2D, textureStyleID)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        # copy output into texture
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, spoutSenderWidth,
                     spoutSenderHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, output)

        # setup window to draw to screen
        glActiveTexture(GL_TEXTURE0)

        # clean start
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        # reset drawing perspective
        glLoadIdentity()

        # draw texture on screen
        glBegin(GL_QUADS)

        glTexCoord(0, 0)
        glVertex2f(0, 0)

        glTexCoord(1, 0)
        glVertex2f(spoutSenderWidth, 0)

        glTexCoord(1, 1)
        glVertex2f(spoutSenderWidth, spoutSenderHeight)

        glTexCoord(0, 1)
        glVertex2f(0, spoutSenderHeight)

        glEnd()

        # update window
        pygame.display.flip()

        # Send texture to spout...
        spoutSender.SendTexture(textureStyleID, GL_TEXTURE_2D,
                                spoutSenderWidth, spoutSenderHeight, False, 0)
        # FPS = 1 / time to process loop
        print("FPS: ", 1.0 / (time.time() - start_time))
Exemple #15
0
def main():

    # parse arguments
    args = parse_args()

    # window details
    width = args.camSize[0]
    height = args.camSize[1]
    display = (width,height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout for Python Webcam Sender Example')
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)

    # init capture & set size
    cap = cv2.VideoCapture(args.camID)
    cap.set(3, width)
    cap.set(4, height)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glOrtho(0,width,height,0,1,-1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0,0.0,0.0,0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout sender
    spoutSender = SpoutSDK.SpoutSender()
    spoutSenderWidth = width
    spoutSenderHeight = height
    # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
    spoutSender.CreateSender('Spout for Python Webcam Sender Example', width, height, 0)

    # create texture id for use with Spout
    senderTextureID = glGenTextures(1)

    # initalise our sender texture
    glBindTexture(GL_TEXTURE_2D, senderTextureID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glBindTexture(GL_TEXTURE_2D, 0)

    # loop
    while(True):
        for event in pygame.event.get():
           if event.type == pygame.QUIT:
               pygame.quit()
               quit()

        ret, frame = cap.read()
        frame = cv2.flip(frame, 1 )
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Copy the frame from the webcam into the sender texture
        glBindTexture(GL_TEXTURE_2D, senderTextureID)
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, frame )

        # Send texture to Spout
        # Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
        spoutSender.SendTexture(senderTextureID, GL_TEXTURE_2D, spoutSenderWidth, spoutSenderHeight, False, 0)

        # Clear screen
        glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT )
        # reset the drawing perspective
        glLoadIdentity()

        # Draw texture to screen
        glBegin(GL_QUADS)

        glTexCoord(0,0)
        glVertex2f(0,0)

        glTexCoord(1,0)
        glVertex2f(width,0)

        glTexCoord(1,1)
        glVertex2f(width,height)

        glTexCoord(0,1)
        glVertex2f(0,height)

        glEnd()

        # update window
        pygame.display.flip()

        # unbind our sender texture
        glBindTexture(GL_TEXTURE_2D, 0)
Exemple #16
0
def main():

    # setup UDP
    udp_ip = "127.0.0.1"
    udp_port = 7000
    rec_port = 6000
    
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        print('Setting up UDP on ip={} and port={}'.format(udp_ip, udp_port))
    except:
        print('Failed to create socket')
        sys.exit()
        
    try:
        sock.bind(('', rec_port))
        print('Listening on ip={} and port={}'.format(udp_ip, rec_port))
    except:
        print('Bind failed')
        sys.exit()
    
    starting_msg = "Ready"
    sock.sendto( msg_to_bytes(starting_msg), (udp_ip, udp_port))

    # load nmmetwork and prepare to generate
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    
    background = True
    print()
    print('LISTENING')
    seed = 1

    # window details
    width = spout_size[0] 
    height = spout_size[1] 
    display = (width,height)
    
    req_type = spout_type
    receiverName = "none" 
    senderName = spout_name
    #silent = args.silent
    
    # window setup
    pygame.init() 
    pygame.display.set_caption(senderName)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,width,height,0,1,-1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0,0.0,0.0,0.0)
    glEnable(GL_TEXTURE_2D)

    if req_type == 'input' or req_type == 'input-output':
        # init spout receiver
        spoutReceiverWidth = width
        spoutReceiverHeight = height
        # create spout receiver
        spoutReceiver = SpoutSDK.SpoutReceiver()
	    # Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
        spoutReceiver.pyCreateReceiver(receiverName,spoutReceiverWidth,spoutReceiverHeight, False)
        # create textures for spout receiver and spout sender 
        textureReceiveID = glGenTextures(1)
        
        # initalise receiver texture
        glBindTexture(GL_TEXTURE_2D, textureReceiveID)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

        # copy data into texture
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spoutReceiverWidth, spoutReceiverHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, None ) 
        glBindTexture(GL_TEXTURE_2D, 0)

    if req_type == 'output' or req_type == 'input-output':
        # init spout sender
        spoutSender = SpoutSDK.SpoutSender()
        spoutSenderWidth = width
        spoutSenderHeight = height
	    # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
        spoutSender.CreateSender(senderName, spoutSenderWidth, spoutSenderHeight, 0)
        # create textures for spout receiver and spout sender 
    textureSendID = glGenTextures(1)

    # loop for graph frame by frame
    while(True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                spoutReceiver.ReleaseReceiver()
                pygame.quit()
                quit()
        
        if req_type == 'input' or req_type == 'input-output':
            # receive texture
            # Its signature in c++ looks like this: bool pyReceiveTexture(const char* theName, unsigned int theWidth, unsigned int theHeight, GLuint TextureID, GLuint TextureTarget, bool bInvert, GLuint HostFBO);
            if sys.version_info[1] != 7:
                spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth, spoutReceiverHeight, textureReceiveID, GL_TEXTURE_2D, False, 0)
            else:
                spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth, spoutReceiverHeight, textureReceiveID.item(), GL_TEXTURE_2D, False, 0)

            glBindTexture(GL_TEXTURE_2D, textureReceiveID)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
            # copy pixel byte array from received texture   
            data = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, outputType=None)  #Using GL_RGB can use GL_RGBA 
            glBindTexture(GL_TEXTURE_2D, 0)
            # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
            data.shape = (data.shape[1], data.shape[0], data.shape[2])
        else:
            data = np.ones((width,height,3))*255
        
        # call our main function
        output = main_pipeline(data)
        
        # setup the texture so we can load the output into it
        glBindTexture(GL_TEXTURE_2D, textureSendID);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        # copy output into texture
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, output )
            
        # setup window to draw to screen
        glActiveTexture(GL_TEXTURE0)
        # clean start
        glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT )
        # reset drawing perspective
        glLoadIdentity()
        # draw texture on screen
        glBegin(GL_QUADS)

        glTexCoord(0,0)        
        glVertex2f(0,0)

        glTexCoord(1,0)
        glVertex2f(width,0)

        glTexCoord(1,1)
        glVertex2f(width,height)

        glTexCoord(0,1)
        glVertex2f(0,height)

        glEnd()
        
        if silent:
            pygame.display.iconify()
                
        # update window
        pygame.display.flip()        

        if req_type == 'output' or req_type == 'input-output':
            # Send texture to spout...
            # Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
            if sys.version_info[1] != 6:
                spoutSender.SendTexture(textureSendID, GL_TEXTURE_2D, spoutSenderWidth, spoutSenderHeight, False, 0)
            else:
                spoutSender.SendTexture(textureSendID.item(), GL_TEXTURE_2D, spoutSenderWidth, spoutSenderHeight, False, 0)
Exemple #17
0
def main():
    if tf.__version__.split('.')[0] != "1":
        raise Exception("Tensorflow version 1 required")

    if a.seed is None:
        a.seed = random.randint(0, 2**31 - 1)

    tf.set_random_seed(a.seed)
    np.random.seed(a.seed)
    random.seed(a.seed)

    if not os.path.exists(a.output_dir):
        os.makedirs(a.output_dir)

    if a.mode == "test" or a.mode == "spout":
        if a.checkpoint_one is None:
        	#a.checkpoint = a.output_dir
            raise Exception("checkpoint_one required for test mode")

        if a.checkpoint_two is None:
            raise Exception("checkpoint_two required for test mode")

        # load some options from the checkpoint
        options = {"which_direction", "ngf", "ndf", "lab_colorization"}
        with open(os.path.join(a.checkpoint_one, "options.json")) as f:
            for key, val in json.loads(f.read()).items():
                if key in options:
                    print("loaded", key, "=", val)
                    setattr(a, key, val)
        # disable these features in test mode
        a.scale_size = CROP_SIZE
        a.flip = False

    for k, v in a._get_kwargs():
        print(k, "=", v)

    with open(os.path.join(a.output_dir, "options.json"), "w") as f:
        f.write(json.dumps(vars(a), sort_keys=True, indent=4))

    if a.mode == "spout":
        print("spout mode")

        if a.lab_colorization:
            raise Exception("export not supported for lab_colorization")

        def create_model():
            input = tf.placeholder(tf.float32, shape=[512,512,3], name="input")
            input_image = tf.image.convert_image_dtype(input, dtype=tf.float32)
            input_image.set_shape([CROP_SIZE, CROP_SIZE, 3])
            batch_input = tf.expand_dims(input_image, axis=0)

            with tf.variable_scope("generator"):
                batch_output = deprocess(create_generator(preprocess(batch_input), 3))

            output_image = tf.image.convert_image_dtype(batch_output, dtype=tf.uint8)[0]
            #output_png = tf.image.encode_png(output_image)

            logdir = a.output_dir if (a.trace_freq > 0 or a.summary_freq > 0) else None
            init_op = tf.global_variables_initializer()
            restore_saver = tf.train.Saver()
            export_saver = tf.train.Saver()
            return {
                'input': input,
                'output_image': output_image,
                'init_op': init_op,
                'restore_saver': restore_saver
            }

        model1 = None
        model2 = None
        sess1 = None
        sess2 = None

        with tf.Graph().as_default() as g1:
            if(a.use_cpu_one):
                with tf.device("/cpu:0"):
                    sess1 = tf.Session( graph = g1 )
                    model1 = create_model()
                    sess1.run(model1["init_op"])
                    print("loading model from checkpoint_one")
                    checkpoint_one = tf.train.latest_checkpoint(a.checkpoint_one)
                    model1["restore_saver"].restore(sess1, checkpoint_one)
            else:
                sess1 = tf.Session( graph = g1 )
                model1 = create_model()
                sess1.run(model1["init_op"])
                print("loading model from checkpoint_one")
                checkpoint_one = tf.train.latest_checkpoint(a.checkpoint_one)
                model1["restore_saver"].restore(sess1, checkpoint_one)

        #tf.reset_default_graph()
        
        with tf.Graph().as_default() as g2:
            if(a.use_cpu_two):
                with tf.device("/cpu:0"):
                    sess2 = tf.Session( graph = g2 )
                    model2 = create_model()
                    sess2.run(model2["init_op"])
                    print("loading model from checkpoint_two")
                    checkpoint_two = tf.train.latest_checkpoint(a.checkpoint_two)
                    model2["restore_saver"].restore(sess2, checkpoint_two)
            else:
                sess2 = tf.Session( graph = g2 )
                model2 = create_model()
                sess2.run(model2["init_op"])
                print("loading model from checkpoint_two")
                checkpoint_two = tf.train.latest_checkpoint(a.checkpoint_two)
                model2["restore_saver"].restore(sess2, checkpoint_two)

        #tf.reset_default_graph()
        
        if True:
            width = 512
            height = 512
            display = (width*2,height)
        
            pygame.init() 
            pygame.display.set_caption('Spout Python Sender')
            pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
            pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)
        
            # OpenGL init
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            glOrtho(0,width*2,height,0,1,-1)
            glMatrixMode(GL_MODELVIEW)
            glDisable(GL_DEPTH_TEST)
            glClearColor(0.0,0.0,0.0,0.0)
            glEnable(GL_TEXTURE_2D)
        
            # init spout receiver
            receiverName = "FBO"
            spoutReceiver = SpoutSDK.SpoutReceiver()
            spoutReceiverWidth = width
            spoutReceiverHeight = height
            spoutReceiver.pyCreateReceiver(receiverName, spoutReceiverWidth, spoutReceiverHeight, False)
        
            # init spout sender
            # spoutSender = SpoutSDK.SpoutSender()
            spoutSenderWidth = width
            spoutSenderHeight = height
            # spoutSender.CreateSender('Spout Python Sender', spoutSenderWidth, spoutSenderHeight, 0)
        
            print("width, height = ", width, height)
        
            # init spout sender texture ID
            senderTextureID = glGenTextures(1)
            receiverTextureID = glGenTextures(1)
        
            if type(senderTextureID) is not int:
                senderTextureID = senderTextureID.item()
                receiverTextureID = receiverTextureID.item()
        
            # initialize texture
            glBindTexture(GL_TEXTURE_2D, receiverTextureID)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
            # fill texture with blank data
            glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,0,0,spoutSenderWidth,spoutSenderHeight,0)
            glBindTexture(GL_TEXTURE_2D, 0)
        
            while True:
                glBindTexture(GL_TEXTURE_2D, receiverTextureID)
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

                flag = spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth, spoutReceiverHeight, receiverTextureID, GL_TEXTURE_2D, False, 0)

                # copy pixel byte array from received texture   
                data = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, outputType=None)  #Using GL_RGB can use GL_RGBA 
                glBindTexture(GL_TEXTURE_2D, 0)
                # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
                
                # img = Image.fromarray(data)
                # img.show()
                # sys.exit()

                #data.shape = (1, data.shape[2], data.shape[1], data.shape[0])
                #data = (np.transpose(data, (2, 0, 1))[np.newaxis, :, :, :] / 255.0 * 2.0 - 1.0).astype(float)
                data = (data / 255.0 * 2.0 - 1.0).astype(float)
                #input_data = tf.convert_to_tensor(data, dtype=tf.float32)

                # --- if save image as png
                # result = sess.run(output_png, feed_dict={input: data})
                # with open("test.png", 'wb') as fd:
                #     fd.write(result)
                # return

                data_img = sess1.run(model1["output_image"], feed_dict={model1['input']: data})
                data = (data_img / 255.0 * 2.0 - 1.0).astype(float)
                result = sess2.run(model2["output_image"], feed_dict={model2['input']: data})
                # result = sess2.run(model2["output_image"], feed_dict={model2['input']: data})

                scipy.misc.imsave('result.jpg', result)
                send_osc("/image", "result.jpg", a.file_server_url)

                #output = util.tensor2im(visuals["fake_B"])

                # setup the texture so we can load the stylised output into it
                glBindTexture(GL_TEXTURE_2D, senderTextureID)
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
                # copy output into texture
                #glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, spoutSenderWidth, spoutSenderHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data )
                glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, spoutSenderWidth, spoutSenderHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, result )
                
                # setup window to draw to screen
                glActiveTexture(GL_TEXTURE0)

                # clean start
                glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT )
                # reset drawing perspective
                glLoadIdentity()

                # draw texture on screen
                glBegin(GL_QUADS)
                glTexCoord(0,0)        
                glVertex2f(0,0)
                glTexCoord(1,0)
                glVertex2f(spoutSenderWidth,0)
                glTexCoord(1,1)
                glVertex2f(spoutSenderWidth,spoutSenderHeight)
                glTexCoord(0,1)
                glVertex2f(0,spoutSenderHeight)
                glEnd()

                glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, spoutSenderWidth, spoutSenderHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data_img )
                glBegin(GL_QUADS)
                glTexCoord(0,0)        
                glVertex2f(spoutSenderWidth,0)
                glTexCoord(1,0)
                glVertex2f(spoutSenderWidth*2,0)
                glTexCoord(1,1)
                glVertex2f(spoutSenderWidth*2,spoutSenderHeight)
                glTexCoord(0,1)
                glVertex2f(spoutSenderWidth,spoutSenderHeight)
                glEnd()
                        
                # update window
                pygame.display.flip()        

                #spoutSender.SendTexture(senderTextureID, GL_TEXTURE_2D, spoutSenderWidth, spoutSenderHeight, False, 0)

                #pygame.time.wait(10)
                pygame.time.wait(a.wait_millis)
                #IPython.embed()
                #pygame.quit()
                #sys.exit(1)

                for event in pygame.event.get():
                    if event.type == QUIT:
                        spoutReceiver.ReleaseReceiver()
                        pygame.quit()
                        sess1.close()
                        sess2.close()
                        sys.exit()

        return

    examples = load_examples()
    print("examples count = %d" % examples.count)

    # inputs and targets are [batch_size, height, width, channels]
    model = create_model(examples.inputs, examples.targets)

    # undo colorization splitting on images that we use for display/output
    if a.lab_colorization:
        if a.which_direction == "AtoB":
            # inputs is brightness, this will be handled fine as a grayscale image
            # need to augment targets and outputs with brightness
            targets = augment(examples.targets, examples.inputs)
            outputs = augment(model.outputs, examples.inputs)
            # inputs can be deprocessed normally and handled as if they are single channel
            # grayscale images
            inputs = deprocess(examples.inputs)
        elif a.which_direction == "BtoA":
            # inputs will be color channels only, get brightness from targets
            inputs = augment(examples.inputs, examples.targets)
            targets = deprocess(examples.targets)
            outputs = deprocess(model.outputs)
        else:
            raise Exception("invalid direction")
    else:
        inputs = deprocess(examples.inputs)
        targets = deprocess(examples.targets)
        outputs = deprocess(model.outputs)

    def convert(image):
        if a.aspect_ratio != 1.0:
            # upscale to correct aspect ratio
            size = [CROP_SIZE, int(round(CROP_SIZE * a.aspect_ratio))]
            image = tf.image.resize_images(image, size=size, method=tf.image.ResizeMethod.BICUBIC)

        return tf.image.convert_image_dtype(image, dtype=tf.uint8, saturate=True)

    # reverse any processing on images so they can be written to disk or displayed to user
    with tf.name_scope("convert_inputs"):
        converted_inputs = convert(inputs)

    with tf.name_scope("convert_targets"):
        converted_targets = convert(targets)

    with tf.name_scope("convert_outputs"):
        converted_outputs = convert(outputs)

    with tf.name_scope("encode_images"):
        display_fetches = {
            "paths": examples.paths,
            "inputs": tf.map_fn(tf.image.encode_png, converted_inputs, dtype=tf.string, name="input_pngs"),
            "targets": tf.map_fn(tf.image.encode_png, converted_targets, dtype=tf.string, name="target_pngs"),
            "outputs": tf.map_fn(tf.image.encode_png, converted_outputs, dtype=tf.string, name="output_pngs"),
        }

    # summaries
    with tf.name_scope("inputs_summary"):
        tf.summary.image("inputs", converted_inputs)

    with tf.name_scope("targets_summary"):
        tf.summary.image("targets", converted_targets)

    with tf.name_scope("outputs_summary"):
        tf.summary.image("outputs", converted_outputs)

    with tf.name_scope("predict_real_summary"):
        tf.summary.image("predict_real", tf.image.convert_image_dtype(model.predict_real, dtype=tf.uint8))

    with tf.name_scope("predict_fake_summary"):
        tf.summary.image("predict_fake", tf.image.convert_image_dtype(model.predict_fake, dtype=tf.uint8))

    tf.summary.scalar("discriminator_loss", model.discrim_loss)
    tf.summary.scalar("generator_loss_GAN", model.gen_loss_GAN)
    tf.summary.scalar("generator_loss_L1", model.gen_loss_L1)

    for var in tf.trainable_variables():
        tf.summary.histogram(var.op.name + "/values", var)

    for grad, var in model.discrim_grads_and_vars + model.gen_grads_and_vars:
        tf.summary.histogram(var.op.name + "/gradients", grad)

    with tf.name_scope("parameter_count"):
        parameter_count = tf.reduce_sum([tf.reduce_prod(tf.shape(v)) for v in tf.trainable_variables()])

    saver = tf.train.Saver(max_to_keep=5)

    logdir = a.output_dir if (a.trace_freq > 0 or a.summary_freq > 0) else None
    sv = tf.train.Supervisor(logdir=logdir, save_summaries_secs=0, saver=None)
    with sv.managed_session() as sess:
        print("parameter_count =", sess.run(parameter_count))

        if a.checkpoint_one is not None:
            print("loading model from checkpoint_one")
            checkpoint = tf.train.latest_checkpoint(a.checkpoint_one)
            saver.restore(sess, checkpoint)

        max_steps = 2**32
        if a.max_epochs is not None:
            max_steps = examples.steps_per_epoch * a.max_epochs
        if a.max_steps is not None:
            max_steps = a.max_steps

        if a.mode == "test":
            # testing
            # at most, process the test data once
            max_steps = min(examples.steps_per_epoch, max_steps)
            for step in range(max_steps):
                results = sess.run(display_fetches)
                filesets = save_images(results)
                for i, f in enumerate(filesets):
                    print("evaluated image", f["name"])
                index_path = append_index(filesets)

            print("wrote index at", index_path)

        # elif a.mode == "spout":
        #     # testing with spout
        #     # at most, process the test data once
        #     print("spout mode")
        #     # max_steps = min(examples.steps_per_epoch, max_steps)
        #     # for step in range(max_steps):
        #     #     results = sess.run(display_fetches)
        #     #     filesets = save_images(results)
        #     #     for i, f in enumerate(filesets):
        #     #         print("evaluated image", f["name"])
        #     #     index_path = append_index(filesets)

        #     writer = tf.summary.FileWriter("graph", sess.graph)
        #     results = sess.run(display_fetches)
        #     writer.close()
        
        #     # model.
        #     # results = sess.run(model, feed_dict=[])
        
        else:
            # training
            start = time.time()

            for step in range(max_steps):
                def should(freq):
                    return freq > 0 and ((step + 1) % freq == 0 or step == max_steps - 1)

                options = None
                run_metadata = None
                if should(a.trace_freq):
                    options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
                    run_metadata = tf.RunMetadata()

                fetches = {
                    "train": model.train,
                    "global_step": sv.global_step,
                }

                if should(a.progress_freq):
                    fetches["discrim_loss"] = model.discrim_loss
                    fetches["gen_loss_GAN"] = model.gen_loss_GAN
                    fetches["gen_loss_L1"] = model.gen_loss_L1

                if should(a.summary_freq):
                    fetches["summary"] = sv.summary_op

                if should(a.display_freq):
                    fetches["display"] = display_fetches

                results = sess.run(fetches, options=options, run_metadata=run_metadata)

                if should(a.summary_freq):
                    print("recording summary")
                    sv.summary_writer.add_summary(results["summary"], results["global_step"])

                if should(a.display_freq):
                    print("saving display images")
                    filesets = save_images(results["display"], step=results["global_step"])
                    append_index(filesets, step=True)

                if should(a.trace_freq):
                    print("recording trace")
                    sv.summary_writer.add_run_metadata(run_metadata, "step_%d" % results["global_step"])

                if should(a.progress_freq):
                    # global_step will have the correct step count if we resume from a checkpoint
                    train_epoch = math.ceil(results["global_step"] / examples.steps_per_epoch)
                    train_step = (results["global_step"] - 1) % examples.steps_per_epoch + 1
                    rate = (step + 1) * a.batch_size / (time.time() - start)
                    remaining = (max_steps - step) * a.batch_size / rate
                    print("progress  epoch %d  step %d  image/sec %0.1f  remaining %dm" % (train_epoch, train_step, rate, remaining / 60))
                    print("discrim_loss", results["discrim_loss"])
                    print("gen_loss_GAN", results["gen_loss_GAN"])
                    print("gen_loss_L1", results["gen_loss_L1"])

                if should(a.save_freq):
                    print("saving model")
                    saver.save(sess, os.path.join(a.output_dir, "model"), global_step=sv.global_step)

                if sv.should_stop():
                    break
Exemple #18
0
def main():
    # window details
    width = 800
    height = 600
    display = (width, height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout Python Sender')
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)

    # OpenGL init
    # setup OpenGL perspective
    glMatrixMode(GL_PROJECTION)
    gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
    # setup default colours, blending modes, rotation and translation parameters
    glMatrixMode(GL_MODELVIEW)
    # reset the drawing perspective
    glLoadIdentity()
    # can disable depth buffer because we aren't dealing with multiple geometry in our scene
    glDisable(GL_DEPTH_TEST)
    glEnable(GL_ALPHA_TEST)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glColor4f(1.0, 1.0, 1.0, 1.0)
    glTranslatef(0, 0, -5)
    glRotatef(25, 2, 1, 0)

    # init spout sender
    spoutSender = SpoutSDK.SpoutSender()
    spoutSenderWidth = width
    spoutSenderHeight = height
    # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
    spoutSender.CreateSender('Spout Python Sender', spoutSenderWidth,
                             spoutSenderHeight, 0)

    # init spout sender texture ID
    senderTextureID = glGenTextures(1)

    # initialise texture
    glBindTexture(GL_TEXTURE_2D, senderTextureID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    # fill texture with blank data
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, spoutSenderWidth,
                     spoutSenderHeight, 0)
    glBindTexture(GL_TEXTURE_2D, 0)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        # setup frame
        glActiveTexture(GL_TEXTURE0)
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Perform a rotation and since we aren't resetting our perspective with glLoadIdentity, then each frame will perform a successive rotation on top of what we already see
        glRotatef(1, 3, 1, 1)

        # draw cube
        Cube()

        # bind our senderTexture and copy the window's contents into the texture
        glBindTexture(GL_TEXTURE_2D, senderTextureID)
        glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, spoutSenderWidth,
                         spoutSenderHeight, 0)
        glBindTexture(GL_TEXTURE_2D, 0)

        # send texture to Spout
        # Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
        spoutSender.SendTexture(np.int(senderTextureID), GL_TEXTURE_2D,
                                spoutSenderWidth, spoutSenderHeight, True, 0)

        # update display
        pygame.display.flip()

        pygame.time.wait(10)
def main():

    # parse arguments
    args = parse_args()

    # window details
    width = args.window_size[0]
    height = args.window_size[1]
    display = (width, height)

    # window setup
    pygame.init()
    pygame.display.set_caption('Spout Receiver')
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, width, height, 0, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout receiver
    receiverName = args.spout_name
    spoutReceiverWidth = args.spout_size[0]
    spoutReceiverHeight = args.spout_size[1]

    print("spoutReceiverWidth", spoutReceiverWidth)
    print("spoutReceiverHeight", spoutReceiverHeight)

    # create spout receiver
    spoutReceiver = SpoutSDK.SpoutReceiver()

    # Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
    spoutReceiver.pyCreateReceiver(receiverName, spoutReceiverWidth,
                                   spoutReceiverHeight, True)

    # create texture for spout receiver
    textureReceiveID = glGenTextures(1).item()

    # initalise receiver texture
    glBindTexture(GL_TEXTURE_2D, textureReceiveID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

    # copy data into texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spoutReceiverWidth,
                 spoutReceiverHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
    glBindTexture(GL_TEXTURE_2D, 0)

    # loop for graph frame by frame
    while (True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                spoutReceiver.ReleaseReceiver()
                pygame.quit()
                quit()

        count = spoutReceiver.GetSenderCount()

        # receive texture
        # Its signature in c++ looks like this: bool pyReceiveTexture(const char* theName, unsigned int theWidth, unsigned int theHeight, GLuint TextureID, GLuint TextureTarget, bool bInvert, GLuint HostFBO);
        flag = spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth,
                                              spoutReceiverHeight,
                                              textureReceiveID, GL_TEXTURE_2D,
                                              False, 0)

        if (flag):
            #print(datetime.datetime.now(), "Connected! Receiving")

            glBindTexture(GL_TEXTURE_2D, textureReceiveID)

            # copy pixel byte array from received texture - this example doesn't use it, but may be useful for those who do want pixel info
            # data = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, outputType=None)  #Using GL_RGB can use GL_RGBA

            # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
            # data.shape = (data.shape[1], data.shape[0], data.shape[2])

            # setup window to draw to screen
            glActiveTexture(GL_TEXTURE0)

            # clean start
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            # reset drawing perspective
            glLoadIdentity()

            # draw texture on screen
            # glPushMatrix() use these lines if you want to scale your received texture
            # glScale(0.3, 0.3, 0.3)
            glBegin(GL_QUADS)

            glTexCoord(0, 0)
            glVertex2f(0, 0)

            glTexCoord(1, 0)
            glVertex2f(spoutReceiverWidth, 0)

            glTexCoord(1, 1)
            glVertex2f(spoutReceiverWidth, spoutReceiverHeight)

            glTexCoord(0, 1)
            glVertex2f(0, spoutReceiverHeight)

            glEnd()
            # glPopMatrix() make sure to pop your matrix if you're doing a scale
            # update window
            pygame.display.flip()
        else:
            print("sender count: ", count)
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
# reset the drawing perspective
glLoadIdentity()
# can disable depth buffer because we aren't dealing with multiple geometry in our scene
glDisable(GL_DEPTH_TEST)
glEnable(GL_ALPHA_TEST)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0,0.0,0.0,0.0)
glColor4f(1.0, 1.0, 1.0, 1.0);   
glTranslatef(0,0, -5)
glRotatef(25, 2, 1, 0)
spoutSender = SpoutSDK.SpoutSender()
spoutSenderWidth = width
spoutSenderHeight = height
spoutSender.CreateSender('Spout Python Sender', spoutSenderWidth, spoutSenderHeight, 0)
senderTextureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, 0)
glBindTexture(GL_TEXTURE_2D, senderTextureID)


try:

    IMAGE_RESULT_DATA   = np.zeros((PRESET_IMG_SIZE[1], PRESET_IMG_SIZE[0], 3), np.uint8)

    t_begin = 0
    t_end   = 0
    t_fps   = 0
async def show_spout():

    # parse arguments
    # args = parse_args()

    # window details
    # width = args.window_size[0]
    # height = args.window_size[1]
    width = 256
    height = 256
    display = (width, height)

    # window setup
    # pygame.init()
    # pygame.display.set_caption('Spout Receiver')
    # pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    glfw.init()
    window = glfw.create_window(width, height, "spout receiver", None, None)
    if not window:
        print("couldnt open window")
        glfw.terminate()
    glfw.make_context_current(window)
    glEnable(GL_DEPTH_TEST)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, width, height, 0, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout receiver
    receiverName = "Unity"
    spoutReceiverWidth = 256
    spoutReceiverHeight = 256
    # create spout receiver
    spoutReceiver = SpoutSDK.SpoutReceiver()

    # Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
    spoutReceiver.pyCreateReceiver(receiverName, spoutReceiverWidth,
                                   spoutReceiverHeight, False)

    # create texture for spout receiver
    textureReceiveID = glGenTextures(1)

    # initalise receiver texture
    glBindTexture(GL_TEXTURE_2D, textureReceiveID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

    # copy data into texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, spoutReceiverWidth,
                 spoutReceiverHeight, 0, GL_RED, GL_UNSIGNED_BYTE, None)
    glBindTexture(GL_TEXTURE_2D, 0)

    model.load_weights('model.h5')

    # prevents openCL usage and unnecessary logging messages
    cv2.ocl.setUseOpenCL(False)

    # dictionary which assigns each label an emotion (alphabetical order)
    emotion_dict = {
        0: "Angry",
        1: "Disgusted",
        2: "Fearful",
        3: "Happy",
        4: "Neutral",
        5: "Sad",
        6: "Surprised"
    }

    # cap.release()
    # cv2.destroyAllWindows()

    # dispatch = dispatcher.Dispatcher()
    # server = osc_server.AsyncIOOSCUDPServer(("127.0.0.1", 6969), dispatch, asyncio.get_event_loop())
    # transport, protocol = await server.create_serve_endpoint()
    oscclient = SimpleUDPClient("255.255.255.255", 6969, True)

    await loop(spoutReceiver, receiverName, spoutReceiverHeight,
               spoutReceiverWidth, textureReceiveID, emotion_dict, oscclient,
               window)

    # transport.close()
    # pygame.display.flip()
    glfw.terminate()
    spoutReceiver.ReleaseReceiver()
    cv2.destroyAllWindows()
Exemple #22
0
def generate_images(network_pkl, truncation_psi):

    # spout setup
    width = 1024
    height = 1024
    display = (width, height)

    senderName = "outputGAN"
    receiverName = "input"
    silent = True

    # window setup
    pygame.init()
    pygame.display.set_caption(senderName)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

    # OpenGL init
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, width, height, 0, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glEnable(GL_TEXTURE_2D)

    # init spout receiver
    spoutReceiverWidth = width
    spoutReceiverHeight = height
    # create spout receiver
    spoutReceiver = SpoutSDK.SpoutReceiver()
    # Its signature in c++ looks like this: bool pyCreateReceiver(const char* theName, unsigned int theWidth, unsigned int theHeight, bool bUseActive);
    spoutReceiver.pyCreateReceiver(receiverName, width, height, False)
    # create textures for spout receiver and spout sender
    textureReceiveID = glGenTextures(1)

    # initalise receiver texture
    glBindTexture(GL_TEXTURE_2D, textureReceiveID)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

    # copy data into texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, None)
    glBindTexture(GL_TEXTURE_2D, 0)

    spoutSender = SpoutSDK.SpoutSender()
    spoutSenderWidth = width
    spoutSenderHeight = height
    # Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
    spoutSender.CreateSender(senderName, spoutSenderWidth, spoutSenderHeight,
                             0)
    # create textures for spout receiver and spout sender
    textureSendID = glGenTextures(1)

    # setup UDP
    udp_ip = "127.0.0.1"
    udp_port = 7000
    rec_port = 6000

    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        print('Setting up UDP on ip={} and port={}'.format(udp_ip, udp_port))
    except:
        print('Failed to create socket')
        sys.exit()

    try:
        sock.bind(('', rec_port))
        print('Listening on ip={} and port={}'.format(udp_ip, rec_port))
    except:
        print('Bind failed')
        sys.exit()

    starting_msg = "Ready"
    sock.sendto(msg_to_bytes(starting_msg), (udp_ip, udp_port))

    # load nmmetwork and prepare to generate
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [
        var for name, var in Gs.components.synthesis.vars.items()
        if name.startswith('noise')
    ]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                      nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False

    print()
    print('LISTENING')

    while (True):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sock.close()
                quit()

        #background = False

        # receive texture
        # Its signature in c++ looks like this: bool pyReceiveTexture(const char* theName, unsigned int theWidth, unsigned int theHeight, GLuint TextureID, GLuint TextureTarget, bool bInvert, GLuint HostFBO);
        spoutReceiver.pyReceiveTexture(receiverName, spoutReceiverWidth,
                                       spoutReceiverHeight,
                                       textureReceiveID.item(), GL_TEXTURE_2D,
                                       False, 0)

        glBindTexture(GL_TEXTURE_2D, textureReceiveID)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        # copy pixel byte array from received texture
        data = glGetTexImage(GL_TEXTURE_2D,
                             0,
                             GL_RGB,
                             GL_UNSIGNED_BYTE,
                             outputType=None)  #Using GL_RGB can use GL_RGBA
        glBindTexture(GL_TEXTURE_2D, 0)
        # swap width and height data around due to oddness with glGetTextImage. http://permalink.gmane.org/gmane.comp.python.opengl.user/2423
        data.shape = (data.shape[1], data.shape[0], data.shape[2])

        update = data[0, 0, 0]

        if update > 1:
            z = data[0, :512, 0]
            z = z / 255.0 * 7 - 3.5
            z = np.array(z)
            z = np.expand_dims(z, axis=0)

            if truncation_psi is not None:
                Gs_kwargs.truncation_psi = truncation_psi

            #print('Generating image for seed %d ...' % (seed))
            rnd = np.random.RandomState(0)
            #z = rnd.randn(1, *Gs.input_shape[1:]) # [minibatch, component]
            #print(z)
            #print(z.shape)
            tflib.set_vars(
                {var: rnd.randn(*var.shape.as_list())
                 for var in noise_vars})  # [height, width]
            images = Gs.run(z, None,
                            **Gs_kwargs)  # [minibatch, height, width, channel]

            output = images[0]

            # setup the texture so we can load the output into it
            glBindTexture(GL_TEXTURE_2D, textureSendID)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
            # copy output into texture
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
                         GL_UNSIGNED_BYTE, output)

            # setup window to draw to screen
            glActiveTexture(GL_TEXTURE0)
            # clean start
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            # reset drawing perspective
            glLoadIdentity()
            # draw texture on screen
            glBegin(GL_QUADS)

            glTexCoord(0, 0)
            glVertex2f(0, 0)

            glTexCoord(1, 0)
            glVertex2f(width, 0)

            glTexCoord(1, 1)
            glVertex2f(width, height)

            glTexCoord(0, 1)
            glVertex2f(0, height)

            glEnd()

            if silent:
                pygame.display.iconify()

            # update window
            pygame.display.flip()

            spoutSender.SendTexture(textureSendID.item(), GL_TEXTURE_2D,
                                    spoutSenderWidth, spoutSenderHeight, False,
                                    0)

        #sock.sendto(msg_to_bytes(reply), (udp_ip, udp_port))

    sock.close()
    pygame.quit()
    quit()