A Flask-based REST API that extracts visual data from images including text recognition (OCR), metadata extraction, and color analysis.
- Text Extraction (OCR): Extract text from images using Tesseract OCR
- Image Metadata: Extract EXIF data, dimensions, format, and file information
- Color Analysis: Identify dominant colors and calculate color statistics
- Multiple Format Support: PNG, JPG, JPEG, GIF, BMP, TIFF, WebP
- Base64 Encoding: Optional return of processed image as base64
Before running this application, you need to install Tesseract OCR on your system:
- Download Tesseract installer from: https://github.com/UB-Mannheim/tesseract/wiki
- Install Tesseract (note the installation path)
- Add Tesseract to your system PATH or set the path in code:
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
brew install tesseractsudo apt-get update
sudo apt-get install tesseract-ocr-
Clone or download this project
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- Windows:
venv\Scripts\activate - macOS/Linux:
source venv/bin/activate
- Windows:
-
Install dependencies:
pip install -r requirements.txt
python main.pyThe API will start on http://localhost:5000
- URL:
GET / - Description: Check if the API is running
- Response: JSON with API status and available endpoints
- URL:
POST /extract - Description: Upload an image and extract visual data
- Request: Multipart form data with
imagefield - Response: JSON with extracted data
# Upload an image for processing
curl -X POST -F "image=@path/to/your/image.jpg" http://localhost:5000/extractimport requests
url = "http://localhost:5000/extract"
files = {"image": open("path/to/your/image.jpg", "rb")}
response = requests.post(url, files=files)
data = response.json()
print(data)const formData = new FormData();
formData.append('image', fileInput.files[0]);
fetch('http://localhost:5000/extract', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => console.log(data));The API returns a JSON response with the following structure:
{
"success": true,
"timestamp": "2024-01-15T10:30:00.000Z",
"original_filename": "document.jpg",
"file_size": 1048576,
"extracted_text": {
"raw_text": "Extracted text from the image...",
"detailed_text": [
{
"text": "Hello",
"confidence": 95,
"bbox": {"x": 100, "y": 50, "width": 80, "height": 25}
}
],
"success": true
},
"metadata": {
"format": "JPEG",
"mode": "RGB",
"size": {"width": 1920, "height": 1080},
"has_transparency": false,
"exif": {...},
"file_size": 1048576
},
"color_analysis": {
"mean_color": {"r": 128, "g": 64, "b": 192},
"dominant_colors": [
{
"color": {"r": 255, "g": 255, "b": 255},
"hex": "#ffffff",
"percentage": 45.6
}
]
},
"image_base64": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."
}You can modify these settings in main.py:
MAX_FILE_SIZE: Maximum upload file size (default: 16MB)ALLOWED_EXTENSIONS: Supported file formatsUPLOAD_FOLDER: Temporary upload directory- OCR confidence threshold (default: 30)
The API handles various error scenarios:
- 400: Bad request (no file, invalid format, etc.)
- 413: File too large
- 500: Internal server error
.
├── main.py # Main Flask application
├── requirements.txt # Python dependencies
├── README.md # This file
└── uploads/ # Temporary upload directory (created automatically)
- Files are temporarily stored and automatically deleted after processing
- CORS is enabled for cross-origin requests
- File size limits are enforced
- Only specific image formats are allowed
- Tesseract not found: Make sure Tesseract is installed and in your PATH
- Memory errors: Reduce image size or increase system memory
- Permission errors: Ensure the application has write permissions for the uploads folder
This project is open source and available under the MIT License.