Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,46 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import tensorflow as tf
|
| 4 |
import numpy as np
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
st.title("物体识别应用")
|
| 8 |
st.write("通过摄像头识别物体,从左到右显示主要物体的名称")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
img = frame.to_ndarray(format="bgr24")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import cv2
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.applications.mobilenet import preprocess_input
|
| 6 |
+
from tensorflow.keras.applications.mobilenet import decode_predictions
|
| 7 |
|
| 8 |
st.title("物体识别应用")
|
| 9 |
st.write("通过摄像头识别物体,从左到右显示主要物体的名称")
|
| 10 |
|
| 11 |
+
# 设置摄像头
|
| 12 |
+
video_capture = cv2.VideoCapture(0)
|
| 13 |
+
stframe = st.empty()
|
| 14 |
|
| 15 |
+
# 加载 MobileNet SSD 预训练模型
|
| 16 |
+
model = tf.keras.applications.MobileNet(weights="imagenet")
|
|
|
|
| 17 |
|
| 18 |
+
def detect_objects(frame):
|
| 19 |
+
# 预处理图像
|
| 20 |
+
image_resized = cv2.resize(frame, (224, 224))
|
| 21 |
+
image_array = np.expand_dims(image_resized, axis=0)
|
| 22 |
+
processed_image = preprocess_input(image_array)
|
| 23 |
+
|
| 24 |
+
# 使用模型进行预测
|
| 25 |
+
preds = model.predict(processed_image)
|
| 26 |
+
decoded_preds = decode_predictions(preds, top=3)[0] # 取前3个结果
|
| 27 |
+
objects = [f"{label}: {round(score * 100, 2)}%" for (_, label, score) in decoded_preds]
|
| 28 |
+
return objects
|
| 29 |
|
| 30 |
+
# 读取摄像头流并显示
|
| 31 |
+
while True:
|
| 32 |
+
ret, frame = video_capture.read()
|
| 33 |
+
if not ret:
|
| 34 |
+
st.write("无法读取摄像头数据。")
|
| 35 |
+
break
|
| 36 |
|
| 37 |
+
# 检测物体
|
| 38 |
+
objects = detect_objects(frame)
|
| 39 |
+
detected_text = " | ".join(objects)
|
| 40 |
|
| 41 |
+
# 显示检测结果
|
| 42 |
+
cv2.putText(frame, detected_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2, cv2.LINE_AA)
|
| 43 |
+
|
| 44 |
+
# 将 OpenCV 图像格式转换为 Streamlit 显示格式
|
| 45 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 46 |
+
stframe.image(frame_rgb, caption="检测到的物体", channels="RGB")
|