Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
im about to build video analytic using python. im able to detect object in video using tensorflow. But i cannot get the time when the object appear in video. i have scenario like this :

i have 16 seconds video .
in the 5th second, the object appears on the video.
in the 7th second, the object disappear on the video.
so it will print "Time Calculation "00:00:02"

I tried to use VideoCapture's method get() using property identifier like this:

 video_capture.get(CV_CAP_PROP_POS_MSEC)

Here is the code for tracking the object


What I have tried:

cap = cv2.VideoCapture("video")

while cap.isOpened(): 
  ret, frame = cap.read()
  image_np = np.array(frame)

  input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
  detections = detect_fn(input_tensor)

  num_detections = int(detections.pop('num_detections'))
  detections = {key: value[0, :num_detections].numpy()
              for key, value in detections.items()}
  detections['num_detections'] = num_detections

  # detection_classes should be ints.
  detections['detection_classes'] = 
  detections['detection_classes'].astype(np.int64)

  label_id_offset = 1
  image_np_with_detections = image_np.copy()

  viz_utils.visualize_boxes_and_labels_on_image_array(
        
            image_np_with_detections,
            detections['detection_boxes'],
            detections['detection_classes']+label_id_offset,
            detections['detection_scores'],
            category_index,
            use_normalized_coordinates=True,
            max_boxes_to_draw=5,
            min_score_thresh=.8,
            agnostic_mode=False
            
                                )

  cv2.imshow('object detection',  cv2.resize(image_np_with_detections, (800, 600)))

  if cv2.waitKey(10) & 0xFF == ord('q'):
      cap.release()
      cv2.destroyAllWindows()
      break
Posted
Updated 30-Aug-22 4:22am
v2
Comments
Richard MacCutchan 30-Aug-22 7:31am    
Use the appropriate property of the VideoCapture object to get the time of the frame. See OpenCV: Flags for video I/O[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900