Created
January 24, 2018 10:07
-
-
Save kasperkamperman/32319cb2c265f2d1fa833334ad7db979 to your computer and use it in GitHub Desktop.
raw sketch to test Azure posting and feedback in Processing
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
// download HttpClient 4.5.5 from https://hc.apache.org/downloads.cgi | |
// Download Binary 4.5.5 zip: http://ftp.tudelft.nl/apache//httpcomponents/httpclient/binary/httpcomponents-client-4.5.5-bin.zip | |
// Create a folder named code as a subfolder from this sketch | |
// Copy all the files in the "lib" folder to the code folder. | |
// Microsoft Cognetive Services - FACE API | |
// Docs: https://docs.microsoft.com/en-us/azure/cognitive-services/face/quickstarts/java | |
// Thanks to: https://stackoverflow.com/questions/39541634/how-to-send-a-local-image-instead-of-url-to-microsoft-cognitive-face-api-using-j | |
import java.net.URI; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.client.utils.URIBuilder; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.util.EntityUtils; | |
//import org.json.JSONObject; | |
import processing.video.*; | |
//import java.util.Map; | |
import java.util.*; //hashmap, iterator | |
//import java.util.TreeMap; | |
//import java.lang.Double; | |
// FACE API | |
String subscriptionKey = "<your own api key over here>"; | |
String uriBase = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect";//analyze"; | |
//https://docs.microsoft.com/en-us/azure/cognitive-services/face/quickstarts/java | |
HttpClient httpclient = new DefaultHttpClient(); | |
URIBuilder builder; | |
PImage img; | |
Capture cam; | |
PGraphics2D videoScreenshot; | |
// camera in the setup in combination with other code, sometimes give a | |
// Waited for 5000s error: | |
// java.lang.RuntimeException: Waited 5000ms for: <281471a9, 26d38adb>[count 2, qsz 0, owner <main-FPSAWTAnimator#00-Timer0>] - <main-FPSAWTAnimator#00-Timer0-FPSAWTAnimator#00-Timer1> | |
// so we will do it in the draw loop | |
boolean cameraInitDone = false; | |
//HashMap<String, Object> map = new HashMap<String, Object>(); | |
TreeMap<String, Object> map = new TreeMap<String, Object>(); | |
public void setup() | |
{ | |
size(800,600,P2D); | |
smooth(); | |
//videoScreenshot = (PGraphics2D) createGraphics(100,100, P2D); | |
img = loadImage("RH_Louise_Lillian_Gish.jpg"); | |
} | |
void draw() { | |
background(0); | |
//println(cameraInitDone); | |
if(!cameraInitDone) { | |
fill(255); | |
text("waiting for camera",10,10); | |
} | |
if(frameCount>1) { | |
if(!cameraInitDone) setupCamera(); | |
} | |
if(cameraInitDone) { | |
if (cam.available() == true) { | |
cam.read(); | |
//videoScreenshot.beginDraw(); | |
//videoScreenshot.image(cam, 0, 0 , videoScreenshot.width, videoScreenshot.height); | |
//videoScreenshot.endDraw(); | |
} | |
image(videoScreenshot,0,0,width,height); | |
image(cam,0,0, cam.width/10, cam.height/10); | |
} | |
} | |
void keyPressed() { | |
if(cameraInitDone) { | |
videoScreenshot.beginDraw(); | |
videoScreenshot.image(cam, 0, 0 , videoScreenshot.width, videoScreenshot.height); | |
videoScreenshot.endDraw(); | |
videoScreenshot.save(dataPath("videoscreenshot.jpg")); | |
thread("requestToAzureFaceAPI"); | |
} | |
} | |
void requestToAzureFaceAPI() { | |
try | |
{ | |
URIBuilder builder = new URIBuilder(uriBase); | |
// Request parameters. All of them are optional. | |
builder.setParameter("returnFaceId", "false"); | |
builder.setParameter("returnFaceLandmarks", "true"); | |
builder.setParameter("returnFaceAttributes", "age,gender,smile,emotion,glasses,hair,facialHair"); | |
// Prepare the URI for the REST API call. | |
URI uri = builder.build(); | |
HttpPost request = new HttpPost(uri); | |
// Request headers. | |
//request.setHeader("Content-Type", "application/json"); | |
request.setHeader("Content-Type", "application/octet-stream"); | |
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey); | |
File file = new File(dataPath("videoscreenshot.jpg")); | |
FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM); | |
request.setEntity(reqEntity); | |
// Execute the REST API call and get the response entity. | |
HttpResponse response = httpclient.execute(request); | |
HttpEntity entity = response.getEntity(); | |
if (entity != null) | |
{ | |
// Format and display the JSON response. | |
println("REST Response:\n"); | |
String data = EntityUtils.toString(entity).trim(); | |
//println(data); | |
JSONArray jsonArray = parseJSONArray(data); | |
//JSONObject jsonObject = jsonArray.getJSONObject(0); | |
JSONObject jsonObject = jsonArray.getJSONObject(0); //parseJSONObject(data); | |
// display the keys, however only upper keys are displayed. | |
String[] properties = (String[]) jsonObject.keys().toArray(new String[jsonObject.size()]); | |
printArray(properties); | |
// | |
handleJsonObject(null, jsonObject); | |
//println("done"); | |
println("\n\nMap:"); | |
for (Map.Entry e : map.entrySet()) { | |
println(e.getKey(), "=", e.getValue()); | |
} | |
float val = (float) map.get("faceLandmarks.noseRightAlarOutTip.y"); | |
println("test float " + val); | |
//for (Map.Entry e : map.entrySet()) { | |
// String outerKey = e.getKey(); | |
// String innerValue = e.getValue().get(innerKey); | |
// System.out.println("OuterKey: " + outerKey + " InnerKey: " + innerKey + " VALUE:" + innerValue); | |
//} | |
//val = (float) map.get("faceRectangle.height"); | |
//println("Casey is " + val); | |
//String s = map.get("faceAttributes.hair.hairColor[5].color"); | |
//println("Casey is " + s); | |
if (data.charAt(0) == '[') { | |
//JSONArray jsonArray = parseJSONArray(data); | |
//JSONObject jsonObject = jsonArray.getJSONObject(0); | |
println(jsonObject.keys()); | |
println(jsonObject.getJSONObject("faceRectangle")); | |
println(jsonObject.getJSONObject("faceAttributes")); | |
JSONObject faceAttributes = jsonObject.getJSONObject("faceAttributes"); | |
println(faceAttributes.getJSONObject("facialHair")); | |
println(faceAttributes.getJSONObject("emotion")); | |
JSONObject hair = faceAttributes.getJSONObject("hair"); | |
println("bald : "+hair.getInt("bald")); | |
println("invisible : "+hair.getBoolean("invisible")); | |
JSONArray hairColor = hair.getJSONArray("hairColor"); | |
println("hairColor : "+hair.getBoolean("invisible")); | |
println("glasses : "+faceAttributes.getString("glasses")); | |
println("smile : "+faceAttributes.getInt("smile")); | |
println("gender : "+faceAttributes.getString("gender")); | |
println("age : "+faceAttributes.getInt("age")); | |
//for (int i = 1; i < jsonObject.size(); i++) { | |
// JSONObject result = jsonObject.getJSONObject(i); | |
//} | |
//println(jsonObject.getString("faceAttributes")); | |
} | |
else if (data.charAt(0) == '{') { | |
JSONObject jObject = parseJSONObject(data); | |
println(jObject); | |
} | |
else { | |
println(data); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
// Display error message. | |
System.out.println(e.getMessage()); | |
} | |
} | |
void setupCamera() | |
{ | |
String[] cameras = Capture.list(); | |
if (cameras.length == 0) { | |
println("There are no cameras available for capture."); | |
exit(); | |
} | |
else { | |
println("Available cameras:"); | |
printArray(cameras); | |
// The camera can be initialized directly using an element | |
// from the array returned by list(): | |
cam = new Capture(this, cameras[0]); | |
// Start capturing the images from the camera | |
cam.start(); | |
while(cam.available() != true) { | |
delay(1);//println("waiting for camera"); | |
} | |
cam.read(); | |
videoScreenshot = (PGraphics2D) createGraphics(cam.width, cam.height, P2D); | |
cameraInitDone = true; | |
} | |
} | |
void handleObject(String name, Object object) { | |
Class type = object.getClass(); | |
//println("Name: ", name, " Type: ", type); | |
if (type.equals(java.lang.String.class) || type.equals(java.lang.Integer.class)) { | |
// this is a leaf to store | |
map.put(name, object); | |
} | |
else if (type.equals(java.lang.Double.class)) { | |
// convert Double to float, which is more standard in Processing | |
map.put(name, (float) (double) object); | |
} | |
else if (type.equals(processing.data.JSONObject.class)) { | |
// it's a JSONObject | |
handleJsonObject(name, (JSONObject)object); | |
} else if (type.equals(processing.data.JSONArray.class)) { | |
// it's a JSONArray, process each element in turn | |
JSONArray array = (JSONArray)object; | |
for (int i = 0; i < array.size(); i++) { | |
Object o = array.get(i); | |
//println(i + ": " + o.getClass()); | |
handleObject(name + "[" + i + "]", o); | |
} | |
} | |
} | |
void handleJsonObject(String name, JSONObject jsonObject) { | |
// loop over all the sub-objects in this object | |
for (Object key : jsonObject.keys()) { | |
//println(key); | |
String keyStr = (String)key; | |
Object next = jsonObject.get(keyStr); | |
handleObject(toName(name, keyStr), next); | |
} | |
} | |
String toName(String base, String key) { | |
if (base == null) { | |
return key; | |
} else { | |
return base + "." + key; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment