Spaces:
Runtime error
Runtime error
Upload 16 files
Browse files- .gitattributes +6 -0
- data/.ipynb_checkpoints/office-checkpoint.jpg +3 -0
- data/.ipynb_checkpoints/table-checkpoint.jpg +3 -0
- data/breakfast.mp4 +3 -0
- data/classroom.jpg +3 -0
- data/office.jpg +3 -0
- data/table.jpg +3 -0
- models/yolov8n.pt +3 -0
- models/yolov8n_openvino_model/metadata.yaml +98 -0
- models/yolov8n_openvino_model/yolov8n.bin +3 -0
- models/yolov8n_openvino_model/yolov8n.xml +0 -0
- streamlit_app.py +61 -0
- utils/.ipynb_checkpoints/object_detection_brainai-checkpoint.py +99 -0
- utils/.ipynb_checkpoints/object_detection_brainai_gradio-checkpoint.py +60 -0
- utils/__pycache__/object_detection_brainai.cpython-311.pyc +0 -0
- utils/object_detection_brainai.py +99 -0
- utils/object_detection_brainai_gradio.py +60 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,9 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
data/.ipynb_checkpoints/office-checkpoint.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
data/.ipynb_checkpoints/table-checkpoint.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
data/breakfast.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
data/classroom.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
data/office.jpg filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
data/table.jpg filter=lfs diff=lfs merge=lfs -text
|
data/.ipynb_checkpoints/office-checkpoint.jpg
ADDED
|
Git LFS Details
|
data/.ipynb_checkpoints/table-checkpoint.jpg
ADDED
|
Git LFS Details
|
data/breakfast.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c72555727af845f336b68f204186b8012781979dafa7e7abb3a4671590f4049c
|
| 3 |
+
size 1396102
|
data/classroom.jpg
ADDED
|
Git LFS Details
|
data/office.jpg
ADDED
|
Git LFS Details
|
data/table.jpg
ADDED
|
Git LFS Details
|
models/yolov8n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f59b3d833e2ff32e194b5bb8e08d211dc7c5bdf144b90d2c8412c47ccfc83b36
|
| 3 |
+
size 6549796
|
models/yolov8n_openvino_model/metadata.yaml
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
description: Ultralytics YOLOv8n model trained on coco.yaml
|
| 2 |
+
author: Ultralytics
|
| 3 |
+
date: '2025-03-05T09:38:49.501129'
|
| 4 |
+
version: 8.3.62
|
| 5 |
+
license: AGPL-3.0 License (https://ultralytics.com/license)
|
| 6 |
+
docs: https://docs.ultralytics.com
|
| 7 |
+
stride: 32
|
| 8 |
+
task: detect
|
| 9 |
+
batch: 1
|
| 10 |
+
imgsz:
|
| 11 |
+
- 640
|
| 12 |
+
- 640
|
| 13 |
+
names:
|
| 14 |
+
0: person
|
| 15 |
+
1: bicycle
|
| 16 |
+
2: car
|
| 17 |
+
3: motorcycle
|
| 18 |
+
4: airplane
|
| 19 |
+
5: bus
|
| 20 |
+
6: train
|
| 21 |
+
7: truck
|
| 22 |
+
8: boat
|
| 23 |
+
9: traffic light
|
| 24 |
+
10: fire hydrant
|
| 25 |
+
11: stop sign
|
| 26 |
+
12: parking meter
|
| 27 |
+
13: bench
|
| 28 |
+
14: bird
|
| 29 |
+
15: cat
|
| 30 |
+
16: dog
|
| 31 |
+
17: horse
|
| 32 |
+
18: sheep
|
| 33 |
+
19: cow
|
| 34 |
+
20: elephant
|
| 35 |
+
21: bear
|
| 36 |
+
22: zebra
|
| 37 |
+
23: giraffe
|
| 38 |
+
24: backpack
|
| 39 |
+
25: umbrella
|
| 40 |
+
26: handbag
|
| 41 |
+
27: tie
|
| 42 |
+
28: suitcase
|
| 43 |
+
29: frisbee
|
| 44 |
+
30: skis
|
| 45 |
+
31: snowboard
|
| 46 |
+
32: sports ball
|
| 47 |
+
33: kite
|
| 48 |
+
34: baseball bat
|
| 49 |
+
35: baseball glove
|
| 50 |
+
36: skateboard
|
| 51 |
+
37: surfboard
|
| 52 |
+
38: tennis racket
|
| 53 |
+
39: bottle
|
| 54 |
+
40: wine glass
|
| 55 |
+
41: cup
|
| 56 |
+
42: fork
|
| 57 |
+
43: knife
|
| 58 |
+
44: spoon
|
| 59 |
+
45: bowl
|
| 60 |
+
46: banana
|
| 61 |
+
47: apple
|
| 62 |
+
48: sandwich
|
| 63 |
+
49: orange
|
| 64 |
+
50: broccoli
|
| 65 |
+
51: carrot
|
| 66 |
+
52: hot dog
|
| 67 |
+
53: pizza
|
| 68 |
+
54: donut
|
| 69 |
+
55: cake
|
| 70 |
+
56: chair
|
| 71 |
+
57: couch
|
| 72 |
+
58: potted plant
|
| 73 |
+
59: bed
|
| 74 |
+
60: dining table
|
| 75 |
+
61: toilet
|
| 76 |
+
62: tv
|
| 77 |
+
63: laptop
|
| 78 |
+
64: mouse
|
| 79 |
+
65: remote
|
| 80 |
+
66: keyboard
|
| 81 |
+
67: cell phone
|
| 82 |
+
68: microwave
|
| 83 |
+
69: oven
|
| 84 |
+
70: toaster
|
| 85 |
+
71: sink
|
| 86 |
+
72: refrigerator
|
| 87 |
+
73: book
|
| 88 |
+
74: clock
|
| 89 |
+
75: vase
|
| 90 |
+
76: scissors
|
| 91 |
+
77: teddy bear
|
| 92 |
+
78: hair drier
|
| 93 |
+
79: toothbrush
|
| 94 |
+
args:
|
| 95 |
+
batch: 1
|
| 96 |
+
half: false
|
| 97 |
+
int8: false
|
| 98 |
+
dynamic: false
|
models/yolov8n_openvino_model/yolov8n.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:48b86fce278a5a6833dc015b7af8cbcb7174004a5f9b6ff7ec724f753f361681
|
| 3 |
+
size 12708656
|
models/yolov8n_openvino_model/yolov8n.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
streamlit_app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from utils import object_detection_brainai as odb
|
| 3 |
+
object_detection = odb.ObjectDetectionModel()
|
| 4 |
+
|
| 5 |
+
st.set_page_config(
|
| 6 |
+
page_title = "객체 인식",
|
| 7 |
+
page_icon = ":black_cat:",
|
| 8 |
+
layout = "wide")
|
| 9 |
+
|
| 10 |
+
st.title(":blue[Object Detection] :black_cat:")
|
| 11 |
+
|
| 12 |
+
st.sidebar.header("메뉴")
|
| 13 |
+
source_radio = st.sidebar.radio("선택하세요", ["IMAGE", "VIDEO", "GAME", "WEBCAM"])
|
| 14 |
+
|
| 15 |
+
if source_radio == "IMAGE":
|
| 16 |
+
st.write(":green[왼쪽 메뉴 'Browse files' 버튼을 클릭하여 이미지 파일을 선택하면 AI 추론이 시작됩니다.]")
|
| 17 |
+
st.sidebar.header("이미지 파일 업로드")
|
| 18 |
+
input_img = st.sidebar.file_uploader("이미지 파일을 선택하세요.", type=("jpg", "png"))
|
| 19 |
+
|
| 20 |
+
if input_img is not None:
|
| 21 |
+
result_img, detected_object = object_detection.process(input_img)
|
| 22 |
+
col1, col2 = st.columns(2)
|
| 23 |
+
with col1:
|
| 24 |
+
st.image(result_img)
|
| 25 |
+
with col2:
|
| 26 |
+
st.header(detected_object)
|
| 27 |
+
|
| 28 |
+
else:
|
| 29 |
+
col1, col2 = st.columns(2)
|
| 30 |
+
with col1:
|
| 31 |
+
st.image("data/table.jpg")
|
| 32 |
+
with col2:
|
| 33 |
+
st.header("Objected detected: chair, potted plant, vase, dining table")
|
| 34 |
+
|
| 35 |
+
if source_radio == "VIDEO":
|
| 36 |
+
st.write(":green[왼쪽 메뉴 'Browse files' 버튼을 클릭하여 비디오 파일을 선택하면 AI 추론이 시작됩니다.]")
|
| 37 |
+
st.sidebar.header("비디오 파일 업로드")
|
| 38 |
+
input_video = st.sidebar.file_uploader("비디오 파일을 선택하세요.", type=("mp4"))
|
| 39 |
+
if input_video is not None:
|
| 40 |
+
output_video_path = object_detection.play_video(input_video)
|
| 41 |
+
st.video(output_video_path)
|
| 42 |
+
else:
|
| 43 |
+
st.video("data/breakfast.mp4")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
utils/.ipynb_checkpoints/object_detection_brainai-checkpoint.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import numpy as np
|
| 4 |
+
import PIL
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
class ObjectDetectionModel():
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.model = YOLO("models/yolov8n_openvino_model", task = "detect")
|
| 11 |
+
self.class_names = self.model.names
|
| 12 |
+
|
| 13 |
+
def process(self, img):
|
| 14 |
+
if isinstance(img, np.ndarray):
|
| 15 |
+
uploaded_img_cv = img
|
| 16 |
+
else:
|
| 17 |
+
uploaded_img = PIL.Image.open(img)
|
| 18 |
+
uploaded_img_cv = np.array(uploaded_img)
|
| 19 |
+
if uploaded_img_cv.shape[-1] == 4:
|
| 20 |
+
uploaded_img_cv = cv2.cvtColor(uploaded_img_cv, cv2.COLOR_RGBA2RGB)
|
| 21 |
+
|
| 22 |
+
result = self.model(uploaded_img_cv)
|
| 23 |
+
img_plot = result[0].plot()
|
| 24 |
+
|
| 25 |
+
detected_classes = set()
|
| 26 |
+
for cls in result[0].boxes.cls:
|
| 27 |
+
class_id = int(box.cls[0])
|
| 28 |
+
class_name = self.class_names[class_id]
|
| 29 |
+
detected_classes.add(class_name)
|
| 30 |
+
detected_objects = f'Objects Detected: {", ".join(detected_classes) if detected_classes else "No objects detected"}'
|
| 31 |
+
|
| 32 |
+
return img_plot, detected_objects
|
| 33 |
+
|
| 34 |
+
def play_video(self, video_path):
|
| 35 |
+
|
| 36 |
+
uploaded_video = io.BytesIO(video_path.read())
|
| 37 |
+
temporary_location = "upload.mp4"
|
| 38 |
+
with open(temporary_location, "wb") as temp_out:
|
| 39 |
+
temp_out.write(uploaded_video.read())
|
| 40 |
+
temp_out.close()
|
| 41 |
+
|
| 42 |
+
camera = cv2.VideoCapture(temporary_location)
|
| 43 |
+
frame_width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 44 |
+
frame_height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 45 |
+
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 46 |
+
|
| 47 |
+
fourcc = cv2.VideoWriter_fourcc(*'X264')
|
| 48 |
+
output_video_path = 'output_video.mp4'
|
| 49 |
+
out = cv2.VideoWriter(output_video_path,fourcc,fps, (frame_width,frame_height))
|
| 50 |
+
processed_frames = []
|
| 51 |
+
|
| 52 |
+
total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 53 |
+
frame_count = 0
|
| 54 |
+
progress_bar = st.progress(0)
|
| 55 |
+
st_frame = st.empty()
|
| 56 |
+
|
| 57 |
+
while(True):
|
| 58 |
+
ret, frame = camera.read()
|
| 59 |
+
if not ret:
|
| 60 |
+
break
|
| 61 |
+
|
| 62 |
+
result = self.model(frame, verbose=False)
|
| 63 |
+
img_plot = result[0].plot()
|
| 64 |
+
processed_frames.append(img_plot)
|
| 65 |
+
|
| 66 |
+
st_frame.image(img_plot, channels = "BGR")
|
| 67 |
+
frame_count +=1
|
| 68 |
+
progress_bar.progress(frame_count/total_frames, text = None)
|
| 69 |
+
|
| 70 |
+
camera.release()
|
| 71 |
+
for frame in processed_frames:
|
| 72 |
+
out.write(frame)
|
| 73 |
+
out.release()
|
| 74 |
+
|
| 75 |
+
st_frame.empty()
|
| 76 |
+
progress_bar.empty()
|
| 77 |
+
|
| 78 |
+
return output_video_path
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
utils/.ipynb_checkpoints/object_detection_brainai_gradio-checkpoint.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
|
| 4 |
+
class ObjectDetectionModel():
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.model = YOLO("models/yolov8n_openvino_model", task = "detect")
|
| 7 |
+
|
| 8 |
+
def process(self, img):
|
| 9 |
+
result = self.model(img)
|
| 10 |
+
img_plot = result[0].plot()
|
| 11 |
+
|
| 12 |
+
return img_plot
|
| 13 |
+
|
| 14 |
+
def play_video(self, video_path):
|
| 15 |
+
camera = cv2.VideoCapture(video_path)
|
| 16 |
+
frame_width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 17 |
+
frame_height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 18 |
+
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 19 |
+
|
| 20 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 21 |
+
output_video_path = 'output_video.mp4'
|
| 22 |
+
out = cv2.VideoWriter(output_video_path,fourcc,fps, (frame_width,frame_height))
|
| 23 |
+
processed_frames = []
|
| 24 |
+
|
| 25 |
+
while(True):
|
| 26 |
+
ret, frame = camera.read()
|
| 27 |
+
if not ret:
|
| 28 |
+
break
|
| 29 |
+
|
| 30 |
+
result = self.model(frame, verbose=False)
|
| 31 |
+
img_plot = result[0].plot()
|
| 32 |
+
processed_frames.append(img_plot)
|
| 33 |
+
camera.release()
|
| 34 |
+
for frame in processed_frames:
|
| 35 |
+
out.write(frame)
|
| 36 |
+
out.release()
|
| 37 |
+
|
| 38 |
+
return output_video_path
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
utils/__pycache__/object_detection_brainai.cpython-311.pyc
ADDED
|
Binary file (4.98 kB). View file
|
|
|
utils/object_detection_brainai.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import numpy as np
|
| 4 |
+
import PIL
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
class ObjectDetectionModel():
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.model = YOLO("models/yolov8n_openvino_model", task = "detect")
|
| 11 |
+
self.class_names = self.model.names
|
| 12 |
+
|
| 13 |
+
def process(self, img):
|
| 14 |
+
if isinstance(img, np.ndarray):
|
| 15 |
+
uploaded_img_cv = img
|
| 16 |
+
else:
|
| 17 |
+
uploaded_img = PIL.Image.open(img)
|
| 18 |
+
uploaded_img_cv = np.array(uploaded_img)
|
| 19 |
+
if uploaded_img_cv.shape[-1] == 4:
|
| 20 |
+
uploaded_img_cv = cv2.cvtColor(uploaded_img_cv, cv2.COLOR_RGBA2RGB)
|
| 21 |
+
|
| 22 |
+
result = self.model(uploaded_img_cv)
|
| 23 |
+
img_plot = result[0].plot()
|
| 24 |
+
|
| 25 |
+
detected_classes = set()
|
| 26 |
+
for cls in result[0].boxes.cls:
|
| 27 |
+
class_id = int(box.cls[0])
|
| 28 |
+
class_name = self.class_names[class_id]
|
| 29 |
+
detected_classes.add(class_name)
|
| 30 |
+
detected_objects = f'Objects Detected: {", ".join(detected_classes) if detected_classes else "No objects detected"}'
|
| 31 |
+
|
| 32 |
+
return img_plot, detected_objects
|
| 33 |
+
|
| 34 |
+
def play_video(self, video_path):
|
| 35 |
+
|
| 36 |
+
uploaded_video = io.BytesIO(video_path.read())
|
| 37 |
+
temporary_location = "upload.mp4"
|
| 38 |
+
with open(temporary_location, "wb") as temp_out:
|
| 39 |
+
temp_out.write(uploaded_video.read())
|
| 40 |
+
temp_out.close()
|
| 41 |
+
|
| 42 |
+
camera = cv2.VideoCapture(temporary_location)
|
| 43 |
+
frame_width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 44 |
+
frame_height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 45 |
+
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 46 |
+
|
| 47 |
+
fourcc = cv2.VideoWriter_fourcc(*'X264')
|
| 48 |
+
output_video_path = 'output_video.mp4'
|
| 49 |
+
out = cv2.VideoWriter(output_video_path,fourcc,fps, (frame_width,frame_height))
|
| 50 |
+
processed_frames = []
|
| 51 |
+
|
| 52 |
+
total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 53 |
+
frame_count = 0
|
| 54 |
+
progress_bar = st.progress(0)
|
| 55 |
+
st_frame = st.empty()
|
| 56 |
+
|
| 57 |
+
while(True):
|
| 58 |
+
ret, frame = camera.read()
|
| 59 |
+
if not ret:
|
| 60 |
+
break
|
| 61 |
+
|
| 62 |
+
result = self.model(frame, verbose=False)
|
| 63 |
+
img_plot = result[0].plot()
|
| 64 |
+
processed_frames.append(img_plot)
|
| 65 |
+
|
| 66 |
+
st_frame.image(img_plot, channels = "BGR")
|
| 67 |
+
frame_count +=1
|
| 68 |
+
progress_bar.progress(frame_count/total_frames, text = None)
|
| 69 |
+
|
| 70 |
+
camera.release()
|
| 71 |
+
for frame in processed_frames:
|
| 72 |
+
out.write(frame)
|
| 73 |
+
out.release()
|
| 74 |
+
|
| 75 |
+
st_frame.empty()
|
| 76 |
+
progress_bar.empty()
|
| 77 |
+
|
| 78 |
+
return output_video_path
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
utils/object_detection_brainai_gradio.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
|
| 4 |
+
class ObjectDetectionModel():
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.model = YOLO("models/yolov8n_openvino_model", task = "detect")
|
| 7 |
+
|
| 8 |
+
def process(self, img):
|
| 9 |
+
result = self.model(img)
|
| 10 |
+
img_plot = result[0].plot()
|
| 11 |
+
|
| 12 |
+
return img_plot
|
| 13 |
+
|
| 14 |
+
def play_video(self, video_path):
|
| 15 |
+
camera = cv2.VideoCapture(video_path)
|
| 16 |
+
frame_width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 17 |
+
frame_height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 18 |
+
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 19 |
+
|
| 20 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 21 |
+
output_video_path = 'output_video.mp4'
|
| 22 |
+
out = cv2.VideoWriter(output_video_path,fourcc,fps, (frame_width,frame_height))
|
| 23 |
+
processed_frames = []
|
| 24 |
+
|
| 25 |
+
while(True):
|
| 26 |
+
ret, frame = camera.read()
|
| 27 |
+
if not ret:
|
| 28 |
+
break
|
| 29 |
+
|
| 30 |
+
result = self.model(frame, verbose=False)
|
| 31 |
+
img_plot = result[0].plot()
|
| 32 |
+
processed_frames.append(img_plot)
|
| 33 |
+
camera.release()
|
| 34 |
+
for frame in processed_frames:
|
| 35 |
+
out.write(frame)
|
| 36 |
+
out.release()
|
| 37 |
+
|
| 38 |
+
return output_video_path
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|