{"id":319108,"date":"2021-03-05T15:00:37","date_gmt":"2021-03-05T15:00:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=319108"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=319108","title":{"rendered":"\u041a\u0430\u043a \u043f\u0440\u0438\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u043a \u0441\u0430\u0439\u0442\u0443 \u043f\u043e-\u0431\u044b\u0441\u0442\u0440\u043e\u043c\u0443"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sa\/fh\/en\/safhenqnyltoiatl2x-83trc6lc.gif\"><\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u043f\u0440\u0438\u043b\u043e\u0436\u0438\u0432 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0443\u0441\u0438\u043b\u0438\u044f, \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c python 3.7+flask+tensorflow 2.0+keras+\u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0432\u043a\u0440\u0430\u043f\u043b\u0435\u043d\u0438\u044f js \u0438 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 web-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0440\u0438\u0441\u0443\u044f \u043d\u0430 \u0445\u043e\u043b\u0441\u0442\u0435, \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435 \u0446\u0438\u0444\u0440\u044b, \u0430 \u0440\u0430\u043d\u0435\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 CNN, \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0440\u0438\u0441\u0443\u043d\u043e\u043a \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041c\u043e\u0434\u0435\u043b\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0430 \u043d\u0430 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0440\u0443\u043a\u043e\u043f\u0438\u0441\u043d\u044b\u0445 \u0446\u0438\u0444\u0440 MNIST, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0441\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u0440\u0443\u0442\u0438\u0442\u044c\u0441\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f windows 7. <br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0432\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u0427\u0435\u043c \u043f\u0435\u0447\u0430\u043b\u044c\u043d\u044b \u043a\u043d\u0438\u0433\u0438 \u043f\u043e \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c\u0443 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e, \u0442\u0430\u043a, \u043f\u043e\u0436\u0430\u043b\u0443\u0439, \u0442\u0435\u043c, \u0447\u0442\u043e \u043a\u043e\u0434 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0430\u0435\u0442 \u043f\u043e\u0447\u0442\u0438 \u0441 \u0432\u044b\u0445\u043e\u0434\u043e\u043c \u0441\u0430\u043c\u043e\u0439 \u043a\u043d\u0438\u0433\u0438. \u0418 \u0445\u043e\u0440\u043e\u0448\u043e, \u0435\u0441\u043b\u0438 \u0430\u0432\u0442\u043e\u0440 \u0438\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u0432\u043e\u0435 \u0434\u0438\u0442\u044f, \u0441\u043e\u043f\u0440\u043e\u0432\u043e\u0436\u0434\u0430\u044f \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044f \u043a\u043e\u0434, \u043d\u043e, \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0432\u0441\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u043f\u0438\u0448\u0443\u0442 \u2014 \u0432\u043e\u0442 \u0432\u0430\u043c requirements.txt, \u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0435 \u043f\u0430\u043a\u0435\u0442\u044b, \u0438 \u0432\u0441\u0435 \u0437\u0430\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>  \u0422\u0430\u043a \u0432\u044b\u0448\u043b\u043e \u0438 \u0432 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437. \u0427\u0438\u0442\u0430\u044f \u00abHands-On Python Deep Learning for the Web\u00bb \u0430\u0432\u0442\u043e\u0440\u0441\u0442\u0432\u0430 Anubhav Singh, Sayak Paul, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u0441\u0435 \u0448\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u043e. \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0432\u043e\u0439 \u0433\u043b\u0430\u0432\u044b \u043f\u0440\u0430\u0437\u0434\u043d\u0438\u043a \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0441\u044f. \u0421\u0430\u043c\u043e\u0435 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0435 \u0431\u044b\u043b\u043e \u0442\u043e, \u0447\u0442\u043e \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u0432 requirements \u0432 \u0446\u0435\u043b\u043e\u043c \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u043b\u0438\u0441\u044c.<\/p>\n<p>  \u041c\u0430\u0441\u043b\u043e \u0432 \u043e\u0433\u043e\u043d\u044c \u043f\u043e\u0434\u043b\u0438\u043b\u0438 \u0438 \u0441\u0430\u043c\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 tensorflow \u0438 keras. \u041e\u0434\u0438\u043d \u043f\u0430\u043a\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0434\u0440\u0443\u0433\u0438\u043c \u0438, \u043b\u0438\u0431\u043e \u0434\u0430\u0443\u043d\u0433\u0440\u0435\u0439\u0434 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445 \u043b\u0438\u0431\u043e \u0431\u0443\u0431\u0435\u043d \u0448\u0430\u043c\u0430\u043d\u0430.<br \/>  \u041d\u043e \u0438 \u044d\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0432\u0441\u0435. \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b \u0435\u0449\u0435 \u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b \u043e\u0442 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u0436\u0435\u043b\u0435\u0437\u0430! <\/p>\n<p>  \u0422\u0430\u043a, \u0437\u0430 \u043d\u0435\u0438\u043c\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b \u0436\u0435\u043b\u0435\u0437\u0430, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u043b\u0441\u044f tensorflow 2.0 \u043d\u0430 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0443 \u0441 Celeron j1900 \u0438, \u043a\u0430\u043a \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0442\u0430\u043c \u043d\u0435\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 AVX2: <br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/9k\/u8\/n1\/9ku8n1syambgz02eiokf2a2qv1y.png\"><\/p>\n<p>  \u0418 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0447\u0435\u0440\u0435\u0437 <b>pip install tensorflow<\/b> \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u043b. <\/p>\n<p>  \u041d\u043e \u043d\u0435 \u0432\u0441\u0435 \u0442\u0430\u043a \u0433\u0440\u0443\u0441\u0442\u043d\u043e \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0436\u0435\u043b\u0430\u043d\u0438\u044f \u0438 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430!<\/p>\n<p>  \u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 tensorflow 2.0 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 wheel \u2014 <a href=\"https:\/\/github.com\/fo40225\/tensorflow-windows-wheel\/tree\/master\/2.0.0\/py37\/CPU\/sse2\" rel=\"nofollow noopener noreferrer\">github.com\/fo40225\/tensorflow-windows-wheel\/tree\/master\/2.0.0\/py37\/CPU\/sse2<\/a> \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 x86: vc_redist.x86.exe, x64: vc_redist.x64.exe (https:\/\/support.microsoft.com\/en-us\/help\/2977003\/the-latest-supported-visual-c-downloads).<\/p>\n<p>  Keras \u0431\u044b\u043b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u043d \u00ab\u0441\u0442\u0430\u043b \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u00bb \u0441 tensorflow \u2014 Keras==2.3.0.<\/p>\n<p>  \u041f\u043e\u044d\u0442\u043e\u043c\u0443 <\/p>\n<pre><code class=\"python\">pip install tensorflow-2.0.0-cp37-cp37m-win_amd64.whl<\/code><\/pre>\n<p>  \u0438 <\/p>\n<pre><code class=\"python\">pip install keras==2.3.0<\/code><\/pre>\n<p>  <\/p>\n<h3>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u0434 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">flask_app.py<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\"> #code work with scipy==1.6.1, tensorflow @ file:\/\/\/D:\/python64\/tensorflow-2.0.0-cp37-cp37m-win_amd64.whl, #Keras==2.3.0  from flask import Flask, render_template, request import imageio #https:\/\/imageio.readthedocs.io\/en\/stable\/examples.html #from scipy.misc import imread, imresize #from matplotlib.pyplot import imread import numpy as np import tensorflow as tf from tensorflow.keras.models import model_from_json from skimage import transform,io  json_file = open('model.json','r') model_json = json_file.read() json_file.close() model = model_from_json(model_json) model.load_weights(&quot;weights.h5&quot;) model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']) #graph = tf.get_default_graph() graph = tf.compat.v1.get_default_graph()  app = Flask(__name__)  @app.route('\/') def index():     return render_template(&quot;index.html&quot;) import re import base64  def convertImage(imgData1):     imgstr = re.search(r'base64,(.*)', str(imgData1)).group(1)     with open('output.png', 'wb') as output:         output.write(base64.b64decode(imgstr))  @app.route('\/predict\/', methods=['GET', 'POST']) def predict():     global model, graph          imgData = request.get_data()     convertImage(imgData)     #print(imgData)         #x = imread('output.png', mode='L')     #x.shape     #(280, 280)     x = imageio.imread('output.png',pilmode='L')     #x = imresize(x, (28, 28))     #x = x.resize(x, (28, 28))     x = transform.resize(x, (28,28), mode='symmetric', preserve_range=True)     #(28, 28)     #type(x)     #&lt;class 'numpy.ndarray'&gt;      x = x.reshape(1, 28, 28, 1)     #(1, 28, 28, 1)      x = tf.cast(x, tf.float32)          # perform the prediction     out = model.predict(x)             #print(np.argmax(out, axis=1))     # convert the response to a string     response = np.argmax(out, axis=1)     return str(response[0])  if __name__ == &quot;__main__&quot;:     # run the app locally on the given port     app.run(host='0.0.0.0', port=80) # optional if we want to run in debugging mode     app.run(debug=True) <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  \u041f\u043e\u0434\u0433\u0440\u0443\u0437\u0438\u043b\u0438 \u043f\u0430\u043a\u0435\u0442\u044b:  <\/p>\n<pre><code class=\"python\"> from flask import Flask, render_template, request import imageio #https:\/\/imageio.readthedocs.io\/en\/stable\/examples.html #from scipy.misc import imread, imresize #from matplotlib.pyplot import imread import numpy as np import tensorflow as tf from tensorflow.keras.models import model_from_json from skimage import transform,io <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c imread, imresize \u0443\u0441\u0442\u0430\u0440\u0435\u043b\u0438 \u0435\u0449\u0435 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d scipy==1.0. \u041d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043a\u0430\u043a \u0443 \u0430\u0432\u0442\u043e\u0440\u0430 \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e, \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f, \u0447\u0442\u043e \u043a\u043d\u0438\u0433\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u043e\u0432\u0430 (2019). \u0421 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 scipy==1.6.1 \u043a\u043d\u0438\u0436\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043a\u043e\u0434\u0430 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u043b. <\/p>\n<p>  \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0441 \u0434\u0438\u0441\u043a\u0430, \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438:<\/p>\n<pre><code class=\"python\"> json_file = open('model.json','r') model_json = json_file.read() json_file.close() model = model_from_json(model_json) model.load_weights(&quot;weights.h5&quot;) model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']) #graph = tf.get_default_graph() graph = tf.compat.v1.get_default_graph() <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0430 \u0437\u0430\u043c\u0435\u043d\u0430 \u043d\u0430 tf.compat.v1.get_default_graph() \u0432 \u0432\u0438\u0434\u0443 \u043d\u0435\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u044c, \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0430\u044f\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043d\u0430 flask. \u00ab\u041f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u043a\u0430\u00bb \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b:<\/p>\n<pre><code class=\"python\"> @app.route('\/') def index():     return render_template(&quot;index.html&quot;) <\/code><\/pre>\n<p>  \u0427\u0430\u0441\u0442\u044c, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0449\u0430\u044f \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u0432 \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432:<\/p>\n<pre><code class=\"python\"> import re import base64  def convertImage(imgData1):     imgstr = re.search(r'base64,(.*)', str(imgData1)).group(1)     with open('output.png', 'wb') as output:         output.write(base64.b64decode(imgstr)) <\/code><\/pre>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\"> def predict():     global model, graph          imgData = request.get_data()     convertImage(imgData)     #print(imgData)         #x = imread('output.png', mode='L')     #x.shape     #(280, 280)     x = imageio.imread('output.png',pilmode='L')     #x = imresize(x, (28, 28))     #x = x.resize(x, (28, 28))     x = transform.resize(x, (28,28), mode='symmetric', preserve_range=True)     #(28, 28)     #type(x)     #&lt;class 'numpy.ndarray'&gt;      x = x.reshape(1, 28, 28, 1)     #(1, 28, 28, 1)      x = tf.cast(x, tf.float32)          # perform the prediction     out = model.predict(x)             #print(np.argmax(out, axis=1))     # convert the response to a string     response = np.argmax(out, axis=1)     return str(response[0]) <\/code><\/pre>\n<p>  \u0417\u0430\u043a\u043e\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0441\u0442\u0440\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u044b \u043d\u0430 \u0440\u0430\u0431\u043e\u0447\u0438\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0432\u044b\u0432\u043e\u0434\u044b \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438.<\/p>\n<h3>\u041a\u0430\u043a \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/h3>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 <b>python flask_app.py<\/b> \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 flask-\u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 index.html \u0441 \u0432\u043a\u0440\u0430\u043f\u043b\u0435\u043d\u0438\u0435\u043c js. <\/p>\n<p>  \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0440\u0438\u0441\u0443\u0435\u0442 \u043d\u0430 \u0445\u043e\u043b\u0441\u0442\u0435 \u0446\u0438\u0444\u0440\u0443, \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u0442 \u00abpredict\u00bb. \u041a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u00ab\u0443\u043b\u0435\u0442\u0430\u0435\u0442\u00bb \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440, \u0433\u0434\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0446\u0438\u0444\u0440\u043e\u0432\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u0414\u0430\u043b\u0435\u0435 \u0432 \u0431\u043e\u0439 \u0432\u0441\u0442\u0443\u043f\u0430\u0435\u0442 CNN, \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u044e\u0449\u0430\u044f \u0446\u0438\u0444\u0440\u0443 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u043e\u0442\u0432\u0435\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0446\u0438\u0444\u0440\u044b.<\/p>\n<p>  \u0421\u0435\u0442\u044c \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u0430\u0435\u0442 \u0432\u0435\u0440\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442, \u0442.\u043a. \u043e\u0431\u0443\u0447\u0430\u043b\u0430\u0441\u044c \u0432\u0441\u0435\u0433\u043e \u043d\u0430 10 \u044d\u043f\u043e\u0445\u0430\u0445. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u00ab\u0441\u043f\u043e\u0440\u043d\u0443\u044e\u00bb \u0446\u0438\u0444\u0440\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u0442\u0440\u0430\u043a\u0442\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443.<\/p>\n<p>  *\u041c\u043e\u0436\u043d\u043e \u043f\u043e\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u0441\u043b\u0430\u0439\u0434\u0435\u0440, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u044f \u0438\u043b\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u044f \u0442\u043e\u043b\u0449\u0438\u043d\u0443 \u043d\u0430\u0447\u0435\u0440\u0442\u0430\u043d\u0438\u044f \u0446\u0438\u0444\u0440\u044b \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f.<\/p>\n<h3>\u0412\u0442\u043e\u0440\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u2014 \u0447\u0435\u0440\u0435\u0437 API,curl<\/h3>\n<p>  \u041f\u043e\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u0432\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0441 \u0446\u0438\u0444\u0440\u043e\u0439 \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438 \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u0442 \u00ab\u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u00bb:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sd\/p3\/uw\/sdp3uwmo6xpir30xx7vtkzp98ac.gif\"><\/p>\n<p>  \u0417\u0430\u043c\u0435\u043d\u0438\u043c index.js \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439:<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">index.js:<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">$(&quot;form&quot;).submit(function(evt){ \tevt.preventDefault(); \tvar formData = new FormData($(this)[0]); \t$.ajax({ \t\turl: '\/predict\/', \t\ttype: 'POST', \t\tdata: formData, \t\tasync: false, \t\tcache: false, \t\tcontentType: false, \t\tenctype: 'multipart\/form-data', \t\tprocessData: false, \t\tsuccess: function (response) { \t\t\t$('#result').empty().append(response); \t\t} \t}); \treturn false; }); <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  \u0428\u0430\u0431\u043b\u043e\u043d \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0442\u0430\u043a\u0436\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f:<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">index.html<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"xml\">&lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;title&gt;MNIST CNN&lt;\/title&gt; &lt;\/head&gt; &lt;body&gt; &lt;h1&gt;MNIST Handwritten Digits Prediction&lt;\/h1&gt; &lt;form&gt; &lt;input type=&quot;file&quot; name=&quot;img&quot;&gt;&lt;\/input&gt; &lt;input type=&quot;submit&quot;&gt;&lt;\/input&gt; &lt;\/form&gt; &lt;hr&gt; &lt;h3&gt;Prediction: &lt;span id=&quot;result&quot;&gt;&lt;\/span&gt;&lt;\/h3&gt; &lt;script src='https:\/\/code.jquery.com\/jquery-3.6.0.min.js'&gt;&lt;\/script&gt; &lt;script src=&quot;{{ url_for('static',filename='index.js') }}&quot;&gt;&lt;\/script&gt; &lt;\/body&gt; &lt;\/html&gt; <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430:<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">flask_app2.py<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\"> #code work with scipy==1.6.1, tensorflow @ file:\/\/\/D:\/python64\/tensorflow-2.0.0-cp37-cp37m-win_amd64.whl, #Keras==2.3.0  from flask import Flask, render_template, request import imageio #https:\/\/imageio.readthedocs.io\/en\/stable\/examples.html #from scipy.misc import imread, imresize #from matplotlib.pyplot import imread import numpy as np import tensorflow as tf from tensorflow.keras.models import model_from_json from skimage import transform,io   json_file = open('model.json','r') model_json = json_file.read() json_file.close() model = model_from_json(model_json) model.load_weights(&quot;weights.h5&quot;) model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']) #graph = tf.get_default_graph() graph = tf.compat.v1.get_default_graph()  app = Flask(__name__)  @app.route('\/') def index():     return render_template(&quot;index.html&quot;)  import re import base64  def convertImage(imgData1):     imgstr = re.search(r'base64,(.*)', str(imgData1)).group(1)     with open('output.png', 'wb') as output:         output.write(base64.b64decode(imgstr))  @app.route('\/predict\/', methods=['POST']) def predict():     global model, graph          imgData = request.get_data()     try:         stringToImage(imgData)     except:         f = request.files['img']         f.save('image.png')             #x = imread('output.png', mode='L')     #x.shape     #(280, 280)     x = imageio.imread('image.png',pilmode='L')     #x = imresize(x, (28, 28))     #x = x.resize(x, (28, 28))     x = transform.resize(x, (28,28), mode='symmetric', preserve_range=True)     #(28, 28)     #type(x)     #&lt;class 'numpy.ndarray'&gt;      x = x.reshape(1, 28, 28, 1)     #(1, 28, 28, 1)      x = tf.cast(x, tf.float32)          # perform the prediction     out = model.predict(x)             #print(np.argmax(out, axis=1))     # convert the response to a string     response = np.argmax(out, axis=1)     return str(response[0])  if __name__ == &quot;__main__&quot;:      # run the app locally on the given port     app.run(host='0.0.0.0', port=80) # optional if we want to run in debugging mode     app.run(debug=True)  <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u0435 \u043f\u043e\u0445\u043e\u0436\u0435 \u2014 python flask_app2.py<\/p>\n<h3>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 curl (\u0434\u043b\u044f windows)<\/h3>\n<p>  \u0421\u043a\u0430\u0447\u0438\u0432\u0430\u0435\u043c <a href=\"https:\/\/curl.se\/windows\/\" rel=\"nofollow noopener noreferrer\">curl<\/a><\/p>\n<p>  \u0412 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 windows \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443:<\/p>\n<pre><code class=\"python\"> curl -X POST -F img=@1.png http:\/\/localhost\/predict\/ <\/code><\/pre>\n<p>  \u0433\u0434\u0435 1.png \u2014 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u0441 \u0446\u0438\u0444\u0440\u043e\u0439 (\u0438\u043b\u0438 \u043e\u043d\u0430 \u0436\u0435 \u0441 \u043f\u0443\u0442\u0435\u043c \u043a \u043d\u0435\u0439).<br \/>  \u0412 \u043e\u0442\u0432\u0435\u0442 \u043f\u0440\u0438\u043b\u0435\u0442\u0438\u0442 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u043d\u043d\u0430\u044f \u0446\u0438\u0444\u0440\u0430.<\/p>\n<p>  \u0424\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u2014 <a href=\"https:\/\/disk.yandex.ru\/d\/rOk3xxGhgXPsIQ\" rel=\"nofollow noopener noreferrer\">\u0441\u043a\u0430\u0447\u0430\u0442\u044c<\/a>.<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/545660\/\"> https:\/\/habr.com\/ru\/post\/545660\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sa\/fh\/en\/safhenqnyltoiatl2x-83trc6lc.gif\"><\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u043f\u0440\u0438\u043b\u043e\u0436\u0438\u0432 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0443\u0441\u0438\u043b\u0438\u044f, \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c python 3.7+flask+tensorflow 2.0+keras+\u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0432\u043a\u0440\u0430\u043f\u043b\u0435\u043d\u0438\u044f js \u0438 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 web-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0440\u0438\u0441\u0443\u044f \u043d\u0430 \u0445\u043e\u043b\u0441\u0442\u0435, \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435 \u0446\u0438\u0444\u0440\u044b, \u0430 \u0440\u0430\u043d\u0435\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 CNN, \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0440\u0438\u0441\u0443\u043d\u043e\u043a \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041c\u043e\u0434\u0435\u043b\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0430 \u043d\u0430 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0440\u0443\u043a\u043e\u043f\u0438\u0441\u043d\u044b\u0445 \u0446\u0438\u0444\u0440 MNIST, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0441\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u0440\u0443\u0442\u0438\u0442\u044c\u0441\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f windows 7.   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-319108","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=319108"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319108\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}