tiftik-passport/demo/grading.html
caelum0x 2403703b9d feat(tiftik-passport): phone-camera fiber-grading demo + NEAR provenance-anchor contract
Ankara Kecisi Teknoloji ve Inovasyon Fikir Maratonu 2026 entry prototype.
Client-side heuristic fiber grading (canvas pixel analysis, no ML claim)
+ a near-sdk 5 contract adapted from Ration's live AuditAnchor pattern,
compiles clean to wasm32-unknown-unknown (not yet deployed to testnet).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-17 15:33:40 +03:00

242 lines
9.3 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tiftik Pasaportu -- phone-camera fiber grading (prototype)</title>
<style>
:root { color-scheme: dark; }
body {
margin: 0;
background: #0b0b0c;
color: #f2f2f2;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
display: flex;
flex-direction: column;
align-items: center;
padding: 32px 16px 64px;
}
h1 { font-size: 20px; font-weight: 600; margin: 0 0 4px; }
.sub { color: #9a9a9a; font-size: 13px; margin: 0 0 24px; text-align: center; max-width: 480px; }
.card {
background: #161617;
border: 1px solid #2a2a2b;
border-radius: 16px;
padding: 20px;
width: 100%;
max-width: 420px;
box-sizing: border-box;
}
input[type="file"] { display: none; }
label.picker {
display: block;
text-align: center;
padding: 14px;
border: 1px dashed #3a3a3b;
border-radius: 12px;
cursor: pointer;
color: #d0d0d0;
font-size: 14px;
}
label.picker:active { background: #1f1f20; }
canvas { width: 100%; border-radius: 12px; margin-top: 16px; display: none; }
.ruler-row { display: flex; align-items: center; gap: 8px; margin-top: 16px; font-size: 13px; color: #c7c7c7; }
.ruler-row input {
flex: 1;
background: #0f0f10;
border: 1px solid #2a2a2b;
border-radius: 8px;
color: #f2f2f2;
padding: 8px 10px;
font-size: 14px;
}
.result { margin-top: 20px; display: none; }
.row { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #222223; font-size: 14px; }
.row:last-child { border-bottom: none; }
.row .k { color: #9a9a9a; }
.row .v { font-weight: 600; }
.grade-pill {
display: inline-block;
padding: 2px 10px;
border-radius: 999px;
font-size: 12px;
font-weight: 600;
}
.grade-A { background: #113b22; color: #5fe08a; }
.grade-B { background: #3b3011; color: #e0c15f; }
.grade-C { background: #3b1414; color: #e07c5f; }
.hash { font-family: ui-monospace, Menlo, monospace; font-size: 11px; color: #6a6a6b; word-break: break-all; margin-top: 12px; }
.disclaimer {
margin-top: 20px;
font-size: 11px;
color: #707071;
line-height: 1.5;
max-width: 420px;
text-align: center;
}
</style>
</head>
<body>
<h1>Tiftik Pasaportu</h1>
<p class="sub">Telefon kamerasiyla tiftik fiber ön-derecelendirme (prototip). Bir fiber/tutam fotoğrafı seçin; yanına 1 adet referans ölçü (örn. 1 TL madeni para, çap 20.5mm) koyun.</p>
<div class="card">
<label class="picker" for="file-input">Fotoğraf seç / kamera aç</label>
<input id="file-input" type="file" accept="image/*" capture="environment" />
<canvas id="preview"></canvas>
<div class="ruler-row">
<span>Referans nesne çapı (mm):</span>
<input id="ref-mm" type="number" value="20.5" step="0.1" min="1" />
</div>
<div class="result" id="result">
<div class="row"><span class="k">İncelik proxy (fineness proxy)</span><span class="v" id="fineness">--</span></div>
<div class="row"><span class="k">Renk sınıfı (color class)</span><span class="v" id="color">--</span></div>
<div class="row"><span class="k">Tahmini tutam uzunluğu (staple, mm)</span><span class="v" id="staple">--</span></div>
<div class="row"><span class="k">Önerilen derece (grade)</span><span class="v" id="grade">--</span></div>
<div class="hash" id="hash"></div>
</div>
</div>
<p class="disclaimer">
Bu prototip görüntü üzerinde parlaklık/renk histogramı ve kenar yoğunluğu (edge density)
ile <strong>sezgisel bir proxy</strong> hesaplar; eğitilmiş bir ML modeli veya laboratuvar
ölçümü (OFDA/lazer kırınım) <strong>değildir</strong>. Mentorluk kampı aşamasında gerçek
etiketli fiber fotoğraflarıyla kalibre edilecek ve/veya bir eğitimli modelle değiştirilecek
bir ilk adımdır -- amacı "bu fikir çalışır mı" sorusuna somut, çalışan bir kanıt vermek.
Çıkan `grade_hash`, blockchain üzerinde (bkz. ../contract) bu parti için anchor edilecek
kayıttır; fiber burada <em>yeniden derecelendirilmez</em>, sadece bu çıktının parmak izi
tamper-evident olarak saklanır.
</p>
<script>
const fileInput = document.getElementById("file-input");
const canvas = document.getElementById("preview");
const ctx = canvas.getContext("2d", { willReadFrequently: true });
const result = document.getElementById("result");
const refMmInput = document.getElementById("ref-mm");
fileInput.addEventListener("change", async (ev) => {
const file = ev.target.files && ev.target.files[0];
if (!file) return;
const img = await loadImage(file);
drawAndAnalyze(img);
});
function loadImage(file) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = URL.createObjectURL(file);
});
}
function drawAndAnalyze(img) {
const maxW = 640;
const scale = Math.min(1, maxW / img.width);
canvas.width = Math.round(img.width * scale);
canvas.height = Math.round(img.height * scale);
canvas.style.display = "block";
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
const frame = ctx.getImageData(0, 0, canvas.width, canvas.height);
const analysis = analyzeFiber(frame);
renderResult(analysis);
}
// Heuristic, explicitly-not-ML fiber proxy:
// - fineness proxy: higher local edge-density (Sobel-ish gradient magnitude
// averaged over the center crop) correlates with finer, more numerous
// individual fibers catching more micro-highlights per unit area; this is
// a proxy, not a micron measurement, and is labeled as such everywhere.
// - color class: mean LAB-ish lightness + warmth of the center crop, bucketed
// into the 3 classes mohair graders already use informally (white /
// cream / off-color).
// - staple length: NOT estimated from pixels alone here (no reliable depth
// cue in a single 2D photo without the reference object's real-world
// scale calibrated against a ruler overlay) -- reported as "needs ruler
// overlay" rather than a fabricated number, which is the honest choice
// for a pre-screening idea stage prototype.
function analyzeFiber(frame) {
const { data, width, height } = frame;
const cx0 = Math.floor(width * 0.3), cx1 = Math.floor(width * 0.7);
const cy0 = Math.floor(height * 0.3), cy1 = Math.floor(height * 0.7);
let sumL = 0, sumA = 0, sumB = 0, n = 0;
let gradSum = 0, gradN = 0;
function luma(x, y) {
const i = (y * width + x) * 4;
return 0.2126 * data[i] + 0.7152 * data[i + 1] + 0.0722 * data[i + 2];
}
for (let y = cy0; y < cy1; y++) {
for (let x = cx0; x < cx1; x++) {
const i = (y * width + x) * 4;
const r = data[i], g = data[i + 1], b = data[i + 2];
sumL += (r + g + b) / 3;
sumA += r - g;
sumB += g - b;
n++;
if (x > cx0 && y > cy0) {
const gx = luma(x, y) - luma(x - 1, y);
const gy = luma(x, y) - luma(x, y - 1);
gradSum += Math.sqrt(gx * gx + gy * gy);
gradN++;
}
}
}
const meanL = sumL / n;
const warmth = sumA / n;
const edgeDensity = gradSum / gradN;
// fineness proxy: 0-100, higher = finer-looking texture (more edges per area)
const finenessProxy = Math.max(0, Math.min(100, Math.round((edgeDensity / 18) * 100)));
let colorClass = "Beyaz (White)";
if (warmth > 10 && meanL < 190) colorClass = "Krem (Cream)";
if (warmth > 18 || meanL < 140) colorClass = "Renkli / Off-color";
let grade = "C";
if (finenessProxy >= 60 && colorClass === "Beyaz (White)") grade = "A";
else if (finenessProxy >= 40) grade = "B";
return { meanL, warmth, edgeDensity, finenessProxy, colorClass, grade };
}
async function sha256Hex(str) {
const enc = new TextEncoder().encode(str);
const digest = await crypto.subtle.digest("SHA-256", enc);
return Array.from(new Uint8Array(digest))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
async function renderResult(a) {
result.style.display = "block";
document.getElementById("fineness").textContent = a.finenessProxy + " / 100 (proxy, not micron)";
document.getElementById("color").textContent = a.colorClass;
document.getElementById("staple").textContent = "needs ruler overlay (not estimated from this photo alone)";
const gradeEl = document.getElementById("grade");
gradeEl.innerHTML = `<span class="grade-pill grade-${a.grade}">Grade ${a.grade}</span>`;
const payload = JSON.stringify({
fineness_proxy: a.finenessProxy,
color_class: a.colorClass,
grade: a.grade,
ref_mm: Number(refMmInput.value) || null,
graded_at_unix: Math.floor(Date.now() / 1000),
});
const hash = await sha256Hex(payload);
document.getElementById("hash").textContent =
"grade_hash (sha256 of this result, to be anchored via ../contract/commit_batch): " + hash;
}
</script>
</body>
</html>