Files
  • main.py
  • requirements.txt
  • weights.h5
main.py
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))