Gagan0141 commited on
Commit
239b461
·
verified ·
1 Parent(s): 7bd4cd6

Update 2.py

Browse files
Files changed (1) hide show
  1. 2.py +29 -59
2.py CHANGED
@@ -1,59 +1,29 @@
1
- import streamlit as st
2
- from ultralytics import YOLO
3
- from PIL import Image
4
- import tempfile
5
- import os
6
-
7
- # import pkg_resources
8
-
9
- # def generate_requirements(filename="requirements.txt"):
10
- # """
11
- # Generate a requirements.txt file containing installed pip packages and versions.
12
- # """
13
- # # Get all installed packages
14
- # installed_packages = pkg_resources.working_set
15
- # packages_list = sorted([f"{pkg.key}=={pkg.version}" for pkg in installed_packages])
16
-
17
- # # Write to requirements.txt
18
- # with open(filename, "w") as f:
19
- # for package in packages_list:
20
- # f.write(package + "\n")
21
-
22
- # print(f"Requirements saved to {filename}")
23
-
24
- # if __name__ == "__main__":
25
- # generate_requirements()
26
-
27
-
28
- # Load YOLO model (COCO-pretrained)
29
- model = YOLO('yolov8s.pt') # or 'yolov10s.pt'
30
-
31
- st.title("YOLO Object Detection with Streamlit")
32
- st.write("Upload an image and see object detection results in real-time!")
33
-
34
- # Upload image
35
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
36
-
37
- if uploaded_file is not None:
38
- # Convert uploaded file to PIL image
39
- image = Image.open(uploaded_file).convert("RGB")
40
- st.image(image, caption="Uploaded Image", width='stretch')
41
-
42
- # Temporary save to disk for YOLO inference
43
- with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
44
- temp_path = tmp_file.name
45
- image.save(temp_path)
46
-
47
- # Run YOLO inference
48
- results = model(temp_path)
49
-
50
- # Show detection results in Streamlit
51
- st.image(results[0].plot(), caption="Detected Objects", width='stretch')
52
-
53
- # # Save results to output folder
54
- # output_dir = "outputs"
55
- # os.makedirs(output_dir, exist_ok=True)
56
- # results.save(output_dir)
57
- # st.write(f"Detection results saved in `{output_dir}/`")
58
- # Clean up temporary file
59
- os.remove(temp_path)
 
1
+ import streamlit as st
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+ import tempfile
5
+ import os
6
+
7
+ model = YOLO('yolov8s.pt') # or 'yolov10s.pt'
8
+
9
+ st.title("YOLO Object Detection with Streamlit")
10
+ st.write("Upload an image and see object detection results in real-time!")
11
+
12
+ # Upload image
13
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
14
+
15
+ if uploaded_file is not None:
16
+ # Convert uploaded file to PIL image
17
+ image = Image.open(uploaded_file).convert("RGB")
18
+ st.image(image, caption="Uploaded Image", width='stretch')
19
+
20
+ # Temporary save to disk for YOLO inference
21
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
22
+ temp_path = tmp_file.name
23
+ image.save(temp_path)
24
+
25
+ # Run YOLO inference
26
+ results = model(temp_path)
27
+
28
+ # Show detection results in Streamlit
29
+ st.image(results[0].plot(), caption="Detected Objects", width='stretch')