Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused code and assign time/date randomly to ingested video #20

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 15 additions & 72 deletions VideoRAGQnA/embedding/extract_store_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ def calculate_intervals(video_path, chunk_duration, clip_duration):

cap.release()
return intervals


def random_date_in_past_6_months():
# Get the current date and time
now = datetime.datetime.now()

# Calculate the date 6 months ago from now
six_months_ago = now - datetime.timedelta(days=6*30)

# Generate a random date between six_months_ago and now
random_date = six_months_ago + (now - six_months_ago) * random.random()

return random_date

def process_all_videos(config):
path = config['videos']
meta_output_dir = config['meta_output_dir']
Expand All @@ -41,75 +53,6 @@ def process_all_videos(config):
chunk_duration = config['chunk_duration']
clip_duration = config['clip_duration']

def extract_frames(video_path, meta_output_dir, date_time, local_timezone):
video = os.path.splitext(os.path.basename(video_path))[0]
# Create a directory to store frames and metadata
os.makedirs(meta_output_dir, exist_ok=True)

# Open the video file
cap = cv2.VideoCapture(video_path)

if int(cv2.__version__.split('.')[0]) < 3:
fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
else:
fps = cap.get(cv2.CAP_PROP_FPS)

total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)

#print (f'fps {fps}')
#print (f'total frames {total_frames}')

mod = int(fps // N)
if mod == 0: mod = 1

print (f'total frames {total_frames}, N {N}, mod {mod}')

# Variables to track frame count and desired frames
frame_count = 0

# Metadata dictionary to store timestamp and image paths
metadata = {}

while cap.isOpened():
ret, frame = cap.read()

if not ret:
break

frame_count += 1

if frame_count % mod == 0:
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # Convert milliseconds to seconds
frame_path = os.path.join(image_output_dir, f"{video}_{frame_count}.jpg")
time = date_time.strftime("%H:%M:%S")
date = date_time.strftime("%Y-%m-%d")
hours, minutes, seconds = map(float, time.split(":"))
year, month, day = map(int, date.split("-"))

cv2.imwrite(frame_path, frame) # Save the frame as an image


metadata[frame_count] = {"timestamp": timestamp, "frame_path": frame_path,"date": date, "year": year, "month": month, "day": day,
"time": time, "hours": hours, "minutes": minutes, "seconds": seconds}
if selected_db == 'vdms':
# Localize the current time to the local timezone of the machine
#Tahani might not need this
current_time_local = date_time.replace(tzinfo=datetime.timezone.utc).astimezone(local_timezone)

# Convert the localized time to ISO 8601 format with timezone offset
iso_date_time = current_time_local.isoformat()
metadata[frame_count]['date_time'] = {"_date": str(iso_date_time)}

# Save metadata to a JSON file
metadata_file = os.path.join(meta_output_dir, f"{video}_metadata.json")
with open(metadata_file, "w") as f:
json.dump(metadata, f, indent=4)

# Release the video capture and close all windows
cap.release()
print(f"{frame_count/mod} Frames extracted and metadata saved successfully.")
return fps, total_frames, metadata_file

videos = [file for file in os.listdir(path) if file.endswith('.mp4')] # TODO: increase supported video formats

# print (f'Total {len(videos)} videos will be processed')
Expand All @@ -119,8 +62,8 @@ def extract_frames(video_path, meta_output_dir, date_time, local_timezone):
metadata[each_video] = {}
keyname = each_video
video_path = os.path.join(path, each_video)
date_time = datetime.datetime.now() # FIXME CHECK: is this correct?
#date_time = t.ctime(os.stat(video_path).st_ctime)
# for current datetime use datetime.datetime.now()
date_time = random_date_in_past_6_months()
# Get the local timezone of the machine
local_timezone = get_localzone()
if emb_type == 'video':
Expand Down
2 changes: 1 addition & 1 deletion VideoRAGQnA/video-rag-ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def display_messages():
'Was there any person wearing a blue shirt seen in the last 6 hours?',
'Was there any person wearing a blue shirt seen last Sunday?',
'Was a person wearing glasses seen in the last 30 minutes?',
'Was a person wearing glasses seen in the last 72 hours?',
'Was a person wearing glasses seen in the last 6 months?',
),
key='example_video'
)
Expand Down
Loading