-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIFLASK.py
More file actions
149 lines (114 loc) · 4.84 KB
/
Copy pathAPIFLASK.py
File metadata and controls
149 lines (114 loc) · 4.84 KB
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
# from flask import Flask, request, jsonify
# import tensorflow as tf
# from PIL import Image
# import numpy as np
# # Load model
# model = tf.keras.models.load_model('faceshape.h5')
# # Initialize Flask app
# app = Flask(__name__) # Ganti _name_ menjadi __name__
# # Define API endpoint for prediction
# @app.route('/predict', methods=['POST'])
# def predict():
# try:
# # Menerima gambar dari permintaan POST
# file = request.files['image']
# if file is None:
# return jsonify({'error': 'No image provided'}), 400
# img = Image.open(file) # Menggunakan Image.open() untuk membuka gambar
# # Mengubah gambar menjadi array numpy
# img = img.resize((150, 150)) # Mengubah ukuran gambar menjadi (150, 150)
# img_array = np.array(img)
# img_array = np.expand_dims(img_array, axis=0)
# img_array = img_array / 255.0 # Normalisasi
# # Memprediksi kelas gambar
# predictions = model.predict(img_array)
# predictionsmax = np.argmax(predictions)
# class_names = ['Bulat', 'Lonjong', 'Oval'] # Ganti dengan kelas yang sesuai
# predicted_class = class_names[predictionsmax]
# # Mengembalikan hasil prediksi dalam format JSON
# result = {'prediction': predicted_class}
# return jsonify(result)
# except Exception as e:
# return jsonify({'error': str(e)}), 500
# # Run the Flask app
# if __name__ == '__main__': # Ganti _name_ menjadi __name__
# app.run(host='0.0.0.0', port=5000)
# from flask import Flask, request, jsonify, send_file
# import tensorflow as tf
# from PIL import Image, ImageDraw, ImageFont
# import numpy as np
# import os
# # Load model
# model = tf.keras.models.load_model('faceshape.h5')
# # Initialize Flask app
# app = Flask(__name__)
# # Define API endpoint for prediction
# @app.route('/predict', methods=['POST'])
# def predict():
# try:
# # Receive image from POST request
# file = request.files['image']
# if file is None:
# return jsonify({'error': 'No image provided'}), 400
# img = Image.open(file)
# # Convert image to numpy array
# img = img.resize((300, 300))
# img_array = np.array(img)
# img_array = np.expand_dims(img_array, axis=0)
# img_array = img_array / 255.0
# # Predict the image class
# predictions = model.predict(img_array)
# predictionsmax = np.argmax(predictions)
# class_names = ['Bulat', 'Lonjong', 'Oval']
# predicted_class = class_names[predictionsmax]
# # Draw the predicted text on the image
# draw = ImageDraw.Draw(img)
# font = ImageFont.truetype("arial.ttf", 30) # Ganti font sesuai kebutuhan
# draw.text((10, 10), f"{predicted_class}", (255, 255, 255), font=font)
# # Save the image with prediction
# img_path = "predicted_image.jpg"
# img.save(img_path) # Save the image with prediction locally
# # Menampilkan hasil prediksi class di terminal
# print(f"Hasil prediksi class: {predicted_class}")
# # Return the saved image as response
# return send_file(img_path, mimetype=f'image/{img.format.lower()}')
# except Exception as e:
# return jsonify({'error': str(e)}), 500
# # Run the Flask app
# if __name__ == '__main__':
# app.run(host='0.0.0.0', port=5000)
from flask import Flask, request, jsonify
import tensorflow as tf
from PIL import Image
import numpy as np
# Load model
model = tf.keras.models.load_model('faceshape_model.h5')
# Initialize Flask app
app = Flask(__name__) # Ganti _name_ menjadi __name__
# Define API endpoint for prediction
@app.route('/predict', methods=['POST'])
def predict():
try:
# Menerima gambar dari permintaan POST
file = request.files['image']
if file is None:
return jsonify({'error': 'No image provided'}), 400
img = Image.open(file) # Menggunakan Image.open() untuk membuka gambar
# Mengubah gambar menjadi array numpy
img = img.resize((300, 300)) # Mengubah ukuran gambar menjadi (300, 300)
img_array = np.array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0 # Normalisasi
# Memprediksi kelas gambar
predictions = model.predict(img_array)
predictionsmax = np.argmax(predictions)
class_names = ['Bulat', 'Lonjong', 'Oval'] # Ganti dengan kelas yang sesuai
predicted_class = class_names[predictionsmax]
# Mengembalikan hasil prediksi dalam format JSON
result = {'prediction': predicted_class}
return jsonify(result)
except Exception as e:
return jsonify({'error': str(e)}), 500
# Run the Flask app
if __name__ == '__main__': # Ganti _name_ menjadi __name__
app.run(host='0.0.0.0', port=5000)