Update utils/object_detection_brainai.py
Browse files
utils/object_detection_brainai.py
CHANGED
|
@@ -39,7 +39,7 @@ class ObjectDetectionModel():
|
|
| 39 |
|
| 40 |
return img_plot, f'Objects Detected: {", ".join(detected_classes) if detected_classes else "No objects detected"}'
|
| 41 |
|
| 42 |
-
def
|
| 43 |
uploaded_video = io.BytesIO(input_video.read())
|
| 44 |
temporary_location = "upload.mp4"
|
| 45 |
with open(temporary_location, "wb") as out:
|
|
@@ -75,6 +75,53 @@ class ObjectDetectionModel():
|
|
| 75 |
|
| 76 |
return temp_file.name
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
def call_class(self):
|
| 79 |
random_class = random.choice(list(self.game_classes))
|
| 80 |
return random_class
|
|
|
|
| 39 |
|
| 40 |
return img_plot, f'Objects Detected: {", ".join(detected_classes) if detected_classes else "No objects detected"}'
|
| 41 |
|
| 42 |
+
def play_video_orig(self, input_video):
|
| 43 |
uploaded_video = io.BytesIO(input_video.read())
|
| 44 |
temporary_location = "upload.mp4"
|
| 45 |
with open(temporary_location, "wb") as out:
|
|
|
|
| 75 |
|
| 76 |
return temp_file.name
|
| 77 |
|
| 78 |
+
def play_video(self, video_path):
|
| 79 |
+
|
| 80 |
+
uploaded_video = io.BytesIO(video_path.read())
|
| 81 |
+
temporary_location = "upload.mp4"
|
| 82 |
+
with open(temporary_location, "wb") as temp_out:
|
| 83 |
+
temp_out.write(uploaded_video.read())
|
| 84 |
+
temp_out.close()
|
| 85 |
+
|
| 86 |
+
camera = cv2.VideoCapture(temporary_location)
|
| 87 |
+
frame_width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 88 |
+
frame_height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 89 |
+
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 90 |
+
|
| 91 |
+
fourcc = cv2.VideoWriter_fourcc(*'X264')
|
| 92 |
+
output_video_path = 'output_video.mp4'
|
| 93 |
+
out = cv2.VideoWriter(output_video_path,fourcc,fps, (frame_width,frame_height))
|
| 94 |
+
processed_frames = []
|
| 95 |
+
|
| 96 |
+
total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 97 |
+
frame_count = 0
|
| 98 |
+
progress_bar = st.progress(0)
|
| 99 |
+
st_frame = st.empty()
|
| 100 |
+
|
| 101 |
+
while(True):
|
| 102 |
+
ret, frame = camera.read()
|
| 103 |
+
if not ret:
|
| 104 |
+
break
|
| 105 |
+
|
| 106 |
+
result = self.model(frame, verbose=False)
|
| 107 |
+
img_plot = result[0].plot()
|
| 108 |
+
processed_frames.append(img_plot)
|
| 109 |
+
|
| 110 |
+
st_frame.image(img_plot, channels = "BGR")
|
| 111 |
+
frame_count +=1
|
| 112 |
+
progress_bar.progress(frame_count/total_frames, text = None)
|
| 113 |
+
|
| 114 |
+
camera.release()
|
| 115 |
+
for frame in processed_frames:
|
| 116 |
+
out.write(frame)
|
| 117 |
+
out.release()
|
| 118 |
+
|
| 119 |
+
st_frame.empty()
|
| 120 |
+
progress_bar.empty()
|
| 121 |
+
|
| 122 |
+
return output_video_path
|
| 123 |
+
|
| 124 |
+
|
| 125 |
def call_class(self):
|
| 126 |
random_class = random.choice(list(self.game_classes))
|
| 127 |
return random_class
|