void Update()
        {
            if (!AiliaImageSource.IsPrepared || !modelPrepared)
            {
                return;
            }

            if (output == null)
            {
                float rawImageRatio = rawImageSize.x / rawImageSize.y;
                float ratio         = AiliaImageSource.Width / (float)AiliaImageSource.Height;
                raw_image.rectTransform.sizeDelta = new Vector2(ratio / rawImageRatio * rawImageSize.x, rawImageSize.y);

                SetShape(imageSegmentaionModels);

                // texture & buffer allocate
                labelTexture = new Texture2D(OutputWidth, OutputHeight, TextureFormat.RGBA32, false);
                AiliaImageSource.Resize(InputWidth, InputHeight);
                input       = new float[InputWidth * InputHeight * InputChannel];
                output      = new float[OutputWidth * OutputHeight * OutputChannel];
                outputImage = new Color32[OutputWidth * OutputHeight];
            }

            if (oneshot)
            {
                oneshot = false;

                // Make input data
                Rect      rect       = new Rect(0, 0, InputWidth, InputHeight);
                long      start_time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
                Color32[] inputImage = null;
                if (!gpu_mode || inputDataProcessingShader == null)
                {
                    inputImage = AiliaImageSource.GetPixels32(rect, true);
                    InputDataProcessingCPU(imageSegmentaionModels, inputImage, input);
                }
                else
                {
                    originalTexture = AiliaImageSource.GetTexture(rect);
                    InputDataProcessing(imageSegmentaionModels, originalTexture, input, true);
                }
                long end_time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                // Predict
                long start_time2 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
                bool result      = ailiaModel.Predict(output, input);

                // convert result to image
                if (imageSegmentaionModels == ImageSegmentaionModels.hair_segmentation)
                {
                    LabelPaintHairSegmentation(output, outputImage, Color.red);
                }
                else if (imageSegmentaionModels == ImageSegmentaionModels.pspnet_hair_segmentation)
                {
                    LabelPaintPspnet(output, outputImage, Color.red);
                }
                else
                {
                    LabelPaint(output, outputImage, OutputChannel, colorPalette);
                }

                long end_time2 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                if (label_text != null)
                {
                    label_text.text = ((end_time - start_time) + (end_time2 - start_time2)).ToString() + "ms\n" + ailiaModel.EnvironmentName();
                }

                // for viewer
                if (!gpu_mode || inputDataProcessingShader == null)
                {
                    originalTexture = new Texture2D(InputWidth, InputHeight, TextureFormat.RGBA32, false);
                    originalTexture.SetPixels32(inputImage);
                    originalTexture.Apply();
                }
                raw_image.texture = originalTexture;
                blendMaterial.SetTexture(mainTexId, originalTexture);

                labelTexture.SetPixels32(outputImage);
                labelTexture.Apply();
                blendMaterial.SetTexture(blendTexId, labelTexture);

                blendMaterial.SetFloat(mainVFlipId, 0);
                blendMaterial.SetFloat(blendVFlipId, 1);

                raw_image.gameObject.SetActive(true);
            }

            // When space key down, draw original image
            if (Input.GetKey(KeyCode.Space))
            {
                blendMaterial.SetFloat(blendFlagId, 0);
            }
            else
            {
                blendMaterial.SetFloat(blendFlagId, 1);
            }
        }
