yasserrmd commited on
Commit
6437c14
1 Parent(s): 15b92fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -47,17 +47,22 @@ def generate_journal_with_images(video_path, frame_interval=30,confidence_thresh
47
  journal_entries = []
48
  image_paths = []
49
  frame_count = 0
50
- last_processed_frame = None
51
  output_folder = "detected_frames"
52
  os.makedirs(output_folder, exist_ok=True) # Create folder to store images
 
 
53
 
54
  while cap.isOpened():
55
  ret, frame = cap.read()
56
  if not ret:
57
  break
58
 
59
- # Process every Nth frame or if the current frame is different from the last processed frame
60
- if frame_count % frame_interval == 0 or (last_processed_frame is not None and is_frame_different(last_processed_frame, frame)):
 
 
 
 
61
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
62
 
63
  # Make predictions using YOLOv10 on the current frame
@@ -80,23 +85,20 @@ def generate_journal_with_images(video_path, frame_interval=30,confidence_thresh
80
  cv2.imwrite(frame_filename, annotated_frame[:, :, ::-1]) # Convert back to BGR for saving
81
  image_paths.append(frame_filename)
82
 
83
- # Get current timestamp in the video
84
- timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # Convert ms to seconds
85
-
86
  # Categorize the detected objects into activities
87
  activity_summary = categorize_activity(detected_objects)
88
 
89
  # Store the activities with their timestamp
90
  for activity, objects in activity_summary.items():
91
- journal_entries.append(f"At {timestamp:.2f} seconds: {', '.join(objects[0])}")
92
 
93
- last_processed_frame = frame # Update the last processed frame
94
 
95
  frame_count += 1
96
 
97
  cap.release()
98
 
99
- return journal_entries, image_paths
100
 
101
 
102
  def display_journal_with_images(video):
 
47
  journal_entries = []
48
  image_paths = []
49
  frame_count = 0
 
50
  output_folder = "detected_frames"
51
  os.makedirs(output_folder, exist_ok=True) # Create folder to store images
52
+
53
+ last_processed_second = -1 # Keep track of the last processed second
54
 
55
  while cap.isOpened():
56
  ret, frame = cap.read()
57
  if not ret:
58
  break
59
 
60
+ # Get the current timestamp in the video
61
+ current_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # Convert ms to seconds
62
+ current_second = int(current_time) # Round down to the nearest second
63
+
64
+ # Process only one frame per second
65
+ if current_second > last_processed_second:
66
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
67
 
68
  # Make predictions using YOLOv10 on the current frame
 
85
  cv2.imwrite(frame_filename, annotated_frame[:, :, ::-1]) # Convert back to BGR for saving
86
  image_paths.append(frame_filename)
87
 
 
 
 
88
  # Categorize the detected objects into activities
89
  activity_summary = categorize_activity(detected_objects)
90
 
91
  # Store the activities with their timestamp
92
  for activity, objects in activity_summary.items():
93
+ journal_entries.append(f"At {current_time:.2f} seconds: {', '.join(objects[0])}")
94
 
95
+ last_processed_second = current_second # Update the last processed second
96
 
97
  frame_count += 1
98
 
99
  cap.release()
100
 
101
+ return journal_entries, image_paths
102
 
103
 
104
  def display_journal_with_images(video):