ria-com / nomeroff-net

Nomeroff Net. Automatic numberplate recognition system.
GNU General Public License v3.0
459 stars 159 forks source link

Не получается обернуть код через Flask, прошу помощи. #69

Closed Sergio-Badanin closed 4 years ago

Sergio-Badanin commented 4 years ago

Друзья, день добрый!

Пытаюсь обернуть ваш код через Flask. Что бы выбор файла картинки был через веб. В питоне не особо силен, помогите если не сложно разобраться со следующей проблемой:

Собственно сам код:

import os
import numpy as np
import sys
import matplotlib.image as mpimg

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
NOMEROFF_NET_DIR = os.path.abspath('../../')
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}

MASK_RCNN_DIR = os.path.join(NOMEROFF_NET_DIR, 'Mask_RCNN')
MASK_RCNN_LOG_DIR = os.path.join(NOMEROFF_NET_DIR, 'logs')

sys.path.append(NOMEROFF_NET_DIR)

from flask import Flask, render_template, request, url_for
from werkzeug.utils import secure_filename
from NomeroffNet import  filters, RectDetector, TextDetector, OptionsDetector, Detector, textPostprocessing, textPostprocessingAsync

nnet = Detector(MASK_RCNN_DIR, MASK_RCNN_LOG_DIR)
nnet.loadModel("latest")

rectDetector = RectDetector()
optionsDetector = OptionsDetector()
optionsDetector.load("latest")
textDetector = TextDetector.get_static_module("eu")()
textDetector.load("latest")

app = Flask(__name__)

@app.route("/", methods=["POST", "GET"])
def index():
    if request.method == "POST":
        file = request.files["file"]
        if file and (file.content_type.rsplit('/', 1)[1] in ALLOWED_EXTENSIONS).__bool__():
            filename = secure_filename(file.filename)
            file.save(NOMEROFF_NET_DIR + '/examples/images/' + filename)
            imgPath = (NOMEROFF_NET_DIR + '/examples/images/' +  filename)
            img = mpimg.imread(imgPath)
            NP = nnet.detect([img])
            cv_img_masks = filters.cv_img_mask(NP)
            arrPoints = rectDetector.detect(cv_img_masks)
            zones = rectDetector.get_cv_zonesBGR(img, arrPoints)
            regionIds, stateIds, countLines = optionsDetector.predict(zones)
            regionNames = optionsDetector.getRegionLabels(regionIds)
            textArr = textDetector.predict(zones)
            textArr = textPostprocessing(textArr, regionNames)
            print(textArr)
    return render_template("index.html")

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

После выбора файла и загрузки через веб, вылетает следующее:

 * Serving Flask app "site" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
