File size: 1,167 Bytes
1e7aec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 导入所需的库
import streamlit as st  # 导入streamlit库
from transformers import pipeline  # 导入transformers库中的pipeline模块
from PIL import Image  # 导入PIL库中的Image模块
#PIL库是Python图像处理库,可以处理图片的格式转换、缩放、裁剪、旋转等操作

# 创建一个图像分类的pipeline
pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")

# 设置应用的标题
st.title("Hot Dog? Or Not?")

# 上传一个热狗候选图像
file_name = st.file_uploader("Upload a hot dog candidate image")

# 如果有上传的图像
if file_name is not None:
    col1, col2 = st.columns(2)  # 创建两列布局

    image = Image.open(file_name)  # 打开上传的图像
    col1.image(image, use_column_width=True)  # 在第一列显示图像
    #use_column_width=True表示使用整列的宽度显示图片
    
    predictions = pipeline(image)  # 使用pipeline进行图像分类

    col2.header("Probabilities")  # 在第二列显示标题
    for p in predictions:
        col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")  # 在第二列显示分类结果及概率