The Real Banana
The Real Banana

Reputation: 31

yolo v3 how to extract an image of a detected object

I'm working on a project to detect objects, and I'm working on extracting images of objects that are getting detected by yolo v3 using Anaconda. This is how I installed Python and got yolo v3 running: https://github.com/reigngt09/yolov3workflow/tree/master/2_YoloV3_Execute The problem is I do not have any knowledge in Python. Is it possible to extract images and store them in a separate file while the video is running?

Upvotes: 1

Views: 5401

Answers (1)

Roy Lee
Roy Lee

Reputation: 329

This is one of available method.

  1. Extract images from Video
  2. Call detect function per each images
  3. Store each images with bounding box

below code I used,

import cv2
import detect as dt
from darknet import Darknet
from PIL import Image

vidcap = cv2.VideoCapture('your/video/path.mp4')
success, image = vidcap.read()
count = 0

m = Darknet('your/cfg/file/path.cfg')
m.load_weights('weight/file/path.weights')
use_cuda = 1
m.cuda()

while success:
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    im_pil = Image.fromarray(image)
    im_pil = im_pil.resize((m.width, m.height))
    boxes = dt.do_detect(m, im_pil, 0.5, 0.4, use_cuda)

    result = open('your/save/file/path/frame%04d.txt'%(count), 'w')
    for i in range(len(boxes)):
        result.write(boxes[i])
    count = count + 1
    success, image = vidcap.read()
    result.close()

Upvotes: 4

Related Questions