You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5 KiB

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