Skip to content

Commit ce6d19a

Browse files
committed
20210310
1 parent c894cce commit ce6d19a

4 files changed

Lines changed: 30 additions & 20 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ video2sub 本身没有实现任何 OCR 识别算法,而是使用外部库进
1414

1515
|文件名|说明|
1616
|-|-|
17-
|video2sub-20210308.zip|下载后解压并运行 **launcher.exe** 即可。|
17+
|video2sub-20210310.zip|下载后解压并运行 **launcher.exe** 即可。|
1818
|chineseocr-gpu.zip|GPU 版 chineseocr 识别引擎,需要 NVIDIA 显卡。下载后解压并运行 **chineseocr-gpu.exe** 即可。
1919
|chineseocr-cpu.zip|CPU 版 chineseocr 识别引擎,无需显卡,但速度很慢,不建议使用。
2020

2121
## 系统需求
2222

23-
- Windows 7 或更高版本
23+
- Windows 7 (64位)或更高版本
2424
- 4GB 以上内存
2525
- NVIDIA 显卡:用于 chineseocr 加速(纯 CPU 也能运行,就是太慢)

launcher.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,7 @@ class BackendDiedError(Exception):
176176
video = None
177177
backendlog = None
178178
backendlog_file = os.path.join(bundle_dir, 'backendlog_%s_%d.txt' % (datetime.now().strftime('%Y%m%d_%H%M%S'), os.getpid()))
179-
180-
def drop_backendlog():
181-
global backendlog
182-
if backendlog is not None:
183-
backendlog.close()
184-
backendlog = None
185-
if os.path.exists(backendlog_file):
186-
os.remove(backendlog_file)
179+
backendlog_preserve = False
187180

188181
def alloc_hostport():
189182
host = gconfig['host']
@@ -222,11 +215,15 @@ def run_backend(file, silent=False, nosession=False):
222215
global lastwatch
223216
global backendlog
224217
hostport = alloc_hostport()
225-
video = os.path.basename(file)
226-
drop_backendlog()
218+
cmdline = exe('video2sub.py') + [hostport[0], str(hostport[1]), file]
227219
if silent:
228-
backendlog = open(backendlog_file, 'wb')
229-
backend = subprocess.Popen(exe('video2sub.py') + [hostport[0], str(hostport[1]), file], stdout=backendlog if silent else None, stderr=subprocess.STDOUT if silent else None)
220+
if backendlog is not None:
221+
backendlog.close()
222+
with open(backendlog_file, 'a', errors='replace') as f:
223+
f.write('>>>>> [%s] >>>>> %s\n' % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), json.dumps(cmdline, ensure_ascii=False)))
224+
backendlog = open(backendlog_file, 'ab')
225+
backend = subprocess.Popen(cmdline, stdout=backendlog if silent else None, stderr=subprocess.STDOUT if silent else None)
226+
video = os.path.basename(file)
230227
url = 'http://%s:%d' % hostport
231228
session = None
232229
lastwatch = 0
@@ -272,7 +269,7 @@ def api(path, data=b'', retry=True):
272269
data = json.dumps(data).encode('utf-8')
273270
req.add_header('Content-Type', 'application/json')
274271
req.data = data
275-
req = urllib.request.urlopen(req, timeout=5)
272+
req = urllib.request.urlopen(req)
276273
rsp = req.read()
277274
try:
278275
return json.loads(rsp.decode('utf-8'))
@@ -357,7 +354,7 @@ def watch():
357354
print('迭代次数过多,已放弃')
358355
break
359356
if i > 0:
360-
print('迭代(第%d次)'%i)
357+
print('迭代第%d次'%i)
361358

362359
if state['lastjob'] > 0:
363360
print('数据库中有未处理完毕的项目,执行“继续OCR”操作')
@@ -430,6 +427,8 @@ def do_join():
430427
assert False
431428
if success >= 0:
432429
print('共%d个文件,成功%d个,失败%d个,耗时%.2f秒'%(len(files), success, len(files)-success, time.time()-start_time))
430+
if success != len(files):
431+
backendlog_preserve = True
433432
if windows:
434433
print('按回车退出程序')
435434
input()
@@ -445,9 +444,13 @@ def do_join():
445444
backendlog.close()
446445
with open(backendlog_file, 'r', errors='replace') as f:
447446
lines = f.readlines()
447+
for i in range(len(lines)):
448+
if lines[-i - 1].startswith('>>>>>'):
449+
lines = lines[-i:] if i > 0 else []
450+
break
448451
maxshow = 50
449452
if len(lines) > maxshow:
450-
print('===== 只显示了后端日志 (%s) 的最后 %d 行 =====' % (backendlog_file, maxshow))
453+
print('===== 后端日志的最后 %d 行 ====='%maxshow)
451454
lines = lines[-maxshow:]
452455
else:
453456
print('===== 后端日志 =====')
@@ -464,6 +467,13 @@ def do_join():
464467
frontend.kill()
465468
frontend.wait()
466469

470+
if backendlog is not None:
471+
backendlog.close()
472+
if not fail and not backendlog_preserve and os.path.exists(backendlog_file):
473+
os.remove(backendlog_file)
474+
else:
475+
print('(后端日志已存储为: %s)'%backendlog_file)
476+
467477
if fail:
468478
print('=====')
469479
if windows:
@@ -473,5 +483,4 @@ def do_join():
473483
print('遇到致命错误')
474484
sys.exit(1)
475485
else:
476-
drop_backendlog()
477486
sys.exit(0)

screenshot.png

-21.2 KB
Loading

video2sub.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,9 @@ def serve_exportcsv():
847847
def serve_importcsv():
848848
try:
849849
with conn:
850-
csvfile = flask.request.files['csv']
851-
r = csv.reader(io.TextIOWrapper(csvfile, encoding='utf_8_sig'))
850+
csvfile = flask.request.files['csv'].read()
851+
encoding = 'utf_8_sig' if csvfile.startswith(b'\xEF\xBB\xBF') else None
852+
r = csv.reader(io.TextIOWrapper(io.BytesIO(csvfile), encoding=encoding))
852853
col = next(r)
853854
colmap = dict([(c, i) for i, c in enumerate(col)])
854855
coltype = dict(c.execute("SELECT name,type FROM pragma_table_info('ocrresult')").fetchall())

0 commit comments

Comments
 (0)