-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
90 lines (62 loc) · 2.86 KB
/
app.py
File metadata and controls
90 lines (62 loc) · 2.86 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
import io
from tensorflow import keras
import numpy as np
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import JSONResponse
from PIL import Image, ImageDraw, ImageFont
from tensorflow import keras
model = keras.models.load_model('./models/MobileNetV5_with_new_classes_v4.h5')
labels_list = ['Ayam-Geprek', 'Ayam-Goreng', 'Bakso', 'Burger', 'Ketoprak',
'Martabak-manis (1 Potong)', 'Mie-Goreng', 'Mie-Rebus', 'Nasi-Putih', 'Nasi-Ayam-Goreng',
'Nasi-Goreng', 'Nasi-Padang', 'Nasi-Uduk', 'Nasi-Pecel-Lele',
'Pecel-Sayur', 'Roti-Putih', 'Sate-Ayam', 'Soto', 'Tahu-Goreng',
'Tahu-Telur', 'Telur-Ceplok', 'Tempe-Goreng', 'Ayam-kentucky']
def preprocess_image(file_content: bytes):
# Convert file content to BytesIO
image_stream = io.BytesIO(file_content)
#Load the image
img = keras.preprocessing.image.load_img(image_stream, target_size=(224,224))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0
# Reload the original image for annotation
image_stream.seek(0)
original_image = Image.open(image_stream)
return img_array, original_image
def predict_image(img_array: np.ndarray):
# Make the prediction
predictions = model.predict(img_array)
predicted_class_index = np.argmax(predictions[0])
predicted_class = labels_list[predicted_class_index]
confidence = predictions[0][predicted_class_index] * 100
return predicted_class, confidence
def draw_bounding_box(image: Image, predicted_class: str, confidence:float):
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
# Load a larger font
font_size = 30 # Increase this value to make the text larger
try:
font = ImageFont.truetype("arial.ttf", font_size) # Ensure the font file is available on your system
except IOError:
font = ImageFont.load_default() # Fallback to default font if TTF font is not available
#define bounding box coordinates (sementara pake entire image)
box_coords = [(10, 10), (image.size[0] - 10, image.size[1] - 10)]
# Draw Bounding Box
draw.rectangle(box_coords, outline="red", width=5)
# Draw text
text = f"{predicted_class}: {confidence:.2f}%"
text_location = (10, 10)
text_bbox = draw.textbbox(text_location, text, font=font)
padding = 5
# Increase the bounding box size by adding padding
padded_bbox = [
(text_bbox[0] - padding, text_bbox[1] - padding),
(text_bbox[2] + padding, text_bbox[3] + padding)
]
draw.rectangle(padded_bbox, fill="red")
draw.text(text_location, text, fill="white", font=font)
# Save annotated image to a BytesIO object
annotated_image_stream = io.BytesIO()
image.save(annotated_image_stream, format="JPEG")
annotated_image_stream.seek(0)
return annotated_image_stream