Skip to content

Commit

Permalink
add post request
Browse files Browse the repository at this point in the history
  • Loading branch information
Spyispie120 committed Dec 4, 2019
1 parent 550d737 commit 0dd99d5
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Users\\ducng\\Anaconda3\\python.exe"
}
Empty file added Classifier/__init__.py
Empty file.
Binary file added Classifier/__pycache__/net.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void OnClickCollectSingleData()

}

private void FillInData(float[] data, GameObject body)
public void FillInData(float[] data, GameObject body)
{
int index = 0;
Transform baseJoint = null;
Expand All @@ -100,6 +100,7 @@ private void FillInData(float[] data, GameObject body)
index += 3;
}
}

private void InitDic()
{
foreach (int action in Enum.GetValues(typeof(GameAction.Action)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,119 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameAction;
using System;
using Action = GameAction.Action;
using UnityEngine.Networking;

public class GameActionManager : MonoBehaviour
{
// Start is called before the first frame update
public GameObject player;
public DataManager dataManager;
public Action gameAction = Action.STAND;
public static string URL = "http://localhost:5000/get_inference";

private bool isStreaming;
private IEnumerator stream;
void Start()
{

}

public void StartTakingDataPose()
{
stream = TakingPoseData(10);
isStreaming = !isStreaming;

if(!isStreaming)
{
StopAllCoroutines();
}
}

/// <summary>
///
/// </summary>
/// <param name="n">Frame per second</param>
/// <returns></returns>
public IEnumerator TakingPoseData(int n)
{
while (isStreaming)
{
for (int i = 0; i < n; i++)
{
foreach (KeyValuePair<ulong, GameObject> entry in dataManager.bsv.getBodies())
{
GameObject body = entry.Value;

if (body == null)
{
Debug.LogError("No body found");
}

float[] data = new float[25 * 3];
dataManager.FillInData(data, body);
Array.ForEach(data, write);
StartCoroutine(SendPoseData(data, ProcessResponse));

}


yield return new WaitForSeconds(1.0f / n);
}
}
}

private void write(float val)
{
Debug.Log(val);
}

private IEnumerator SendPoseData(float[] data, Action<string> response)
{
Debug.Log("Send data to Server");
System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
string url = URL;
Debug.Log("url: " + url);
SerializedData serializedData;
serializedData.joint_set = data;

string body = JsonUtility.ToJson(serializedData);

// Post request
UnityWebRequest www = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
byte[] jsonToSend = System.Text.Encoding.UTF8.GetBytes(body);
www.SetRequestHeader("content-type", "application/json");
www.uploadHandler = new UploadHandlerRaw(jsonToSend)
{
contentType = "application/json"
};
www.downloadHandler = new DownloadHandlerBuffer();

Debug.Log("Send");
yield return www.SendWebRequest(); // SEND REQUEST

if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
response("");
}
else
{
Debug.Log("Form upload complete!");
response(www.downloadHandler.text);
}

Debug.Log("Server send/receive: " + stopwatch.ElapsedMilliseconds);
}

private void ProcessResponse(string response)
{
Debug.Log(response);
}



// Update is called once per frame
void Update()
{
Expand All @@ -28,3 +129,9 @@ void Update()
}
}
}

[Serializable]
struct SerializedData {
[SerializeField]
public float[] joint_set;
}

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

Binary file modified Kinect455/Kinect4Win/KinectForWindow/Library/ArtifactDB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Kinect455/Kinect4Win/KinectForWindow/Library/SourceAssetDB
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"m_ExpandedPrefabGameObjectFileIDs":[],"m_ExpandedSceneGameObjectInstanceIDs":[-1170],"m_ScrollY":0.0,"m_LastClickedFileID":0,"m_LastClickedInstanceID":0}
{"m_ExpandedPrefabGameObjectFileIDs":[],"m_ExpandedSceneGameObjectInstanceIDs":[-9090,-1170,13092],"m_ScrollY":0.0,"m_LastClickedFileID":0,"m_LastClickedInstanceID":-9090}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cameraMode":{"drawMode":0,"name":"Shaded","section":"Shading Mode"},"sceneLighting":true,"audioPlay":false,"sceneViewState":{"showFog":true,"showMaterialUpdate":false,"showSkybox":true,"showFlares":true,"showImageEffects":true,"showParticleSystems":true},"in2DMode":true,"pivot":{"x":9.762249946594239,"y":4.032095909118652,"z":3.21484375},"rotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"size":28.566701889038087,"orthographic":true}
{"cameraMode":{"drawMode":0,"name":"Shaded","section":"Shading Mode"},"sceneLighting":true,"audioPlay":false,"sceneViewState":{"showFog":true,"showMaterialUpdate":false,"showSkybox":true,"showFlares":true,"showImageEffects":true,"showParticleSystems":true},"in2DMode":true,"pivot":{"x":607.6792602539063,"y":239.8092803955078,"z":-118.7295913696289},"rotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"size":677.44140625,"orthographic":true}
11 changes: 7 additions & 4 deletions ServerCode/pyServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
import time
from flask import Flask, jsonify, request
from os import path
import sys

sys.path.insert(0, "../Classifier/")
path_directory = path.join(path.dirname(
path.realpath(__file__)), './../Classifier')

from path.join(path_directory, '/net') import Net
from net import Net
# from path.join(path_directory, '/net') import Net

app = Flask(__name__)


# initialize the model
PATH = path.join(path_directory, '/model.pth')
model = Net(*args, **kwargs)
PATH = path.join(path_directory, 'model.pth')
model = Net()
model.load_state_dict(torch.load(PATH))
model.eval()

Expand Down Expand Up @@ -45,7 +48,7 @@ def get_inference(skeleton):

# return a json str, pose ranges from 0 - 6
# refer to label dict for pose classes
@app.route('/get_inference', methods='POST')
@app.route('/get_inference', methods=['POST'])
def getPose():
skeleton = json.loads(request.get_json)[
'joint_set'] # change this name later
Expand Down

0 comments on commit 0dd99d5

Please sign in to comment.