Esempio n. 2
0
        void Update()
        {
            if (!AiliaImageSource.IsPrepared || !modelPrepared)
            {
                return;
            }

            if (outputWC == null)
            {
                float rawImageRatio = rawImageSize.x / rawImageSize.y;
                float ratio         = AiliaImageSource.Width / (float)AiliaImageSource.Height;
                raw_image.rectTransform.sizeDelta = new Vector2(ratio / rawImageRatio * rawImageSize.x, rawImageSize.y);

                SetShape();

                // texture & buffer allocate
                resultRenderTexture = new RenderTexture(OutputWidth, OutputHeight, 0, RenderTextureFormat.ARGBFloat);
                resultRenderTexture.enableRandomWrite = true;
                resultRenderTexture.Create();

                originalTexture = AiliaImageSource.GetTexture(AiliaImageUtil.Crop.No);
                AiliaImageSource.Resize(InputWidth, InputHeight);
            }

            if (oneshot)
            {
                oneshot = false;

                // Make input data
                long start_time   = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
                var  inputTexture = AiliaImageSource.GetTexture(new Rect(0, 0, InputWidth, InputHeight));
                InputDataPocessing(inputTexture, input, true);
                long end_time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                // Predict
                long start_time2 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
                bool result      = ailiaModelWC.Predict(outputWC, input);

                MiddleDataPocessing(outputWC, inputBM);
                result = ailiaModelBM.Predict(outputBM, inputBM);
                // convert result to image
                OutputDataProcessing(outputBM, resultRenderTexture);

                long end_time2 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                if (label_text != null)
                {
                    label_text.text = ((end_time - start_time) + (end_time2 - start_time2)).ToString() + "ms\n" + ailiaModelWC.EnvironmentName();
                }

                // for viewer
                raw_image.texture = originalTexture;
                blendMaterial.SetTexture(mainTexId, originalTexture);
                blendMaterial.SetTexture(uvTexId, resultRenderTexture);

                blendMaterial.SetFloat(mainVFlipId, 1);

                raw_image.gameObject.SetActive(true);
            }

            // When space key down, draw original image
            if (Input.GetKey(KeyCode.Space))
            {
                blendMaterial.SetFloat(blendFlagId, 0);
            }
            else
            {
                blendMaterial.SetFloat(blendFlagId, 1);
            }
        }
        void Update()
        {
            if (!ContentImageSource.IsPrepared || !StyleImageSource.IsPrepared || !modelPrepared)
            {
                return;
            }

            if (vggContentOutput == null)
            {
                float rawImageRatio = rawImageSize.x / rawImageSize.y;
                float ratio         = ContentImageSource.Width / (float)ContentImageSource.Height;
                raw_image.rectTransform.sizeDelta = new Vector2(ratio / rawImageRatio * rawImageSize.x, rawImageSize.y);

                SetShape();

                // texture & buffer allocate
                resultRenderTexture = new RenderTexture(OutputWidth, OutputHeight, 0, RenderTextureFormat.ARGBFloat);
                resultRenderTexture.enableRandomWrite = true;
                resultRenderTexture.Create();

                contentTexture = ContentImageSource.GetTexture(AiliaImageUtil.Crop.No);
                styleTexture   = StyleImageSource.GetTexture(AiliaImageUtil.Crop.No);
                ContentImageSource.Resize(InputWidth, InputHeight);
                StyleImageSource.Resize(InputWidth, InputHeight);
            }

            if (oneshot)
            {
                oneshot = false;

                // Make input data
                long start_time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
                InputDataPocessing(ContentImageSource.GetTexture(AiliaImageUtil.Crop.No), contentImage, true);
                InputDataPocessing(StyleImageSource.GetTexture(AiliaImageUtil.Crop.No), styleImage, true);
                long end_time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                // Predict
                long start_time2 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
                bool result      = ailiaModelVgg.Predict(vggContentOutput, contentImage);
                result = ailiaModelVgg.Predict(vggStyleOutput, styleImage);

                MiddleDataPocessing(vggContentOutput, vggStyleOutput, decoderInput);
                result = ailiaModelDecoder.Predict(output, decoderInput);
                // convert result to image
                OutputDataProcessing(output, resultRenderTexture);

                long end_time2 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                if (label_text != null)
                {
                    label_text.text = ((end_time - start_time) + (end_time2 - start_time2)).ToString() + "ms\n" + ailiaModelVgg.EnvironmentName();
                }

                // for viewer
                raw_image.texture = contentTexture;
                blendMaterial.SetTexture(mainTexId, contentTexture);
                blendMaterial.SetTexture(blendTexId, resultRenderTexture);

                blendMaterial.SetFloat(mainVFlipId, 0);
                blendMaterial.SetFloat(blendVFlipId, 1);
                blendMaterial.SetFloat(blendFlagId, 1);

                raw_image.gameObject.SetActive(true);
            }

            // When space key down, draw original image
            if (Input.GetKeyDown(KeyCode.Space))
            {
                previewMode++;
                if (previewMode > 2)
                {
                    previewMode = 0;
                }
                float rawImageRatio;
                float ratio;
                switch (previewMode)
                {
                case 0:
                    rawImageRatio = rawImageSize.x / rawImageSize.y;
                    ratio         = contentTexture.width / (float)contentTexture.height;
                    raw_image.rectTransform.sizeDelta = new Vector2(ratio / rawImageRatio * rawImageSize.x, rawImageSize.y);
                    blendMaterial.SetFloat(blendFlagId, 1);
                    blendMaterial.SetFloat(blendVFlipId, 1);
                    blendMaterial.SetTexture(blendTexId, resultRenderTexture);
                    mode_text.text = "ailia Adain\nSpace key down to switch image (<color=#f66>result</color> -> style -> original)";
                    break;

                case 1:
                    rawImageRatio = rawImageSize.x / rawImageSize.y;
                    ratio         = styleTexture.width / (float)styleTexture.height;
                    raw_image.rectTransform.sizeDelta = new Vector2(ratio / rawImageRatio * rawImageSize.x, rawImageSize.y);
                    blendMaterial.SetFloat(blendVFlipId, 0);
                    blendMaterial.SetTexture(blendTexId, styleTexture);
                    mode_text.text = "ailia Adain\nSpace key down to switch image (result -> <color=#f66>style</color> -> original)";
                    break;

                case 2:
                    rawImageRatio = rawImageSize.x / rawImageSize.y;
                    ratio         = contentTexture.width / (float)contentTexture.height;
                    raw_image.rectTransform.sizeDelta = new Vector2(ratio / rawImageRatio * rawImageSize.x, rawImageSize.y);
                    blendMaterial.SetFloat(blendFlagId, 0);
                    blendMaterial.SetFloat(blendVFlipId, 1);
                    mode_text.text = "ailia Adain\nSpace key down to switch image (result -> style -> <color=#f66>original</color>)";
                    break;
                }
            }
        }
