Update app.py
Browse files
app.py
CHANGED
@@ -159,9 +159,11 @@ def convert_video_to_mp4(input_path, output_path):
|
|
159 |
def detect_objects_in_video(video_path):
|
160 |
temp_output_path = "/tmp/output_video.mp4"
|
161 |
temp_frames_dir = tempfile.mkdtemp()
|
162 |
-
|
163 |
-
|
|
|
164 |
frame_count = 0
|
|
|
165 |
|
166 |
try:
|
167 |
# Convert video to MP4 if necessary
|
@@ -190,34 +192,38 @@ def detect_objects_in_video(video_path):
|
|
190 |
frame_path = os.path.join(temp_frames_dir, f"frame_{frame_count}.jpg")
|
191 |
cv2.imwrite(frame_path, frame)
|
192 |
|
193 |
-
# Process predictions for frame
|
194 |
predictions = yolo_model.predict(frame_path, confidence=50, overlap=80).json()
|
195 |
|
196 |
-
#
|
197 |
-
|
198 |
for prediction in predictions['predictions']:
|
199 |
class_name = prediction['class']
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
# Update cumulative count for all frames
|
211 |
-
for class_name, count in frame_class_count.items():
|
212 |
all_class_count[class_name] = all_class_count.get(class_name, 0) + count
|
213 |
|
214 |
-
#
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
-
#
|
|
|
218 |
for class_name, count in all_class_count.items():
|
219 |
count_text += f"{class_name}: {count}\n"
|
220 |
-
count_text += f"\nTotal
|
221 |
|
222 |
# Overlay the counts text onto the frame
|
223 |
y_offset = 20
|
|
|
159 |
def detect_objects_in_video(video_path):
|
160 |
temp_output_path = "/tmp/output_video.mp4"
|
161 |
temp_frames_dir = tempfile.mkdtemp()
|
162 |
+
|
163 |
+
all_class_count = {} # To store cumulative counts for all classes (from previous frames)
|
164 |
+
last_frame_objects = {} # To store the objects detected in the last frame
|
165 |
frame_count = 0
|
166 |
+
count_text = "" # Initialize count_text properly
|
167 |
|
168 |
try:
|
169 |
# Convert video to MP4 if necessary
|
|
|
192 |
frame_path = os.path.join(temp_frames_dir, f"frame_{frame_count}.jpg")
|
193 |
cv2.imwrite(frame_path, frame)
|
194 |
|
195 |
+
# Process predictions for the current frame
|
196 |
predictions = yolo_model.predict(frame_path, confidence=50, overlap=80).json()
|
197 |
|
198 |
+
# Get current frame object counts
|
199 |
+
current_frame_objects = {}
|
200 |
for prediction in predictions['predictions']:
|
201 |
class_name = prediction['class']
|
202 |
+
current_frame_objects[class_name] = current_frame_objects.get(class_name, 0) + 1
|
203 |
+
|
204 |
+
# Draw bounding boxes on the frame
|
205 |
+
x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
|
206 |
+
cv2.rectangle(frame, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
|
207 |
+
cv2.putText(frame, class_name, (int(x-w/2), int(y-h/2-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
|
208 |
+
|
209 |
+
# Update all class counts: Add objects detected in this frame and remove objects not detected in this frame
|
210 |
+
for class_name, count in current_frame_objects.items():
|
211 |
+
# Add to all_class_count
|
|
|
|
|
212 |
all_class_count[class_name] = all_class_count.get(class_name, 0) + count
|
213 |
|
214 |
+
# Now check for objects that were in last frame but not in the current frame
|
215 |
+
for class_name in list(all_class_count.keys()):
|
216 |
+
# If the object was not detected in the current frame, reduce its count
|
217 |
+
if class_name not in current_frame_objects:
|
218 |
+
all_class_count[class_name] -= 1
|
219 |
+
if all_class_count[class_name] <= 0:
|
220 |
+
del all_class_count[class_name] # Remove from all_class_count if count drops to 0
|
221 |
|
222 |
+
# Update count_text with current object count
|
223 |
+
count_text = ""
|
224 |
for class_name, count in all_class_count.items():
|
225 |
count_text += f"{class_name}: {count}\n"
|
226 |
+
count_text += f"\nTotal Products: {sum(all_class_count.values())}"
|
227 |
|
228 |
# Overlay the counts text onto the frame
|
229 |
y_offset = 20
|