Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
from collections import defaultdict
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
class Input(BaseModel):
|
| 12 |
+
image_base64: str
|
| 13 |
+
|
| 14 |
+
def save_base64_image_cv(base64_str, output_path="final.png"):
|
| 15 |
+
img_data = base64.b64decode(base64_str)
|
| 16 |
+
nparr = np.frombuffer(img_data, np.uint8)
|
| 17 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
|
| 18 |
+
|
| 19 |
+
if img.shape[2] == 4:
|
| 20 |
+
alpha = img[:, :, 3] / 255.0
|
| 21 |
+
rgb = img[:, :, :3]
|
| 22 |
+
white_bg = np.ones_like(rgb, dtype=np.uint8) * 255
|
| 23 |
+
img = (rgb * alpha[:, :, None] + white_bg * (1 - alpha[:, :, None])).astype(np.uint8)
|
| 24 |
+
|
| 25 |
+
cv2.imwrite(output_path, img)
|
| 26 |
+
|
| 27 |
+
def extract_icon_positions(image_path):
|
| 28 |
+
img = cv2.imread(image_path)
|
| 29 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 30 |
+
_, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)
|
| 31 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 32 |
+
|
| 33 |
+
icons, pos = [], []
|
| 34 |
+
for c in contours:
|
| 35 |
+
x, y, w, h = cv2.boundingRect(c)
|
| 36 |
+
if w > 10 and h > 10:
|
| 37 |
+
roi = cv2.resize(thresh[y:y+h, x:x+w], (50, 50))
|
| 38 |
+
icons.append(roi)
|
| 39 |
+
pos.append((x, y))
|
| 40 |
+
return icons, pos
|
| 41 |
+
|
| 42 |
+
def img_hash(img):
|
| 43 |
+
img = cv2.resize(img, (8, 8))
|
| 44 |
+
return (img > img.mean()).astype(np.uint8).flatten()
|
| 45 |
+
|
| 46 |
+
def find_rarest(icon_features, positions):
|
| 47 |
+
hashes = [img_hash(i) for i in icon_features]
|
| 48 |
+
groups = defaultdict(list)
|
| 49 |
+
|
| 50 |
+
for i, h in enumerate(hashes):
|
| 51 |
+
for g in groups.values():
|
| 52 |
+
if np.sum(h != hashes[g[0]]) < 3:
|
| 53 |
+
g.append(i)
|
| 54 |
+
break
|
| 55 |
+
else:
|
| 56 |
+
groups[len(groups)] = [i]
|
| 57 |
+
|
| 58 |
+
idx = min(groups.values(), key=len)[0]
|
| 59 |
+
return positions[idx]
|
| 60 |
+
|
| 61 |
+
@app.post("/solve")
|
| 62 |
+
def solve(data: Input):
|
| 63 |
+
try:
|
| 64 |
+
save_base64_image_cv(data.image_base64)
|
| 65 |
+
icons, pos = extract_icon_positions("final.png")
|
| 66 |
+
x, y = find_rarest(icons, pos)
|
| 67 |
+
return {"x": x, "y": y}
|
| 68 |
+
finally:
|
| 69 |
+
if os.path.exists("final.png"):
|
| 70 |
+
os.remove("final.png")
|