Пример #1
0
        protected void TransferOne(Texture2D contentTexture, Texture2D styleTexture, Vector2Int contentResize, Vector2Int styleResize, Action <Texture2D> onFinished)
        {
            styleTransferModel = new UniversalStyleTransferModel(CNTK.DeviceDescriptor.GPUDevice(0), styleTransferModelData.bytes);
            styleTransferModel.CreateModelWithDimensions(contentResize, styleResize);

            var tempContentTexture = Images.GetReadableTextureFromUnreadable(contentTexture);

            byte[] contentBytes = tempContentTexture.GetRGB24FromTexture2D(contentResize);


            var tempStyleTexture = Images.GetReadableTextureFromUnreadable(styleTexture);

            byte[] styleBytes = tempStyleTexture.GetRGB24FromTexture2D(styleResize);

            DestroyImmediate(tempStyleTexture);
            DestroyImmediate(tempContentTexture);

            Task.Run(() =>
            {
                byte[] result = styleTransferModel.TransferStyle(contentBytes, styleBytes, styleTransferParams.ToArray());
                GC.Collect();
                RunOnMainThread(() => {
                    Texture2D tex = new Texture2D(contentResize.x, contentResize.y, TextureFormat.RGB24, false);
                    tex.LoadRawTextureData(result);
                    tex.Apply();

                    onFinished.Invoke(tex);
                });
            });
        }
Пример #2
0
        protected void StartTransfer()
        {
            IsRunningTransfer = true;
            if (gpuDevice != null)
            {
                styleTransferModel = new UniversalStyleTransferModel(gpuDevice, styleTransferModelData.bytes);
            }
            else if (cpuDevice != null)
            {
                styleTransferModel = new UniversalStyleTransferModel(cpuDevice, styleTransferModelData.bytes);
            }
            else
            {
                Debug.LogError("There must be a bug that no device if found");
            }
            styleTransferModel.CreateModelWithDimensions(contentSize, styleSize);
            //var tempContentTexture = Images.GetReadableTextureFromUnreadable(contentWindow.showTexture);
            byte[] contentBytes = contentWindow.showTexture.GetRGB24FromTexture2D(contentSize);
            //contentBytes = contentWindow.showTexture.GetRawTextureData();
            //DestroyImmediate(tempContentTexture);
            //var tempStyleTexture = Images.GetReadableTextureFromUnreadable(styleWindow.showTexture);
            byte[] styleBytes = styleWindow.showTexture.GetRGB24FromTexture2D(styleSize);
            //DestroyImmediate(tempStyleTexture);

            /*
             * //run in main thread
             * byte[] result = styleTransferModel.TransferStyle(contentBytes, styleBytes, styleTransferParams.ToArray());
             * Texture2D tex2 = new Texture2D(contentSize.x, contentSize.y, TextureFormat.RGB24, false);
             * tex2.LoadRawTextureData(result);
             * tex2.Apply();
             * if (resultWindow.showTexture != null)
             * {
             *  DestroyImmediate(resultWindow.showTexture);
             * }
             * resultWindow.showTexture = tex2;*/

            //run in ahother thread
            Task.Run(() =>
            {
                try
                {
                    byte[] result = styleTransferModel.TransferStyle(contentBytes, styleBytes, styleTransferParams.ToArray());
                    GC.Collect();
                    RunOnMainThread(() => {
                        Texture2D tex = new Texture2D(contentSize.x, contentSize.y, TextureFormat.RGB24, false);
                        tex.LoadRawTextureData(result);
                        tex.Apply();

                        var resultTexture = tex;
                        if (useOriginalAlpha)
                        {
                            resultTexture = Images.GetTextureWithAlpha(tex, contentWindow.showTexture);
                            DestroyImmediate(tex);
                        }

                        if (resultWindow.showTexture != null)
                        {
                            DestroyImmediate(resultWindow.showTexture);
                        }
                        resultWindow.showTexture = resultTexture;
                        IsRunningTransfer        = false;
                    });
                }
                catch (Exception e) {
                    Debug.LogError(e.Message);
                    RunOnMainThread(() => {
                        IsRunningTransfer = false;
                    });
                }
            });
        }