@21natzil/

Emoji Recognition

Python

Can AIs look at emojis, and figure out what it is?

fork
loading
Files
  • main.py
  • requirements.txt
  • weights.h5

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.006170511235550125","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"ZnJvbSBpbyBpbXBvcnQgQnl0ZXNJTwppbXBvcnQgb3MKCmltcG9ydCByZXF1ZXN0cwpmcm9tIGltYWdlYWkuUHJlZGljdGlvbiBpbXBvcnQgSW1hZ2VQcmVkaWN0aW9uCgoKRU1PSklfQVBJID0gImh0dHBzOi8veG4tLWktN2lxLndzL2Vtb2ppLWltYWdlL3swfS5wbmc/Zm9ybWF0PWVtb2ppb25lJmFyPTF4MSIKCmlmIG5vdCBvcy5wYXRoLmV4aXN0cygid2VpZ2h0cy5oNSIpOgogIHdpdGggb3Blbigid2VpZ2h0cy5oNSIsICJ3YiIpIGFzIGY6CiAgICByZXNwID0gcmVxdWVzdHMuZ2V0KCJodHRwczovL2dpdGh1Yi5jb20vZmNob2xsZXQvZGVlcC1sZWFybmluZy1tb2RlbHMvcmVsZWFzZXMvZG93bmxvYWQvdjAuMi9yZXNuZXQ1MF93ZWlnaHRzX3RmX2RpbV9vcmRlcmluZ190Zl9rZXJuZWxzLmg1IikKICAgIGYud3JpdGUocmVzcC5jb250ZW50KQoKCmFpID0gSW1hZ2VQcmVkaWN0aW9uKCkKYWkuc2V0TW9kZWxUeXBlQXNSZXNOZXQoKQphaS5zZXRNb2RlbFBhdGgoIi4vd2VpZ2h0cy5oNSIpCmFpLmxvYWRNb2RlbCgpCgp3aGlsZSBUcnVlOgogIGVtb2ppID0gaW5wdXQoIkVudGVyIGFuIGVtb2ppOiAiKQogIGVtb2ppX3Jlc3AgPSByZXF1ZXN0cy5nZXQoCiAgICBFTU9KSV9BUEkuZm9ybWF0KGVtb2ppKQogICkKICBpZiBlbW9qaV9yZXNwLnN0YXR1c19jb2RlID09IDQwNDoKICAgIHByaW50KCJJbnZhbGlkIEVtb2ppIikKICAgIGNvbnRpbnVlCiAgc3RyZWFtID0gQnl0ZXNJTyhlbW9qaV9yZXNwLmNvbnRlbnQpCiAgc3RyZWFtLnNlZWsoMCkKICAjIHAgPSBhaS5wcmVkaWN0SW1hZ2Uoc3RyZWFtKQogICMgcHJpbnQocCkKICAjIGV4aXQoMCkKICBwcmVkaWN0aW9ucywgcHJvYmFiaWxpdGllcyA9IGFpLnByZWRpY3RJbWFnZShzdHJlYW0pCiAgZm9yIGksIHByZWRpY3Rpb24gaW4gZW51bWVyYXRlKHByZWRpY3Rpb25zKToKICAgIHByaW50KCJNeSB7MH1zdCBndWVzcyBpcyBhIHsxfSIuZm9ybWF0KGkgKyAxLCBwcmVkaWN0aW9uKSkK"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from io import BytesIO
import os

import requests
from imageai.Prediction import ImagePrediction


EMOJI_API = "https://xn--i-7iq.ws/emoji-image/{0}.png?format=emojione&ar=1x1"

if not os.path.exists("weights.h5"):
  with open("weights.h5", "wb") as f:
    resp = requests.get("https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5")
    f.write(resp.content)


ai = ImagePrediction()
ai.setModelTypeAsResNet()
ai.setModelPath("./weights.h5")
ai.loadModel()

while True:
  emoji = input("Enter an emoji: ")
  emoji_resp = requests.get(
    EMOJI_API.format(emoji)
  )
  if emoji_resp.status_code == 404:
    print("Invalid Emoji")
    continue
  stream = BytesIO(emoji_resp.content)
  stream.seek(0)
  # p = ai.predictImage(stream)
  # print(p)
  # exit(0)
  predictions, probabilities = ai.predictImage(stream)
  for i, prediction in enumerate(predictions):
    print("My {0}st guess is a {1}".format(i + 1, prediction))