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.

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