A blockchain-based platform that uses AI to detect greenery from images and tokenizes carbon credits on Polygon Mumbai testnet.
- Overview
- Architecture
- Prerequisites
- Installation
- Running the Project
- Testing
- API Documentation
- Project Structure
- Development
- Troubleshooting
GreenLink is a complete carbon credit tokenization platform with:
- AI-Powered Greenery Detection: Uses OpenCV and PyTorch with GPU acceleration
- FastAPI Backend: RESTful API with JWT authentication
- PostGIS Database: Spatial data storage for GPS coordinates
- Docker Containerization: Easy deployment and scaling
- Blockchain Integration: ERC-1155 tokens on Polygon Mumbai (Phase 3)
GreenLink/
βββ backend/ # FastAPI backend with authentication
βββ ai_service/ # AI greenery detection service
βββ db/ # PostgreSQL/PostGIS database
βββ frontend/ # Next.js frontend (Phase 4)
βββ blockchain/ # Smart contracts (Phase 3)
βββ docker-compose.yml
- Docker Desktop (with WSL2 integration on Windows)
- Python 3.11+ (for local testing)
- Git
- GPU: NVIDIA RTX 3050 or better (for AI acceleration)
- RAM: 8GB minimum, 16GB recommended
- Storage: 10GB free space
-
Install Docker Desktop
- Download from docker.com
- Enable WSL2 integration (Windows)
- Start Docker Desktop
-
Install Python Dependencies (for local testing)
pip install requests pillow
git clone <repository-url>
cd GreenLink_webdocker --version
docker info# Build all services
docker compose up --build -d
# Check if all containers are running
docker compose ps# Start all services in background
docker compose up -d
# View logs
docker compose logs -f# Stop all services
docker compose down
# Stop and remove volumes (resets database)
docker compose down -v# Restart specific service
docker compose restart backend
docker compose restart ai_service
# Restart all services
docker compose restartpython simple_api_test.pyExpected Output:
π Testing GreenLink API...
β
Backend is running
β
AI Service: GreenLink AI Service v2.0.0
GPU: True
π Your API is working!
python check_services.pyExpected Output:
π GreenLink Phase 2 - Service Check
==================================================
π Checking Backend Service...
β
Backend: GreenLink backend running.
π€ Checking AI Service...
β
AI Service: healthy
GPU: True
Device: cuda
π Checking AI Service Status...
β
AI Status: Version 2.0.0
GPU Name: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Memory: 4.0 GB
π Checking Backend AI Integration...
β
Backend AI: GreenLink AI Service v2.0.0
GPU Available: True
python test_full_workflow.pyExpected Output:
π Testing Full Upload & Analysis Workflow
============================================================
2οΈβ£ Logging in...
β
Login successful
3οΈβ£ Creating test image...
βΉοΈ PIL not available, using existing test image...
4οΈβ£ Uploading image...
β
Upload successful: Submission ID 3
5οΈβ£ Analyzing with AI...
β
AI Analysis complete!
Greenery: 42.0%
Carbon Value: 0.21 tonnes CO2
GPS: [40.7128, -74.0060] (from upload)
6οΈβ£ Listing submissions...
π Full workflow test completed successfully!
β
API is working correctly!
python test_ai_simple.pypython test_phase3.pyExpected Output:
π Testing Phase 3: Blockchain Integration
============================================================
1οΈβ£ Checking blockchain status...
2οΈβ£ Creating test user...
β
User created: Blockchain Test User (ID: 3)
3οΈβ£ Logging in...
β
Login successful
4οΈβ£ Uploading test image...
β
Upload successful: Submission ID 5
5οΈβ£ Analyzing with AI...
β
AI Analysis complete!
Greenery: 42.0%
Carbon Value: 0.21 tonnes CO2
6οΈβ£ Registering submission on blockchain...
7οΈβ£ Minting carbon credit token...
8οΈβ£ Getting user tokens...
9οΈβ£ Getting marketplace listings...
π Getting user credits from database...
π Phase 3 Blockchain Integration Test Completed!
curl http://localhost:8000/curl http://localhost:8001/curl http://localhost:8000/ai-status- Backend API Docs: http://localhost:8000/docs
- AI Service Docs: http://localhost:8001/docs
# Register user
POST /users
{
"name": "Farmer John",
"email": "farmer@example.com",
"password": "mypass123"
}
# Login
POST /token
{
"username": "farmer@example.com",
"password": "mypass123"
}# Upload image
POST /upload
Authorization: Bearer <token>
Content-Type: multipart/form-data
- file: image.jpg
- latitude: 40.7128
- longitude: -74.0060
# Analyze with AI
POST /analyze/{submission_id}
Authorization: Bearer <token>
# List submissions
GET /submissions
Authorization: Bearer <token># Health check
GET /
# AI service status
GET /ai-status
# User info
GET /users/me
Authorization: Bearer <token># Blockchain status
GET /blockchain/status
# Mint carbon credit token
POST /blockchain/mint/{submission_id}
Authorization: Bearer <token>
# Get user tokens
GET /blockchain/tokens
Authorization: Bearer <token>
# Get marketplace listings
GET /blockchain/marketplace
# Register submission on blockchain
POST /blockchain/register-submission/{submission_id}
Authorization: Bearer <token>GreenLink_web/
βββ backend/
β βββ app/
β β βββ main.py # FastAPI application
β β βββ models.py # SQLAlchemy models
β β βββ schemas.py # Pydantic schemas
β β βββ crud.py # Database operations
β β βββ auth.py # JWT authentication
β β βββ database.py # Database connection
β β βββ blockchain.py # Blockchain integration
β βββ requirements.txt # Python dependencies
β βββ Dockerfile # Backend container
βββ ai_service/
β βββ main.py # AI service FastAPI
β βββ greenery_detector.py # Core AI logic
β βββ requirements.txt # ML dependencies
β βββ Dockerfile # AI service container
βββ blockchain/ # Smart contracts (Phase 3)
β βββ contracts/
β β βββ CarbonCreditToken.sol
β β βββ CarbonCreditMarket.sol
β β βββ GreenLinkRegistry.sol
β βββ scripts/
β β βββ deploy.js # Deployment script
β βββ test/
β β βββ contracts.test.js
β βββ hardhat.config.js # Hardhat configuration
βββ db/
β βββ init.sql # Database schema
βββ docker-compose.yml # Service orchestration
βββ test_*.py # Test scripts
βββ README.md # This file
# Edit backend code
code backend/app/main.py
# Restart backend to apply changes
docker compose restart backend
# View backend logs
docker compose logs backend -f# Edit AI service code
code ai_service/greenery_detector.py
# Restart AI service to apply changes
docker compose restart ai_service
# View AI service logs
docker compose logs ai_service -f# Connect to database
docker compose exec db psql -U admin -d greenlink
# View tables
\dt
# View data
SELECT * FROM users;
SELECT * FROM submissions;# Backend environment
DATABASE_URL=postgresql://admin:admin@db:5432/greenlink
AI_SERVICE_URL=http://ai_service:8001
# AI Service environment
NVIDIA_VISIBLE_DEVICES=all
NVIDIA_DRIVER_CAPABILITIES=compute,utility# Start Docker Desktop
docker desktop start
# Check Docker status
docker info# Check what's using the port
netstat -ano | findstr :8000
netstat -ano | findstr :8001
# Kill process or change ports in docker-compose.yml# Check GPU in container
docker compose exec ai_service nvidia-smi
# Check AI service logs
docker compose logs ai_service# Restart database
docker compose restart db
# Check database logs
docker compose logs db
# Reset database (WARNING: loses data)
docker compose down -v
docker compose up -d# Check AI service health
curl http://localhost:8001/
# View AI service logs
docker compose logs ai_service -f
# Restart AI service
docker compose restart ai_service# All services
docker compose logs -f
# Specific service
docker compose logs backend -f
docker compose logs ai_service -f
docker compose logs db -f# Enter backend container
docker compose exec backend bash
# Enter AI service container
docker compose exec ai_service bash
# Enter database container
docker compose exec db psql -U admin -d greenlink# Container resource usage
docker stats
# GPU usage
docker compose exec ai_service nvidia-smi# Connect to database
docker compose exec db psql -U admin -d greenlink
# Check table sizes
SELECT schemaname, tablename, attname, n_distinct, correlation
FROM pg_stats
WHERE tablename IN ('users', 'submissions', 'credits');- Environment Variables: Set production database URLs
- SSL/TLS: Configure HTTPS certificates
- Load Balancer: Set up reverse proxy (nginx)
- Monitoring: Add logging and metrics
- Backup: Configure database backups
# Scale AI service (requires load balancer)
docker compose up -d --scale ai_service=3
# Scale backend (requires load balancer)
docker compose up -d --scale backend=2- β Phase 1: Backend + Database + Authentication
- β Phase 2: AI Greenery Detection + GPU Acceleration
- π Phase 3: Blockchain Integration (Polygon Mumbai)
- π Phase 4: Frontend + Marketplace
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License.
For issues and questions:
- Check the troubleshooting section
- View the logs:
docker compose logs - Create an issue in the repository
π± GreenLink - Making Carbon Credits Accessible Through AI and Blockchain