-
Notifications
You must be signed in to change notification settings - Fork 0
/
dogs.py
238 lines (195 loc) · 7.7 KB
/
dogs.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
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
#py dogs.py pilou.jpg
#mainframe inclusion
if 'MainFrame':
import os
import sys
old_stdout = sys.stdout
old_stderr = sys.stderr
#sys.stdout = open(os.devnull, "w")
#sys.stderr = open(os.devnull, "w")
#}{
requiredModules='Flask keras tensorflow webptools pysftp numpy requests wget scikit-multilearn'.split(' ')
if True:
fn='modules-versions.txt'
os.system('pip freeze > '+fn)
#ftpput(fn)
installed=''
with open(fn) as f:
installed += f.read()
for module in requiredModules:
if(module+'==' not in installed):
#print('Trying to install :',module)
os.system('pip install '+module)
os.system('pip freeze > '+fn);
#os.system('rm -f alpow.py gv.py');os.system('wget https://alpow.fr/gv.py')os.system('wget https://alpow.fr/alpow.py');
import gv;import alpow;from alpow import *
SG('noClassRewrite',False);
SG('webRepo','https://1.x24.fr/a/jupyter/');SG('sftp',{'cd':'dogs6','web':GG('webRepo'),'h':'-','u':'-','p':'-'});SG('useFTP',False);#ReadOnly
import tensorflow
def load(fn='allVars',onlyIfNotSet=1):
fns=fn.split(',')
for fn in fns:
fn=fn.strip(', \n')
ok=1
if(len(fn)==0):
continue
if(onlyIfNotSet):
if fn in globals().keys():
#override empty lists, dict, dataframe and items
if type(globals()[fn])==type:
continue;
elif type(globals()[fn])==pd.DataFrame:
if globals()[fn].shape[0]>0:
continue
elif(type(globals()[fn])==dict):
if(len(globals()[fn])>0):
continue
elif(type(globals()[fn])==list):
if(len(globals()[fn])>0):
continue
elif(type(globals()[fn])==scipy.sparse.csr.csr_matrix):
if(globals()[fn].shape[0]>0):
continue
elif(type(globals()[fn])==np.ndarray):
if(globals()[fn].shape[0]>0):
continue
#si déjà définie, passer au prochain
elif(globals()[fn]):
continue
globals().update(alpow.resume(fn))
#endfor fn
return;
#load('X_train')
def extract(x):
liste=list(x.keys())
for i in liste:
globals()[i]=x[i]
p('extracted : ',','.join(liste))
#jeuDonnees=compact('y_test,')
def compact(variables):
x={}
for i in variables.split(','):
x[i]=globals()[i]
p('compacted : ',variables)
return x
def loadIfNotSet(x):
if x not in globals().keys():
load(x)
#les grosses variables
###############}{
import numpy as np
import warnings,psutil
from tensorflow import keras
'''
from keras.preprocessing.image import array_to_img
import keras.applications.resnet50
import keras.applications.inception_v3
import keras.applications.xception
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import VGG16
# keras image preparation
from keras.applications.vgg16 import preprocess_input
# decode prediction
from keras.applications.vgg16 import decode_predictions
import sklearn.metrics
import keras.applications.vgg16
import keras.engine.sequential
from keras.utils import to_categorical
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
#from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D, Conv2D, MaxPooling2D
#from keras.models import Sequential
from tensorflow.keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D, Conv2D, MaxPooling2D
from tensorflow.keras.models import Sequential
'''
import joblib
import tensorflow
def load_prepare_img(path_img,width=224,height=224,simple=False):
img_raw = load_img(path_img, target_size=(width, height))
img = img_to_array(img_raw)
if(simple):
return preprocess_input(img)#3 dimensions for my models
img = img[np.newaxis, :]#4 dimensions
return preprocess_input(img) # Prétraiter l'image comme le veut VGG-16
####}{
#incremental data
#AttributeError: module 'tensorflow' has no attribute 'placeholder'
#load('modelInfo')
ftpls()
if 'common':
def img2class(img,short=False):
if(not os.path.exists(img)):
p('sorry '+img+' does not exists')
testPics2=np.array([load_prepare_img(i,simple=True) for i in [img]])
predictions=mdl.predict(testPics2)
max=np.amax(predictions)
predictionsC=np.argmax(predictions, axis=-1)
decoded=globals()['raceLabel'+nbBreeds].inverse_transform(predictionsC)
if(short):
return decoded[0]
return '#Result: ' + decoded[0] + ' ;with probability of '+str(round(max,2))
#autant le faire tourner avec Flask, non ?
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input
nbBreeds='120'
mdlname='resnet50Trimmed_e_120'
weights=mdlname+'.h5'
if not os.path.exists(weights):
if not os.path.exists(weights+'.zip'):
import requests
r=requests.get('http://1.x24.fr/a/'+weights+'.zip',stream=True)
with open(weights+'.zip','wb') as f:
f.write(r.raw.read())
p('unzip once')
os.system('unzip '+weights+'.zip');
load(mdlname+',raceLabel120')
mdl=globals()[mdlname]
sys.stdout = old_stdout
sys.stderr = old_stderr
#cmdline not flask
if len(sys.argv)>1:
print(img2class(sys.argv[1]))
#flask mode
if len(sys.argv)<2:
from flask import Flask, render_template, url_for, request
app = Flask(__name__)
@app.route('/')
def home():
return """<html><head><title>Dog Breed Predictor from image</title>
<style>html{font-size:10px;background:#000 url('//x24.fr/0/b1.jpg##20200103SkiRandoClusaz') top center fixed;}/*background-size:110%;background-repeat:repeat;*/
body{height:100vh;color:#FFF;margin:0;}
body,pre{font:2rem 'Dancing Script',Assistant,roboto,calibri,corbel,verdana;}
*{transition:all .5s}
a{color:#0F0;}
a:hover{color:#FC0;}
fieldset{margin:0 2vw;background:rgba(255,255,255,0.1);}
legend{padding:0 3rem;}/*margin:auto;*/
input,textarea{width:100%;padding-left:1rem;font-size:2rem}
textarea{height:30vh;}
input[type=submit]{
cursor:pointer;height:5vh;font-size:4rem;
}
input[type=submit]:hover{filter:invert(100%);}
h1,h2{color:#F00;margin:0;}
</style>
</head><body><center><fieldset><legend>Dog Breed Predictor from image</legend><form method='post' enctype='multipart/form-data' action='/'>Local filepath : <input name=filepath value='pilou.jpg'><br>Or upload file here<input type=file name=dogPicture><br><input type=submit value=submit></form></fieldset></center></body></html>
""";
@app.route('/', methods=['POST'])
def post():
if (request.method == 'POST'):
#print(('filepath' in request.form.keys()))
if 'filepath' in request.form.keys():
if(len(request.form['filepath'])>1):
return img2class(request.form['filepath'],True)
if 'dogPicture' in request.files.keys():
file = request.files['dogPicture']
x='uploads/'+file.filename
file.save(x)
return img2class(x,True)
return 'ko'
app.run(host='0.0.0.0', port=8081, debug=True)
p('end')
#py dogs.py &
#curl -k 127.0.0.1:8081 -F "input=unexpected"
#curl -k 127.0.0.1:8081 -F "filepath=pilou.jpg"
#curl -k 127.0.0.1:8081 -F "[email protected]"