ake178178's picture
Create camera.py
254560c verified
raw
history blame
708 Bytes
# 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()