diff --git a/Dockerfile b/Dockerfile index 82ba912..8079e1c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,12 +4,11 @@ FROM python:3.12.6 # WORKDIR /app +COPY requirements.txt /app +RUN pip install --no-cache-dir --upgrade -r requirements.txt + # COPY . /app - -RUN pip install --no-cache-dir --upgrade -r requirements.txt - - # CMD ["uvicorn", "main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "80"] \ No newline at end of file diff --git a/main.py b/main.py index 22e47f1..78a35d9 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,25 @@ +import asyncio +import os +from typing import Optional + +import aiohttp # 用于异步 HTTP 请求 +import easyocr +import torch from fastapi import FastAPI, UploadFile, File, Query from fastapi.responses import JSONResponse -import easyocr -from typing import Optional -import asyncio -import aiohttp # 用于异步 HTTP 请求 MODEL_DIR = "./models" +USE_GPU = os.environ.get('USE_GPU', 'false') == 'true' +NNPACK = os.environ.get('NNPACK', 'false') == 'true' +torch.backends.nnpack.enabled = NNPACK + app = FastAPI() # 初始化常用的语言组合 OCR 读取器 -reader_sim = easyocr.Reader(lang_list=['ch_sim', 'en'], model_storage_directory=MODEL_DIR) +reader_sim = easyocr.Reader( + lang_list=['ch_sim', 'en'], + model_storage_directory=MODEL_DIR, + gpu=USE_GPU) # 存储 OCR 读取器的缓存字典 readers = {} @@ -40,7 +50,8 @@ async def get_reader(lang_1: Optional[str], lang_2: Optional[str]): try: # 尝试初始化 OCR 读取器 readers[lang_combination] = easyocr.Reader(lang_list=list(lang_combination), - model_storage_directory=MODEL_DIR) + model_storage_directory=MODEL_DIR, + gpu=USE_GPU) except ValueError as ve: return None, str(ve) # 返回错误信息 except Exception as e: diff --git a/manifest.yaml b/manifest.yaml index 409c53a..31125b2 100644 --- a/manifest.yaml +++ b/manifest.yaml @@ -33,15 +33,20 @@ spec: claimName: easyocr-pvc containers: - name: easyocr - image: leafdev.top/ecosystem/ocr:v0.0.1 + image: leafdev.top/ecosystem/ocr:v0.0.4 + env: + - name: USE_GPU + value: "false" + - name: NNPACK + value: "false" ports: - containerPort: 80 protocol: TCP name: http - resources: - requests: - cpu: 1000m - memory: 1024Mi +# resources: +# requests: +# cpu: 1000m +# memory: 1024Mi volumeMounts: - mountPath: /app/models name: easyocr-models