mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
feat(onebot): ocr_image
API
This commit is contained in:
parent
abb468c3f8
commit
ba565e7c38
@ -201,6 +201,16 @@ export class NTQQFileApi extends Service {
|
|||||||
)
|
)
|
||||||
return data.notifyInfo.filePath
|
return data.notifyInfo.filePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async ocrImage(path: string) {
|
||||||
|
return await invoke(
|
||||||
|
'nodeIKernelNodeMiscService/wantWinScreenOCR',
|
||||||
|
[
|
||||||
|
{ url: path },
|
||||||
|
{ timeout: 5000 }
|
||||||
|
]
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NTQQFileCacheApi extends Service {
|
export class NTQQFileCacheApi extends Service {
|
||||||
|
@ -14,7 +14,8 @@ import {
|
|||||||
NodeIKernelRichMediaService,
|
NodeIKernelRichMediaService,
|
||||||
NodeIKernelTicketService,
|
NodeIKernelTicketService,
|
||||||
NodeIKernelTipOffService,
|
NodeIKernelTipOffService,
|
||||||
NodeIKernelRobotService
|
NodeIKernelRobotService,
|
||||||
|
NodeIKernelNodeMiscService
|
||||||
} from './services'
|
} from './services'
|
||||||
|
|
||||||
export enum NTClass {
|
export enum NTClass {
|
||||||
@ -94,6 +95,7 @@ interface NTService {
|
|||||||
nodeIKernelTicketService: NodeIKernelTicketService
|
nodeIKernelTicketService: NodeIKernelTicketService
|
||||||
nodeIKernelTipOffService: NodeIKernelTipOffService
|
nodeIKernelTipOffService: NodeIKernelTipOffService
|
||||||
nodeIKernelRobotService: NodeIKernelRobotService
|
nodeIKernelRobotService: NodeIKernelRobotService
|
||||||
|
nodeIKernelNodeMiscService: NodeIKernelNodeMiscService
|
||||||
}
|
}
|
||||||
|
|
||||||
interface InvokeOptions<ReturnType> {
|
interface InvokeOptions<ReturnType> {
|
||||||
|
15
src/ntqqapi/services/NodeIKernelNodeMiscService.ts
Normal file
15
src/ntqqapi/services/NodeIKernelNodeMiscService.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
export interface NodeIKernelNodeMiscService {
|
||||||
|
wantWinScreenOCR(...args: unknown[]): Promise<{
|
||||||
|
code: number
|
||||||
|
errMsg: string
|
||||||
|
result: {
|
||||||
|
text: string
|
||||||
|
[key: `pt${number}`]: {
|
||||||
|
x: string
|
||||||
|
y: string
|
||||||
|
}
|
||||||
|
charBox: unknown[]
|
||||||
|
score: ''
|
||||||
|
}[]
|
||||||
|
}>
|
||||||
|
}
|
@ -10,3 +10,4 @@ export * from './NodeIKernelRichMediaService'
|
|||||||
export * from './NodeIKernelTicketService'
|
export * from './NodeIKernelTicketService'
|
||||||
export * from './NodeIKernelTipOffService'
|
export * from './NodeIKernelTipOffService'
|
||||||
export * from './NodeIKernelRobotService'
|
export * from './NodeIKernelRobotService'
|
||||||
|
export * from './NodeIKernelNodeMiscService'
|
||||||
|
63
src/onebot11/action/go-cqhttp/OCRImage.ts
Normal file
63
src/onebot11/action/go-cqhttp/OCRImage.ts
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
|
import { ActionName } from '../types'
|
||||||
|
import { uri2local } from '@/common/utils/file'
|
||||||
|
import { access, unlink } from 'node:fs/promises'
|
||||||
|
|
||||||
|
interface Payload {
|
||||||
|
image: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TextDetection {
|
||||||
|
text: string
|
||||||
|
confidence: number
|
||||||
|
coordinates: {
|
||||||
|
x: number //int32
|
||||||
|
y: number
|
||||||
|
}[]
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
texts: TextDetection[]
|
||||||
|
language: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export class OCRImage extends BaseAction<Payload, Response> {
|
||||||
|
actionName = ActionName.GoCQHTTP_OCRImage
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
image: Schema.string().required()
|
||||||
|
})
|
||||||
|
|
||||||
|
protected async _handle(payload: Payload) {
|
||||||
|
const { errMsg, isLocal, path, success } = await uri2local(this.ctx, payload.image, true)
|
||||||
|
if (!success) {
|
||||||
|
throw new Error(errMsg)
|
||||||
|
}
|
||||||
|
await access(path)
|
||||||
|
|
||||||
|
const data = await this.ctx.ntFileApi.ocrImage(path)
|
||||||
|
if (!isLocal) {
|
||||||
|
unlink(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
const texts = data.result.map(item => {
|
||||||
|
const ret: TextDetection = {
|
||||||
|
text: item.text,
|
||||||
|
confidence: 1,
|
||||||
|
coordinates: []
|
||||||
|
}
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
const pt = item[`pt${i + 1}`]
|
||||||
|
ret.coordinates.push({
|
||||||
|
x: parseInt(pt.x),
|
||||||
|
y: parseInt(pt.y)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
texts,
|
||||||
|
language: ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -73,6 +73,7 @@ import { GetGroupFileUrl } from './go-cqhttp/GetGroupFileUrl'
|
|||||||
import { GetGroupNotice } from './go-cqhttp/GetGroupNotice'
|
import { GetGroupNotice } from './go-cqhttp/GetGroupNotice'
|
||||||
import { GetRobotUinRange } from './llonebot/GetRobotUinRange'
|
import { GetRobotUinRange } from './llonebot/GetRobotUinRange'
|
||||||
import { DeleteFriend } from './go-cqhttp/DeleteFriend'
|
import { DeleteFriend } from './go-cqhttp/DeleteFriend'
|
||||||
|
import { OCRImage } from './go-cqhttp/OCRImage'
|
||||||
|
|
||||||
export function initActionMap(adapter: Adapter) {
|
export function initActionMap(adapter: Adapter) {
|
||||||
const actionHandlers = [
|
const actionHandlers = [
|
||||||
@ -151,6 +152,7 @@ export function initActionMap(adapter: Adapter) {
|
|||||||
new GetGroupFileUrl(adapter),
|
new GetGroupFileUrl(adapter),
|
||||||
new GetGroupNotice(adapter),
|
new GetGroupNotice(adapter),
|
||||||
new DeleteFriend(adapter),
|
new DeleteFriend(adapter),
|
||||||
|
new OCRImage(adapter),
|
||||||
]
|
]
|
||||||
const actionMap = new Map<string, BaseAction<any, unknown>>()
|
const actionMap = new Map<string, BaseAction<any, unknown>>()
|
||||||
for (const action of actionHandlers) {
|
for (const action of actionHandlers) {
|
||||||
|
@ -86,4 +86,5 @@ export enum ActionName {
|
|||||||
GoCQHTTP_GetGroupFileUrl = 'get_group_file_url',
|
GoCQHTTP_GetGroupFileUrl = 'get_group_file_url',
|
||||||
GoCQHTTP_GetGroupNotice = '_get_group_notice',
|
GoCQHTTP_GetGroupNotice = '_get_group_notice',
|
||||||
GoCQHTTP_DeleteFriend = 'delete_friend',
|
GoCQHTTP_DeleteFriend = 'delete_friend',
|
||||||
|
GoCQHTTP_OCRImage = 'ocr_image',
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user