Create camera.py
Browse files
camera.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# camera.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
def take_picture():
|
| 9 |
+
# 打开摄像头
|
| 10 |
+
cap = cv2.VideoCapture(0)
|
| 11 |
+
|
| 12 |
+
st.text("摄像头正在启动...")
|
| 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 |
+
take_picture()
|