Install DinoHash via npm:
npm install @proteus-labs/dinohashPackage links:
Use DinoHash in your JavaScript or TypeScript projects. The package uses ONNX Runtime for inference.
const { downloadModel, loadModel, hash } = require('@proteus-labs/dinohash');
const path = require('path');
async function main() {
// Download the model from Hugging Face
const modelUrl = 'https://huggingface.co/backslashh/DINOHash/resolve/main/dinov2_vits14_reg_96bit_dynamic.onnx';
const modelPath = path.join(__dirname, './models/dinov2_vits14_reg_96bit.onnx');
// Download and load the model
await downloadModel(modelUrl, modelPath);
const session = await loadModel(modelPath, 'cpu'); // Use 'cuda' for GPU inference if available
// Hash images
const imagePaths = [
path.join(__dirname, 'image1.jpg'),
path.join(__dirname, 'image2.jpg')
];
const results = await hash(session, imagePaths);
console.log(results);
}
main();import { downloadModel, loadModel, hash } from '@proteus-labs/dinohash';
import path from 'path';
const modelUrl = 'https://huggingface.co/backslashh/DINOHash/resolve/main/dinov2_vits14_reg_96bit_dynamic.onnx';
const modelPath = path.join(process.cwd(), './models/dinov2_vits14_reg_96bit.onnx');
await downloadModel(modelUrl, modelPath);
const session = await loadModel(modelPath, 'cpu');
const hashes = await hash(session, ['image1.jpg', 'image2.jpg']);Download the ONNX model from Hugging Face to a local path.
modelUrl (string): URL to the ONNX model file (e.g., from Hugging Face)modelPath (string): Local file path where the model should be savedPromise<void> - Resolves when download completesLoad the ONNX model for inference.
modelPath (string): Path to the downloaded ONNX model fileprovider (string): Inference provider - 'cpu' or 'cuda' (for GPU)Promise<InferenceSession> - ONNX Runtime inference sessionGenerate perceptual hashes for one or more images using the loaded model session.
session (InferenceSession): The loaded ONNX model sessionimagePaths (string | string[]): Single image path or array of image pathsPromise<string | string[]> - Hash string(s) for the input image(s)The DinoHash model is hosted on Hugging Face:
Model: backslashh/DINOHash
License: MIT
Model Type: Image Feature Extraction (ONNX)
Architecture: DINOv2 ViT-S/14
The ONNX model file can be downloaded directly:
https://huggingface.co/backslashh/DINOHash/resolve/main/dinov2_vits14_reg_96bit_dynamic.onnxconst { downloadModel, loadModel, hash } = require('@proteus-labs/dinohash');
const path = require('path');
const fs = require('fs');
async function hashImages(imagePaths) {
// Ensure models directory exists
const modelsDir = path.join(__dirname, 'models');
if (!fs.existsSync(modelsDir)) {
fs.mkdirSync(modelsDir, { recursive: true });
}
// Model configuration
const modelUrl = 'https://huggingface.co/backslashh/DINOHash/resolve/main/dinov2_vits14_reg_96bit_dynamic.onnx';
const modelPath = path.join(modelsDir, 'dinov2_vits14_reg_96bit.onnx');
// Download model if not already present
if (!fs.existsSync(modelPath)) {
console.log('Downloading model...');
await downloadModel(modelUrl, modelPath);
}
// Load model (use 'cuda' for GPU if available)
const session = await loadModel(modelPath, 'cpu');
// Generate hashes
const hashes = await hash(session, imagePaths);
return hashes;
}
// Usage
hashImages(['image1.jpg', 'image2.jpg'])
.then(hashes => {
console.log('Hashes:', hashes);
// Compare hashes (hamming distance)
if (hashes.length === 2) {
const similarity = compareHashes(hashes[0], hashes[1]);
console.log('Similarity:', similarity);
}
})
.catch(console.error);
function compareHashes(hash1, hash2) {
// Simple hamming distance comparison
let distance = 0;
for (let i = 0; i < Math.min(hash1.length, hash2.length); i++) {
if (hash1[i] !== hash2[i]) distance++;
}
return 1 - (distance / Math.max(hash1.length, hash2.length));
}Check out our documentation or contact us for support and custom integrations.