10.10.77.200 - - [04/Mar/2020 10:07:28] "GET / HTTP/1.1" 200 -
[2020-03-04 10:07:32,699] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/src/nomeroff-net/examples/py/site.py", line 43, in index
    NP = nnet.detect([img])
  File "/usr/src/nomeroff-net/NomeroffNet/Detector.py", line 98, in detect
    return self.MODEL.detect(self.normalize(images), verbose=verbose)
  File "/usr/src/nomeroff-net/Mask_RCNN/mrcnn/model.py", line 2524, in detect
    self.keras_model.predict([molded_images, image_metas, anchors], verbose=0)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1395, in predict
    self._make_predict_function()
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 557, in _make_predict_function
    **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py", line 3010, in function
    return Function(inputs, outputs, updates=updates, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py", line 2808, in __init__
    with tf.control_dependencies(self.outputs):
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 5426, in control_dependencies
    return get_default_graph().control_dependencies(control_inputs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 4867, in control_dependencies
    c = self.as_graph_element(c)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3796, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3875, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("mrcnn_detection/Reshape_1:0", shape=(1, 100, 6), dtype=float32) is not an element of this graph.
10.10.77.200 - - [04/Mar/2020 10:07:32] "POST / HTTP/1.1" 500 -

В чем может быть проблема, ткните пожалуйста носом куда копать?

Sergio-Badanin commented 4 years ago

Забыл сказать, что если запускаю просто код "Hello World", то все нормально проходит и распознавание отрабатывается.

GalymzhanAbdimanap commented 4 years ago

@Sergio-Badanin
передавайте graph = tf.get_default_graph() когда вызываете функций распознавании, а в функциях распознавания добавьте ` def function_recognition(): with graph.as_default():

operations of function

` попробуйте так и отпишитесь если работает

Sergio-Badanin commented 4 years ago

Поставил после img = mpimg.imread(imgPath):

@app.route("/", methods=["POST", "GET"])
def index():
    if request.method == "POST":
        file = request.files["file"]
        if file and (file.content_type.rsplit('/', 1)[1] in ALLOWED_EXTENSIONS).__bool__():
            filename = secure_filename(file.filename)
            file.save(NOMEROFF_NET_DIR + '/examples/images/' + filename)
            imgPath = (NOMEROFF_NET_DIR + '/examples/images/' +  filename)
            img = mpimg.imread(imgPath)
            graph = tf.get_default_graph()
            NP = nnet.detect([img])
            cv_img_masks = filters.cv_img_mask(NP)
            arrPoints = rectDetector.detect(cv_img_masks)
            zones = rectDetector.get_cv_zonesBGR(img, arrPoints)
            regionIds, stateIds, countLines = optionsDetector.predict(zones)
            regionNames = optionsDetector.getRegionLabels(regionIds)
            textArr = textDetector.predict(zones)
            textArr = textPostprocessing(textArr, regionNames)
            print(textArr)
    return render_template("index.html")

Выдает ошибку:

[2020-03-04 12:27:13,016] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/src/nomeroff-net/examples/py/site.py", line 52, in index
    graph = tf.get_default_graph()
NameError: name 'tf' is not defined
GalymzhanAbdimanap commented 4 years ago

это на верху где импорты

import tensorflow as tf

graph = tf.get_default_graph()

`

и снизу где def index

`with graph.as_default():

   NP = nnet.detect([img])
   cv_img_masks = filters.cv_img_mask(NP)

.....`

Sergio-Badanin commented 4 years ago

@GalymzhanAbdimanap , премного благодарен!

Заработало в таком варианте:

import os
import numpy as np
import sys
import matplotlib.image as mpimg
import tensorflow as tf
import warnings

graph = tf.get_default_graph()

warnings.filterwarnings('ignore')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
NOMEROFF_NET_DIR = os.path.abspath('../../')
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}

MASK_RCNN_DIR = os.path.join(NOMEROFF_NET_DIR, 'Mask_RCNN')
MASK_RCNN_LOG_DIR = os.path.join(NOMEROFF_NET_DIR, 'logs')

sys.path.append(NOMEROFF_NET_DIR)

from flask import Flask, render_template, request, url_for
from werkzeug.utils import secure_filename
from NomeroffNet import  filters, RectDetector, TextDetector, OptionsDetector, Detector, textPostprocessing, textPostprocessingAsync

nnet = Detector(MASK_RCNN_DIR, MASK_RCNN_LOG_DIR)
nnet.loadModel("latest")

rectDetector = RectDetector()
optionsDetector = OptionsDetector()
optionsDetector.load("latest")
textDetector = TextDetector.get_static_module("eu")()
textDetector.load("latest")

app = Flask(__name__)

@app.route("/", methods=["POST", "GET"])
def index():
    global graph
    if request.method == "POST":
        file = request.files["file"]
        if file and (file.content_type.rsplit('/', 1)[1] in ALLOWED_EXTENSIONS).__bool__():
            filename = secure_filename(file.filename)
            file.save(NOMEROFF_NET_DIR + '/examples/images/' + filename)
            imgPath = (NOMEROFF_NET_DIR + '/examples/images/' +  filename)
            img = mpimg.imread(imgPath)
            with graph.as_default():
                NP = nnet.detect([img])
                cv_img_masks = filters.cv_img_mask(NP)
                arrPoints = rectDetector.detect(cv_img_masks)
                zones = rectDetector.get_cv_zonesBGR(img, arrPoints)
                regionIds, stateIds, countLines = optionsDetector.predict(zones)
                regionNames = optionsDetector.getRegionLabels(regionIds)
                textArr = textDetector.predict(zones)
                textArr = textPostprocessing(textArr, regionNames)
                print(textArr)
    return render_template("index.html")

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)