Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on calling detector.detectObjectsFromImage from Flask API method without loading model in the method #531

Closed
vattisampath opened this issue May 10, 2020 · 9 comments

Comments

@vattisampath
Copy link

Hi,

Thanks for amazing library making it easy for image object detection. All works fine when i try to test the object detections.

But when I try to detect objects from Flask API method, it throws me error. I m trying to load the model globally once before starting the Flask server application and trying to get image name from POST data and by giving image name as input, i m trying to get detection,

but I m getting error when trying to detect the objects in image without loading the model again in the post method.

below is the code I m using, please look into it and let me know where is the issue.

import flask
from flask import request, abort, make_response, jsonify
from imageai.Detection.Custom import CustomObjectDetection
import logging

global detector
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("invoices/models/detection_model-ex-080--loss-0021.357.h5")
detector.setJsonPath("invoices/json/detection_config.json")
detector.loadModel()

app = flask.Flask(name)
app.config["DEBUG"] = True
logging.basicConfig(level=logging.DEBUG)

@app.route('/predict', methods=['POST'])
def predict():
global detector
if not request.json or not 'file' in request.json:
abort(400)
file = request.json['file']

# detector.loadModel()
detections, extracted_objects_array = detector.detectObjectsFromImage(input_image="images/test/" + file,
                                output_image_path="images/results/" + file,
                                extract_detected_objects=True,
                                minimum_percentage_probability=90,
                                thread_safe=True)

for detection, object_path in zip(detections, extracted_objects_array):
    app.logger.info("  " + detection["name"] + " => " + object_path + " with %f", detection["percentage_probability"])
    app.logger.info("  Points :: %s", detection["box_points"])
    app.logger.info("---------------")

return jsonify(entities)

if name == 'main':
app.run(host='0.0.0.0', threaded=True)

On calling this API, i m getting below error.
image

If I run the same code by uncommenting, detector.loadModel() just above the detectObjectsFromImage method, then it works fine but I dont want to load the model everytime an API is called.

Please help me in understanding the issue and clarify it.

Thanks in advance and thanks for your great effort so far..

@chettkulkarni
Copy link

this is a known issue, this is threading issue apparently. Even I have the same problem in Django

@vattisampath
Copy link
Author

vattisampath commented May 10, 2020

thanks for your reponse @chettkulkarni

but in another thread at #159, in Dec 2019, they have mentioned that we have a solution keeping threads safe.

in the code given in above link, its working fine for them. Why come its not working with my above code.. ??

is it dependant on the libraries I use .. ?? I m just using basic sample example with keras, opencv n tensorflow libraries only..

Please let me know if you have any further info on this.

@xiluzi
Copy link

xiluzi commented May 11, 2020

import flask
from flask import request, abort, make_response, jsonify
from imageai.Detection.Custom import CustomObjectDetection
import logging

global detector
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("invoices/models/hololens-ex-60--loss-2.76.h5")
detector.setJsonPath("invoices/json/detection_config.json")
detector.loadModel()

app = flask.Flask(__name__)
app.config["DEBUG"] = True
logging.basicConfig(level=logging.DEBUG)


@app.route('/predict')
def predict():
    global detector
    # if not request.json or 'file' not in request.json:
    #     abort(400)
    # file = request.json['file']
    # detector.loadModel()
    detections, extracted_objects_array = detector.detectObjectsFromImage(input_image="images/test/" + 'holo2.jpg',
                                                                          output_image_path="images/results/" + 'holo2.jpg',
                                                                          extract_detected_objects=True,
                                                                          minimum_percentage_probability=90,
                                                                          thread_safe=True)

    for detection, object_path in zip(detections, extracted_objects_array):
        app.logger.info("  " + detection["name"] + " => " + object_path + " with %f", detection["percentage_probability"])
        app.logger.info("  Points :: %s", detection["box_points"])
        app.logger.info("---------------")

    return jsonify({'msg': '111'})


if __name__ == '__main__':
    app.run(threaded=True)

Are you sure? It work fine in my computer.

@vattisampath
Copy link
Author

Thanks so much for quick response @xiluzi

image

You can check above API response at this link http://95.217.62.99:5000/process
and below is the error I get on server
image

And below is my PIP list libraries, I m using.. Please let me know if any library differences is there.
And the server configuration is i9 Intel 16 core processor with 64GB RAM with no GPUs.
image

If I put 5 or 10 predections in the same API method, it wont take much time to process those extra images. Response time is same as detecting one image.

I couldnt understand where I m going wrong. Can you please help me in finding out the cause for this issue.

Thanks for your time and effort to help me.
Hoping you are doing good in this corona conditions. Stay safe .. !

@xiluzi
Copy link

xiluzi commented May 12, 2020

absl-py==0.7.1
astor==0.8.0
Click==7.0
cycler==0.10.0
Flask==1.1.1
gast==0.2.2
google-pasta==0.1.7
grpcio==1.22.0
h5py==2.9.0
imageai==2.1.5
itsdangerous==1.1.0
Jinja2==2.10.1
Keras==2.2.4
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
kiwisolver==1.1.0
Markdown==3.1.1
MarkupSafe==1.1.1
matplotlib==3.1.1
numpy==1.17.0
opencv-contrib-python==4.1.0.25
opencv-python==4.1.0.25
Pillow==6.1.0
protobuf==3.9.0
pykalman==0.9.5
pyparsing==2.4.2
python-dateutil==2.8.0
PyYAML==5.1.2
scipy==1.3.0
six==1.12.0
tensorboard==1.14.0
tensorflow==1.14.0
tensorflow-estimator==1.14.0
termcolor==1.1.0
Werkzeug==0.15.5
wrapt==1.11.2

This‘s my lib.But I run it in windows.
So can you run my code above?the models,json,and test_pic is from this link: https://github.com/OlafenwaMoses/ImageAI/blob/master/imageai/Detection/Custom/CUSTOMDETECTION.md

@vattisampath
Copy link
Author

Thanks @xiluzi for your time and effort.

I have tried running above code with its model and matching your library versions and on Ubuntu 16.04 OS and still got some errors.
image

You can see the response @ http://95.217.62.99:5000/process. I dont have windows machine to test it on.

Please let me know what might be the issue and ways to reduce the loading model duration.

Thanks in advance

@xiluzi
Copy link

xiluzi commented May 13, 2020

Sorry. I don't know what's different between our code and env. The last suggestion I can give you is, maybe you can try to pip uninstall tensorflow-gpu and retry.

@vattisampath
Copy link
Author

No worries @xiluzi .. Thanks for your time so far..
I m not using GPU at all but anyway I tried with entire new environment but still no use.

Can you please do one favor to me to get some clue in this issue.
Can you please zip your entire folder along with python file and evn folder with model n json data.
I ll try with the same libraries what you are using on my server and check it.

If its still not working the same exact folder, then its issue on my server and I ll try on another server or windows machine.
Also please let me know your exact python version. I m using 3.5 python. Hope that should be fine.

Please help me by sending ur env n other files where it is working for you.

Thanks in advance.

@skoroneos
Copy link

Try to disable threads in Flask

app.run(host='0.0.0.0',threaded = False)

and it will probably work.
Had the same issue for custom image detection model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants