Search code examples
python-3.xopencvtensorflowdarkflow

Write the coordinates of detected bounding boxes in a video to a txt or csv file


so i am using darkflow to detect objects(hats) in a video. It detects people wearing the hats and draws bounding boxes around the hat in a video. Now I want to save the top right and bottom left coordinates of the detected bounding boxes into a txt or csv file for further processing. I wrote the code in opencv-python. I can display the video and it draws bounding box successfully but i don't know how to save the coordinates of the boxes. Any idea how to do it?

I am using Mark jay's code for my purpose

#import libraries
import cv2
from darkflow.net.build import TFNet
import numpy as np
import time

#load model and weights and threshold 
option = {
    'model': 'cfg/yolo-5c.cfg',
    'load': 'bin/yolo.weights',
    'threshold': 0.15,
    'gpu': 1.0
}


tfnet = TFNet(option)

#open video file 
capture = cv2.VideoCapture('videofile_1080_20fps.avi')
colors = [tuple(255 * np.random.rand(3)) for i in range(5)]

#read video file and set parameters for object detection 
while (capture.isOpened()):
    stime = time.time()
    ret, frame = capture.read()
    if ret:
        results = tfnet.return_predict(frame) 
        for color, result in zip(colors, results):
            tl = (result['topleft']['x'], result['topleft']['y']) # show top left coordinate 
            br = (result['bottomright']['x'], result['bottomright']['y']) #show bottom right coordinate
            label = result['label'] # show label
            frame = cv2.rectangle(frame, tl, br, color, 7) 
            frame = cv2.putText(frame, label, tl, cv2.FONT_HERSHEY_COMPLEX, 
1, (0, 0, 0), 2)
        cv2.imshow('frame', frame)
        print('FPS {:.1f}'.format(1 / (time.time() - stime)))
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        capture.release()
        cv2.destroyAllWindows()
        break

So as you can see i can display the video and detect object which draws bounding boxes. Now my goal is to save the pixel coordinates of these bounding boxes, just the top left and bottom right one. Any ideas guys?


Solution

  • The 'result' parameter in your code is the set of coordinates. You make a list and append it with the values from the result and then in your 'else' write it to a .txt file.

    Cheers!