Skip to content

Commit

Permalink
generating inappropriate emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
wins2on committed Jan 21, 2018
1 parent e587a48 commit b85db0c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 46 deletions.
3 changes: 2 additions & 1 deletion emojifier.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

let apiEmojiKey;
let apiTextKey;
chrome.storage.local.get('apiEmojiKey', (data) => {
Expand Down Expand Up @@ -63,7 +64,7 @@ function replaceImage(faces) {
for (const {faceRectangle, scores} of faces) {
console.log(faceRectangle, scores);
const image = new Image();
image.src = imgNode.src;
image.src = chrome.extension.getURL(`emojis/${getClosestEmoji(scores)}`);
const rect = imgNode.getBoundingClientRect();
console.log(faceRectangle.top)
console.log(faceRectangle.top * imgNode.naturalHeight / imgNode.height)
Expand Down
49 changes: 40 additions & 9 deletions get_emoji.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
import {emojis} from './tag_emojis'
const emojis = {
"001-embarrassed-4.png": [0, 0, 0, 0.3, 0.4, 0, 0, 0.3],
"002-sad-14.png": [0.2, 0.1, 0.1, 0.1, 0, 0, 0.5, 0],
"004-surprised-1.png": [0, 0, 0, 0.2, 0, 0, 0, 0.8],
"006-laughing-3.png": [0, 0.4, 0, 0, 0.6, 0, 0, 0],
"009-surprised.png": [0, 0, 0, 0.2, 0.2, 0, 0, 0.6],
"011-suspicious.png": [0, 0.3, 0.3, 0.4, 0, 0, 0, 0],
"015-laughing-2.png": [0, 0, 0, 0, 0.8, 0.2, 0, 0],
"019-happy-4.png": [0, 0, 0, 0.3, 0.4, 0, 0, 0.3],
"032-happy-5.png": [0, 0, 0, 0, 0.5, 0.5, 0, 0],
"033-sad-13.png": [0.2, 0, 0, 0.1, 0, 0.2, 0.5, 0],
"044-muted.png": [0.1, 0, 0, 0.3, 0, 0.6, 0, 0],
"045-shocked-6.png": [0, 0, 0, 0.1, 0, 0, 0, 0.9],
"047-embarrassed-3.png": [0.2, 0, 0.1, 0.6, 0, 0, 0, 0.1],
"048-shocked-5.png": [0, 0, 0, 0.2, 0, 0, 0, 0.8],
"049-shocked-4.png": [0, 0, 0, 0, 0, 0, 0, 1],
"050-embarrassed-2.png": [0, 0, 0, 0.5, 0, 0, 0.5, 0],
"053-crying-2.png": [0, 0, 0, 0, 0, 0, 1, 0],
"055-sad-12.png": [0.1, 0.1, 0, 0.6, 0, 0, 0.2, 0],
"056-sad-11.png": [0, 0, 0, 0.3, 0, 0, 0.7, 0],
"060-sad-7.png": [0, 0, 0, 0, 0, 0, 1, 0],
"061-sad-6.png": [0, 0, 0, 0.1, 0, 0, 0.9, 0],
"062-angry-3.png": [0.9, 0.1, 0, 0, 0, 0, 0, 0],
"065-angry-2.png": [1, 0, 0, 0, 0, 0, 0, 0],
"066-angry-1.png": [0.8, 0.2, 0, 0, 0, 0, 0, 0],
"080-angry.png": [0.5, 0, 0, 0, 0, 0.5, 0, 0],
"104-ghost.png": [0.11, 0, 0.1, 0.61, 0, 0, 0, 0.18],
"098-happy.png": [0, 0, 0, 0, 9, 0.01, 0, 0.09],
"096-crying.png": [0, 0, 0, 0, 1, 0, 0, 0],
"086-happy-4.png": [0.1, 0, 0.1, 0, 0.6, 0.11, 0.05, 0.04],
"083-smart.png": [0, 0.2, 0.2, 0, 0.2, 0.2, 0, 0.2],
"081-sleeping.png": [0.05, 0.05, 0.1, 0.1, 0, 0.75, 0.5, 0]
};

function getEmotionVector(jsonFile){
var inputVector = [];
const emotions = jsonFile[0].scores;
function getEmotionVector(emotions){
let inputVector = [];
for (const value of Object.values(emotions)) {
inputVector.push(value);
}

return inputVector;
}

function getClosedEmoji(jsonFile){
const inputEmotion = getEmotionVector(jsonFile);
function getClosestEmoji(scores){
const inputEmotion = getEmotionVector(scores);
let minDistanceEmotion = Number.POSITIVE_INFINITY;
let closedEmoji;
let closestEmoji;
let distanceSquared;

for (const [fileName,values] of Object.entries(emojis)) {
Expand All @@ -22,10 +53,10 @@ function getClosedEmoji(jsonFile){
distanceSquared += Math.pow(values[i] - inputEmotion[i],2);
}
if(Math.sqrt(distanceSquared) < minDistanceEmotion){
closedEmoji = fileName;
closestEmoji = fileName;
minDistanceEmotion = Math.sqrt(distanceSquared);
}
}

return closedEmoji;
return closestEmoji;
}
6 changes: 4 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["emojifier.js"]
"js": ["get_emoji.js", "emojifier.js"]
}
],

"permissions": [
"storage",
"<all_urls>"
]
],

"web_accessible_resources": ["emojis/*.png"]

}
2 changes: 1 addition & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function restoreOptions() {
document.querySelector("#apiEmojiKey").value = result.apiEmojiKey || "";
});
chrome.storage.local.get("apiTextKey", ({apiTextKey}) => {
document.querySelector("#apiTextKey").value = result.apiEmojiKey || "";
document.querySelector("#apiTextKey").value = result.apiTextKey || "";
});
}

Expand Down
33 changes: 0 additions & 33 deletions tag_emojis.js

This file was deleted.

0 comments on commit b85db0c

Please sign in to comment.