dlxj commited on
Commit ·
145407d
1
Parent(s): c5cfb65
自动解决 CF Pages 访问本地 https OCR 服务
Browse files- .gitignore +3 -0
- main_v6.py +54 -1
- readme.txt +4 -0
- run_https.ps1 +61 -0
.gitignore
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
|
|
| 1 |
.venv/
|
| 2 |
.venv_cpu/
|
| 3 |
.venv_gpu/
|
| 4 |
#PPv6/
|
| 5 |
out.json
|
| 6 |
.vscode/settings.json
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
.venv/
|
| 3 |
.venv_cpu/
|
| 4 |
.venv_gpu/
|
| 5 |
#PPv6/
|
| 6 |
out.json
|
| 7 |
.vscode/settings.json
|
| 8 |
+
certs/
|
| 9 |
+
tools/mkcert.exe
|
main_v6.py
CHANGED
|
@@ -66,6 +66,7 @@ dic_cache = {}
|
|
| 66 |
from flask import Flask, request, jsonify
|
| 67 |
import threading
|
| 68 |
import platform
|
|
|
|
| 69 |
|
| 70 |
app = Flask(__name__)
|
| 71 |
|
|
@@ -78,6 +79,39 @@ import cv2
|
|
| 78 |
|
| 79 |
from collections import OrderedDict
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
class DecimalEncoder(json.JSONEncoder):
|
| 82 |
def default(self, o):
|
| 83 |
if isinstance(o, decimal.Decimal):
|
|
@@ -241,6 +275,7 @@ ch, chinese_cht, en, japan, af, az, bs, ca, cs, cy, da, de, es, et, eu, fi, fr,
|
|
| 241 |
# 限制同时处理的请求数量为1
|
| 242 |
ocr_semaphore = threading.Semaphore(1)
|
| 243 |
@app.route('/ppocrv6', methods=['post'])
|
|
|
|
| 244 |
def ppv6():
|
| 245 |
# request.json 只能够接受方法为POST、Body为raw,header 内容为 application/json类型的数据
|
| 246 |
# print(request.json, type(request.json))
|
|
@@ -297,4 +332,22 @@ if __name__ == '__main__':
|
|
| 297 |
cv2.imwrite("output/SWX0005_00000_00001_output_img.png", pre['output_img']) # draw_box.py
|
| 298 |
pass
|
| 299 |
else:
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
from flask import Flask, request, jsonify
|
| 67 |
import threading
|
| 68 |
import platform
|
| 69 |
+
import os
|
| 70 |
|
| 71 |
app = Flask(__name__)
|
| 72 |
|
|
|
|
| 79 |
|
| 80 |
from collections import OrderedDict
|
| 81 |
|
| 82 |
+
def _parse_csv_env(name: str, default: str):
|
| 83 |
+
raw = os.environ.get(name, default)
|
| 84 |
+
parts = [p.strip() for p in raw.split(",")]
|
| 85 |
+
return [p for p in parts if p]
|
| 86 |
+
|
| 87 |
+
_cors_allowed_origins = set(_parse_csv_env("PPV5_CORS_ALLOWED_ORIGINS", "https://typst-app-clone.pages.dev"))
|
| 88 |
+
|
| 89 |
+
def _add_cors_headers(resp):
|
| 90 |
+
origin = request.headers.get("Origin")
|
| 91 |
+
if origin and origin in _cors_allowed_origins:
|
| 92 |
+
resp.headers["Access-Control-Allow-Origin"] = origin
|
| 93 |
+
resp.headers["Vary"] = "Origin"
|
| 94 |
+
resp.headers["Access-Control-Allow-Methods"] = "POST, OPTIONS"
|
| 95 |
+
req_headers = request.headers.get("Access-Control-Request-Headers")
|
| 96 |
+
resp.headers["Access-Control-Allow-Headers"] = req_headers or "Content-Type"
|
| 97 |
+
resp.headers["Access-Control-Max-Age"] = "86400"
|
| 98 |
+
|
| 99 |
+
if request.headers.get("Access-Control-Request-Private-Network") == "true":
|
| 100 |
+
resp.headers["Access-Control-Allow-Private-Network"] = "true"
|
| 101 |
+
|
| 102 |
+
return resp
|
| 103 |
+
|
| 104 |
+
@app.before_request
|
| 105 |
+
def _handle_options_preflight():
|
| 106 |
+
if request.method != "OPTIONS":
|
| 107 |
+
return None
|
| 108 |
+
resp = app.make_response(("", 204))
|
| 109 |
+
return _add_cors_headers(resp)
|
| 110 |
+
|
| 111 |
+
@app.after_request
|
| 112 |
+
def _after_request(resp):
|
| 113 |
+
return _add_cors_headers(resp)
|
| 114 |
+
|
| 115 |
class DecimalEncoder(json.JSONEncoder):
|
| 116 |
def default(self, o):
|
| 117 |
if isinstance(o, decimal.Decimal):
|
|
|
|
| 275 |
# 限制同时处理的请求数量为1
|
| 276 |
ocr_semaphore = threading.Semaphore(1)
|
| 277 |
@app.route('/ppocrv6', methods=['post'])
|
| 278 |
+
@app.route('/ocr', methods=['post'])
|
| 279 |
def ppv6():
|
| 280 |
# request.json 只能够接受方法为POST、Body为raw,header 内容为 application/json类型的数据
|
| 281 |
# print(request.json, type(request.json))
|
|
|
|
| 332 |
cv2.imwrite("output/SWX0005_00000_00001_output_img.png", pre['output_img']) # draw_box.py
|
| 333 |
pass
|
| 334 |
else:
|
| 335 |
+
use_https = os.environ.get("PPV5_USE_HTTPS", "").lower() in ("1", "true", "yes", "on")
|
| 336 |
+
host = os.environ.get("PPV5_HOST", "127.0.0.1" if use_https else "0.0.0.0")
|
| 337 |
+
port = int(os.environ.get("PPV5_PORT", "9346"))
|
| 338 |
+
|
| 339 |
+
if use_https:
|
| 340 |
+
cert_file = os.environ.get("PPV5_TLS_CERT", os.path.join(".", "certs", "localhost.pem"))
|
| 341 |
+
key_file = os.environ.get("PPV5_TLS_KEY", os.path.join(".", "certs", "localhost-key.pem"))
|
| 342 |
+
if not (os.path.exists(cert_file) and os.path.exists(key_file)):
|
| 343 |
+
raise FileNotFoundError(
|
| 344 |
+
"TLS 证书不存在。请先用 mkcert 生成并安装根证书,然后生成 localhost 证书:\n"
|
| 345 |
+
" mkcert -install\n"
|
| 346 |
+
" mkcert -key-file certs\\localhost-key.pem -cert-file certs\\localhost.pem localhost 127.0.0.1 ::1\n"
|
| 347 |
+
f"当前期望证书路径:{cert_file}\n"
|
| 348 |
+
f"当前期望私钥路径:{key_file}\n"
|
| 349 |
+
"也可用环境变量 PPV5_TLS_CERT / PPV5_TLS_KEY 指定实际路径。"
|
| 350 |
+
)
|
| 351 |
+
app.run(host=host, port=port, debug=True, ssl_context=(cert_file, key_file))
|
| 352 |
+
else:
|
| 353 |
+
app.run(host=host, port=port, debug=True)
|
readme.txt
CHANGED
|
@@ -1,4 +1,8 @@
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
.venv/Scripts/python main_v6.py
|
| 3 |
.venv/Scripts/python post.py
|
| 4 |
|
|
|
|
| 1 |
|
| 2 |
+
run_https.ps1
|
| 3 |
+
运行,自动解决 CF Pages 访问本地 https OCR 服务
|
| 4 |
+
|
| 5 |
+
|
| 6 |
.venv/Scripts/python main_v6.py
|
| 7 |
.venv/Scripts/python post.py
|
| 8 |
|
run_https.ps1
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
$ErrorActionPreference = "Stop"
|
| 2 |
+
|
| 3 |
+
$root = $PSScriptRoot
|
| 4 |
+
if (-not $root) { $root = (Get-Location).Path }
|
| 5 |
+
|
| 6 |
+
$toolsDir = Join-Path $root "tools"
|
| 7 |
+
$certsDir = Join-Path $root "certs"
|
| 8 |
+
|
| 9 |
+
New-Item -ItemType Directory -Force -Path $toolsDir | Out-Null
|
| 10 |
+
New-Item -ItemType Directory -Force -Path $certsDir | Out-Null
|
| 11 |
+
|
| 12 |
+
$mkcert = Get-Command mkcert -ErrorAction SilentlyContinue
|
| 13 |
+
$mkcertPath = $null
|
| 14 |
+
if ($mkcert) {
|
| 15 |
+
$mkcertPath = $mkcert.Source
|
| 16 |
+
} else {
|
| 17 |
+
$mkcertPath = Join-Path $toolsDir "mkcert.exe"
|
| 18 |
+
if (-not (Test-Path $mkcertPath)) {
|
| 19 |
+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
| 20 |
+
$rel = Invoke-RestMethod -Uri "https://api.github.com/repos/FiloSottile/mkcert/releases/latest"
|
| 21 |
+
$asset = $rel.assets | Where-Object { $_.name -eq "mkcert-v$($rel.tag_name.TrimStart('v'))-windows-amd64.exe" } | Select-Object -First 1
|
| 22 |
+
if (-not $asset) {
|
| 23 |
+
$asset = $rel.assets | Where-Object { $_.name -match "windows-amd64\.exe$" } | Select-Object -First 1
|
| 24 |
+
}
|
| 25 |
+
if (-not $asset) {
|
| 26 |
+
throw "无法从 GitHub release 自动定位 mkcert 的 windows-amd64.exe"
|
| 27 |
+
}
|
| 28 |
+
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $mkcertPath -UseBasicParsing
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
& $mkcertPath -install | Out-Host
|
| 33 |
+
|
| 34 |
+
$certFile = Join-Path $certsDir "localhost.pem"
|
| 35 |
+
$keyFile = Join-Path $certsDir "localhost-key.pem"
|
| 36 |
+
& $mkcertPath -key-file $keyFile -cert-file $certFile "localhost" "127.0.0.1" "::1" | Out-Host
|
| 37 |
+
|
| 38 |
+
$pythonCandidates = @(
|
| 39 |
+
(Join-Path $root ".venv\Scripts\python.exe"),
|
| 40 |
+
(Join-Path $root ".venv_cpu\Scripts\python.exe")
|
| 41 |
+
)
|
| 42 |
+
$pythonPath = $pythonCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
|
| 43 |
+
if (-not $pythonPath) {
|
| 44 |
+
$py = Get-Command python -ErrorAction SilentlyContinue
|
| 45 |
+
if ($py) { $pythonPath = $py.Source }
|
| 46 |
+
}
|
| 47 |
+
if (-not $pythonPath) {
|
| 48 |
+
throw "找不到 python。请先创建 venv(.venv 或 .venv_cpu)或确保 python 在 PATH 里。"
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
$env:PPV5_USE_HTTPS = "1"
|
| 52 |
+
$env:PPV5_HOST = "127.0.0.1"
|
| 53 |
+
$env:PPV5_PORT = "9346"
|
| 54 |
+
$env:PPV5_TLS_CERT = $certFile
|
| 55 |
+
$env:PPV5_TLS_KEY = $keyFile
|
| 56 |
+
if (-not $env:PPV5_CORS_ALLOWED_ORIGINS) {
|
| 57 |
+
$env:PPV5_CORS_ALLOWED_ORIGINS = "https://typst-app-clone.pages.dev"
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
Set-Location $root
|
| 61 |
+
& $pythonPath (Join-Path $root "main_v6.py")
|