Update app.py
Browse files
app.py
CHANGED
|
@@ -2,40 +2,44 @@ From fastapi import FastAPI, UploadFile, File
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import uvicorn
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
from PIL import Image
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
#
|
| 12 |
bg_gray = cv2.cvtColor(bg_img, cv2.COLOR_BGR2GRAY)
|
| 13 |
target_gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
_, _, _, max_loc = cv2.minMaxLoc(res)
|
| 18 |
|
| 19 |
-
# max_loc[0]
|
| 20 |
return max_loc[0]
|
| 21 |
|
| 22 |
@app.post("/solve")
|
| 23 |
async def solve(background: UploadFile = File(...), target: UploadFile = File(...)):
|
| 24 |
try:
|
| 25 |
-
# Baca gambar background
|
| 26 |
bg_bytes = await background.read()
|
| 27 |
-
bg_img = cv2.imdecode(np.frombuffer(bg_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 28 |
-
|
| 29 |
-
# Baca gambar target (potongan puzzle)
|
| 30 |
target_bytes = await target.read()
|
|
|
|
|
|
|
| 31 |
target_img = cv2.imdecode(np.frombuffer(target_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
distance =
|
| 35 |
|
| 36 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
return {"status": "error", "message": str(e)}
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import uvicorn
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
def get_distance(bg_img, target_img):
|
| 9 |
+
# 1. Preprocessing: Ubah ke Gray dan Blur untuk ngilangin noise
|
| 10 |
bg_gray = cv2.cvtColor(bg_img, cv2.COLOR_BGR2GRAY)
|
| 11 |
target_gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY)
|
| 12 |
|
| 13 |
+
# 2. Canny Edge Detection: Ini kuncinya! Kita cari garis tepi
|
| 14 |
+
bg_edge = cv2.Canny(bg_gray, 100, 200)
|
| 15 |
+
target_edge = cv2.Canny(target_gray, 100, 200)
|
| 16 |
+
|
| 17 |
+
# 3. Template Matching pada hasil Edge Detection
|
| 18 |
+
res = cv2.matchTemplate(bg_edge, target_edge, cv2.TM_CCOEFF_NORMED)
|
| 19 |
_, _, _, max_loc = cv2.minMaxLoc(res)
|
| 20 |
|
| 21 |
+
# max_loc[0] adalah koordinat X terbaik
|
| 22 |
return max_loc[0]
|
| 23 |
|
| 24 |
@app.post("/solve")
|
| 25 |
async def solve(background: UploadFile = File(...), target: UploadFile = File(...)):
|
| 26 |
try:
|
|
|
|
| 27 |
bg_bytes = await background.read()
|
|
|
|
|
|
|
|
|
|
| 28 |
target_bytes = await target.read()
|
| 29 |
+
|
| 30 |
+
bg_img = cv2.imdecode(np.frombuffer(bg_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 31 |
target_img = cv2.imdecode(np.frombuffer(target_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 32 |
|
| 33 |
+
# Eksekusi pencarian jarak
|
| 34 |
+
distance = get_distance(bg_img, target_img)
|
| 35 |
|
| 36 |
+
return {
|
| 37 |
+
"status": "success",
|
| 38 |
+
"distance": int(distance),
|
| 39 |
+
"info": "Edge detection applied for maximum accuracy"
|
| 40 |
+
}
|
| 41 |
except Exception as e:
|
| 42 |
return {"status": "error", "message": str(e)}
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|