-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (34 loc) · 1.24 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
46
47
48
49
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from ultralytics import YOLO
from collections import Counter
st.header("Object Detection using YOLOv5s")
img = st.file_uploader("Upload an image.", type=['png','jpg'])
if img is not None:
model = YOLO('yolov5su.pt')
PIL_image = Image.open(img).convert("RGB")
image = np.asarray(PIL_image)
results = model.predict(image)
for result in results:
objects = np.array(result.boxes.cls).astype(int).tolist()
result_array = result.plot()
data = Image.fromarray(result_array)
fig = plt.figure(figsize=(6,5))
plt.imshow(data)
plt.xticks([])
plt.yticks([])
plt.grid(False)
st.write(fig)
object_names = []
for obj in objects:
if obj in result.names:
name = result.names[obj]
object_names.append(name)
unique_object_names = set(object_names)
counted_objects = Counter(object_names)
found = []
for names in unique_object_names:
found.append(' {} ({}) '.format(names, counted_objects[names]).upper())
st.info("Objects Identified: " + ", ".join(found))