8-calves / object_detector_benchmark /create_yolo_testset.py
Tony Fang
edited .pkl and added files to create the dir for obj detection
9388bc0
raw
history blame contribute delete
1.65 kB
import cv2
import os
import pandas as pd
from tqdm import tqdm
# Path to the input video
video_path = "../pmfeed_4_3_16.mp4"
# Directory to save frames
output_dir = "8_calves_yolo/test/images"
os.makedirs(output_dir, exist_ok=True)
# Load the video
video_capture = cv2.VideoCapture(video_path)
frame_number = 1
with tqdm(total=67760, desc="Extracting Frames") as pbar:
while True:
# Read the frame
ret, frame = video_capture.read()
# If no frame is returned, the video has ended
if not ret:
break
# Save the frame with a sequential name
frame_filename = os.path.join(output_dir, f"frame_{frame_number:05d}.png")
cv2.imwrite(frame_filename, frame)
# Increment the frame number
frame_number += 1
pbar.update(1)
# Release the video capture
video_capture.release()
print(f"Frames saved to: {output_dir}")
output_dir = "8_calves_yolo/test/labels"
os.makedirs(output_dir, exist_ok=True)
df = pd.read_pickle("../pmfeed_4_3_16_bboxes_and_labels.pkl")
df.reset_index(drop=True, inplace=True)
i = 0
j = 0
frame_id = 1
with tqdm(total=len(df)) as pbar:
while i < len(df):
while j < len(df) and df.loc[j]["frame_id"] == df.loc[i]["frame_id"]:
j += 1
pbar.update(1)
temp = df.loc[i:j - 1]
temp.to_csv(
f'{output_dir}/frame_{frame_id:05d}.txt',
header=None,
index=None,
sep=" ",
mode="w",
columns=["class", "x", "y", "w", "h"]
)
frame_id += 1
i = j
print(f"Labels saved to: {output_dir}")