Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

main.py 2.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  1. import os
  2. import urllib.request
  3. from flask import Flask, flash, request, redirect, url_for, render_template
  4. from werkzeug.utils import secure_filename
  5. from keras.preprocessing.image import load_img
  6. from keras.preprocessing.image import img_to_array
  7. from keras.models import load_model
  8. import numpy as np
  9. # Constants
  10. CLASSES = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
  11. LBL = dict(zip(range(10), CLASSES))
  12. MODEL_PATH = os.path.join(os.getcwd(), 'saves/model.h5')
  13. ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
  14. UPLOAD_FOLDER = os.path.join(os.getcwd(), 'static/uploads')
  15. app = Flask(__name__)
  16. app.secret_key = "secret key"
  17. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  18. def allowed_file(filename):
  19. return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  20. # load and prepare the image
  21. def load_image(filename):
  22. # load the image
  23. img = load_img(os.path.join(UPLOAD_FOLDER, filename), target_size=(32, 32))
  24. # convert to array
  25. img = img_to_array(img)
  26. # reshape into a single sample with 3 channels
  27. img = img.reshape(1, 32, 32, 3)
  28. # prepare pixel data
  29. img = img.astype('float32')
  30. img = img / 255.0
  31. return img
  32. def predict(filename):
  33. # load the image
  34. img = load_image(filename)
  35. # load model
  36. model = load_model(MODEL_PATH)
  37. # predict the class
  38. res = model.predict(img)
  39. label = np.argmax(res)
  40. print(res, label)
  41. label_name = LBL[label]
  42. return label_name
  43. @app.route('/')
  44. def upload_form():
  45. return render_template('upload.html')
  46. @app.route('/', methods=['POST'])
  47. def upload_image():
  48. if 'file' not in request.files:
  49. flash('No file part')
  50. return redirect(request.url)
  51. file = request.files['file']
  52. if file.filename == '':
  53. flash('No image selected for uploading')
  54. return redirect(request.url)
  55. if file and allowed_file(file.filename):
  56. filename = secure_filename(file.filename)
  57. file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
  58. flash('Image successfully uploaded and displayed below')
  59. label = predict(filename)
  60. flash(f'Prediction: {label.title()}')
  61. return render_template('upload.html', filename=filename)
  62. else:
  63. flash('Allowed image types are -> png, jpg, jpeg, gif')
  64. return redirect(request.url)
  65. @app.route('/display/<filename>')
  66. def display_image(filename):
  67. return redirect(url_for('static', filename='uploads/' + filename), code=301)
  68. if __name__ == "__main__":
  69. app.run()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...