CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 15:46:54 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=c7a15733c4d4704bfbcc35ca0b952d4951e45c1ab532d65744f8f8d028317457a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%227wsxDG95guYUmNVazCfbZd9iVeDQlsBF%22%3B%7D; HttpOnly; Path=/
cf-ray: 98cf7e2d6fad3e92-BLR
Simba 2.0 KNN - Recognize number - Pastebin.com
SHARE
TWEET

Simba 2.0 KNN - Recognize number
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TARGET THE DEBUG IMAGE BEFORE RUNNING
- // PRESS ENTER TO TEST
- // PRESS BACK TO RESET
- function BytesToInt32(Bytes: TByteArray; StartIndex: Integer): Integer;
- begin
- Result := (Bytes[StartIndex] shl 24) or (Bytes[StartIndex + 1] shl 16) or
- (Bytes[StartIndex + 2] shl 8) or Bytes[StartIndex + 3];
- end;
- function ParseMNISTFile(FileName: string): array of TSingleMatrix;
- var
- FileData: TByteArray;
- MagicNumber, NumImages, NumRows, NumCols: Integer;
- i, r, c, Offset: Integer;
- begin
- if not FileExists(FileName) then
- begin
- WriteLn('File not found: ', FileName);
- Exit;
- end;
- // Read the entire file into memory
- FileData := FileReadBytes(FileName);
- if Length(FileData) = 0 then
- begin
- WriteLn('Failed to read file or file is empty.');
- Exit;
- end;
- // Parse the header (16 bytes)
- MagicNumber := BytesToInt32(FileData, 0);
- NumImages := BytesToInt32(FileData, 4);
- NumRows := BytesToInt32(FileData, 8);
- NumCols := BytesToInt32(FileData, 12);
- if MagicNumber <> 2051 then
- begin
- WriteLn('Invalid magic number: ', MagicNumber);
- Exit;
- end;
- // Allocate memory for images
- SetLength(Result, NumImages);
- for i := 0 to NumImages - 1 do
- SetLength(Result[i], NumRows, NumCols);
- // Parse image data
- Offset := 16; // Image data starts after the header
- for i := 0 to NumImages - 1 do
- begin
- for r := 0 to NumRows - 1 do
- begin
- for c := 0 to NumCols - 1 do
- begin
- Result[i][r][c] := FileData[Offset];
- Inc(Offset);
- end;
- end;
- end;
- end;
- function ParseMNISTLabels(FileName: string): array of Byte;
- var
- FileData: TByteArray;
- MagicNumber, NumLabels, Offset: Integer;
- i: Integer;
- begin
- if not FileExists(FileName) then
- begin
- WriteLn('File not found: ', FileName);
- Exit;
- end;
- // Read the entire file into memory
- FileData := FileReadBytes(FileName);
- if Length(FileData) = 0 then
- begin
- WriteLn('Failed to read file or file is empty.');
- Exit;
- end;
- // Parse the header (8 bytes)
- MagicNumber := BytesToInt32(FileData, 0);
- NumLabels := BytesToInt32(FileData, 4);
- if MagicNumber <> 2049 then
- begin
- WriteLn('Invalid magic number: ', MagicNumber);
- Exit;
- end;
- // Allocate memory for labels
- SetLength(Result, NumLabels);
- // Parse labels (starting at offset 8)
- Offset := 8;
- for i := 0 to NumLabels - 1 do
- begin
- Result[i] := FileData[Offset];
- Inc(Offset);
- end;
- end;
- function VectorDataBlock(Img: TSingleMatrix): TSingleArray;
- var
- TPA: TPointArray;
- Bounds: TBoxArray;
- i: Int32;
- begin
- SetLength(Result, 16);
- TPA := Img.Indices(128, __GE__);
- Bounds := TPA.Bounds.Partition(4,4);
- for i:=0 to High(Bounds) do
- Result[i] := TPA.ExtractBox(Bounds[i]).Length / ((24*24) / (4*4));
- end;
- function ComputeVectors(Images: array of TSingleMatrix; Labels: TByteArray): TKDItems;
- var
- i: Int32;
- begin
- SetLength(Result, Length(Images));
- for i:=0 to High(Images) do
- begin
- Result[i].Ref := Labels[i];
- Result[i].Vector := VectorDataBlock(Images[i]);//VectorData(Images[i]);
- end;
- end;
- var
- Tree: TKDTree;
- x,y,i,j: Int32;
- Images,TrainImages: array of TSingleMatrix;
- Labels,TrainLabels: array of Byte;
- TrainingData, TestData: TKDItems;
- best,t: Double;
- accuracy,bidx: Int32;
- Raw: TSingleMatrix;
- Test: TSingleArray;
- Half, Temp, Image: TImage;
- begin
- TrainImages := ParseMNISTFile('Scripts\MNIST\train-images.idx3-ubyte');
- TrainLabels := ParseMNISTLabels('Scripts\MNIST\train-labels.idx1-ubyte');
- t := PerformanceTimer();
- TrainingData := ComputeVectors(TrainImages, TrainLabels);
- WriteLn PerformanceTimer() - t;
- WriteLn('Training on ', Length(TrainingData), ' samples');
- t := PerformanceTimer();
- Tree.Init(TrainingData);
- WriteLn PerformanceTimer() - t;
- Image := TImage.Create(700,256);
- DebugImageDisplay(700,256);
- Image.Show();
- Image.DrawColor := $FFFFFF;
- while True do
- begin
- if Target.MousePressed(EMouseButton.LEFT) then
- Image.DrawCircleFilled(Target.MouseXY,10);
- DebugImageUpdate(Image);
- if Target.KeyPressed(EKeyCode.RETURN) then
- begin
- Half := Image.Copy([0,0,255,255]);
- Temp := Half.Downsample(11);
- Temp.SetSize(24,24);
- Temp.ToMatrix();
- Raw.SetSize(24,24);
- for y:=0 to Temp.Height-1 do
- for x:=0 to Temp.Width-1 do
- Raw[y,x] := Temp.Pixel[x,y].ToHSL().L * 2.55;
- Temp.Show();
- Temp.Free();
- Half.Free();
- Test := VectorDataBlock(Raw);
- t := PerformanceTimer();
- bidx := Tree.KNearestClassify(Test, 7);
- Writeln(PerformanceTimer() - t, 'ms');
- Image.FontSize := 12;
- Image.DrawText('Predicted: ', [300, 30]);
- Image.FontSize := 90;
- Image.DrawText(ToStr(bidx), [300, 50]);
- Sleep(1000);
- DebugImageUpdate(Image);
- end;
- if Target.KeyPressed(EKeyCode.BACK) then
- Image.Clear();
- Sleep(10);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
-
⭐⭐⭐Profit Method⭐⭐
Java | 6 sec ago | 0.10 KB
-
⭐✅ Marketplace Glitch ✅ Working ✅ NEVER SEEN...
JavaScript | 6 sec ago | 0.24 KB
-
Free Crypto Method (NEVER SEEN BEFORE)⭐⭐ M
JavaScript | 11 sec ago | 0.24 KB
-
⭐⭐⭐Exploit 500$ in 15 Minutes⭐⭐
Java | 18 sec ago | 0.10 KB
-
✅⭐ Make $2500 in 15 minutes ✅ NEVER SEEN BEFO...
JavaScript | 19 sec ago | 0.24 KB
-
⭐✅ Swapzone Glitch ✅ Working⭐⭐⭐ 4
JavaScript | 22 sec ago | 0.24 KB
-
⭐⭐⭐Instant Profit Method⭐⭐
Java | 31 sec ago | 0.10 KB
-
✅ Make $2500 in 20 minutes⭐⭐⭐ Q
JavaScript | 38 sec ago | 0.24 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand