Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,9 @@ from gtts import gTTS
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# 加载 Hugging Face 模型
|
| 10 |
@st.cache_resource
|
| 11 |
def load_model():
|
|
@@ -25,42 +28,45 @@ run = st.button('打开摄像头并开始识别')
|
|
| 25 |
if run:
|
| 26 |
st.text("正在打开摄像头,请稍等...")
|
| 27 |
camera = cv2.VideoCapture(0)
|
| 28 |
-
|
| 29 |
-
while True:
|
| 30 |
-
ret, frame = camera.read()
|
| 31 |
-
if not ret:
|
| 32 |
-
st.error("无法读取摄像头")
|
| 33 |
-
break
|
| 34 |
-
|
| 35 |
-
# 显示摄像头画面
|
| 36 |
-
st.image(frame, channels="BGR")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# 释放摄像头
|
| 65 |
-
camera.release()
|
| 66 |
|
|
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
|
| 9 |
+
# 提示用户手动检查权限
|
| 10 |
+
st.warning("请确保已允许应用访问您的摄像头。对于 Windows 用户,请检查 [设置 -> 隐私 -> 摄像头]。对于 macOS/iOS 用户,请检查 [系统偏好设置 -> 安全性与隐私 -> 摄像头]。")
|
| 11 |
+
|
| 12 |
# 加载 Hugging Face 模型
|
| 13 |
@st.cache_resource
|
| 14 |
def load_model():
|
|
|
|
| 28 |
if run:
|
| 29 |
st.text("正在打开摄像头,请稍等...")
|
| 30 |
camera = cv2.VideoCapture(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# 检查摄像头是否成功打开
|
| 33 |
+
if not camera.isOpened():
|
| 34 |
+
st.error("无法打开摄像头,请检查摄像头权限设置")
|
| 35 |
+
else:
|
| 36 |
+
while True:
|
| 37 |
+
ret, frame = camera.read()
|
| 38 |
+
if not ret:
|
| 39 |
+
st.error("无法读取摄像头画面")
|
| 40 |
+
break
|
| 41 |
+
|
| 42 |
+
# 显示摄像头画面
|
| 43 |
+
st.image(frame, channels="BGR")
|
| 44 |
+
|
| 45 |
+
# 每10秒进行一次拍照
|
| 46 |
+
time.sleep(10)
|
| 47 |
+
|
| 48 |
+
# 保存照片
|
| 49 |
+
img_path = "captured_image.jpg"
|
| 50 |
+
cv2.imwrite(img_path, frame)
|
| 51 |
|
| 52 |
+
# 读取图像并转换为模型输入
|
| 53 |
+
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 54 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 55 |
|
| 56 |
+
# 进行物体识别
|
| 57 |
+
with torch.no_grad():
|
| 58 |
+
outputs = model(**inputs)
|
| 59 |
+
logits = outputs.logits
|
| 60 |
+
predicted_label = torch.argmax(logits, dim=1).item()
|
| 61 |
|
| 62 |
+
# 获取识别到的物体标签
|
| 63 |
+
label = model.config.id2label[predicted_label]
|
| 64 |
+
st.write(f"识别到的物体: {label}")
|
| 65 |
|
| 66 |
+
# 将标签转化为语音
|
| 67 |
+
tts = gTTS(text=f"The object detected is {label}", lang='en')
|
| 68 |
+
tts.save("output.mp3")
|
| 69 |
+
os.system("mpg321 output.mp3") # 播放语音
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# 释放摄像头
|
| 72 |
+
camera.release()
|