Created
September 12, 2020 10:14
-
-
Save rkandas/76823b3b5f93b40cc1231dd1385775cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using TensorFlowLite; | |
using UnityEngine; | |
[RequireComponent(typeof(Camera))] | |
[ExecuteInEditMode] | |
public class StyleTransferPostProcessing : MonoBehaviour | |
{ | |
[SerializeField, FilePopup("*.tflite")] string predictionFileName = "style_predict_quantized_256.tflite"; | |
[SerializeField, FilePopup("*.tflite")] string transferFileName = "style_transfer_quantized_dynamic.tflite"; | |
[SerializeField] Texture2D styleImage = null; | |
float[] styleBottleneck; | |
[SerializeField] ComputeShader compute = null; | |
StyleTransfer styleTransfer; | |
[SerializeField] Material adaptiveBlend; | |
[SerializeField] private bool blendWithSource; | |
void Start() | |
{ | |
string predictionModelPath = Path.Combine(Application.streamingAssetsPath, predictionFileName); | |
using (var predict = new StylePredict(predictionModelPath)) | |
{ | |
predict.Invoke(styleImage); | |
styleBottleneck = predict.GetStyleBottleneck(); | |
} | |
string transferModelPath = Path.Combine(Application.streamingAssetsPath, transferFileName); | |
styleTransfer = new StyleTransfer(transferModelPath, styleBottleneck, compute); | |
} | |
void OnDestroy() | |
{ | |
styleTransfer?.Dispose(); | |
} | |
private void OnRenderImage(RenderTexture src, RenderTexture dest) | |
{ | |
if (styleTransfer != null) | |
{ | |
styleTransfer.Invoke(src); | |
var renderTex = styleTransfer.GetResultTexture(); | |
if (blendWithSource) | |
{ | |
Graphics.Blit(src, dest); | |
Graphics.Blit(renderTex, dest, adaptiveBlend); | |
} | |
else | |
{ | |
Graphics.Blit(renderTex,dest); | |
} | |
} | |
else | |
{ | |
Graphics.Blit(src,dest); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment