File size: 1,647 Bytes
9388bc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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}")