函数签名[source] | |
---|---|
st.camera_input(label, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False, label_visibility="visible") | |
参数 | |
label (str) | 一个简短的标签,向用户解释此小部件的用途。 标签可以选择性地包含以下类型的GitHub风格的Markdown:粗体、斜体、删除线、内联代码、链接和图片。图片显示为图标,最大高度等于字体高度。 不支持的Markdown元素会被解包,因此只渲染它们的子元素(文本内容)。通过反斜杠转义不支持的元素,将其显示为字面字符。例如, "1\. 不是有序列表"。 有关其他支持的Markdown指令,请参见st.markdown的body参数。 出于可访问性原因,您不应设置空标签,但如有需要,可以使用label_visibility隐藏它。将来,我们可能会通过引发异常来禁止空标签。 |
key (str or int) | 一个可选的字符串或整数,用作小部件的唯一键。 如果省略,将根据小部件的内容生成一个键。 任何两个小部件都不能有相同的键。 |
help (str) | 一个可选的小提示,显示在小部件标签旁边。 只有当label_visibility="visible"时,Streamlit才会显示这个小提示。 |
on_change (callable) | 当此camera_input的值发生变化时调用的可选回调函数。 |
args (tuple) | 传递给回调函数的可选参数元组。 |
kwargs (dict) | 一个可选的字典,用于传递给回调函数。 |
disabled (bool) | 一个可选的布尔值,如果设置为True,则禁用相机输入。默认值为False。 |
label_visibility ("visible", "hidden", or "collapsed") | 标签的可见性。默认是"visible"。如果这是"hidden",Streamlit会显示一个空白的间隔符而不是标签,这有助于保持小部件与其他小部件对齐。如果这是"collapsed",Streamlit不会显示标签或间隔符。 |
返回 | |
(None or UploadedFile) | UploadedFile 类是 BytesIO 的子类,因此它是“类文件”的。这意味着你可以在任何需要文件的地方传递它的实例。 |
示例
import streamlit as st enable = st.checkbox("Enable camera") picture = st.camera_input("Take a picture", disabled=not enable) if picture: st.image(picture)
要将图像文件缓冲区读取为字节,您可以在UploadedFile
对象上使用getvalue()
。
import streamlit as st
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
# To read image file buffer as bytes:
bytes_data = img_file_buffer.getvalue()
# Check the type of bytes_data:
# Should output: <class 'bytes'>
st.write(type(bytes_data))
重要
st.camera_input
返回一个 UploadedFile
类的对象,它是 BytesIO 的子类。因此它是一个“类文件”对象。这意味着你可以将它传递到任何需要文件的地方,类似于 st.file_uploader
。
Image processing examples
您可以使用st.camera_input
的输出进行各种下游任务,包括图像处理。下面,我们展示了如何将st.camera_input
小部件与流行的图像和数据处理库(如Pillow、NumPy、OpenCV、TensorFlow、torchvision和PyTorch)一起使用。
虽然我们提供了最流行的用例和库的示例,但欢迎您根据自己的需求和喜欢的库来调整这些示例。
Pillow (PIL) and NumPy
将图像文件缓冲区读取为PIL图像并将其转换为NumPy数组:
import streamlit as st
from PIL import Image
import numpy as np
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
# To read image file buffer as a PIL Image:
img = Image.open(img_file_buffer)
# To convert PIL Image to numpy array:
img_array = np.array(img)
# Check the type of img_array:
# Should output: <class 'numpy.ndarray'>
st.write(type(img_array))
# Check the shape of img_array:
# Should output shape: (height, width, channels)
st.write(img_array.shape)
OpenCV (cv2)
使用OpenCV读取图像文件缓冲区:
import streamlit as st
import cv2
import numpy as np
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
# To read image file buffer with OpenCV:
bytes_data = img_file_buffer.getvalue()
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
# Check the type of cv2_img:
# Should output: <class 'numpy.ndarray'>
st.write(type(cv2_img))
# Check the shape of cv2_img:
# Should output shape: (height, width, channels)
st.write(cv2_img.shape)
TensorFlow
确保你已经安装了TensorFlow。
使用TensorFlow将图像文件缓冲区读取为3维uint8张量:
import streamlit as st
import tensorflow as tf
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
# To read image file buffer as a 3D uint8 tensor with TensorFlow:
bytes_data = img_file_buffer.getvalue()
img_tensor = tf.io.decode_image(bytes_data, channels=3)
# Check the type of img_tensor:
# Should output: <class 'tensorflow.python.framework.ops.EagerTensor'>
st.write(type(img_tensor))
# Check the shape of img_tensor:
# Should output shape: (height, width, channels)
st.write(img_tensor.shape)
Torchvision
确保你已经安装了Torchvision(它不随PyTorch一起提供)和PyTorch。
要使用torchvision.io
将图像文件缓冲区读取为3维uint8张量:
import streamlit as st
import torch
import torchvision
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
# To read image file buffer as a 3D uint8 tensor with `torchvision.io`:
bytes_data = img_file_buffer.getvalue()
torch_img = torchvision.io.decode_image(
torch.frombuffer(bytes_data, dtype=torch.uint8)
)
# Check the type of torch_img:
# Should output: <class 'torch.Tensor'>
st.write(type(torch_img))
# Check the shape of torch_img:
# Should output shape: torch.Size([channels, height, width])
st.write(torch_img.shape)
PyTorch
使用PyTorch将图像文件缓冲区读取为3维uint8张量:
import streamlit as st
import torch
import numpy as np
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
# To read image file buffer as a 3D uint8 tensor with PyTorch:
bytes_data = img_file_buffer.getvalue()
torch_img = torch.ops.image.decode_image(
torch.from_numpy(np.frombuffer(bytes_data, np.uint8)), 3
)
# Check the type of torch_img:
# Should output: <class 'torch.Tensor'>
st.write(type(torch_img))
# Check the shape of torch_img:
# Should output shape: torch.Size([channels, height, width])
st.write(torch_img.shape)
还有问题吗?
我们的 论坛 充满了有用的信息和Streamlit专家。