import re import ast import cv2 example_output = "In frame 06, a adult in grey clothes is on the right of the baby at position [0.397, 0.006, 0.716, 0.998]. The adult continues to be on the right of the baby until frame 99, where the adult is positioned at [0.397, 0.006, 0.716, 0.998]." def search_a_file_in_directory(directory, file_name): import os for root, dirs, files in os.walk(directory): if file_name in files: return os.path.join(root, file_name) return None def extract_frame_and_position(output: str): # Find the first frame number in the string match = re.search(r'frame (\d+)', output) if match: # Parse frame number frame0 = int(match.group(1)) # Remove the matched substring from the string output = re.sub(r'frame (\d+)', '', output, count=1) # Find the first position in the string match = re.search(r'position (\[.*?\])', output) if match: # Parse position position0 = ast.literal_eval(match.group(1)) # Remove the matched substring from the string output = re.sub(r'position (\[.*?\])', '', output, count=1) return [[frame0, position0],[1, 2]] def main(N = 100): vidor_path_base = 'vidor/train/video' video_path = search_a_file_in_directory(vidor_path_base, '5541679096.mp4') results = extract_frame_and_position(example_output) frame0 = results[0][0] bbox0 = results[0][1] frame1 = results[1][0] bbox1 = results[1][1] cap = cv2.VideoCapture(video_path) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) real_frame0 = int(frame0 * total_frames / N) real_frame1 = int(frame1 * total_frames / N) cap.set(cv2.CAP_PROP_POS_FRAMES, real_frame0) ret, frame = cap.read() if ret: height, width = frame.shape[:2] xmin = int(bbox0[0] * width) ymin = int(bbox0[1] * height) xmax = int(bbox0[2] * width) ymax = int(bbox0[3] * height) cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2) # Display the frame cv2.imshow('Frame', frame) cv2.waitKey(0) cv2.destroyAllWindows() cap.release() if __name__ == "__main__": main()