Update utils/object_detection_brainai.py
Browse files
utils/object_detection_brainai.py
CHANGED
|
@@ -16,7 +16,6 @@ class ObjectDetectionModel():
|
|
| 16 |
|
| 17 |
with open("utils/game_classes.txt", "r") as file:
|
| 18 |
self.game_classes = [line.strip() for line in file if line.strip()]
|
| 19 |
-
|
| 20 |
|
| 21 |
def process_image(self, img):
|
| 22 |
|
|
@@ -39,37 +38,38 @@ class ObjectDetectionModel():
|
|
| 39 |
|
| 40 |
return img_plot, f'Objects Detected: {", ".join(detected_classes) if detected_classes else "No objects detected"}'
|
| 41 |
|
| 42 |
-
def play_video(self,
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
temporary_location
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
temp_out.close()
|
| 49 |
|
| 50 |
camera = cv2.VideoCapture(temporary_location)
|
| 51 |
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 52 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 53 |
-
processed_frames
|
| 54 |
-
|
| 55 |
total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 56 |
-
frame_count = 0
|
| 57 |
progress_bar = st.progress(0)
|
|
|
|
| 58 |
st_frame = st.empty()
|
| 59 |
|
| 60 |
-
while(
|
| 61 |
ret, frame = camera.read()
|
| 62 |
-
if not ret:
|
| 63 |
-
break
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
camera.release()
|
| 74 |
clip = mpy.ImageSequenceClip(processed_frames, fps=fps)
|
| 75 |
clip.write_videofile(temp_file.name)
|
|
@@ -78,8 +78,8 @@ class ObjectDetectionModel():
|
|
| 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 |
-
|
|
|
|
| 16 |
|
| 17 |
with open("utils/game_classes.txt", "r") as file:
|
| 18 |
self.game_classes = [line.strip() for line in file if line.strip()]
|
|
|
|
| 19 |
|
| 20 |
def process_image(self, img):
|
| 21 |
|
|
|
|
| 38 |
|
| 39 |
return img_plot, f'Objects Detected: {", ".join(detected_classes) if detected_classes else "No objects detected"}'
|
| 40 |
|
| 41 |
+
def play_video(self, input_video):
|
| 42 |
+
uploaded_video = io.BytesIO(input_video.read())
|
| 43 |
+
temporary_location = "upload.mp4"
|
| 44 |
+
with open(temporary_location, "wb") as out:
|
| 45 |
+
out.write(uploaded_video.read())
|
| 46 |
+
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 |
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(camera.isOpened()):
|
| 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)
|
|
|
|
| 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 |
+
|