File size: 708 Bytes
254560c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# camera.py

import streamlit as st
import cv2
import numpy as np
from PIL import Image

def take_picture():
    # 打开摄像头
    cap = cv2.VideoCapture(0)
    
    st.text("摄像头正在启动...")
    
    if cap.isOpened():
        ret, frame = cap.read()
        if ret:
            st.image(frame, channels="BGR")  # 显示视频帧
            # 保存照片
            img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
            img.save("captured_image.jpg")
            st.success("照片已保存!")
        else:
            st.error("无法捕获图像")
    else:
        st.error("无法打开摄像头")
    cap.release()

if __name__ == "__main__":
    take_picture()