API Reference

Installation

Install DinoHash via npm:

npm install @proteus-labs/dinohash

Package links:

JavaScript/TypeScript Usage

Use DinoHash in your JavaScript or TypeScript projects. The package uses ONNX Runtime for inference.

Basic Usage

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();

Using ES Modules

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']);

API Functions

downloadModel(modelUrl, modelPath)

Download the ONNX model from Hugging Face to a local path.

Parameters

  • modelUrl (string): URL to the ONNX model file (e.g., from Hugging Face)
  • modelPath (string): Local file path where the model should be saved

Returns

Promise<void> - Resolves when download completes

loadModel(modelPath, provider)

Load the ONNX model for inference.

Parameters

  • modelPath (string): Path to the downloaded ONNX model file
  • provider (string): Inference provider - 'cpu' or 'cuda' (for GPU)

Returns

Promise<InferenceSession> - ONNX Runtime inference session

hash(session, imagePaths)

Generate perceptual hashes for one or more images using the loaded model session.

Parameters

  • session (InferenceSession): The loaded ONNX model session
  • imagePaths (string | string[]): Single image path or array of image paths

Returns

Promise<string | string[]> - Hash string(s) for the input image(s)

Hugging Face Model

The DinoHash model is hosted on Hugging Face:

Model: backslashh/DINOHash

License: MIT

Model Type: Image Feature Extraction (ONNX)

Architecture: DINOv2 ViT-S/14

Model File

The ONNX model file can be downloaded directly:

https://huggingface.co/backslashh/DINOHash/resolve/main/dinov2_vits14_reg_96bit_dynamic.onnx

Complete Example

const { 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));
}

Resources

NPM Package

Install and use DinoHash in your JavaScript/TypeScript projects

View on npm →

GitHub Repository

Source code and documentation

View on GitHub →

Hugging Face Model

Use the model directly from Hugging Face Hub

View on Hugging Face →

Homepage

Learn more about Proteus

Visit proteus.photos →

Need Help?

Check out our documentation or contact us for support and custom integrations.