Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,47 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
return
|
| 13 |
|
| 14 |
def main():
|
| 15 |
-
st.title("
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
for desc in descriptions:
|
| 45 |
-
st.write("- " + desc)
|
| 46 |
-
|
| 47 |
-
time.sleep(0.1)
|
| 48 |
-
|
| 49 |
-
if __name__ == "__main__":
|
| 50 |
main()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
+
import torch
|
| 5 |
from PIL import Image
|
| 6 |
+
import camera # 引入拍照功能
|
| 7 |
import numpy as np
|
| 8 |
+
import cv2
|
| 9 |
|
| 10 |
+
# 物体识别函数
|
| 11 |
+
def detect_objects(image_path):
|
| 12 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # 使用YOLOv5模型
|
| 13 |
+
img = Image.open(image_path)
|
| 14 |
+
results = model(img)
|
| 15 |
+
return results
|
| 16 |
|
| 17 |
def main():
|
| 18 |
+
st.title("摄像头拍照并进行物体识别")
|
| 19 |
+
|
| 20 |
+
# 拍照
|
| 21 |
+
if st.button('拍照'):
|
| 22 |
+
camera.take_picture()
|
| 23 |
+
st.write("照片已拍摄并保存")
|
| 24 |
+
|
| 25 |
+
# 物体识别
|
| 26 |
+
if st.button('物体识别'):
|
| 27 |
+
st.write("正在进行物体识别...")
|
| 28 |
+
image_path = "captured_image.jpg"
|
| 29 |
+
|
| 30 |
+
results = detect_objects(image_path)
|
| 31 |
+
# 显示原始图片
|
| 32 |
+
st.image(image_path, caption='原始图片', use_column_width=True)
|
| 33 |
+
|
| 34 |
+
# 显示识别的结果
|
| 35 |
+
results.render() # 在图片上绘制检测到的物体
|
| 36 |
+
detected_img = Image.fromarray(results.imgs[0])
|
| 37 |
+
|
| 38 |
+
st.image(detected_img, caption='物体识别结果', use_column_width=True)
|
| 39 |
+
|
| 40 |
+
# 显示从左到右的物体列表
|
| 41 |
+
st.write("识别到的物体:")
|
| 42 |
+
objects = results.pandas().xyxy[0]['name'].tolist()
|
| 43 |
+
objects_sorted_by_x = sorted(objects)
|
| 44 |
+
st.write(objects_sorted_by_x)
|
| 45 |
+
|
| 46 |
+
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
main()
|