當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Streamlit st.camera_input用法及代碼示例

顯示一個從用戶的網絡攝像頭返回圖片的小部件。

函數簽名

st.camera_input(label, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False)
參數說明

label (str)

一個簡短的標簽,向用戶解釋這個小部件的用途。

key (str or int)

一個可選的字符串或整數,用作小部件的唯一鍵。如果省略,將根據其內容為小部件生成一個 key 。相同類型的多個小部件可能不共享相同的鍵。

help (str)

顯示在相機輸入旁邊的工具提示。

on_change (callable)

當此 camera_input 的值更改時調用的可選回調。

args (tuple)

傳遞給回調的可選參數元組。

kwargs (dict)

一個可選的 kwargs 字典傳遞給回調。

disabled (bool)

一個可選的布爾值,如果設置為 True,則禁用相機輸入。默認值為假。此參數隻能由關鍵字提供。

返回說明

(None or UploadedFile)

UploadedFile 類是 BytesIO 的子類,因此它是"file-like"。這意味著您可以將它們傳遞到預期文件的任何位置。

例子

import streamlit as st

picture = st.camera_input("Take a picture")

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 的子類。因此它是一個"file-like" 對象。這意味著您可以將它傳遞到預期文件的任何位置,類似於 st.file_uploader

相關用法


注:本文由純淨天空篩選整理自streamlit.io大神的英文原創作品 st.camera_input。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。