Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,8 @@ from fastapi import FastAPI, UploadFile, File
|
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
-
import io
|
6 |
import tempfile
|
7 |
|
8 |
-
|
9 |
app = FastAPI()
|
10 |
|
11 |
# Load model Keras
|
@@ -29,41 +27,51 @@ def home():
|
|
29 |
|
30 |
@app.post("/predict")
|
31 |
async def predict(file: UploadFile = File(...)):
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
40 |
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
huruf = gray[y:y+h, x:x+w]
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
-
huruf = np.array(huruf) / 255.0
|
59 |
-
huruf = huruf.reshape(1, 128, 128, 1)
|
60 |
|
61 |
-
#
|
62 |
-
|
63 |
-
label = labels[np.argmax(prediction)]
|
64 |
-
hasil_klasifikasi.append(label)
|
65 |
|
66 |
-
|
67 |
-
hasil_teks = "".join(hasil_klasifikasi)
|
68 |
|
69 |
-
|
|
|
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
import cv2
|
|
|
5 |
import tempfile
|
6 |
|
|
|
7 |
app = FastAPI()
|
8 |
|
9 |
# Load model Keras
|
|
|
27 |
|
28 |
@app.post("/predict")
|
29 |
async def predict(file: UploadFile = File(...)):
|
30 |
+
try:
|
31 |
+
# Simpan file gambar sementara
|
32 |
+
temp = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
33 |
+
temp.write(await file.read())
|
34 |
+
temp.close()
|
35 |
+
|
36 |
+
# Baca gambar menggunakan OpenCV
|
37 |
+
image = cv2.imread(temp.name)
|
38 |
+
if image is None:
|
39 |
+
return {"error": "Gagal membaca gambar"}
|
40 |
+
|
41 |
+
# Konversi ke grayscale
|
42 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
43 |
|
44 |
+
# Thresholding agar huruf menjadi putih (255) dan latar belakang hitam (0)
|
45 |
+
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
|
|
|
46 |
|
47 |
+
# Connected Component Analysis
|
48 |
+
num_labels, labels_im, stats, centroids = cv2.connectedComponentsWithStats(binary, connectivity=8)
|
49 |
|
50 |
+
# Ambil bounding box dari tiap huruf
|
51 |
+
min_area = 500 # Filter noise kecil
|
52 |
+
hasil_klasifikasi = []
|
53 |
+
for i in range(1, num_labels): # label 0 adalah background
|
54 |
+
x, y, w, h, area = stats[i]
|
55 |
+
if area < min_area:
|
56 |
+
continue # Skip noise kecil
|
57 |
|
58 |
+
huruf = gray[y:y+h, x:x+w]
|
59 |
+
huruf = cv2.resize(huruf, (128, 128)) # Resize ke input model
|
60 |
+
huruf = np.array(huruf) / 255.0
|
61 |
+
huruf = huruf.reshape(1, 128, 128, 1)
|
62 |
|
63 |
+
# Prediksi model
|
64 |
+
prediction = model.predict(huruf)
|
65 |
+
label = labels[np.argmax(prediction)]
|
66 |
+
hasil_klasifikasi.append((x, label)) # Simpan posisi dan hasil prediksi
|
|
|
67 |
|
68 |
+
# Urutkan huruf berdasarkan posisi X (kiri ke kanan)
|
69 |
+
hasil_klasifikasi.sort(key=lambda x: x[0])
|
|
|
|
|
70 |
|
71 |
+
# Gabungkan hasil klasifikasi menjadi teks akhir
|
72 |
+
hasil_teks = "".join([h[1] for h in hasil_klasifikasi])
|
|
|
|
|
73 |
|
74 |
+
return {"prediction": hasil_teks}
|
|
|
75 |
|
76 |
+
except Exception as e:
|
77 |
+
return {"error": str(e)}
|