-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreed.py
86 lines (57 loc) · 2.71 KB
/
Breed.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""Program
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/16oK8TdZMVd_Wthzr3NX0a_E8N2OUjSLZ
Ceci est un script qui permet de prédire la race d'un chien à partir d'une url.
Très important: lorsque vous coller l'url, veillez à ajouter les guillements
---
1. Model
---
"""
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tensorflow
import urllib.request
#load the trained model to classify the images
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
# Version google colab
from google.colab import drive
drive.mount('/content/drive')
# https://colab.research.google.com/drive/1_j5mwlB1TlbqQaLS3tIe5VAF8OklQvTm?usp=share_link // lien vers le modèle
model = load_model('/content/drive/MyDrive/Colab Notebooks/Nasnet_trained_model.h5')
# Version Local
# 1. télécharger le modèle à l'adresse suivante: https://colab.research.google.com/drive/1_j5mwlB1TlbqQaLS3tIe5VAF8OklQvTm?usp=share_link
# 2. télécharger le fichier classes à l'adresse suivante: https://drive.google.com/file/d/1J51upZVqL82YA5bb9_P-Q90fEdDbWO21/view?usp=share_link
# 3. déposer les fichiers dans le même répertoire que le script
# model = load_model('/Nasnet_hypermodel.h5')
import pickle
infile = open("/content/drive/MyDrive/Colab Notebooks/classes.pickle",'rb')
classes = pickle.load(infile)
infile.close()
preprocess_input = tensorflow.keras.applications.nasnet.preprocess_input
def predict(url):
# Public domain image
urllib.request.urlretrieve(url, 'img.jpg')
# Load image and resize (doesn't keep aspect ratio)
img_originial = image.load_img('img.jpg', target_size=(331, 331))
# Turn to array of shape (331, 331, 3)
img = image.img_to_array(img_originial)
# Expand array into (1, 224, 224, 3)
img_batch = np.expand_dims(img, 0)
# Preprocess for models that have specific preprocess_input() function
img_preprocessed = preprocess_input(img_batch)
# Prediction
pred = model.predict(img_preprocessed)
y_pred = np.argmax(pred, axis=1)
# Show the result
plt.imshow(img_originial)
plt.title(f'Class: {classes[y_pred[0]]}\n')
plt.show()
predict('https://ptitoutou.com/wp-content/uploads/2020/08/Comment-dresser-un-malinois-1024x682.jpg.webp')
predict('https://minepuppy.com/images/breeds/chihuahua/Chihuahua-breed-red-minepuppy-202080086.JPG')
"""amelioration: eviter les deformation des images en ajouter des pixels noirs pour rendre l image carré"""
predict("https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/Milo%2C_soit_le_chien_de_Pelligton_%28version_plus_petite%29.jpg/375px-Milo%2C_soit_le_chien_de_Pelligton_%28version_plus_petite%29.jpg")