Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions Assets/BicepsCurl.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
2 changes: 2 additions & 0 deletions Assets/BicepsCurl/BicepsCurlUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions Assets/BodyTracking.cs → Assets/BicepsCurl/BodyTracking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class BodyTracking : MonoBehaviour
public float scaleY = 5.0f;
public float scaleZ = 2.0f;

[Header("Calibration")]
public float calibratedScale = 1.0f;
public Vector3 calibratedOffset = Vector3.zero;
public float distanceScale = 1.0f;

[Header("Smoothing")]
public bool enableSmoothing = true;
public float smoothingFactor = 0.8f;
Expand All @@ -37,6 +42,9 @@ public class BodyTracking : MonoBehaviour
private Vector3[] previousPositions = new Vector3[33];
private bool hasInitialized = false;

// Adicione este dicionário no início da classe para armazenar posições anteriores
private Dictionary<int, Vector3> smoothedPositions = new Dictionary<int, Vector3>();

// MediaPipe Pose connections (simplified)
private int[,] connections = new int[,]
{
Expand Down Expand Up @@ -159,6 +167,9 @@ void UpdateLandmarkPosition(int index, string name, Landmark landmark)

Vector3 targetPosition = new Vector3(x, y, z);

// Apply calibration
targetPosition = (targetPosition + calibratedOffset) * calibratedScale * distanceScale;

// Apply smoothing if enabled
if (enableSmoothing && hasInitialized)
{
Expand Down Expand Up @@ -186,6 +197,18 @@ void UpdateLandmarkPosition(int index, string name, Landmark landmark)
{
bodyPoints[index].name = $"Landmark_{index}_{name}";
}

Vector3 newPosition = targetPosition;
if (smoothedPositions.ContainsKey(index))
{
newPosition = Vector3.Lerp(smoothedPositions[index], newPosition, 1 - smoothingFactor);
smoothedPositions[index] = newPosition;
}
else
{
smoothedPositions[index] = newPosition;
}
bodyPoints[index].transform.position = newPosition;
}

void SetupConnectionLines()
Expand Down Expand Up @@ -361,4 +384,36 @@ public void ToggleSmoothing(bool enable)
{
enableSmoothing = enable;
}

// Calibration methods
public void SetCalibration(float scale, Vector3 offset)
{
calibratedScale = scale;
calibratedOffset = offset;
Debug.Log($"Calibração aplicada - Escala: {scale:F2}, Offset: {offset}");
}

public void SetDistanceScale(float scale)
{
distanceScale = scale;
}

// Exemplo de verificação da largura dos ombros (shoulder_width)
public void CheckShoulderWidth()
{
// Supondo que os índices 11 e 12 correspondam aos ombros esquerdo e direito
Vector3 leftShoulder = GetLandmarkWorldPosition(11);
Vector3 rightShoulder = GetLandmarkWorldPosition(12);

float shoulder_width = Vector3.Distance(leftShoulder, rightShoulder);

if (shoulder_width < 1.0f)
{
Debug.Log("Aproxime-se da câmera");
}
else if (shoulder_width > 3.0f)
{
Debug.Log("Afaste-se da câmera");
}
}
}
2 changes: 2 additions & 0 deletions Assets/BicepsCurl/BodyTracking.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Exercice.cs → Assets/BicepsCurl/Exercice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void Start()
// Find UDPReceive if not assigned
if (udpReceive == null)
{
udpReceive = FindObjectOfType<UDPReceive>();
udpReceive = FindFirstObjectByType<UDPReceive>();
}

// Setup UI
Expand Down
2 changes: 2 additions & 0 deletions Assets/BicepsCurl/Exercice.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
2 changes: 2 additions & 0 deletions Assets/BicepsCurl/LineCode.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

182 changes: 182 additions & 0 deletions Assets/BicepsCurl/PythonCommandSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

public class PythonCommandSender : MonoBehaviour
{
[Header("Communication Settings")]
public string pythonIP = "127.0.0.1";
public int commandPort = 5053;
public bool enableLogging = true;

private UdpClient commandClient;
private Thread commandThread;
private bool isRunning = false;

// Command queue for thread-safe communication
private System.Collections.Generic.Queue<string> commandQueue = new System.Collections.Generic.Queue<string>();
private readonly object queueLock = new object();

void Start()
{
InitializeCommandSender();
}

void InitializeCommandSender()
{
try
{
commandClient = new UdpClient();
isRunning = true;

// Start command sending thread
commandThread = new Thread(ProcessCommandQueue);
commandThread.IsBackground = true;
commandThread.Start();

if (enableLogging)
Debug.Log($"Python Command Sender initialized on {pythonIP}:{commandPort}");
}
catch (System.Exception e)
{
Debug.LogError($"Failed to initialize command sender: {e.Message}");
}
}

void ProcessCommandQueue()
{
while (isRunning)
{
string command = null;

lock (queueLock)
{
if (commandQueue.Count > 0)
{
command = commandQueue.Dequeue();
}
}

if (command != null)
{
SendCommandToPython(command);
}

Thread.Sleep(10); // Small delay to prevent excessive CPU usage
}
}

void SendCommandToPython(string jsonCommand)
{
try
{
byte[] data = Encoding.UTF8.GetBytes(jsonCommand);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(pythonIP), commandPort);

commandClient.Send(data, data.Length, endpoint);

if (enableLogging)
Debug.Log($"Sent command to Python: {jsonCommand}");
}
catch (System.Exception e)
{
Debug.LogError($"Failed to send command to Python: {e.Message}");
}
}

public void SendExerciseModeChange(string newMode)
{
var command = new
{
command = "change_exercise_mode",
exercise_mode = newMode,
timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
source = "unity_auto_detection"
};

string jsonCommand = JsonUtility.ToJson(command);

lock (queueLock)
{
commandQueue.Enqueue(jsonCommand);
}
}

public void SendCalibrationData(float scale, Vector3 offset)
{
var command = new
{
command = "update_calibration",
scale = scale,
offset_x = offset.x,
offset_y = offset.y,
offset_z = offset.z,
timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
};

string jsonCommand = JsonUtility.ToJson(command);

lock (queueLock)
{
commandQueue.Enqueue(jsonCommand);
}
}

public void SendPerformanceSettings(int skipFrames, bool showWindow)
{
var command = new
{
command = "update_performance",
skip_frames = skipFrames,
show_window = showWindow,
timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
};

string jsonCommand = JsonUtility.ToJson(command);

lock (queueLock)
{
commandQueue.Enqueue(jsonCommand);
}
}

public void SendGameStateChange(string gameState, float bpm = 120f)
{
var command = new
{
command = "game_state_change",
state = gameState, // "started", "paused", "stopped"
bpm = bpm,
timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
};

string jsonCommand = JsonUtility.ToJson(command);

lock (queueLock)
{
commandQueue.Enqueue(jsonCommand);
}
}

void OnApplicationQuit()
{
isRunning = false;

if (commandThread != null && commandThread.IsAlive)
{
commandThread.Join(1000); // Wait up to 1 second
}

if (commandClient != null)
{
commandClient.Close();
}
}

void OnDestroy()
{
OnApplicationQuit();
}
}
2 changes: 2 additions & 0 deletions Assets/BicepsCurl/PythonCommandSender.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Assets/BicepsCurlUI.cs.meta

This file was deleted.

2 changes: 0 additions & 2 deletions Assets/BodyTracking.cs.meta

This file was deleted.

Loading