-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
317 lines (236 loc) · 6.86 KB
/
Copy pathserver.py
File metadata and controls
317 lines (236 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import os
import io
import json
import base64
import zipfile
import tempfile
import numpy as np
from PIL import Image
import faiss
import numpy as np
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import JSONResponse, StreamingResponse
from fastapi.responses import FileResponse
app = FastAPI()
# ----------------------------
# CONFIG
# ----------------------------
CHUNK_SIZE = (64, 64)
SIMILARITY_THRESHOLD = 0.01 # lower = stricter
DATASET_PATH = "chunk_dataset.json"
# ----------------------------
# LOAD DATASET
# ----------------------------
if os.path.exists(DATASET_PATH):
with open(DATASET_PATH, "r") as f:
CHUNK_DATASET = json.load(f)
else:
CHUNK_DATASET = []
# ----------------------------
# UTIL
# ----------------------------
def mse(a, b):
return np.mean((a.astype("float32") - b.astype("float32")) ** 2)
def similarity(a, b):
return 1.0 - np.mean((a - b) ** 2)
def update_stats(top_k):
best, best_sim = top_k[0]
best["use_count"] += 1
best["match_score"] += best_sim
for entry, sim in top_k:
entry["nearby_score"] += sim
entry["avg_similarity"] = (
entry["avg_similarity"] * 0.9 + sim * 0.1
)
return best
def faiss_search(chunk, k=10):
vec = embed(chunk).reshape(1, -1)
distances, indices = index.search(vec, k)
results = []
for i, dist in zip(indices[0], distances[0]):
results.append((CHUNK_DATASET[i], 1 / (1 + dist)))
return results
def detect_missing_regions(top_k, threshold=0.7):
missing = []
for entry, sim in top_k:
if sim < threshold:
missing.append(entry)
return missing
def embed(chunk):
# simple baseline (replace later with CNN/CLIP)
return chunk.flatten().astype("float32")
def top_k_matches(chunk, k=5):
scored = []
for entry in CHUNK_DATASET:
sim = similarity(chunk, entry["image"])
scored.append((entry, sim))
entry.update({
"use_count": 0,
"match_score": 0.0,
"nearby_score": 0.0,
"avg_similarity": 0.0
})
entry["embedding"] = embed(entry["image"])
scored.sort(key=lambda x: x[1], reverse=True)
return scored[:k]
def compute_rank(e):
return (
e["use_count"] +
e["match_score"] * 2 +
e["nearby_score"] * 0.5 +
e["avg_similarity"] * 3
)
def encode_chunk(chunk):
top_k = top_k_matches(chunk)
best = update_stats(top_k)
best_sim = top_k[0][1]
if best_sim > 0.85:
return {"type": "ref", "id": best["id"]}
else:
return {"type": "raw"}
def chunk_image(img):
w, h = img.size
cw, ch = CHUNK_SIZE
chunks = []
for y in range(0, h, ch):
for x in range(0, w, cw):
chunk = img.crop((x, y, x + cw, y + ch))
chunks.append((x, y, chunk))
return chunks
def save_dataset():
with open("chunks.json", "w") as f:
json.dump(CHUNK_DATASET, f)
def load_dataset():
global CHUNK_DATASET
CHUNK_DATASET = json.load(open("chunks.json"))
def encode_chunk_to_base64(img):
buffer = io.BytesIO()
img.save(buffer, format="PNG")
return base64.b64encode(buffer.getvalue()).decode()
# ----------------------------
# MATCHING
# ----------------------------
def find_best_match(chunk_np):
best = None
best_score = float("inf")
for entry in CHUNK_DATASET:
dataset_chunk = np.array(Image.open(entry["path"]))
score = mse(chunk_np, dataset_chunk)
if score < best_score:
best_score = score
best = entry
if best_score < SIMILARITY_THRESHOLD:
return best, best_score
return None, best_score
# ----------------------------
# DECISION ENGINE
# ----------------------------
def estimate_sizes(chunks, encoding):
raw_size = sum(len(encode_chunk_to_base64(c[2])) for c in chunks)
encoding_size = len(json.dumps(encoding))
return raw_size, encoding_size
def assign_tier(e):
r = compute_rank(e)
if r > 1000:
return 0
elif r > 200:
return 1
else:
return 2
def build_chunk_pack():
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
zip_path = temp_file.name
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as z:
index = []
for entry in CHUNK_DATASET:
chunk_path = entry["path"]
chunk_id = entry["id"]
arcname = f"chunks/{chunk_id}.png"
z.write(chunk_path, arcname)
index.append({
"id": chunk_id,
"path": arcname
})
# Write index.json
z.writestr("index.json", json.dumps(index, indent=2))
return zip_path
@app.get("/chunks")
def get_chunks(top: int = 50):
return sorted(
CHUNK_DATASET,
key=lambda x: compute_rank(x),
reverse=True
)[:top]
@app.get("/download-chunk-pack")
def download_chunk_pack():
zip_path = build_chunk_pack()
return FileResponse(
zip_path,
media_type="application/zip",
filename="noizunet_pack.zip"
)
# ----------------------------
# ROUTES
# ----------------------------
@app.get("/patch/{patch_id}")
def get_patch(patch_id: str):
patch = load_patch(patch_id)
return FileResponse(
patch["path"],
media_type="image/webp"
)
@app.post("/process-image")
async def process_image(file: UploadFile = File(...)):
img = Image.open(file.file).convert("RGB")
chunks = chunk_image(img)
encoding = []
matched_count = 0
dim = CHUNK_DATASET[0]["embedding"].shape[0]
index = faiss.IndexFlatL2(dim)
vectors = np.array([e["embedding"] for e in CHUNK_DATASET])
index.add(vectors)
for x, y, chunk in chunks:
chunk_np = np.array(chunk)
match, score = find_best_match(chunk_np)
top_k = faiss_search(chunk)
if match:
encoding.append({
"type": "ref",
"id": match["id"],
"x": x,
"y": y
})
matched_count += 1
else:
encoding.append({
"type": "raw",
"data": encode_chunk_to_base64(chunk),
"x": x,
"y": y
})
raw_size, encoding_size = estimate_sizes(chunks, encoding)
# 🔥 Decision logic
if encoding_size < raw_size:
mode = "noizu"
else:
mode = "image"
# ----------------------------
# RESPONSE
# ----------------------------
if mode == "noizu":
return {
"mode": "hybrid",
"encoding": [
{"type": "ref", "id": "chunk_001"},
{"type": "ref", "id": "chunk_042"},
{"type": "missing", "patch_id": "p12"}
],
"patches": [
{"id": "p12", "url": "/patch/p12"}
]
}
else:
buffer = io.BytesIO()
img.save(buffer, format="WEBP")
buffer.seek(0)
return StreamingResponse(buffer, media_type="image/webp")