# OCR Setup Guide for Health Metrics Processing

This guide explains how to set up a real OCR API key for the health metrics processing feature in FitnessApp.

## Current Implementation

The current implementation uses a fallback mode that returns predefined values for health metrics. This ensures that the app continues to function while you set up a real OCR service.

## Setting Up OCR.space API (Recommended)

OCR.space offers a free tier that is suitable for most use cases. Here's how to set it up:

1. **Sign up for an API key**:
   - Visit [OCR.space](https://ocr.space/ocrapi)
   - Click on "Free OCR API" and register for an account
   - After registration, you'll receive an API key via email

2. **Update the configuration**:
   - Open `ocr_config.php`
   - Replace the placeholder API key with your actual key:
     ```php
     $OCR_SPACE_API_KEY = 'your-actual-api-key';
     ```
   - Change the OCR service to use OCR.space:
     ```php
     $OCR_SERVICE = 'ocr_space';
     ```

3. **Test the implementation**:
   - Run `php test_ocr_service.php` to verify that the OCR service is working correctly
   - If successful, you should see the extracted metrics from the test image

## Alternative: Setting Up Google Cloud Vision API

For higher accuracy, you can use Google Cloud Vision API. This requires a Google Cloud account and may incur costs.

1. **Create a Google Cloud account**:
   - Visit [Google Cloud](https://cloud.google.com/)
   - Sign up for an account and create a project

2. **Enable the Vision API**:
   - In the Google Cloud Console, navigate to "APIs & Services"
   - Click "Enable APIs and Services"
   - Search for "Vision API" and enable it

3. **Create service account credentials**:
   - In the Google Cloud Console, navigate to "IAM & Admin" > "Service Accounts"
   - Create a new service account
   - Grant it the "Cloud Vision API User" role
   - Create a key for the service account (JSON format)
   - Download the key file

4. **Update the configuration**:
   - Place the key file in your project directory
   - Open `ocr_config.php`
   - Update the credentials file path:
     ```php
     $GOOGLE_CLOUD_CREDENTIALS_FILE = __DIR__ . '/your-credentials-file.json';
     ```
   - Change the OCR service to use Google Vision:
     ```php
     $OCR_SERVICE = 'google_vision';
     ```

5. **Install the Google Cloud Vision PHP client library**:
   - Run `composer require google/cloud-vision`
   - This requires Composer to be installed on your server

## Alternative: Setting Up Tesseract OCR

If you have shell access to your server, you can use Tesseract OCR, which is a free and open-source OCR engine.

1. **Install Tesseract OCR**:
   - On Ubuntu/Debian: `sudo apt-get install tesseract-ocr`
   - On CentOS/RHEL: `sudo yum install tesseract`
   - On macOS: `brew install tesseract`

2. **Update the configuration**:
   - Open `ocr_config.php`
   - Change the OCR service to use Tesseract:
     ```php
     $OCR_SERVICE = 'tesseract';
     ```

3. **Test the implementation**:
   - Run `php test_ocr_service.php` to verify that Tesseract OCR is working correctly

## Troubleshooting

If you encounter issues with the OCR service, check the following:

1. **API Key**: Make sure your API key is correct and active
2. **Server Requirements**: Ensure your server meets the requirements for the OCR service you're using
3. **Log File**: Check the `ocr_debug.log` file for error messages
4. **Fallback Mode**: If all else fails, you can use the fallback mode by setting `$OCR_SERVICE = 'fallback';`

## Customizing Regex Patterns

If the OCR service is not correctly extracting certain metrics, you may need to customize the regex patterns in `ocr_config.php`:

```php
$HEALTH_METRICS_PATTERNS = [
    'weight' => [
        'pattern' => '/(?:weight|wt)[:\s]+(\d+\.?\d*)\s*(?:lbs?|pounds?)/i',
        'group' => 1
    ],
    // ... other patterns ...
];
```

Adjust the patterns based on the format of the health metrics in your images.

## Testing with Your Own Images

You can test the OCR service with your own images by running:

```
php test_ocr_service.php?image=/path/to/your/image.jpg
```

This will process the specified image and display the extracted metrics.

## Conclusion

By setting up a real OCR service, you can enable the app to automatically extract health metrics from uploaded images. This improves the user experience and makes it easier to track health data over time. 