mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-31 18:11:54 +08:00
pikvm/kvmd#66: OCR API
This commit is contained in:
@@ -112,7 +112,7 @@ class HidApi:
|
||||
|
||||
# =====
|
||||
|
||||
def get_keymaps(self) -> Dict: # Ugly hack to generate hid_keymaps_state (see server.py)
|
||||
async def get_keymaps(self) -> Dict: # Ugly hack to generate hid_keymaps_state (see server.py)
|
||||
keymaps: Set[str] = set()
|
||||
for keymap_name in os.listdir(self.__keymaps_dir_path):
|
||||
path = os.path.join(self.__keymaps_dir_path, keymap_name)
|
||||
@@ -127,7 +127,7 @@ class HidApi:
|
||||
|
||||
@exposed_http("GET", "/hid/keymaps")
|
||||
async def __keymaps_handler(self, _: Request) -> Response:
|
||||
return make_json_response(self.get_keymaps())
|
||||
return make_json_response(await self.get_keymaps())
|
||||
|
||||
@exposed_http("POST", "/hid/print")
|
||||
async def __print_handler(self, request: Request) -> Response:
|
||||
|
||||
@@ -23,13 +23,19 @@
|
||||
import io
|
||||
import functools
|
||||
|
||||
from typing import List
|
||||
from typing import Dict
|
||||
|
||||
from aiohttp.web import Request
|
||||
from aiohttp.web import Response
|
||||
|
||||
from PIL import Image as PilImage
|
||||
|
||||
from ....validators import check_string_in_list
|
||||
from ....validators.basic import valid_bool
|
||||
from ....validators.basic import valid_number
|
||||
from ....validators.basic import valid_int_f0
|
||||
from ....validators.basic import valid_string_list
|
||||
from ....validators.kvm import valid_stream_quality
|
||||
|
||||
from .... import aiotools
|
||||
@@ -41,11 +47,14 @@ from ..http import make_json_response
|
||||
from ..streamer import StreamerSnapshot
|
||||
from ..streamer import Streamer
|
||||
|
||||
from ..tesseract import TesseractOcr
|
||||
|
||||
|
||||
# =====
|
||||
class StreamerApi:
|
||||
def __init__(self, streamer: Streamer) -> None:
|
||||
def __init__(self, streamer: Streamer, ocr: TesseractOcr) -> None:
|
||||
self.__streamer = streamer
|
||||
self.__ocr = ocr
|
||||
|
||||
# =====
|
||||
|
||||
@@ -61,7 +70,25 @@ class StreamerApi:
|
||||
allow_offline=valid_bool(request.query.get("allow_offline", "false")),
|
||||
)
|
||||
if snapshot:
|
||||
if valid_bool(request.query.get("preview", "false")):
|
||||
if valid_bool(request.query.get("ocr", "false")):
|
||||
langs = await self.__ocr.get_available_langs()
|
||||
return Response(
|
||||
body=(await self.__ocr.recognize(
|
||||
data=snapshot.data,
|
||||
langs=valid_string_list(
|
||||
arg=str(request.query.get("ocr_langs", "")).strip(),
|
||||
subval=(lambda lang: check_string_in_list(lang, "OCR lang", langs)),
|
||||
name="OCR langs list",
|
||||
),
|
||||
left=int(valid_number(request.query.get("ocr_left", "-1"))),
|
||||
top=int(valid_number(request.query.get("ocr_top", "-1"))),
|
||||
right=int(valid_number(request.query.get("ocr_right", "-1"))),
|
||||
bottom=int(valid_number(request.query.get("ocr_bottom", "-1"))),
|
||||
)),
|
||||
headers=dict(snapshot.headers),
|
||||
content_type="text/plain",
|
||||
)
|
||||
elif valid_bool(request.query.get("preview", "false")):
|
||||
data = await self.__make_preview(
|
||||
snapshot=snapshot,
|
||||
max_width=valid_int_f0(request.query.get("preview_max_width", "0")),
|
||||
@@ -84,6 +111,29 @@ class StreamerApi:
|
||||
|
||||
# =====
|
||||
|
||||
async def get_ocr(self) -> Dict: # XXX: Ugly hack
|
||||
enabled = self.__ocr.is_available()
|
||||
default: List[str] = []
|
||||
available: List[str] = []
|
||||
if enabled:
|
||||
default = await self.__ocr.get_default_langs()
|
||||
available = await self.__ocr.get_available_langs()
|
||||
return {
|
||||
"ocr": {
|
||||
"enabled": enabled,
|
||||
"langs": {
|
||||
"default": default,
|
||||
"available": available,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@exposed_http("GET", "/streamer/ocr")
|
||||
async def __ocr_handler(self, _: Request) -> Response:
|
||||
return make_json_response(await self.get_ocr())
|
||||
|
||||
# =====
|
||||
|
||||
async def __make_preview(self, snapshot: StreamerSnapshot, max_width: int, max_height: int, quality: int) -> bytes:
|
||||
if max_width == 0 and max_height == 0:
|
||||
max_width = snapshot.width // 5
|
||||
|
||||
Reference in New Issue
Block a user