hengc commited on
Commit
b3f7adb
·
verified ·
1 Parent(s): 55c4c43

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from ultralytics import YOLO
3
+ import tempfile
4
+ import pandas as pd
5
+
6
+
7
+ model = YOLO('best.pt')
8
+
9
+ st.title('Spare-it Segmentation Model')
10
+
11
+ input_method = st.radio("Choose the input method:", ("Upload an Image", "Take a Picture"))
12
+
13
+ if input_method == "Upload an Image":
14
+ image_data = st.file_uploader("Upload an image", type=['jpg', 'jpeg', 'png'])
15
+
16
+ elif input_method == "Take a Picture":
17
+ image_data = st.camera_input("Take a picture")
18
+
19
+ if image_data is not None:
20
+ # Create a temporary file to store the input image
21
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp_file:
22
+ tmp_file.write(image_data.getvalue())
23
+ image_path = tmp_file.name
24
+
25
+ results = model(image_path)
26
+
27
+ category_names = results[0].names
28
+
29
+ predictions = {}
30
+ for cls_id, conf in zip(results[0].boxes.cls, results[0].boxes.conf):
31
+ cls_id = int(cls_id)
32
+ conf = float(conf)
33
+ class_name = category_names[cls_id]
34
+ if class_name in predictions:
35
+ predictions[class_name].append(conf)
36
+ else:
37
+ predictions[class_name] = [conf]
38
+
39
+ num_masks = len(results[0].masks.masks)
40
+
41
+ st.write(f"Total {num_masks} objects found.")
42
+ for category, confidences in predictions.items():
43
+ st.write(f"{len(confidences)} {category}: {['{:.2f}'.format(c) for c in confidences]}")
44
+
45
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as output_tmp:
46
+ for result in results:
47
+ result.save(save_dir=output_tmp.name)
48
+ st.image(output_tmp.name, caption='Segmented Image', use_column_width=True)
49
+