Skip to content

Commit

Permalink
Decoupled expressions from module, now in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
dfgHiatus committed Sep 18, 2023
1 parent fc24fa3 commit 96a4e8c
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 97 deletions.
130 changes: 82 additions & 48 deletions BabbleExpressions.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,86 @@
namespace VRCFaceTracking.Babble;
public partial class BabbleOSC
using Newtonsoft.Json;
using System.Reflection;
using VRCFaceTracking.Core.Params.Expressions;
using VRCFaceTracking.Babble.Collections;

namespace VRCFaceTracking.Babble;
public static class BabbleExpressions
{
public Dictionary<string, float> BabbleExpressionMap = new()
private const string BabbleExpressionsPath = "BabbleExpressions.json";

public static readonly TwoKeyDictionary<UnifiedExpressions, string, float> BabbleExpressionMap;
static BabbleExpressions()
{
{ "/cheekPuffLeft", 0f },
{ "/cheekPuffRight", 0f },
{ "/cheekSuckLeft", 0f },
{ "/cheekSuckRight", 0f },
{ "/jawOpen", 0f },
{ "/jawForward", 0f },
{ "/jawLeft", 0f },
{ "/jawRight", 0f },
{ "/noseSneerLeft", 0f },
{ "/noseSnerrRight", 0f },
{ "/mouthFunnel", 0f },
{ "/mouthPucker", 0f },
{ "/mouthLeft", 0f },
{ "/mouthRight", 0f },
{ "/mouthRollUpper", 0f },
{ "/mouthRollLower", 0f },
{ "/mouthShrugUpper", 0f },
{ "/mouthShrugLower", 0f },
{ "/mouthClose", 0f },
{ "/mouthSmileLeft", 0f },
{ "/mouthSmileRight", 0f },
{ "/mouthFrownLeft", 0f },
{ "/mouthFrownRight", 0f },
{ "/mouthDimpleLeft", 0f },
{ "/mouthDimpleRight", 0f },
{ "/mouthUpperUpLeft", 0f },
{ "/mouthUpperUpRight", 0f },
{ "/mouthLowerDownLeft", 0f },
{ "/mouthLowerDownRight", 0f },
{ "/mouthPressLeft", 0f },
{ "/mouthPressRight", 0f },
{ "/mouthStretchLeft", 0f },
{ "/mouthStretchRight", 0f },
{ "/tongueOut", 0f },
{ "/tongueUp", 0f },
{ "/tongueDown", 0f },
{ "/tongueLeft", 0f },
{ "/tongueRight", 0f },
{ "/tongueRoll", 0f },
{ "/tongueBendDown", 0f },
{ "/tongueCurlUp", 0f },
{ "/tongueSquish", 0f },
{ "/tongueFlat", 0f },
{ "/tongueTwistLeft", 0f },
{ "/tognueTwistRight", 0f }
var expressions = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
BabbleExpressionsPath);
var converter = new TwoKeyDictionaryConverter<UnifiedExpressions, string, float>();
if (!File.Exists(expressions))
{
string json = JsonConvert.SerializeObject(
DefaultBabbleExpressionMap, converter);
File.WriteAllText(expressions, json);
}

BabbleExpressionMap = JsonConvert.DeserializeObject
<TwoKeyDictionary<UnifiedExpressions, string, float>>
(File.ReadAllText(expressions), converter)!;
}

