-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
169 lines (160 loc) · 6.57 KB
/
main.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import numpy as np
from PIL import Image
from flask import Flask, jsonify, request
from dotenv import load_dotenv
from werkzeug.utils import secure_filename
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image as tf_image
load_dotenv()
app = Flask(__name__)
# Set Allowed Extension for Upload File
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg'])
app.config['UPLOAD_FOLDER'] = 'static/uploads/'
# Load Model
app.config['MODEL_BONGKAHAN'] = 'models/bongkahan/model_bongkahan_v2.h5'
app.config['MODEL_BRONDOLAN'] = 'models/brondolan/model-brondolan.h5'
app.config['MODEL_NON_SAWIT'] = 'models/deteksi-sawit/model-nonsawitv2.h5'
# Assign to variable
model_non_sawit = load_model(app.config['MODEL_NON_SAWIT'], compile=False)
model_bongkahan = load_model(app.config['MODEL_BONGKAHAN'], compile=False)
model_brondolan = load_model(app.config['MODEL_BRONDOLAN'], compile=False)
# Function to check allowed extension
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# Route Index
@app.route('/', methods=['GET'])
def index():
return jsonify({
'status': {
'code': 200,
'message': 'Model Palomade',
'teamName': 'CH2-PS324'
}
}), 200
# Route Predict Bongkahan
@app.route('/predict-bongkahan', methods=['POST'])
def predictBongkahan():
if request.method == 'POST':
reqImage = request.files['image'] # Get Image Files
if reqImage and allowed_file(reqImage.filename):
filename = secure_filename(reqImage.filename)
reqImage.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
img = Image.open(image_path).convert("RGB")
img = img.resize((150, 150))
x = tf_image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x / 255
detect_non_sawit = model_non_sawit.predict(x)[0][0] # Predict Non Sawit
# Check if Sawit
if(detect_non_sawit > 0.5):
detect_bongkahan = model_bongkahan.predict(x)[0][0] # Predict Bongkahan
# if sawit bongkahan mentah
if(detect_bongkahan > 0.5):
return jsonify({
'status': {
'code': 200,
'message': 'Success predicting',
'data': { 'classType': 'Bongkahan Sawit Mentah', 'precentase': int(detect_bongkahan * 100) },
}
}), 200
# if sawit bongkahan matang
else:
return jsonify({
'status': {
'code': 200,
'message': 'Success predicting',
'data': { 'classType': 'Bongkahan Sawit Matang', 'precentase': (100 - int(detect_bongkahan * 100)) },
}
}), 200
# Check if not bongkahan or brondolan
# non sawit
else:
return jsonify({
'status': {
'code': 200,
'message': 'Success predicting',
'data': { 'classType': 'Bukan Sawit', 'precentase': (100 - int(detect_non_sawit * 100)) },
}
}), 200
# if not allowed file
else:
return jsonify({
'status': {
'code': 400,
'message': 'Invalid file format. Please upload a JPG, JPEG, or PNG image.'
}
}), 400
# if not POST method
else:
return jsonify({
'status': {
'code': 405,
'message': 'Method not allowed'
}
}), 405
# Route Predict Brondolan
@app.route('/predict-brondolan', methods=['POST'])
def predictBrondolan():
if request.method == 'POST':
reqImage = request.files['image'] # Get Image Files
if reqImage and allowed_file(reqImage.filename):
filename = secure_filename(reqImage.filename)
reqImage.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
img = Image.open(image_path).convert("RGB")
img = img.resize((150, 150))
x = tf_image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x / 255
detect_non_sawit = model_non_sawit.predict(x)[0][0] # Predict Non Sawit
# Check if Sawit
if(detect_non_sawit > 0.5):
detect_brondolan = model_brondolan.predict(x)[0][0] # predict brondolan
# if sawit brondolan mentah
if(detect_brondolan > 0.5):
return jsonify({
'status': {
'code': 200,
'message': 'Success predicting',
'data': { 'classType': 'Brondolan Sawit Mentah', 'precentase': int(detect_brondolan * 100) },
}
}), 200
# if sawit brondolan matang
else:
return jsonify({
'status': {
'code': 200,
'message': 'Success predicting',
'data': { 'classType': 'Brondolan Sawit Matang', 'precentase': (100 - int(detect_brondolan * 100)) },
}
}), 200
# non sawit
else:
return jsonify({
'status': {
'code': 200,
'message': 'Success predicting',
'data': { 'classType': 'Bukan Sawit', 'precentase': (100 - int(detect_non_sawit * 100)) },
}
}), 200
# if not allowed file
else:
return jsonify({
'status': {
'code': 400,
'message': 'Invalid file format. Please upload a JPG, JPEG, or PNG image.'
}
}), 400
# if not POST method
else:
return jsonify({
'status': {
'code': 405,
'message': 'Method not allowed'
}
}), 405
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))