Esempio n. 4
0
        void Update()
        {
            if (!AiliaImageSource.IsPrepared || !modelPrepared)
            {
                return;
            }

            if (output == null)
            {
                float rawImageRatio = rawImageSize.x / rawImageSize.y;
                float ratio         = AiliaImageSource.Width / (float)AiliaImageSource.Height;
                raw_image.rectTransform.sizeDelta = new Vector2(ratio / rawImageRatio * rawImageSize.x, rawImageSize.y);

                SetShape(imageManipulationModels);

                // texture & buffer allocate
                if (!gpu_mode || outputDataToTextureShader == null)
                {
                    resultTexture2D = new Texture2D(OutputWidth, OutputHeight, TextureFormat.RGBA32, false);
                }
                else
                {
                    resultRenderTexture = new RenderTexture(OutputWidth, OutputHeight, 0);
                    resultRenderTexture.enableRandomWrite = true;
                    resultRenderTexture.Create();
                }

                baseTexture = AiliaImageSource.GetTexture(new Rect(0, 0, AiliaImageSource.Width, AiliaImageSource.Height));

                AiliaImageSource.Resize(InputWidth, InputHeight);
                input       = new float[InputWidth * InputHeight * InputChannel];
                output      = new float[OutputWidth * OutputHeight * OutputChannel];
                outputImage = new Color32[OutputWidth * OutputHeight];
            }

            if (oneshot)
            {
                oneshot = false;

                // Make input data
                Rect rect       = new Rect(0, 0, InputWidth, InputHeight);
                long start_time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                Color32[] inputImage = null;
                if (!gpu_mode || inputDataProcessingShader == null)
                {
                    inputImage = AiliaImageSource.GetPixels32(rect, true);
                    InputDataPocessingCPU(imageManipulationModels, inputImage, input);
                }
                else
                {
                    originalTexture = AiliaImageSource.GetTexture(rect);
                    InputDataPocessing(imageManipulationModels, originalTexture, input, true);
                }
                long end_time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                // Predict
                long start_time2 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
                bool result      = ailiaModel.Predict(output, input);

                // convert result to image
                if (!gpu_mode || outputDataToTextureShader == null)
                {
                    OutputDataProcessingCPU(imageManipulationModels, output, outputImage, inputImage);
                    if (imageManipulationModels == ImageManipulationModels.Colorization)
                    {
                        resultTexture2D.SetPixels32(outputImage);
                        resultTexture2D.Apply();

                        {
                            var rTexture = AiliaImageUtil.ResizeTexture(resultTexture2D, baseTexture.width, baseTexture.height);
                            var base32   = baseTexture.GetPixels32();
                            var dst32    = rTexture.GetPixels32();

                            Color32[] dstColorBuffer = new Color32[dst32.Length];

                            for (int i = 0; i < dst32.Length; i++)
                            {
                                var px = i % baseTexture.width;
                                var py = (baseTexture.height - 1 - (i / baseTexture.width)) * baseTexture.width;

                                var baselab = AiliaColorConv.Color2Lab(base32[i]);
                                var dstlab  = AiliaColorConv.Color2Lab(dst32[px + py]);

                                var resultlab = new AiliaColorConv.LAB(baselab.L, dstlab.A, dstlab.B);
                                dstColorBuffer[px + py] = AiliaColorConv.Lab2Color(resultlab);
                            }

                            outputImage = dstColorBuffer;
                        }

                        resultTexture2D = new Texture2D(baseTexture.width, baseTexture.height);
                    }
                }
                else
                {
                    OutputDataProcessing(output, resultRenderTexture);
                }

                long end_time2 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                if (label_text != null)
                {
                    label_text.text = ((end_time - start_time) + (end_time2 - start_time2)).ToString() + "ms\n" + ailiaModel.EnvironmentName();
                }

                // for viewer
                if (!gpu_mode || inputDataProcessingShader == null)
                {
                    originalTexture = new Texture2D(InputWidth, InputHeight, TextureFormat.RGBA32, false);
                    originalTexture.SetPixels32(inputImage);
                    originalTexture.Apply();
                }
                raw_image.texture = originalTexture;
                blendMaterial.SetTexture(mainTexId, originalTexture);

                if (!gpu_mode || outputDataToTextureShader == null)
                {
                    resultTexture2D.SetPixels32(outputImage);
                    resultTexture2D.Apply();

                    blendMaterial.SetTexture(blendTexId, resultTexture2D);
                }
                else
                {
                    blendMaterial.SetTexture(blendTexId, resultRenderTexture);
                }

                blendMaterial.SetFloat(mainVFlipId, 0);
                blendMaterial.SetFloat(blendVFlipId, 1);

                raw_image.gameObject.SetActive(true);
            }

            // When space key down, draw original image
            if (Input.GetKey(KeyCode.Space))
            {
                blendMaterial.SetFloat(blendFlagId, 0);
            }
            else
            {
                blendMaterial.SetFloat(blendFlagId, 1);
            }
        }