ake178178 commited on
Commit
2762962
·
verified ·
1 Parent(s): d562899

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -6
app.py CHANGED
@@ -1,9 +1,40 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
3
 
4
- pipe = pipeline('sentiment-analysis')
5
- text = st.text_area('enter some text!')
6
 
7
- if text:
8
- out = pipe(text)
9
- st.json(out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from PIL import Image
3
+ import torch
4
 
5
+ # 载入一个预训练的物体识别模型,这里使用 YOLOv5
6
+ model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
7
 
8
+ def detect_objects(image):
9
+ # 对图像进行处理并通过模型进行预测
10
+ results = model(image)
11
+
12
+ # 解析结果,获取检测到的物体和置信度
13
+ data = results.pandas().xyxy[0]
14
+ return results, data
15
+
16
+ def main():
17
+ st.title("物体识别应用")
18
+
19
+ # 创建一个文件上传器,用户可以上传图片
20
+ uploaded_file = st.file_uploader("请选择一张图片进行物体识别", type=["jpg", "jpeg", "png"])
21
+
22
+ if uploaded_file is not None:
23
+ # 将上传的文件转换为图像
24
+ image = Image.open(uploaded_file)
25
+
26
+ # 显示原始图片
27
+ st.image(image, caption='上传的图片', use_column_width=True)
28
+
29
+ # 检测图片中的物体
30
+ results, data = detect_objects(image)
31
+
32
+ # 显示结果
33
+ st.write("检测结果:")
34
+ st.write(data)
35
+
36
+ # 显示带有检测框的图片
37
+ st.image(results.render()[0], caption='检测结果', use_column_width=True)
38
+
39
+ if __name__ == "__main__":
40
+ main()