Skip to content

GalaxyBotTeam/Image-Classifier

Repository files navigation

GalaxyBot

GalaxyBot Image Classification

This project is a TypeScript application that uses the OpenAI Moderation API for image classification. It's designed to detect and handle NSFW (Not Safe For Work) images uploaded to an S3 bucket.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

  • Node.js or Bun
  • npm (or Bun)
  • TypeScript

Installing

  1. Clone the repository
git clone https://github.com/GalaxyBotTeam/Image-Classifier.git
  1. Navigate to the project directory
cd your-project-directory
  1. Install the dependencies
npm install
# OR using Bun
bun install

Usage

This project consists of two main components:

  1. ImageClassificationHandler.ts: This handles the image classification process. It fetches an image from an S3 bucket, converts it into a TensorFlow tensor, classifies the image using the NSFW.js model, and validates the classification results.

Configuration

The application needs a configuration file to run. Create a config.json file in the root directory of the project with the following content: The content for the config.json is defined in the config-example.json file.

We use MinIO as an S3-compatible object storage server. You can use any S3-compatible object storage server by changing the s3 configuration in the config.json file.

Building and Running the application

Using npm

Build the TypeScript files:

npm run build

Start the application:

npm start

Development mode (with auto-reloading):

npm run dev

Using Bun

Build the TypeScript files:

bun run build

Start the application:

bun start

Development mode (with auto-reloading):

bun run dev

Classifying an image

The application provides a Webserver that listens for POST requests on the /api/v1/classifyImage endpoint. To classify an image, send a POST request to the /api/v1/classifyImage endpoint with the following payload:

Request

{
  "key": "your-s3-key", //The key of the image in the S3 bucket. The bucket is defined in the config file
  "deleteOnClassification": false //Boolean, should the image automaticly deleted if nsfw has been detected?
  "metadata": {
    "userID": "userID", //Guild ID for Discord Log
    "guildID": "guildID" //User ID for Discord Log
  }
}

Response:

Returns a JSON object with the classification results. The flagged field indicates the classification result. The categories and the scores field contains the classification probabilities for each category. In this example, the image is classified as sexual with a probability of 0.9849382780471674. DeleteImage is true if the image has been deleted from the S3 bucket.

{
 "flagged": true,
 "categories": {
  "harassment": false,
  "harassment/threatening": false,
  "sexual": true,
  "hate": false,
  "hate/threatening": false,
  "illicit": false,
  "illicit/violent": false,
  "self-harm/intent": false,
  "self-harm/instructions": false,
  "self-harm": false,
  "sexual/minors": false,
  "violence": false,
  "violence/graphic": false
 },
 "scores": {
  "harassment": 0,
  "harassment/threatening": 0,
  "sexual": 0.9849382780471674,
  "hate": 0,
  "hate/threatening": 0,
  "illicit": 0,
  "illicit/violent": 0,
  "self-harm/intent": 0.0002785803623326577,
  "self-harm/instructions": 0.0002318797868593433,
  "self-harm": 0.004690984132226771,
  "sexual/minors": 0,
  "violence": 0.12459626487974881,
  "violence/graphic": 0.002384378805524485
 },
 "deletedImage": true
}

License

This project is licensed under the MIT License - see the LICENSE.md file for details.