private static readonly TwoKeyDictionary<UnifiedExpressions, string, float> DefaultBabbleExpressionMap = new()
{
{ UnifiedExpressions.CheekPuffLeft, "/cheekPuffLeft", 0f },
{ UnifiedExpressions.CheekPuffRight, "/cheekPuffRight", 0f },
{ UnifiedExpressions.CheekSuckLeft, "/cheekSuckLeft", 0f },
{ UnifiedExpressions.CheekSuckRight, "/cheekSuckRight", 0f },
{ UnifiedExpressions.JawOpen, "/jawOpen", 0f },
{ UnifiedExpressions.JawForward, "/jawForward", 0f },
{ UnifiedExpressions.JawLeft, "/jawLeft", 0f },
{ UnifiedExpressions.JawRight, "/jawRight", 0f },
{ UnifiedExpressions.NoseSneerLeft, "/noseSneerLeft", 0f },
{ UnifiedExpressions.NoseSneerRight, "/noseSneerRight", 0f },
{ UnifiedExpressions.LipFunnelLowerLeft, "/mouthFunnel", 0f },
{ UnifiedExpressions.LipFunnelLowerRight, "/mouthFunnel", 0f },
{ UnifiedExpressions.LipFunnelUpperLeft, "/mouthFunnel", 0f },
{ UnifiedExpressions.LipFunnelUpperRight, "/mouthFunnel", 0f },
{ UnifiedExpressions.LipPuckerLowerLeft, "/mouthPucker", 0f },
{ UnifiedExpressions.LipPuckerLowerRight, "/mouthPucker", 0f },
{ UnifiedExpressions.LipPuckerUpperLeft, "/mouthPucker", 0f },
{ UnifiedExpressions.LipPuckerUpperRight, "/mouthPucker", 0f },
{ UnifiedExpressions.MouthPressLeft, "/mouthLeft", 0f },
{ UnifiedExpressions.MouthPressRight, "/mouthRight", 0f },
{ UnifiedExpressions.LipSuckUpperLeft, "/mouthRollUpper", 0f },
{ UnifiedExpressions.LipSuckUpperRight, "/mouthRollUpper", 0f },
{ UnifiedExpressions.LipSuckLowerLeft, "/mouthRollLower", 0f },
{ UnifiedExpressions.LipSuckLowerRight, "/mouthRollLower", 0f },
{ UnifiedExpressions.MouthRaiserUpper, "/mouthShrugUpper", 0f },
{ UnifiedExpressions.MouthRaiserLower, "/mouthShrugLower", 0f },
{ UnifiedExpressions.MouthClosed, "/mouthClose", 0f },
{ UnifiedExpressions.MouthCornerPullLeft, "/mouthSmileLeft", 0f },
{ UnifiedExpressions.MouthCornerPullRight, "/mouthSmileRight", 0f },
{ UnifiedExpressions.MouthFrownLeft, "/mouthFrownLeft", 0f },
{ UnifiedExpressions.MouthFrownRight, "/mouthFrownRight", 0f },
{ UnifiedExpressions.MouthDimpleLeft, "/mouthDimpleLeft", 0f },
{ UnifiedExpressions.MouthDimpleRight, "/mouthDimpleRight", 0f },
{ UnifiedExpressions.MouthUpperUpLeft, "/mouthUpperUpLeft", 0f },
{ UnifiedExpressions.MouthUpperUpRight, "/mouthUpperUpRight", 0f },
{ UnifiedExpressions.MouthLowerDownLeft, "/mouthLowerDownLeft", 0f },
{ UnifiedExpressions.MouthLowerDownRight, "/mouthLowerDownRight", 0f },
{ UnifiedExpressions.MouthPressLeft, "/mouthPressLeft", 0f },
{ UnifiedExpressions.MouthPressRight, "/mouthPressRight", 0f },
{ UnifiedExpressions.MouthStretchLeft, "/mouthStretchLeft", 0f },
{ UnifiedExpressions.MouthStretchRight, "/mouthStretchRight", 0f },
{ UnifiedExpressions.TongueOut, "/tongueOut", 0f },
{ UnifiedExpressions.TongueUp, "/tongueUp", 0f },
{ UnifiedExpressions.TongueDown, "/tongueDown", 0f },
{ UnifiedExpressions.TongueLeft, "/tongueLeft", 0f },
{ UnifiedExpressions.TongueRight, "/tongueRight", 0f },
{ UnifiedExpressions.TongueRoll, "/tongueRoll", 0f },
{ UnifiedExpressions.TongueBendDown, "/tongueBendDown", 0f },
{ UnifiedExpressions.TongueCurlUp, "/tongueCurlUp", 0f },
{ UnifiedExpressions.TongueSquish, "/tongueSquish", 0f },
{ UnifiedExpressions.TongueFlat, "/tongueFlat", 0f },
{ UnifiedExpressions.TongueTwistLeft, "/tongueTwistLeft", 0f },
{ UnifiedExpressions.TongueTwistRight, "/tongueTwistRight", 0 }
};
}
11 changes: 5 additions & 6 deletions BabbleOSC.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net.Sockets;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using VRCFaceTracking.Core.OSC;

namespace VRCFaceTracking.Babble;
public partial class BabbleOSC

public class BabbleOSC
{
private Socket _receiver;
private bool _loop = true;
Expand Down Expand Up @@ -142,9 +141,9 @@ private void ListenLoop()
var length = _receiver.Receive(buffer);

Msg msg = ParseOSC(buffer, length);
if (msg.success && BabbleExpressionMap.ContainsKey(msg.address))
if (msg.success && BabbleExpressions.BabbleExpressionMap.ContainsKey2(msg.address))
{
BabbleExpressionMap[msg.address] = msg.value;
BabbleExpressions.BabbleExpressionMap.SetByKey2(msg.address, msg.value);
}
}
else
Expand Down
46 changes: 6 additions & 40 deletions BabbleVRC.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using VRCFaceTracking.Core.Params.Data;
using VRCFaceTracking.Core.Params.Expressions;

