Пример #1
0
 public Rect GetCropRect(Crop crop)
 {
     if (!IsPrepared)
     {
         return(Rect.zero);
     }
     return(AiliaImageUtil.GetCropRect(_texture, crop));
 }
 public Rect GetCropRect(Crop crop)
 {
     if (!IsPrepared)
     {
         return(Rect.zero);
     }
     return(AiliaImageUtil.GetCropRect((int)Width, (int)Height, crop));
 }
Пример #3
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);
            }
        }