Spaces:
Sleeping
Sleeping
fix ini
Browse files
app.py
CHANGED
@@ -1,12 +1,9 @@
|
|
1 |
from fastapi import FastAPI, UploadFile, File
|
2 |
-
|
3 |
import numpy as np
|
4 |
-
import cv2
|
5 |
import tensorflow as tf
|
6 |
from PIL import Image
|
7 |
import io
|
8 |
|
9 |
-
|
10 |
app = FastAPI()
|
11 |
|
12 |
# Load model Keras
|
@@ -28,79 +25,16 @@ labels = [
|
|
28 |
def home():
|
29 |
return {"message": "Aksara Lontara API is running"}
|
30 |
|
31 |
-
|
32 |
-
def preprocess_image(image: np.ndarray):
|
33 |
-
""" Melakukan segmentasi karakter menggunakan OpenCV """
|
34 |
-
|
35 |
-
# **1️⃣ Edge Detection (Canny)**
|
36 |
-
edges = cv2.Canny(image, 50, 150)
|
37 |
-
|
38 |
-
# **2️⃣ Morphological Cleaning**
|
39 |
-
kernel = np.ones((3, 3), np.uint8)
|
40 |
-
edges_cleaned = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel, iterations=2)
|
41 |
-
|
42 |
-
# **3️⃣ Connected Component Analysis (CCA)**
|
43 |
-
num_labels, labels, stats, _ = cv2.connectedComponentsWithStats(edges_cleaned, connectivity=8)
|
44 |
-
|
45 |
-
# **4️⃣ Filter huruf berdasarkan area**
|
46 |
-
min_area = 500
|
47 |
-
bounding_boxes = []
|
48 |
-
|
49 |
-
for i in range(1, num_labels): # Skip background
|
50 |
-
x, y, w, h, area = stats[i]
|
51 |
-
if area > min_area:
|
52 |
-
bounding_boxes.append((x, y, w, h))
|
53 |
-
|
54 |
-
# **5️⃣ Urutkan huruf berdasarkan posisi X**
|
55 |
-
bounding_boxes.sort(key=lambda b: b[0])
|
56 |
-
|
57 |
-
# **6️⃣ Gabungkan Bounding Box yang Berdekatan**
|
58 |
-
merged_boxes = []
|
59 |
-
merge_threshold = 20
|
60 |
-
|
61 |
-
for i in range(len(bounding_boxes)):
|
62 |
-
x, y, w, h = bounding_boxes[i]
|
63 |
-
|
64 |
-
if merged_boxes and (x - (merged_boxes[-1][0] + merged_boxes[-1][2])) < merge_threshold:
|
65 |
-
x_prev, y_prev, w_prev, h_prev = merged_boxes.pop()
|
66 |
-
x_new = min(x_prev, x)
|
67 |
-
y_new = min(y_prev, y)
|
68 |
-
w_new = max(x_prev + w_prev, x + w) - x_new
|
69 |
-
h_new = max(y_prev + h_prev, y + h) - y_new
|
70 |
-
merged_boxes.append((x_new, y_new, w_new, h_new))
|
71 |
-
else:
|
72 |
-
merged_boxes.append((x, y, w, h))
|
73 |
-
|
74 |
-
# **7️⃣ Potong dan proses karakter**
|
75 |
-
segmented_chars = []
|
76 |
-
for (x, y, w, h) in merged_boxes:
|
77 |
-
char_segment = image[y:y+h, x:x+w]
|
78 |
-
char_segment = cv2.resize(char_segment, (128, 128), interpolation=cv2.INTER_AREA)
|
79 |
-
segmented_chars.append(char_segment)
|
80 |
-
|
81 |
-
return segmented_chars
|
82 |
-
|
83 |
@app.post("/predict")
|
84 |
async def predict(file: UploadFile = File(...)):
|
85 |
-
# Baca gambar dari file upload
|
86 |
-
image = Image.open(io.BytesIO(await file.read())).convert("L")
|
87 |
-
image =
|
88 |
-
|
89 |
-
#
|
90 |
-
segmented_chars = preprocess_image(image)
|
91 |
-
|
92 |
-
# Jika tidak ada huruf terdeteksi
|
93 |
-
if not segmented_chars:
|
94 |
-
return {"prediction": "No characters detected"}
|
95 |
-
|
96 |
-
# **Prediksi untuk setiap karakter**
|
97 |
-
predictions = []
|
98 |
-
for char in segmented_chars:
|
99 |
-
char_norm = np.array(char) / 255.0 # Normalisasi
|
100 |
-
char_norm = char_norm.reshape(1, 128, 128, 1) # Reshape untuk model
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
|
106 |
-
return {"
|
|
|
1 |
from fastapi import FastAPI, UploadFile, File
|
|
|
2 |
import numpy as np
|
|
|
3 |
import tensorflow as tf
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
|
|
|
7 |
app = FastAPI()
|
8 |
|
9 |
# Load model Keras
|
|
|
25 |
def home():
|
26 |
return {"message": "Aksara Lontara API is running"}
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
@app.post("/predict")
|
29 |
async def predict(file: UploadFile = File(...)):
|
30 |
+
# **Baca gambar dari file upload tanpa pre-processing tambahan**
|
31 |
+
image = Image.open(io.BytesIO(await file.read())).convert("L") # Convert ke grayscale
|
32 |
+
image = image.resize((128, 128)) # Resize ke 128x128 sesuai model
|
33 |
+
image = np.array(image) / 255.0 # Normalisasi
|
34 |
+
image = image.reshape(1, 128, 128, 1) # Reshape untuk model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# **Lakukan prediksi**
|
37 |
+
prediction = model.predict(image)
|
38 |
+
predicted_label = labels[np.argmax(prediction)]
|
39 |
|
40 |
+
return {"prediction": predicted_label}
|