Want to capture and use specific frames

Asked 2 years ago, Updated 2 years ago, 315 views

I read a frame from a video to surround the ROI and create a code to track it.

At that time, I would like to capture the frame I like, such as the フレームth frame, not the first frame, but how should I change it?Please let me know.

import cv2

   # KCF
tracker=cv2.TrackerKCF_create()

 

cap=cv2.VideoCapture(r"C:\Users\****\OneDrive\Desktop\MYpython\sample_speed3.mp4")



while True:
       ret, frame = cap.read()
       if not ret:
           continue
       bbox=(0,0,10,10)
       bbox=cv2.selectROI(frame, False)
       ok=tracker.init(frame,bbox)
       cv2.destroyAllWindows()
       break

while True:
       # Read 1 frame from VideoCapture
       ret, frame = cap.read()
       
       if not ret:
           k = cv2.waitKey(1)
           ifk == 27:
               break
           continue

       # Start timer
       timer=cv2.getTickCount()

       # update the tracker
       track,bbox=tracker.update(frame)

       # calculate FPS
       fps = cv2.getTickFrequency()/(cv2.getTickCount()-timer);

       # write a square on the spot where it
       if track:
           # Tracking success
           p1 = (int(bbox[0]), int(bbox[1]))
           p2=(int(bbox[0]+bbox[2]), int(bbox[1]+bbox[3]))
           cv2.rectangle (frame, p1, p2, (0, 255, 0), 2, 1)
           
       else:
           # display a warning when the tracking is off
           cv2.putText(frame, "Failure", (10,50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1, cv2.LINE_AA);


       # View FPS
       cv2.putText(frame, "FPS:"+str(int(fps))), (10,20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1, cv2.LINE_AA);

       # display a processed image
       cv2.imshow("Tracking", frame)

       # Wait 1ms for key input and break if k is 27 (ESC)
       k = cv2.waitKey(1)
       ifk == 27:
           break



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

   # If the frame cannot be retrieved, it goes through the loop.
   if not ret:
       break
# Release Capture and Close All Window
cap.release()
cv2.destroyAllWindows()



print(p1)

python opencv

2022-10-22 09:13

1 Answers

set()

It seems to go to any frame by using the .

Reference Links
https://note.nkmk.me/python-opencv-videocapture-file-camera/
https://algorithm.joho.info/programming/python/opencv-videocapture-mp4-movie-py/


2022-10-22 09:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.