Skip to content

Instantly share code, notes, and snippets.

@keshuaixu
Last active January 24, 2016 16:02
Show Gist options
  • Save keshuaixu/797a7d6765cd09c4fae5 to your computer and use it in GitHub Desktop.
Save keshuaixu/797a7d6765cd09c4fae5 to your computer and use it in GitHub Desktop.
import java.awt.image.BufferedImage;
import com.neuronrobotics.imageprovider.Detection;
import java.util.List;
import javax.imageio.ImageIO;
import java.net.*;
import java.io.BufferedReader;
import org.apache.commons.io.IOUtils;
import com.google.gson.*;
double happyThreshold = 0.9
/*
OpenCVImageProvider camera0=null;
if(DeviceManager.getSpecificDevice(OpenCVImageProvider.class, "camera")==null){
BowlerStudio.speak("I did not fine a device called camera. Connecting to camera 0.");
camera0 = new OpenCVImageProvider(0);// grab the first camera
DeviceManager.addConnection(camera0,"camera");
}else{
camera0 = (OpenCVImageProvider)DeviceManager.getSpecificDevice(OpenCVImageProvider.class, "camera");
}
// Starting with the connected camera from BowlerStudio
println(camera0)
//Create the default detector using "lbpcascade_frontalface.xml"
//IObjectDetector detector = new HaarDetector("lbpcascade_frontalface.xml")
// Create the input and display images. The display is where the detector writes its detections overlay on the input image
BufferedImage inputImage = AbstractImageProvider.newBufferImage(640,480)
BufferedImage displayImage = AbstractImageProvider.newBufferImage(640,480)
// Loop checking the camera for faces
while(!Thread.interrupted()){
camera0.getLatestImage(inputImage,displayImage) // capture image
// http://stackoverflow.com/questions/8656085/how-to-send-an-image-from-web-service-in-spring
// Create a byte array output stream.
ByteArrayOutputStream bao = new ByteArrayOutputStream();
// Write to output stream
ImageIO.write(inputImage, "jpg", bao);
URL u = new URL("https://api.projectoxford.ai/emotion/v1.0/recognize");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty( "Ocp-Apim-Subscription-Key", "56a40f4cbaba4ee3b4ac8c72c41c54e5" );
conn.setRequestProperty( "Content-Type", "application/octet-stream");
conn.setRequestProperty( "Content-Length", String.valueOf(bao.size()));
OutputStream os = conn.getOutputStream();
os.write(bao.toByteArray());
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
println responseCode;
String responseMessage = conn.getResponseMessage();
println responseMessage;
}
String s = IOUtils.toString(conn.getInputStream(), "UTF-8");
// println s
try {
JsonArray jsonArray = new JsonParser().parse(s).getAsJsonArray();
if (jsonArray.size() == 0){
println "no face"
} else {
double happiness = jsonArray.get(0).getAsJsonObject().get("scores").getAsJsonObject().get("happiness").getAsDouble()
println happiness
if (happiness > happyThreshold){
println "unhappinize"
//slap()
}
}
} catch (Exception e) {
e.printStackTrace()()
}
Thread.sleep(2850)
}
*/
Closure calcHome = { BufferedImage inputImage ->
ByteArrayOutputStream bao = new ByteArrayOutputStream();
// Write to output stream
ImageIO.write(inputImage, "jpg", bao);
URL u = new URL("https://api.projectoxford.ai/emotion/v1.0/recognize");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty( "Ocp-Apim-Subscription-Key", "56a40f4cbaba4ee3b4ac8c72c41c54e5" );
conn.setRequestProperty( "Content-Type", "application/octet-stream");
conn.setRequestProperty( "Content-Length", String.valueOf(bao.size()));
OutputStream os = conn.getOutputStream();
os.write(bao.toByteArray());
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
println responseCode;
String responseMessage = conn.getResponseMessage();
println responseMessage;
}
String s = IOUtils.toString(conn.getInputStream(), "UTF-8");
// println s
try {
JsonArray jsonArray = new JsonParser().parse(s).getAsJsonArray();
if (jsonArray.size() == 0){
println "no face"
return true
} else {
double happiness = jsonArray.get(0).getAsJsonObject().get("scores").getAsJsonObject().get("happiness").getAsDouble()
println happiness
if (happiness > happyThreshold){
println "too happy"
return true
} else {
return false
}
}
} catch (Exception e) {
e.printStackTrace()()
}
}
return calcHome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment