File size: 10,107 Bytes
109be61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# main.py
import numpy as np
from flask import Flask, jsonify, request, render_template

import pyttsx3

import base64

import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf
import requests
import json
import os

from dotenv import load_dotenv

load_dotenv()


app = Flask(__name__)

upload_folder = os.path.join('static', 'img')

app.config['UPLOAD'] = upload_folder


HUGGINGFACEHUB_APT_TOKEN = os.getenv("HUGGINGFACEHUB_APT_TOKEN")

ANIMAL_API_TOKEN = os.getenv("ANIMAL_API_TOKEN")


Animal_danger_classification = {
    "Antelope": "Vulnerable",
    "Badger": "Least Concern",
    "Bat": "Vulnerable",
    "Bear": "Vulnerable",
    "Bee": "Least Concern",
    "Beetle": "Least Concern",
    "Bison": "Least Concern",
    "Boar": "Least Concern",
    "Butterfly": "Vulnerable",
    "Cat": "Least Concern",
    "Caterpillar": "Least Concern",
    "Chimpanzee": "Endangered",
    "Cockroach": "Least Concern",
    "Cow": "Least Concern",
    "Coyote": "Least Concern",
    "Crab": "Least Concern",
    "Crow": "Least Concern",
    "Deer": "Least Concern",
    "Dog": "Least Concern",
    "Dolphin": "Least Concern",
    "Donkey": "Least Concern",
    "Dragonfly": "Vulnerable",
    "Duck": "Least Concern",
    "Eagle": "Least Concern",
    "Elephant": "Endangered",
    "Flamingo": "Vulnerable",
    "Fly": "Least Concern",
    "Fox": "Least Concern",
    "Goat": "Least Concern",
    "Goldfish": "Least Concern",
    "Goose": "Least Concern",
    "Gorilla": "Endangered",
    "Grasshopper": "Least Concern",
    "Hamster": "Least Concern",
    "Hare": "Least Concern",
    "Hedgehog": "Least Concern",
    "Hippopotamus": "Vulnerable",
    "Hornbill": "Vulnerable",
    "Horse": "Least Concern",
    "Hummingbird": "Least Concern",
    "Hyena": "Least Concern",
    "Jellyfish": "Least Concern",
    "Kangaroo": "Least Concern",
    "Koala": "Vulnerable",
    "Ladybugs": "Least Concern",
    "Leopard": "Vulnerable",
    "Lion": "Vulnerable",
    "Lizard": "Least Concern",
    "Lobster": "Least Concern",
    "Mosquito": "Least Concern",
    "Moth": "Least Concern",
    "Mouse": "Least Concern",
    "Octopus": "Least Concern",
    "Okapi": "Endangered",
    "Orangutan": "Endangered",
    "Otter": "Vulnerable",
    "Owl": "Least Concern",
    "Ox": "Least Concern",
    "Oyster": "Least Concern",
    "Panda": "Endangered",
    "Parrot": "Least Concern",
    "Pelecaniformes": "Least Concern",
    "Penguin": "Vulnerable",
    "Pig": "Least Concern",
    "Pigeon": "Least Concern",
    "Porcupine": "Least Concern",
    "Possum": "Least Concern",
    "Raccoon": "Least Concern",
    "Rat": "Least Concern",
    "Reindeer": "Least Concern",
    "Rhinoceros": "Vulnerable",
    "Sandpiper": "Least Concern",
    "Seahorse": "Least Concern",
    "Seal": "Vulnerable",
    "Shark": "Least Concern",
    "Sheep": "Least Concern",
    "Snake": "Least Concern",
    "Sparrow": "Least Concern",
    "Squid": "Least Concern",
    "Squirrel": "Least Concern",
    "Starfish": "Least Concern",
    "Swan": "Least Concern",
    "Tiger": "Vulnerable",
    "Turkey": "Least Concern",
    "Turtle": "Vulnerable",
    "Whale": "Vulnerable",
    "Wolf": "Least Concern",
    "Wombat": "Least Concern",
    "Woodpecker": "Least Concern",
    "Zebra": "Least Concern"
}


ANIMAL_NAMES = ['Antelope',
                'Badger',
                'Bat',
                'Bear',
                'Bee',
                'Beetle',
                'Bison',
                'Boar',
                'Butterfly',
                'Cat',
                'Caterpillar',
                'Chimpanzee',
                'Cockroach',
                'Cow',
                'Coyote',
                'Crab',
                'Crow',
                'Deer',
                'Dog',
                'Dolphin',
                'Donkey',
                'Dragonfly',
                'Duck',
                'Eagle',
                'Elephant',
                'Flamingo',
                'Fly',
                'Fox',
                'Goat',
                'Goldfish',
                'Goose',
                'Gorilla',
                'Grasshopper',
                'Hamster',
                'Hare',
                'Hedgehog',
                'Hippopotamus',
                'Hornbill',
                'Horse',
                'Hummingbird',
                'Hyena',
                'Jellyfish',
                'Kangaroo',
                'Koala',
                'Ladybugs',
                'Leopard',
                'Lion',
                'Lizard',
                'Lobster',
                'Mosquito',
                'Moth',
                'Mouse',
                'Octopus',
                'Okapi',
                'Orangutan',
                'Otter',
                'Owl',
                'Ox',
                'Oyster',
                'Panda',
                'Parrot',
                'Pelecaniformes',
                'Penguin',
                'Pig',
                'Pigeon',
                'Porcupine',
                'Possum',
                'Raccoon',
                'Rat',
                'Reindeer',
                'Rhinoceros',
                'Sandpiper',
                'Seahorse',
                'Seal',
                'Shark',
                'Sheep',
                'Snake',
                'Sparrow',
                'Squid',
                'Squirrel',
                'Starfish',
                'Swan',
                'Tiger',
                'Turkey',
                'Turtle',
                'Whale',
                'Wolf',
                'Wombat',
                'Woodpecker',
                'Zebra']


