Update utils/object_detection_brainai.py
Browse files
utils/object_detection_brainai.py
CHANGED
|
@@ -1,85 +1,84 @@
|
|
| 1 |
-
from ultralytics import YOLO
|
| 2 |
import cv2
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import PIL
|
|
|
|
| 5 |
import io
|
| 6 |
import tempfile
|
| 7 |
-
import streamlit as st
|
| 8 |
import moviepy.editor as mpy
|
| 9 |
import random
|
| 10 |
|
| 11 |
-
class ObjectDetectionModel():
|
| 12 |
-
|
| 13 |
def __init__(self):
|
| 14 |
self.model = YOLO("models/yolov8n_openvino_model", task = "detect")
|
| 15 |
self.class_names = self.model.names
|
| 16 |
|
| 17 |
with open("utils/game_classes.txt", "r") as file:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def process_image(self, img):
|
| 21 |
|
|
|
|
| 22 |
if isinstance(img, np.ndarray):
|
| 23 |
uploaded_img_cv = img
|
| 24 |
else:
|
| 25 |
uploaded_img = PIL.Image.open(img)
|
| 26 |
-
uploaded_img_cv =
|
| 27 |
-
if uploaded_img_cv.shape[-1] == 4:
|
| 28 |
-
uploaded_img_cv = cv2.cvtColor(uploaded_img_cv, cv2.COLOR_RGBA2RGB)
|
| 29 |
-
|
| 30 |
-
result = self.model(uploaded_img_cv
|
| 31 |
img_plot = result[0].plot()
|
| 32 |
|
| 33 |
detected_classes = set()
|
| 34 |
for box in result[0].boxes:
|
| 35 |
-
class_id = int(box.cls[0])
|
| 36 |
-
class_name = self.class_names[class_id]
|
| 37 |
detected_classes.add(class_name)
|
|
|
|
| 38 |
|
| 39 |
-
return img_plot,
|
| 40 |
-
|
| 41 |
-
def play_video(self,
|
| 42 |
-
uploaded_video = io.BytesIO(
|
| 43 |
-
temporary_location = "upload.mp4"
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
camera = cv2.VideoCapture(temporary_location)
|
| 49 |
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 50 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 51 |
-
processed_frames=[]
|
|
|
|
| 52 |
total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 53 |
-
progress_bar = st.progress(0)
|
| 54 |
frame_count = 0
|
|
|
|
| 55 |
st_frame = st.empty()
|
| 56 |
|
| 57 |
-
while(
|
| 58 |
ret, frame = camera.read()
|
| 59 |
-
|
| 60 |
-
if ret:
|
| 61 |
-
img_plot, _ = self.process_image(frame)
|
| 62 |
-
st_frame.image(img_plot, channels = "BGR")
|
| 63 |
-
processed_frames.append(cv2.cvtColor(img_plot,cv2.COLOR_BGR2RGB))
|
| 64 |
-
frame_count +=1
|
| 65 |
-
progress_bar.progress(frame_count/total_frames, text = None)
|
| 66 |
-
|
| 67 |
-
else:
|
| 68 |
-
camera.release()
|
| 69 |
-
st_frame.empty()
|
| 70 |
-
progress_bar.empty()
|
| 71 |
break
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
camera.release()
|
| 74 |
clip = mpy.ImageSequenceClip(processed_frames, fps=fps)
|
| 75 |
clip.write_videofile(temp_file.name)
|
| 76 |
-
|
| 77 |
st_frame.empty()
|
| 78 |
progress_bar.empty()
|
| 79 |
-
|
| 80 |
return temp_file.name
|
| 81 |
|
| 82 |
def call_class(self):
|
| 83 |
random_class = random.choice(list(self.game_classes))
|
|
|
|
| 84 |
return random_class
|
| 85 |
-
|
|
|
|
|
|
|
| 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 |
import tempfile
|
|
|
|
| 8 |
import moviepy.editor as mpy
|
| 9 |
import random
|
| 10 |
|
| 11 |
+
class ObjectDetectionModel():
|
|
|
|
| 12 |
def __init__(self):
|
| 13 |
self.model = YOLO("models/yolov8n_openvino_model", task = "detect")
|
| 14 |
self.class_names = self.model.names
|
| 15 |
|
| 16 |
with open("utils/game_classes.txt", "r") as file:
|
| 17 |
+
self.game_classes = [line.strip() for line in file if line.strip()]
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
def process(self, img):
|
| 20 |
if isinstance(img, np.ndarray):
|
| 21 |
uploaded_img_cv = img
|
| 22 |
else:
|
| 23 |
uploaded_img = PIL.Image.open(img)
|
| 24 |
+
uploaded_img_cv = np.array(uploaded_img)
|
| 25 |
+
if uploaded_img_cv.shape[-1] == 4:
|
| 26 |
+
uploaded_img_cv = cv2.cvtColor(uploaded_img_cv, cv2.COLOR_RGBA2RGB)
|
| 27 |
+
|
| 28 |
+
result = self.model(uploaded_img_cv)
|
| 29 |
img_plot = result[0].plot()
|
| 30 |
|
| 31 |
detected_classes = set()
|
| 32 |
for box in result[0].boxes:
|
| 33 |
+
class_id = int(box.cls[0])
|
| 34 |
+
class_name = self.class_names[class_id]
|
| 35 |
detected_classes.add(class_name)
|
| 36 |
+
detected_objects = f'Objects Detected: {", ".join(detected_classes) if detected_classes else "No objects detected"}'
|
| 37 |
|
| 38 |
+
return img_plot, detected_objects
|
| 39 |
+
|
| 40 |
+
def play_video(self, video_path):
|
| 41 |
+
uploaded_video = io.BytesIO(video_path.read())
|
| 42 |
+
temporary_location = "upload.mp4"
|
| 43 |
+
|
| 44 |
+
with open(temporary_location, "wb") as temp_out:
|
| 45 |
+
temp_out.write(uploaded_video.read())
|
| 46 |
+
temp_out.close()
|
| 47 |
|
| 48 |
camera = cv2.VideoCapture(temporary_location)
|
| 49 |
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 50 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 51 |
+
processed_frames = []
|
| 52 |
+
|
| 53 |
total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
|
| 54 |
frame_count = 0
|
| 55 |
+
progress_bar = st.progress(0)
|
| 56 |
st_frame = st.empty()
|
| 57 |
|
| 58 |
+
while(True):
|
| 59 |
ret, frame = camera.read()
|
| 60 |
+
if not ret:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
break
|
| 62 |
+
|
| 63 |
+
result = self.model(frame, verbose=False)
|
| 64 |
+
img_plot = result[0].plot()
|
| 65 |
+
img_plot_rgb = cv2.cvtColor(img_plot, cv2.COLOR_BGR2RGB)
|
| 66 |
+
processed_frames.append(img_plot_rgb)
|
| 67 |
+
|
| 68 |
+
st_frame.image(img_plot, channels = "BGR")
|
| 69 |
+
frame_count +=1
|
| 70 |
+
progress_bar.progress(frame_count/total_frames, text = None)
|
| 71 |
+
|
| 72 |
camera.release()
|
| 73 |
clip = mpy.ImageSequenceClip(processed_frames, fps=fps)
|
| 74 |
clip.write_videofile(temp_file.name)
|
| 75 |
+
|
| 76 |
st_frame.empty()
|
| 77 |
progress_bar.empty()
|
| 78 |
+
|
| 79 |
return temp_file.name
|
| 80 |
|
| 81 |
def call_class(self):
|
| 82 |
random_class = random.choice(list(self.game_classes))
|
| 83 |
+
|
| 84 |
return random_class
|
|
|