Update utils/object_detection_brainai.py
Browse files
utils/object_detection_brainai.py
CHANGED
|
@@ -39,42 +39,6 @@ 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_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:
|
| 46 |
-
out.write(uploaded_video.read())
|
| 47 |
-
out.close()
|
| 48 |
-
|
| 49 |
-
camera = cv2.VideoCapture(temporary_location)
|
| 50 |
-
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 51 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 52 |
-
processed_frames=[]
|
| 53 |
-
total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 54 |
-
progress_bar = st.progress(0)
|
| 55 |
-
frame_count = 0
|
| 56 |
-
|
| 57 |
-
st_frame = st.empty()
|
| 58 |
-
while(camera.isOpened()):
|
| 59 |
-
ret, frame = camera.read()
|
| 60 |
-
|
| 61 |
-
if ret:
|
| 62 |
-
img_plot, _ = self.process_image(frame)
|
| 63 |
-
st_frame.image(img_plot, channels = "BGR")
|
| 64 |
-
processed_frames.append(cv2.cvtColor(img_plot,cv2.COLOR_BGR2RGB))
|
| 65 |
-
frame_count +=1
|
| 66 |
-
progress_bar.progress(frame_count/total_frames, text = None)
|
| 67 |
-
|
| 68 |
-
else:
|
| 69 |
-
camera.release()
|
| 70 |
-
st_frame.empty()
|
| 71 |
-
progress_bar.empty()
|
| 72 |
-
break
|
| 73 |
-
clip = mpy.ImageSequenceClip(processed_frames,fps=fps)
|
| 74 |
-
clip.write_videofile(temp_file.name)
|
| 75 |
-
|
| 76 |
-
return temp_file.name
|
| 77 |
-
|
| 78 |
def play_video(self, video_path):
|
| 79 |
|
| 80 |
uploaded_video = io.BytesIO(video_path.read())
|
|
@@ -84,13 +48,8 @@ class ObjectDetectionModel():
|
|
| 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))
|
|
@@ -112,15 +71,13 @@ class ObjectDetectionModel():
|
|
| 112 |
progress_bar.progress(frame_count/total_frames, text = None)
|
| 113 |
|
| 114 |
camera.release()
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
out.release()
|
| 118 |
|
| 119 |
st_frame.empty()
|
| 120 |
progress_bar.empty()
|
| 121 |
|
| 122 |
-
return
|
| 123 |
-
|
| 124 |
|
| 125 |
def call_class(self):
|
| 126 |
random_class = random.choice(list(self.game_classes))
|
|
|
|
| 39 |
|
| 40 |
return img_plot, f'Objects Detected: {", ".join(detected_classes) if detected_classes else "No objects detected"}'
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def play_video(self, video_path):
|
| 43 |
|
| 44 |
uploaded_video = io.BytesIO(video_path.read())
|
|
|
|
| 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))
|
|
|
|
| 71 |
progress_bar.progress(frame_count/total_frames, text = None)
|
| 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))
|