namespace VRCFaceTracking.Babble;
Expand All @@ -16,7 +17,7 @@ public override (bool eyeSuccess, bool expressionSuccess) Initialize(bool eyeAva
Assembly a = Assembly.GetExecutingAssembly();
var hmdStream = a.GetManifestResourceStream
("VRCFaceTracking.Babble.BabbleLogo.png");
streams.Add(hmdStream);
streams.Add(hmdStream!);
ModuleInformation = new ModuleMetadata()
{
Name = "Project Babble Face Tracking\nInference Model v2.0.0",
Expand All @@ -29,44 +30,9 @@ public override (bool eyeSuccess, bool expressionSuccess) Initialize(bool eyeAva
public override void Teardown() => babbleOSC.Teardown();
public override void Update()
{
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.TongueOut].Weight = babbleOSC.BabbleExpressionMap["/tongueOut"];

UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.JawOpen].Weight = babbleOSC.BabbleExpressionMap["/jawOpen"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.JawLeft].Weight = -babbleOSC.BabbleExpressionMap["/jawLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.JawRight].Weight = -babbleOSC.BabbleExpressionMap["/jawRight"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.JawForward].Weight = babbleOSC.BabbleExpressionMap["/jawForward"];

UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.NoseSneerLeft].Weight = -babbleOSC.BabbleExpressionMap["/noseSneerLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.NoseSneerRight].Weight = -babbleOSC.BabbleExpressionMap["/noseSneerRight"];

UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.LipPuckerLowerLeft].Weight = babbleOSC.BabbleExpressionMap["/mouthPucker"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.LipPuckerLowerRight].Weight = babbleOSC.BabbleExpressionMap["/mouthPucker"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.LipPuckerUpperLeft].Weight = babbleOSC.BabbleExpressionMap["/mouthPucker"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.LipPuckerUpperRight].Weight = babbleOSC.BabbleExpressionMap["/mouthPucker"];

UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.LipFunnelLowerLeft].Weight = babbleOSC.BabbleExpressionMap["/mouthFunnel"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.LipFunnelLowerRight].Weight = babbleOSC.BabbleExpressionMap["/mouthFunnel"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.LipFunnelUpperLeft].Weight = babbleOSC.BabbleExpressionMap["/mouthFunnel"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.LipFunnelUpperRight].Weight = babbleOSC.BabbleExpressionMap["/mouthFunnel"];

UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthUpperUpLeft].Weight = -babbleOSC.BabbleExpressionMap["/mouthUpperUpLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthUpperUpRight].Weight = -babbleOSC.BabbleExpressionMap["/mouthUpperUpRight"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthLowerDownLeft].Weight = -babbleOSC.BabbleExpressionMap["/mouthLowerDownLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthLowerDownRight].Weight = -babbleOSC.BabbleExpressionMap["/mouthLowerDownRight"];

UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthPressLeft].Weight = -babbleOSC.BabbleExpressionMap["/mouthPressLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthPressRight].Weight = -babbleOSC.BabbleExpressionMap["/mouthPressRight"];

UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthStretchLeft].Weight = -babbleOSC.BabbleExpressionMap["/mouthStretchLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthStretchRight].Weight = -babbleOSC.BabbleExpressionMap["/mouthStretchRight"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthDimpleLeft].Weight = -babbleOSC.BabbleExpressionMap["/mouthDimpleLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthDimpleRight].Weight = -babbleOSC.BabbleExpressionMap["/mouthDimpleRight"];

UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthCornerPullLeft].Weight = -babbleOSC.BabbleExpressionMap["/mouthSmileLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthCornerPullRight].Weight = -babbleOSC.BabbleExpressionMap["/mouthSmileRight"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthFrownLeft].Weight = -babbleOSC.BabbleExpressionMap["/mouthFrownLeft"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.MouthFrownRight].Weight = -babbleOSC.BabbleExpressionMap["/mouthFrownRight"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.CheekPuffLeft].Weight = babbleOSC.BabbleExpressionMap["/cheekPuff"];
UnifiedTracking.Data.Shapes[(int)UnifiedExpressions.CheekPuffRight].Weight = babbleOSC.BabbleExpressionMap["/cheekPuff"];
foreach (var unifiedExpression in BabbleExpressions.BabbleExpressionMap)
{
UnifiedTracking.Data.Shapes[(int) unifiedExpression].Weight = BabbleExpressions.BabbleExpressionMap.GetByKey1 (unifiedExpression);
}
}
}
Loading

0 comments on commit 96a4e8c

Please sign in to comment.