How to Set Up ModelScope with Anaconda and Run OCR Inference via PHP

This guide walks through installing Anaconda, creating a Python 3.10 conda environment, adding PyTorch and ModelScope libraries, installing domain-specific dependencies, verifying NLP pipelines, and using PHPY to call ModelScope's OCR model from PHP, complete with code snippets and troubleshooting tips.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Set Up ModelScope with Anaconda and Run OCR Inference via PHP

What Is ModelScope?

ModelScope is an open‑source model‑as‑a‑service platform that provides a one‑stop solution for AI developers, offering flexible, easy‑to‑use, low‑cost model services for a wide range of tasks.

Environment Installation

System Requirements

Operating System: Linux

Environment manager: Anaconda

Python version: 3.10 Deep‑learning framework: PyTorch

Compute platform: CPU

Install Anaconda

Anaconda bundles over 190 scientific packages and a conda package manager, simplifying Python package installation and environment management.

wget https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh

Run the installer and accept the license:

sh Anaconda3-2023.09-Linux-x86_64.sh
# accept the license terms --yes

Verify the installation:

conda --version

Create and Activate a Python Environment

conda create -n tinywan-modelscope python=3.10
conda activate tinywan-modelscope

Common conda commands:

# List environments
conda env list
# Install a package, e.g., numpy
conda install numpy
# Remove a package
conda remove package_name
# Deactivate environment
conda deactivate
# Delete an environment
conda remove -n your_env_name --all

Install PyTorch

pip3 install torch torchvision torchaudio

Install ModelScope Library

Install the core framework: pip3 install modelscope For domain‑specific capabilities, install extra dependencies. Example for multimodal models: pip install "modelscope[multi-modal]" Example for NLP models:

pip3 install "modelscope[nlp]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

Verify Installation with an NLP Pipeline

Run a Chinese word‑segmentation task to confirm the setup:

python -c "from modelscope.pipelines import pipeline; print(pipeline('word-segmentation')('今天天气不错,适合 出去游玩。开源技术小栈'))"

Using PHPY to Call ModelScope (OCR Example)

PHPY enables PHP code to import and use Python packages, allowing PHP to run ModelScope pipelines.

PHP Code Example (captcha.php)

<?php
/**
 * @link https://modelscope.cn/models/damo/cv_convnextTiny_ocr-recognition-general_damo/summary
 */
$pipeline = PyCore::import('modelscope.pipelines')->pipeline;
$Tasks = PyCore::import('modelscope.utils.constant')->Tasks;
// Use the OCR model
$pipe = $pipeline($Tasks->ocr_recognition, model: 'damo/cv_convnextTiny_ocr-recognition-general_damo');
$file = '/tmp/captcha.png';
file_put_contents($file, file_get_contents('https://business.swoole.com/page/captcha_register'));
echo '识别结果:' . $pipe($file)['text'][0], PHP_EOL;
?>

The official example can be found at:

https://github.com/swoole/phpy/blob/main/examples/modelscope/captcha.php

Model Details

Task: Text recognition from images.

Architecture: Convolutional Backbone extracts visual features. ConvTransformer Blocks perform contextual modeling. CTC loss decodes the output and optimizes gradients.

Required Dependency: OpenCV

The OCR model relies on the OpenCV library for image processing.

pip install opencv-python

Run the PHP Script

/usr/local/php-8.2.14/bin/php captcha.php

Sample output:

识别结果:7R2N

Execution Log (excerpt)

(tinywan-modelscope) $ /usr/local/php-8.2.14/bin/php captcha.php
2024-01-19 22:41:20,505 - modelscope - INFO - PyTorch version 2.1.2 Found.
2024-01-19 22:41:20,506 - modelscope - INFO - Loading ast index from /home/www/.cache/modelscope/ast_indexer
... (log continues) ...
识别结果:电子元器件提供BOM配单

These steps provide a complete end‑to‑end setup for installing ModelScope, adding the OCR model, and invoking it from PHP via PHPY.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonOCRAI inferencePyTorchAnacondaModelScopephpy
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.