ake178178 commited on
Commit
caa944e
·
verified ·
1 Parent(s): 23bd323

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -43
app.py CHANGED
@@ -1,50 +1,47 @@
 
 
1
  import streamlit as st
2
- import cv2
3
  from PIL import Image
4
- import time
5
  import numpy as np
 
6
 
7
- # Placeholder function for analyzing images and returning descriptions
8
- def analyze_image(image):
9
- # Implement object recognition here
10
- # This function should return a list of descriptions for detected objects
11
- # For example:
12
- return ["chair on the left", "table in the center", "cat on the right"]
13
 
14
  def main():
15
- st.title("Object Recognition Assistant for the Visually Impaired")
16
-
17
- # Setup webcam capture
18
- cap = cv2.VideoCapture(0) # Use 0 for the default webcam
19
-
20
- FRAME_WINDOW = st.image([])
21
- last_time = time.time()
22
-
23
- while True:
24
- ret, frame = cap.read()
25
- if not ret:
26
- continue
27
-
28
- # Convert the image color to RGB
29
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
30
- img = Image.fromarray(frame)
31
-
32
- # Display the current frame
33
- FRAME_WINDOW.image(img)
34
-
35
- # Check if 10 seconds have passed
36
- if time.time() - last_time > 10:
37
- last_time = time.time()
38
-
39
- # Analyze the image and get descriptions
40
- descriptions = analyze_image(img)
41
-
42
- # Display the descriptions
43
- st.write("Detected objects:")
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()