MODEL = tf.keras.models.load_model("animal_classification_model.h5")


@app.route('/', methods=['GET'])
def home():
    return render_template("home.html")


@app.route('/generate_speech', methods=['POST'])
def generate_speech():
    information = request.form.get('information')
    print(information)

    # Generate speech using pyttsx3
    talk(information)

    return jsonify({"message": "Speech generated successfully"})


def read_file_as_image(data) -> np.ndarray:
    image = Image.open(BytesIO(data))
    return image


def animal_data(predicted_class):
    api_url = 'https://api.api-ninjas.com/v1/animals?name={}'.format(
        predicted_class)
    response = requests.get(
        api_url, headers={'X-Api-Key': ANIMAL_API_TOKEN})
    if response.status_code == requests.codes.ok:
        pass

    else:
        print("Error:", response.status_code, response.text)

    data = response.text
    dict = json.loads(data)

    data = response.text
    dict = json.loads(data)
    # print(type(dict), "knowing")
    data = [i for i in dict if i["name"].lower() ==
            predicted_class.lower()]

    return data[0]


def talk(text):
    engine = pyttsx3.init()
    newVoiceRate = 120
    engine.setProperty('rate', newVoiceRate)
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[0].id)
    print("started speech")
    engine.say(text)
    engine.runAndWait()
    print("make speech stop")
    engine.stop()


@app.route("/predict", methods=["POST"])
def predict():
    if "file" not in request.files:
        return jsonify({"error": "No file provided"}), 400

    file = request.files["file"]

    if file.filename == "":
        return jsonify({"error": "No selected file"}), 400

    bytes = file.read()

    IMAGE_SIZE = (256, 256)

    # pass
    image = read_file_as_image(bytes)
    print(image, "hey")

    data = BytesIO()
    image.save(data, "JPEG")
    encoded_img_data = base64.b64encode(data.getvalue())

    if file:

        # Convert the file contents to a TensorFlow tensor

        img_array = tf.keras.preprocessing.image.img_to_array(image)
        img_array = tf.expand_dims(img_array, 0)

        # resized_image.shape
        resized_image = tf.image.resize(img_array, IMAGE_SIZE)

        # model prediction
        predictions = MODEL.predict(resized_image)

        # processing predicted output to give valid result
        predicted_class = ANIMAL_NAMES[np.argmax(predictions[0])]
        confidence = round(100 * (np.max(predictions[0])), 2)

        print(predicted_class)

        data = animal_data(predicted_class)
        classification = Animal_danger_classification[predicted_class]
        # print(data)
        # print(type(data))
        information = f'It is a {predicted_class} with {confidence} percent accuracy, It belongs to {data["taxonomy"]["kingdom"]} kingdom and {data["taxonomy"]["family"]} family , It can be found in {data["locations"]}, Its lifespan is mostly {data["characteristics"]["lifespan"]}, skin type {data["characteristics"]["skin_type"]} , and it is a {data["characteristics"]["diet"]}, An interesting fact about {predicted_class}: {data["characteristics"]["slogan"]} by danger classification of extinction it is {classification}'

        return render_template("index.html", information=information,  img_data=encoded_img_data.decode('utf-8'),  classes=data["taxonomy"]["class"], family=data["taxonomy"]["family"], kingdom=data["taxonomy"]["kingdom"], locations=data["locations"], lifespan=data["characteristics"]["lifespan"], skin_type=data["characteristics"]["skin_type"], diet=data["characteristics"]["diet"], fun_fact=data["characteristics"]["slogan"], accuracy=confidence, name=predicted_class, classification=classification)

        # return jsonify({
        #     "class": predicted_class,
        #     "confidence": float(confidence),
        #     "response": data,
        #     "Danger Classification": classification
        # })


if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True)

'''
These are some of the animals whose exact data is not available in the api

{'name': 'boar', 'present': 0}
{'name': 'cat', 'present': 0}
{'name': 'dog', 'present': 0}
{'name': 'ladybugs', 'present': 0}
{'name': 'orangutan', 'present': 0}
{'name': 'panda', 'present': 0}
{'name': 'pelecaniformes', 'present': 0}
{'name': 'sandpiper', 'present': 0}
{'name': 'turtle', 'present': 0}
{'name': 'whale', 'present': 0}
'''