Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,43 +3,32 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
def
|
| 8 |
-
cap = cv2.VideoCapture(0) # 0
|
| 9 |
if not cap.isOpened():
|
| 10 |
-
st.error("
|
| 11 |
-
return None
|
| 12 |
-
return cap
|
| 13 |
-
|
| 14 |
-
# 捕获图片
|
| 15 |
-
def capture_image(cap):
|
| 16 |
ret, frame = cap.read()
|
|
|
|
| 17 |
if not ret:
|
| 18 |
-
st.error("
|
| 19 |
-
return None
|
| 20 |
-
return frame
|
| 21 |
|
| 22 |
-
# 主程序
|
| 23 |
def main():
|
| 24 |
-
st.title("
|
| 25 |
-
|
| 26 |
-
# 摄像头对象
|
| 27 |
-
cap = load_camera()
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
frame =
|
| 32 |
-
|
| 33 |
if frame is not None:
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# 显示图像
|
| 39 |
-
st.image(img, caption="Captured Image", use_column_width=True)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
cap.release()
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
main()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# 创建一个摄像头捕捉函数
|
| 7 |
+
def capture_camera():
|
| 8 |
+
cap = cv2.VideoCapture(0) # 参数0表示默认摄像头
|
| 9 |
if not cap.isOpened():
|
| 10 |
+
st.error("无法打开摄像头")
|
| 11 |
+
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
ret, frame = cap.read()
|
| 13 |
+
cap.release()
|
| 14 |
if not ret:
|
| 15 |
+
st.error("无法从摄像头获取图像")
|
| 16 |
+
return None, None
|
| 17 |
+
return frame, cap
|
| 18 |
|
|
|
|
| 19 |
def main():
|
| 20 |
+
st.title("摄像头拍照应用")
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# 添加一个按钮用于拍照
|
| 23 |
+
if st.button("打开摄像头拍照"):
|
| 24 |
+
frame, cap = capture_camera()
|
|
|
|
| 25 |
if frame is not None:
|
| 26 |
+
# 转换颜色从BGR到RGB
|
| 27 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 28 |
+
# 将图像显示在Streamlit界面上
|
| 29 |
+
st.image(frame, use_column_width=True, caption="捕捉到的图像")
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
st.text("点击上方按钮打开摄像头并拍照。")
|
|
|
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
main()
|