-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
371 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/node_modules | ||
/dist | ||
/Dockerfile | ||
/secrets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,4 @@ lerna-debug.log* | |
!.vscode/extensions.json | ||
|
||
/.env | ||
/secrets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
- [ ] 支持多条记录合并上下文搜索,应对说话喜欢换行的人 | ||
- [ ] 为没有头像的人生成基于名字的默认头像 | ||
- [ ] 配置消息队列分批大小和超时 | ||
- [ ] 抓取链接归档,并进行索引(可能要新增一个搜索字段) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { registerAs } from '@nestjs/config' | ||
|
||
export default registerAs('ocr', () => ({ | ||
enable: process.env.OCR_ENABLE === 'true', | ||
driver: process.env.OCR_DRIVER || 'google', | ||
endpoint: process.env.OCR_ENDPOINT, | ||
credentials: process.env.OCR_CREDENTIALS, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { registerAs } from '@nestjs/config' | ||
|
||
export default registerAs('queue', () => ({ | ||
enable: process.env.QUEUE_ENABLE === 'true', | ||
amqpUrl: process.env.QUEUE_AMQP_URL || 'amqp://guest@localhost', | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { GoogleOCRDriver } from './google-ocr.driver' | ||
import { readFile } from 'fs/promises' | ||
|
||
let googleOcr: GoogleOCRDriver | ||
let image: Buffer | ||
|
||
beforeEach(async () => { | ||
googleOcr = new GoogleOCRDriver() | ||
await googleOcr.config('eu-vision.googleapis.com') | ||
image = await readFile('docs/assets/search-ui.jpg') | ||
}) | ||
|
||
test('simple ocr', async () => { | ||
const result = await googleOcr.recognize(image) | ||
const texts = result.map((x) => x.text).join('\n') | ||
expect(texts).toContain('搜索界面') | ||
expect(texts).toContain('Telegram') | ||
expect(texts).toContain('Archive') | ||
expect(texts).toContain('Server') | ||
expect(texts).toContain('宣传图') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { OCRDriverInterface, OCRResponse } from './ocr-driver.interface' | ||
import { ImageAnnotatorClient } from '@google-cloud/vision' | ||
|
||
export class GoogleOCRDriver implements OCRDriverInterface { | ||
private client!: ImageAnnotatorClient | ||
|
||
async config(endpoint: string): Promise<void> { | ||
this.client = new ImageAnnotatorClient({ apiEndpoint: endpoint }) | ||
} | ||
|
||
public async recognize(image: Uint8Array): Promise<OCRResponse> { | ||
const imgBuffer = image instanceof Buffer ? image : Buffer.from(image) | ||
const detectResult = await this.client.textDetection(imgBuffer) | ||
const { fullTextAnnotation } = detectResult[0] | ||
if (!fullTextAnnotation?.text) { | ||
return [] | ||
} | ||
return [{ text: fullTextAnnotation.text }] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface OCRDriverInterface { | ||
config(endpoint: string, credentials: string): Promise<void> | ||
recognize(image: Uint8Array): Promise<OCRResponse> | ||
} | ||
|
||
export type OCRResponse = Array<{ | ||
text: string | ||
vertices?: Array<{ x: number; y: number }> | ||
confidence?: number | ||
rotation?: number | ||
}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Module } from '@nestjs/common' | ||
|
||
@Module({ | ||
providers: [], | ||
}) | ||
export class OcrModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
@Module({}) | ||
export class QueueModule {} |
Oops, something went wrong.