Son Tran Hoang
Son Tran Hoang

Reputation: 11

How to detect objects in video with darknet?

I am using darknet to detect objects in image, and it is very helpful. Further more, I also want to detect objects from a video in my computer.

I installed Open CV and my computer does not have GPU. I think that I should change something in my darknet.py file. But what code should I add more? Could you give me a clear instruction? Thank in advance.

Upvotes: 0

Views: 4567

Answers (1)

Ramesh-X
Ramesh-X

Reputation: 5055

If you looked at the darknet.py file carefully, you will find an example on how to extract objects from a given image. Did you try it? If you did not I recommend you to try it first before moving into videos.

Then you will need to move into videos. Here is a link to OpenCV documentation, that they explain how to read video files and streams. From this code you will be able to extract frames of the video.

Then you can use the code in darkent.py and feed the frames to it.

If not there are darkent wrappers that can be useful.
Here is a link to one. There is a documentation that you can read and install it in your machine. It is very easy to use. It already has a example on how to use it on images. You can modify it and work with videos.

import numpy as np
import cv2
import pyyolo

cap = cv2.VideoCapture('vtest.avi')
meta_filepath = "/home/rameshpr/Downloads/darknet_google_server/data/obj.data"
cfg_filepath = "/home/rameshpr/Downloads/darknet_google_server/cfg/yolo-lb.cfg"
weights_filepath = "/home/rameshpr/Downloads/darknet_google_server/backup/yolo-v3.weights"


meta = pyyolo.load_meta(meta_filepath)
net = pyyolo.load_net(cfg_filepath, weights_filepath, False)

while(cap.isOpened()):
    ret, frame = cap.read()
    if not ret:
        break

    yolo_img = pyyolo.array_to_image(frame)
    res = pyyolo.detect(net, meta, yolo_img)

    for r in res:
        cv2.rectangle(frame, r.bbox.get_point(pyyolo.BBox.Location.TOP_LEFT, is_int=True),
                      r.bbox.get_point(pyyolo.BBox.Location.BOTTOM_RIGHT, is_int=True), (0, 255, 0), 2)


    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Try the above code after you finished installing pyyolo.

Upvotes: 1

Related Questions