Permalink
Browse files

aiception faces + asteroids = faceteroids

  • Loading branch information...
Razvy000 committed Feb 14, 2017
1 parent 930301c commit 66194663a729e98b960bd2294eed9cee85eee7ef
Showing with 223 additions and 4 deletions.
  1. +177 −0 aiception.js
  2. +43 −3 game.js
  3. +3 −1 index.html
@@ -0,0 +1,177 @@
function face_age(token, image_url, callback){
callEndpoint("https://aiception.com/api/v2.1/face_age", token, image_url, callback)
}
function detect_object(token, image_url, callback){
var initial_wait = 2000;
callEndpoint("https://aiception.com/api/v2.1/detect_object", token, image_url, callback, initial_wait)
}
function adult_content(token, image_url, callback){
var initial_wait = 2000;
callEndpoint("https://aiception.com/api/v2.1/adult_content", token, image_url, callback, initial_wait)
}
function faces(token, image_url, callback){
var initial_wait = 3000;
var retries = 30;
var increment_wait = 300;
// callEndpoint("http://localhost/api/v2.1/face", token, image_url, callback, 3000)
callEndpoint("https://aiception.com/api/v2.1/face", token, image_url, callback, initial_wait, increment_wait, retries)
}
function artistic_image(token, image_url, style_url, callback){
data = {
'image_url' : image_url,
'style_url' : style_url
}
var initial_wait = 60*1000;
var increment_wait = 10*1000;
callEndpoint2("https://aiception.com/api/v2.1/artistic_image", token, data, callback, initial_wait, increment_wait)
}
function callEndpoint(endpoint, token, image_url, callback, initial_wait=1000, increment_wait=100, retries=20){
// 2
$.ajax({
headers: {
"Authorization": "Basic " + btoa(token + ":" + 'password is ignored')
},
type: "POST",
url: endpoint,
//url: "https://aiception.com/api/v2.1/face_age",
// url: "/api/v2.1/face_age",
contentType : 'application/json',
dataType: 'json',
async: true,
// use stringify or else jquery will urlencode the data
data: JSON.stringify({"image_url": image_url}),
error: function (xhr, ajaxOptions, thrownError){
callback(thrownError, null)
},
success: function(task_created, textStatus, jqXHR){
console.log("created task: ", task_created)
age_task_url = task_created['Location'];
// wait 300 ms
setTimeout(function(){
getTask(age_task_url, callback);
}, initial_wait);
}
});
function getTask(task_url, callback){
var counter = 0;
(function pollTaskUntilDone() {
$.ajax({
headers: {
"Authorization": "Basic " + btoa(token + ":" + 'password is ignored')
},
type: "GET",
url: task_url,
contentType : 'application/json; charset=utf-8',
dataType: 'json',
async: true,
error: function (xhr, ajaxOptions, thrownError){
console.error(thrownError)
callback(thrownError, null);
},
success: function(task){
counter++;
// console.log(task);
// probably too many requests
if(!("answer" in task)){
console.error(task)
callback(null, task);
}
// check if done
//if (task.answer.length <= 6){//jQuery.isEmptyObject(task['answer'])){
if($.type(task.answer) === "string"){
if(counter > retries){
callback('giving up', null);
return;
}
// pool every 300 ms
setTimeout(pollTaskUntilDone, 300 + increment_wait*counter);
}else{
callback(null, task)
}
}
});
})();
}
}
function callEndpoint2(endpoint, token, datapayload, callback, initial_wait=1000, increment_wait=100, retries=20){
// 2
$.ajax({
headers: {
"Authorization": "Basic " + btoa(token + ":" + 'password is ignored')
},
type: "POST",
url: endpoint,
//url: "https://aiception.com/api/v2.1/face_age",
// url: "/api/v2.1/face_age",
contentType : 'application/json',
dataType: 'json',
async: true,
// use stringify or else jquery will urlencode the data
data: JSON.stringify(datapayload),
error: function (xhr, ajaxOptions, thrownError){
callback(thrownError, null)
},
success: function(task_created, textStatus, jqXHR){
console.log("created task: ", task_created)
age_task_url = task_created['Location'];
// wait 300 ms
setTimeout(function(){
getTask(age_task_url, callback);
}, initial_wait);
}
});
function getTask(task_url, callback){
var counter = 0;
(function pollTaskUntilDone() {
$.ajax({
headers: {
"Authorization": "Basic " + btoa(token + ":" + 'password is ignored')
},
type: "GET",
url: task_url,
contentType : 'application/json; charset=utf-8',
dataType: 'json',
async: true,
error: function (xhr, ajaxOptions, thrownError){
console.error(thrownError)
callback(thrownError, null);
},
success: function(task){
counter++;
console.log(task);
// probably too many requests
if(!("answer" in task)){
console.error(task)
callback(null, task);
}
// check if done
//if (task.answer.length <= 6){//jQuery.isEmptyObject(task['answer'])){
if($.type(task.answer) === "string"){
if(counter > retries){
callback('giving up', null);
return;
}
// pool every increment_wait ms
setTimeout(pollTaskUntilDone, increment_wait*counter);
}else{
callback(null, task)
}
}
});
})();
}
}
46 game.js
@@ -1,7 +1,12 @@
// Canvas Asteroids
//
// Copyright (c) 2010 Doug McInnes
//
// modified 2017 Razvan AIception
var img = new Image;
var faces_data = []
var token = "YOUR_TOKEN_HERE"
var image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Albert_Einstein_and_Charlie_Chaplin_-_1931.jpg/800px-Albert_Einstein_and_Charlie_Chaplin_-_1931.jpg";
KEY_CODES = {
32: 'space',
@@ -244,6 +249,15 @@ Sprite = function () {
this.context.closePath();
this.context.stroke();
if(this.name == 'asteroid'){
if(!this.face){
this.face = faces_data[Math.floor(Math.random()*faces_data.length)];
}
src_width = this.face[2] - this.face[0]
src_height = this.face[3] - this.face[1]
this.context.drawImage(img, this.face[0], this.face[1], src_width, src_height, -10, -10, 20, 20);
}
};
this.findCollisionCanidates = function () {
if (!this.visible || !this.currentNode) return [];
@@ -280,7 +294,8 @@ Sprite = function () {
px = trans[i*2];
py = trans[i*2 + 1];
// mozilla doesn't take into account transforms with isPointInPath >:-P
if (($.browser.mozilla) ? this.pointInPolygon(px, py) : this.context.isPointInPath(px, py)) {
//if (($.browser.mozilla) ? this.pointInPolygon(px, py) : this.context.isPointInPath(px, py)) {
if ((false) ? this.pointInPolygon(px, py) : this.context.isPointInPath(px, py)) {
other.collision(this);
this.collision(other);
return;
@@ -1124,6 +1139,30 @@ $(function () {
};
})();
var aiceptionLoop = function(){
faces(token, image_url, function(error, success) {
if (error) {
console.log(error);
} else {
console.log(JSON.stringify(success, null, 2));
faces_data = success.answer.faces;
// now call the original mainLoop()
mainLoop();
}
});
img.onload = function() {
canvas.height = img.height;
canvas.width = img.width;
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(this, 0, 0);
};
img.src = image_url;
}
var mainLoop = function () {
context.clearRect(0, 0, Game.canvasWidth, Game.canvasHeight);
@@ -1192,7 +1231,8 @@ $(function () {
}
};
mainLoop();
//mainLoop();
aiceptionLoop();
$(window).keydown(function (e) {
switch (KEY_CODES[e.keyCode]) {
@@ -1,6 +1,8 @@
<html>
<head>
<script src="jquery-1.4.1.min.js"></script>
<!-- <script src="jquery-1.4.1.min.js"></script> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="aiception.js"></script>
<script src="vector_battle_regular.typeface.js"></script>
<script src="ipad.js"></script>
<script src="game.js"></script>

0 comments on commit 6619466

Please sign in to comment.