Skip to content

Commit 89fbe88

Browse files
committed
update
1 parent 520c3f7 commit 89fbe88

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

‎opencv/read_frame.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,29 @@ def process_frame(video_path, start_frame, end_frame, output_list, lock):
1616
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
1717

1818
for _ in range(start_frame, end_frame):
19-
# 获取帧
19+
# 获取帧。grap 好像不能跳帧 cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
2020
ret, _ = cap.read()
21-
if ret is False:
21+
if not ret:
22+
logging.info(f"Thread {start_frame}: failed to retrieve frame")
2223
break
23-
2424
# 获取时间戳
2525
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000.0
2626
thread_output.append(
2727
f"{time.ctime()} Thread {start_frame}: Timestamp - {timestamp}")
2828

2929
# 设置视频流的位置,直接跳过一些帧
30-
cap.set(cv2.CAP_PROP_POS_FRAMES, cap.get(cv2.CAP_PROP_POS_FRAMES) + 60)
30+
cap.set(cv2.CAP_PROP_POS_FRAMES, cap.get(cv2.CAP_PROP_POS_FRAMES) + 20)
3131

3232
if cap.get(cv2.CAP_PROP_POS_FRAMES) >= end_frame:
33+
logging.info(f"Thread {start_frame}: reached end frame")
3334
break
3435

3536
# 将每个线程的输出添加到共享的列表中
3637
# 使用锁确保对共享列表的安全访问
3738
with lock:
3839
output_list.extend(thread_output)
3940
cap.release() # 关闭视频文件
41+
logging.info(f"Thread {start_frame}: finishing")
4042

4143

4244
def main(video_path, num_threads):
@@ -58,7 +60,7 @@ def main(video_path, num_threads):
5860
lock = threading.Lock()
5961

6062
# 使用线程池
61-
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
63+
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
6264
# 提交帧处理任务给线程池
6365
futures = [executor.submit(process_frame, video_path, start, end, output_list, lock)
6466
for start, end in frame_ranges]

0 commit comments

Comments
 (0)