-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
62 lines (43 loc) · 1.61 KB
/
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
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
from keras.models import load_model
import tensorflow as tf
import requests
loaded_model = load_model("Trained_model2.h5")
token = '5670119365:AAHkaefzKJ09qp3yoKGqvg2n-j0JpVig1AE'
userID = -813050596
fname = "birds.txt"
with open(fname ,"r") as f:
BirdClasses = sorted(set([word for line in f for word in line.split()]))
def sendMessage(message) :
# Create url
url = f'https://api.telegram.org/bot{token}/sendMessage'
# Create json link with message
data = {'chat_id': userID, 'text': message}
# POST the message
requests.post(url, data)
def sendPhoto(filepath):
url = "https://api.telegram.org/bot{}/sendPhoto?chat_id={}".format(token, userID)
data = {
'photo':open(filepath, 'rb')
}
requests.post(url, files=data)
def load_and_prep_image(filename, img_shape = 224):
img = tf.io.read_file(filename) #read image
img = tf.image.decode_image(img) # decode the image to a tensor
img = tf.image.resize(img, size = [img_shape, img_shape]) # resize the image
img = img/255. # rescale the image
return img
# predict function
def predict(filename):
# Import the target image and preprocess it
img = load_and_prep_image(filename)
# Make a prediction
pred = loaded_model.predict(tf.expand_dims(img, axis=0))
# Get the predicted class
pred_class = BirdClasses[pred.argmax()]
return pred_class
if __name__ == '__main__' :
filepath = 'test_imgs/img9.jpg'
result = predict(filepath)
# sendMessage(result)
# sendPhoto(filepath)
print('result : ', result)