Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Cloud IDE
CnOCR
提交
f253f378
CnOCR
项目概览
Cloud IDE
/
CnOCR
落后 Fork 源项目 19 个版本
Fork自
breezedeus / CnOCR
通知
47
Star
3
Fork
30
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
CnOCR
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f253f378
编写于
8月 20, 2022
作者:
B
breezedeus
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add screenshot daemon scripts
上级
ead27ff8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
138 addition
and
22 deletion
+138
-22
scripts/screenshot_daemon.py
scripts/screenshot_daemon.py
+64
-0
scripts/screenshot_daemon_with_server.py
scripts/screenshot_daemon_with_server.py
+74
-0
try_service.py
try_service.py
+0
-22
未找到文件。
scripts/screenshot_daemon.py
0 → 100644
浏览文件 @
f253f378
# coding: utf-8
# Copyright (C) 2022, [Breezedeus](https://github.com/breezedeus).
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import
os
import
time
import
glob
from
pprint
import
pformat
import
pyperclip
as
pc
from
cnocr
import
CnOcr
from
cnocr.utils
import
set_logger
logger
=
set_logger
(
log_level
=
'DEBUG'
)
SCREENSHOT_DIR
=
os
.
getenv
(
"SCREENSHOT_DIR"
,
'/Users/king/Pictures/screenshot_from_xnip'
)
OCR_MODEL
=
CnOcr
()
def
get_newest_fp_time
(
screenshot_dir
):
fn_list
=
glob
.
glob1
(
screenshot_dir
,
'*g'
)
fp_list
=
[
os
.
path
.
join
(
screenshot_dir
,
fn
)
for
fn
in
fn_list
]
if
not
fp_list
:
return
None
,
None
fp_list
.
sort
(
key
=
lambda
fp
:
os
.
path
.
getmtime
(
fp
),
reverse
=
True
)
return
fp_list
[
0
],
os
.
path
.
getmtime
(
fp_list
[
0
])
def
ocr_newest
(
screenshot_dir
,
delta_interval
):
while
True
:
newest_fp
,
newest_mod_time
=
get_newest_fp_time
(
screenshot_dir
)
if
(
newest_mod_time
is
not
None
and
time
.
time
()
-
newest_mod_time
<
delta_interval
):
logger
.
info
(
f
'analyzing screenshot file
{
newest_fp
}
'
)
result
=
OCR_MODEL
.
ocr
(
newest_fp
)
texts
=
[
_one
[
'text'
]
for
_one
in
result
]
logger
.
info
(
f
'
\t
OCR results:
{
pformat
(
texts
)
}
\n\n
'
)
if
texts
:
pc
.
copy
(
'
\n
'
.
join
(
texts
))
time
.
sleep
(
1
)
if
__name__
==
'__main__'
:
ocr_newest
(
SCREENSHOT_DIR
,
2
)
scripts/screenshot_daemon_with_server.py
0 → 100644
浏览文件 @
f253f378
# coding: utf-8
# Copyright (C) 2022, [Breezedeus](https://github.com/breezedeus).
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# 也可以下调用命令在命令行调用开启的OCR服务:
# > curl -F image=@docs/examples/huochepiao.jpeg http://0.0.0.0:8000/ocr
import
os
import
time
import
glob
import
requests
from
pprint
import
pformat
import
pyperclip
as
pc
from
cnocr.utils
import
set_logger
logger
=
set_logger
(
log_level
=
'DEBUG'
)
SERVICE_URL
=
os
.
getenv
(
"CNOCR_SERVICE"
,
'http://0.0.0.0:8000/ocr'
)
SCREENSHOT_DIR
=
os
.
getenv
(
"SCREENSHOT_DIR"
,
'/Users/king/Pictures/screenshot_from_xnip'
)
def
ocr
(
image
):
r
=
requests
.
post
(
SERVICE_URL
,
files
=
{
'image'
:
(
image
,
open
(
image
,
'rb'
),
'image/png'
)},
)
return
r
.
json
()
def
get_newest_fp_time
(
screenshot_dir
):
fn_list
=
glob
.
glob1
(
screenshot_dir
,
'*g'
)
fp_list
=
[
os
.
path
.
join
(
screenshot_dir
,
fn
)
for
fn
in
fn_list
]
if
not
fp_list
:
return
None
,
None
fp_list
.
sort
(
key
=
lambda
fp
:
os
.
path
.
getmtime
(
fp
),
reverse
=
True
)
return
fp_list
[
0
],
os
.
path
.
getmtime
(
fp_list
[
0
])
def
ocr_newest
(
screenshot_dir
,
delta_interval
):
while
True
:
newest_fp
,
newest_mod_time
=
get_newest_fp_time
(
screenshot_dir
)
if
(
newest_mod_time
is
not
None
and
time
.
time
()
-
newest_mod_time
<
delta_interval
):
logger
.
info
(
f
'analyzing screenshot file
{
newest_fp
}
'
)
result
=
ocr
(
newest_fp
)
texts
=
[
_one
[
'text'
]
for
_one
in
result
[
'results'
]]
logger
.
info
(
f
'
\t
OCR results:
{
pformat
(
texts
)
}
\n\n
'
)
if
texts
:
pc
.
copy
(
'
\n
'
.
join
(
texts
))
time
.
sleep
(
1
)
if
__name__
==
'__main__'
:
ocr_newest
(
SCREENSHOT_DIR
,
2
)
try_service.py
已删除
100644 → 0
浏览文件 @
ead27ff8
# coding: utf-8
# 等价于命令:
# > curl -F image=@docs/examples/huochepiao.jpeg http://0.0.0.0:8000/ocr
import
requests
url
=
'http://0.0.0.0:8000/ocr'
def
ocr
():
image
=
'docs/examples/huochepiao.jpeg'
r
=
requests
.
post
(
url
,
files
=
{
'image'
:
(
image
,
open
(
image
,
'rb'
),
'image/png'
)},
)
print
(
r
)
print
(
r
.
status_code
)
return
r
.
json
()
print
(
ocr
())
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录