ArsanForEver commited on
Commit
f1ba0d6
·
verified ·
1 Parent(s): 7e8ff25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -31
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
- # Simpan file gambar sementara
33
- temp = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
34
- temp.write(await file.read())
35
- temp.close()
 
 
 
 
 
 
 
 
 
36
 
37
- # Baca gambar menggunakan OpenCV
38
- image = cv2.imread(temp.name)
39
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
40
 
41
- # Thresholding untuk mempermudah deteksi huruf
42
- _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
43
 
44
- # Temukan kontur huruf
45
- contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
 
 
 
 
46
 
47
- # Urutkan kontur dari kiri ke kanan
48
- contours = sorted(contours, key=lambda c: cv2.boundingRect(c)[0])
 
 
49
 
50
- # Loop untuk memotong dan klasifikasi tiap huruf
51
- hasil_klasifikasi = []
52
- for contour in contours:
53
- x, y, w, h = cv2.boundingRect(contour)
54
- huruf = gray[y:y+h, x:x+w]
55
 
56
- # Resize ke ukuran model
57
- huruf = cv2.resize(huruf, (128, 128))
58
- huruf = np.array(huruf) / 255.0
59
- huruf = huruf.reshape(1, 128, 128, 1)
60
 
61
- # Prediksi
62
- prediction = model.predict(huruf)
63
- label = labels[np.argmax(prediction)]
64
- hasil_klasifikasi.append(label)
65
 
66
- # Gabungkan hasil klasifikasi menjadi teks akhir
67
- hasil_teks = "".join(hasil_klasifikasi)
68
 
69
- return {"prediction": hasil_teks}
 
 
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)}