Update camera.py
Browse files
camera.py
CHANGED
|
@@ -1,29 +1,22 @@
|
|
| 1 |
-
# camera.py
|
| 2 |
-
|
| 3 |
-
import streamlit as st
|
| 4 |
import cv2
|
| 5 |
-
import
|
| 6 |
-
from PIL import Image
|
| 7 |
|
| 8 |
def take_picture():
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
if cap.isOpened():
|
| 15 |
-
ret, frame = cap.read()
|
| 16 |
-
if ret:
|
| 17 |
-
st.image(frame, channels="BGR") # 显示视频帧
|
| 18 |
-
# 保存照片
|
| 19 |
-
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 20 |
-
img.save("captured_image.jpg")
|
| 21 |
-
st.success("照片已保存!")
|
| 22 |
-
else:
|
| 23 |
-
st.error("无法捕获图像")
|
| 24 |
-
else:
|
| 25 |
-
st.error("无法打开摄像头")
|
| 26 |
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
import streamlit as st
|
|
|
|
| 3 |
|
| 4 |
def take_picture():
|
| 5 |
+
"""
|
| 6 |
+
使用 OpenCV 拍摄一张照片并返回图像数据。
|
| 7 |
+
"""
|
| 8 |
+
cap = cv2.VideoCapture(0) # 打开默认摄像头
|
| 9 |
+
ret, frame = cap.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
cap.release()
|
| 11 |
+
return frame
|
| 12 |
+
|
| 13 |
+
def show_picture(img):
|
| 14 |
+
"""
|
| 15 |
+
在 Streamlit 中显示图像。
|
| 16 |
+
"""
|
| 17 |
+
st.image(img, channels="BGR")
|
| 18 |
|
| 19 |
if __name__ == "__main__":
|
| 20 |
+
if st.button("拍照"):
|
| 21 |
+
img = take_picture()
|
| 22 |
+
show_picture(img)
|