This project uses a pre-trained BLIP model from Salesforce, which generates captions for images. The model leverages BLIP for both conditional and unconditional image captioning. This project is designed to be integrated with a PHP backend, making it easy to call via shell_exec()
.
- Project Overview
- Requirements
- Apache & PHP Setup for Windows Users
- Python Setup and Dependencies
- Directory Structure
This project uses the BLIP (Bootstrapping Language-Image Pretraining) model from Salesforce to generate captions for images. It can provide both conditional captions (given a prompt) and unconditional captions (without any prompt).
The code integrates Python with a PHP backend using Apache to call Python scripts and display generated captions on a webpage.
-
Ensure that Python is installed and the dependencies are set up as mentioned in the Python Setup and Dependencies section.
-
Make sure Apache is running via XAMPP.
If you are running this project on Windows using Apache and PHP (for local development with XAMPP), follow these steps to set up:
-
Install XAMPP (if not installed yet):
- Download and install XAMPP from here.
- After installation, launch the XAMPP control panel.
-
Place the Project Files in the Correct Directory:
- Move or clone this project folder to the
htdocs
directory of your XAMPP installation. - Example: If XAMPP is installed at
C:\xampp
, place the project inC:\xampp\htdocs\image-caption-generator
.
- Move or clone this project folder to the
-
Ensure PHP Can Access the Python Script:
- Use
shell_exec()
in PHP to execute the Python script. - Make sure PHP has permissions to execute the Python script from the command line.
Example PHP code to call the Python script:
- Use
<?php
// Example path to the image
$image_path = 'C:/xampp/htdocs/image-caption-generator/images/myimage.jpg';
// Command to run the Python script
$command = "python C:/xampp/htdocs/image-caption-generator/generate_caption.py $image_path";
// Execute the command and capture the output
$output = shell_exec($command);
// Display the output (captions)
echo $output;
?>
Before running the Python script, you'll need to install the following dependencies:
torch
: For running the deep learning model.transformers
: For using the BLIP model and processor.Pillow
: For image processing and opening image files.requests
: For handling HTTP requests (if needed).
You can install all the necessary dependencies by running the following command in your Python environment:
pip install -r requirements.txt
The recommended directory structure for your project should look like this:
/xampp/htdocs/image-caption-generator/
├── generate_caption.py # Python script for captioning
├── images/ # Directory to store images
├── uploads/ # Directory to store uploaded images
├── requirements.txt # Python dependencies
├── README.md # This file
└── your_php_script.php # PHP script to call Python script