代码拉取完成,页面将自动刷新
同步操作将从 cyahua/cnocr 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# 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:8501/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:8501/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'\tOCR results: {pformat(texts)}\n\n')
if texts:
pc.copy('\n'.join(texts))
time.sleep(1)
if __name__ == '__main__':
ocr_newest(SCREENSHOT_DIR, 2)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。