ggun6883 commited on
Commit
d2f13fa
·
verified ·
1 Parent(s): f4c2633

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -5,6 +5,7 @@ from fastapi import FastAPI
5
  from pydantic import BaseModel
6
  from collections import defaultdict
7
  import uvicorn
 
8
  import os
9
 
10
  app = FastAPI()
@@ -12,12 +13,11 @@ app = FastAPI()
12
  class InputData(BaseModel):
13
  image_base64: str
14
 
15
- def save_base64_image_cv(base64_str, output_path="final.png"):
16
  image_data = base64.b64decode(base64_str)
17
  nparr = np.frombuffer(image_data, np.uint8)
18
  img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
19
 
20
- # jika RGBA → tempelkan ke background putih
21
  if img is not None and img.shape[2] == 4:
22
  alpha = img[:, :, 3] / 255.0
23
  rgb = img[:, :, :3]
@@ -25,9 +25,8 @@ def save_base64_image_cv(base64_str, output_path="final.png"):
25
  img = (rgb * alpha[:, :, None] + white_bg * (1 - alpha[:, :, None])).astype(np.uint8)
26
 
27
  cv2.imwrite(output_path, img)
28
- return output_path
29
 
30
- def extract_icon_positions(image_path="final.png"):
31
  img = cv2.imread(image_path)
32
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
33
 
@@ -69,29 +68,36 @@ def find_rarest_icon_position(icon_features, positions):
69
 
70
  rarest_group = min(groups.values(), key=len)
71
  idx = rarest_group[0]
72
-
73
  return positions[idx][0], positions[idx][1]
74
 
75
  @app.post("/predict")
76
  async def predict(data: InputData):
 
 
 
 
77
  try:
78
- save_base64_image_cv(data.image_base64, "final.png")
79
 
80
- icon_features, positions = extract_icon_positions("final.png")
81
 
82
  if not positions:
83
  return {"error": "No icons detected"}
84
 
85
  x, y = find_rarest_icon_position(icon_features, positions)
86
 
87
- # hapus file temp
88
- if os.path.exists("final.png"):
89
- os.remove("final.png")
90
-
91
- return {"x": x, "y": y}
92
 
93
  except Exception as e:
94
  return {"error": str(e)}
95
 
 
 
 
 
96
  if __name__ == "__main__":
97
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
5
  from pydantic import BaseModel
6
  from collections import defaultdict
7
  import uvicorn
8
+ import uuid
9
  import os
10
 
11
  app = FastAPI()
 
13
  class InputData(BaseModel):
14
  image_base64: str
15
 
16
+ def save_base64_image_cv(base64_str, output_path):
17
  image_data = base64.b64decode(base64_str)
18
  nparr = np.frombuffer(image_data, np.uint8)
19
  img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
20
 
 
21
  if img is not None and img.shape[2] == 4:
22
  alpha = img[:, :, 3] / 255.0
23
  rgb = img[:, :, :3]
 
25
  img = (rgb * alpha[:, :, None] + white_bg * (1 - alpha[:, :, None])).astype(np.uint8)
26
 
27
  cv2.imwrite(output_path, img)
 
28
 
29
+ def extract_icon_positions(image_path):
30
  img = cv2.imread(image_path)
31
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
32
 
 
68
 
69
  rarest_group = min(groups.values(), key=len)
70
  idx = rarest_group[0]
 
71
  return positions[idx][0], positions[idx][1]
72
 
73
  @app.post("/predict")
74
  async def predict(data: InputData):
75
+
76
+ # file unik → aman untuk paralel
77
+ tmp_file = f"{uuid.uuid4().hex}.png"
78
+
79
  try:
80
+ save_base64_image_cv(data.image_base64, tmp_file)
81
 
82
+ icon_features, positions = extract_icon_positions(tmp_file)
83
 
84
  if not positions:
85
  return {"error": "No icons detected"}
86
 
87
  x, y = find_rarest_icon_position(icon_features, positions)
88
 
89
+ return {
90
+ "x": x,
91
+ "y": y,
92
+ "count": len(positions) # opsional
93
+ }
94
 
95
  except Exception as e:
96
  return {"error": str(e)}
97
 
98
+ finally:
99
+ if os.path.exists(tmp_file):
100
+ os.remove(tmp_file)
101
+
102
  if __name__ == "__main__":
103
  uvicorn.run(app, host="0.0.0.0", port=7860)