Skip to content

Commit 2f05fb0

Browse files
committed
尝试解决划词翻译和保存错题时摄像头不打开的问题,但失败了,可能是我机器的原因,我放弃了。
Let's call it a day!
1 parent d3b0f1f commit 2f05fb0

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

CV/cvworker.py

+3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ def __init__(self):
2828

2929
def takePic(self):
3030
self.cap = cv2.VideoCapture(0) # 参数为0时调用本地摄像头;参数为1时调用外接摄像头
31+
if not self.cap.isOpened():
32+
raise Exception("无法打开摄像头")
3133
self.cap.release() # workaround for cv2.VideoCapture(0) not working after first call
34+
3235
self.cap = cv2.VideoCapture(0)
3336
ret, frame = self.cap.read() # ret(bool)有无读取到图片
3437
cv2.imwrite("/tmp/prism/"+str(self.index)+".jpg", frame)

app/backend.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ def photoTranslate(self):
118118
return results
119119

120120
def liveTranslate(self, var=None):
121-
back = Thread(target=self._liveTranslate,args=(var,))
122-
back.start()
123-
back.join()
121+
self._liveTranslate()
124122
res = self.result
125123
return res
126124

web/back/api.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ def call_live_translate():
7070
global res
7171
global is_live_translate_running
7272
res = Backend.liveTranslate()
73-
db = get_db()
74-
try:
75-
# 将实时翻译结果保存到数据库
76-
db.execute("INSERT INTO PRISM (ID, TYPE, ORI, RES) VALUES (?, ?, ?, ?)",
77-
[now_ID, "LIVE", res.get("origin"), str(res)])
78-
db.commit()
79-
except Exception as e:
80-
print(f"Error saving live translation: {e}")
73+
with app.app_context():
74+
db = get_db()
75+
try:
76+
# 将实时翻译结果保存到数据库
77+
db.execute("INSERT INTO PRISM (ID, TYPE, ORI, RES) VALUES (?, ?, ?, ?)",
78+
[now_ID, "LIVE", res.get("origin"), str(res)])
79+
db.commit()
80+
except Exception as e:
81+
print(f"Error saving live translation: {e}")
8182
is_live_translate_running = False # 实时翻译结束后,重置标志位
8283

8384
@app.route('/livetranslate/end', methods=['GET'])

0 commit comments

Comments
 (0)