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.
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.shRun the installer and accept the license:
sh Anaconda3-2023.09-Linux-x86_64.sh
# accept the license terms --yesVerify the installation:
conda --versionCreate and Activate a Python Environment
conda create -n tinywan-modelscope python=3.10 conda activate tinywan-modelscopeCommon 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 --allInstall PyTorch
pip3 install torch torchvision torchaudioInstall 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.htmlVerify 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.phpModel 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-pythonRun the PHP Script
/usr/local/php-8.2.14/bin/php captcha.phpSample output:
识别结果:7R2NExecution 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
