forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalAssetManager.cs
More file actions
61 lines (49 loc) · 1.83 KB
/
Copy pathLocalAssetManager.cs
File metadata and controls
61 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Mediapipe;
using System;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
/// <summary>
/// Sample implementation of ResourceManager, that reads files from local filesystem.
/// This class is used in UnityEditor environment.
/// </summary>
public sealed class LocalAssetManager : ResourceManager {
private static readonly Lazy<LocalAssetManager> lazy = new Lazy<LocalAssetManager>(() => new LocalAssetManager());
public static LocalAssetManager Instance { get { return lazy.Value; } }
private readonly static string ModelRootPath = Path.Combine(Application.dataPath, "MediaPipe", "SDK", "Models");
private LocalAssetManager() : base() {}
/// <summary>dummy method</summary>
public async Task LoadAllAssetsAsync() {
await Task.CompletedTask;
}
protected override string CacheFileFromAsset(string assetPath) {
var assetName = GetAssetName(assetPath);
var localPath = GetLocalFilePath(assetName);
if (File.Exists(localPath)) {
return localPath;
}
return null;
}
protected override bool ReadFile(string path, IntPtr dst) {
try {
Debug.Log(path);
var localPath = CacheFileFromAsset(path);
var asset = File.ReadAllBytes(localPath);
using (var srcStr = new StdString(asset)) {
srcStr.Swap(new StdString(dst, false));
}
return true;
} catch (Exception e) {
Debug.Log($"Failed to read file `{path}`: ${e.ToString()}");
return false;
}
}
private string GetAssetName(string assetPath) {
var assetName = Path.GetFileNameWithoutExtension(assetPath);
var extension = Path.GetExtension(assetPath);
return (extension == ".tflite" || extension == ".bytes") ? $"{assetName}.bytes" : $"{assetName}.txt";
}
private string GetLocalFilePath(string assetName) {
return Path.Combine(ModelRootPath, assetName);
}
}