arnaucube
arnaucube/objectImageIdentifierAIPublic Self-hosted repository
objectImageIdentifierAI/other/serverPredictorOLD/main.py
other/serverPredictorOLD/main.pyRaw
 1from flask import Flask
 2from flask_restful import Resource, Api, request
 3
 4import matplotlib.pyplot as plt
 5import numpy as np
 6import cv2
 7import io
 8from PIL import Image, ImageOps
 9
10import pickle
11
12app = Flask(__name__)
13app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB
14api = Api(app)
15
16size = 100, 100
17
18
19#load Neural Network, generated with nnTrain
20nn = pickle.load(open('nn.pkl', 'rb'))
21
22class Predict(Resource):
23    def get(self):
24        message = {'message': 'getted route1'}
25        return message
26    def post(self):
27        filer = request.files['file']
28        #open the uploaded image, and transform to the numpy array
29        filer.save("currentimage.png")
30        image = Image.open("currentimage.png")
31        thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
32        image_data = np.asarray(thumb).flatten()
33        imagetopredict = np.array([image_data])
34
35        #predict the class of the image with the neural network
36        prediction = nn.predict(imagetopredict)
37        print "prediction"
38        print prediction[0][0]
39        if prediction[0][0]==0:
40            result = "noobject"
41        else:
42            result = "object"
43        message = {'class': result}
44        return message
45
46
47class Route2(Resource):
48    def get(self):
49        return {'message': 'getted route2'}
50
51
52class Route3(Resource):
53    def get(self):
54        return {'message': 'getted route3'}
55
56
57api.add_resource(Predict, '/predict')
58api.add_resource(Route2, '/route2')
59api.add_resource(Route3, '/route3')
60
61
62if __name__ == '__main__':
63     app.run(port='3045')