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.

69 lines
1.7 KiB

  1. import pandas as pd
  2. import logging
  3. from datetime import datetime
  4. import time
  5. import os
  6. import pickle
  7. import numpy as np
  8. from flask import Flask
  9. from flask_restful import Resource, Api, request
  10. from flask_cors import CORS
  11. from PIL import Image, ImageOps
  12. #image size after resize
  13. size = 100, 100
  14. #model = pickle.load(open('model.pkl', 'rb'))
  15. #print('model loaded')
  16. app = Flask(__name__)
  17. CORS(app)
  18. api = Api(app)
  19. class Predict(Resource):
  20. def post(self):
  21. print("new predict")
  22. start = time.time()
  23. print(request.files['file'])
  24. filer = request.files['file']#open the uploaded image, and transform to the numpy array
  25. #process the img
  26. filer.save("currentimage.png")
  27. image = Image.open("currentimage.png")
  28. thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
  29. image_data = np.array(thumb).flatten()[:100]
  30. #load the selected model
  31. model = pickle.load(open('model.pkl', 'rb'))
  32. print('model.pkl loaded')
  33. #predict with the selected model
  34. r = model.predict([image_data])[0]
  35. logging.info(" [result]: " + r)
  36. roundtrip = time.time() - start
  37. logging.info(" [roundtriptime]: " + str(roundtrip) + "s")
  38. print("Response: ")
  39. print({'result': r})
  40. return({'result': r})
  41. api.add_resource(Predict, '/predict')
  42. if __name__ == '__main__':
  43. log_directory = 'log'
  44. if not os.path.exists(log_directory):
  45. os.makedirs(log_directory)
  46. logfilename = log_directory + "/logging"+ datetime.now().strftime("%Y-%M-%d-%H:%m:%s") + ".log"
  47. logging.basicConfig(filename=logfilename,level=logging.DEBUG)
  48. print("server running")
  49. app.run(port='3200')