hhk7734 / tensorflow-yolov4

YOLOv4 Implemented in Tensorflow 2.
MIT License
136 stars 75 forks source link

How to Extract Detected Objects? #43

Closed Adeel-Intizar closed 3 years ago

Adeel-Intizar commented 3 years ago

Hi, Can you tell me how to extract the detected objects or simply crop those objects? Thanks

Adeel-Intizar commented 3 years ago

I tried the following script but it gives the error, what am i missing here?

for i, bbox in enumerate(pred_bboxes):

    c_x = int(bbox[0])
    c_y = int(bbox[1])
    half_w = int(bbox[2] / 2)
    half_h = int(bbox[3] / 2)
    left = c_x - half_w
    top = c_y - half_h
    right = c_x + half_w
    bottom = c_y + half_h
    img = image.copy()[left:top, right:bottom]
    cv2.imwrite(f"crop/result-{i}.jpg", img)
hhk7734 commented 3 years ago

pred_bboxes == Dim(-1, (x, y, w, h, class_id, probability)) and x, y, w, and h are ratios.

    height, width, _ = image.shape
    c_x = int(bbox[0] * width)
    c_y = int(bbox[1] * height)
    half_w = int(bbox[2] * width / 2)
    half_h = int(bbox[3] * height / 2)
    left = c_x - half_w
    top = c_y - half_h
    right = c_x + half_w
    bottom = c_y + half_h
    img = image.copy()[left:top, right:bottom]
    cv2.imwrite(f"crop/result-{i}.jpg", img)
Adeel-Intizar commented 3 years ago

I used the following script, but it still generates the error, is there something wrong with the script?

import cv2
import numpy as np
import tensorflow as tf
from yolov4.tf import YOLOv4

yolo = YOLOv4()
yolo.classes = "classes.names"
yolo.input_size = (640, 480)
yolo.batch_size = 2
dataset = yolo.load_dataset("names.txt", image_path_prefix="images/", dataset_type="yolo")

for i, (images, gt) in enumerate(dataset):
    for j in range(len(images)):
        _candidates = []
        for candidate in gt:
            grid_size = candidate.shape[1:3]
            _candidates.append(
                tf.reshape(
                    candidate[j], shape=(1, grid_size[0] * grid_size[1] * 3, -1)
                )
            )
        candidates = np.concatenate(_candidates, axis=1)

        frame = images[j, ...] * 255
        im = np.copy(frame)
        height, width, _ = im.shape
        # frame = frame.astype(np.uint8)

        bboxes = yolo.candidates_to_pred_bboxes(candidates[0])
        # bboxes = yolo.fit_pred_bboxes_to_original(pred_bboxes, frame.shape)
        if bboxes.shape[-1] == 5:
            bboxes = np.concatenate([bboxes, np.full((*bboxes.shape[:-1], 1), 2.0)], axis=-1)
        else:
            bboxes = np.copy(bboxes)

        for bbox in bboxes:

            c_x = int(bbox[0] * width)
            c_y = int(bbox[1] * height)
            half_w = int(bbox[2] * width / 2)
            half_h = int(bbox[3] * height / 2)
            left = c_x - half_w
            top = c_y - half_h
            right = c_x + half_w
            bottom = c_y + half_h
            img = im.copy()[left:top, right:bottom]
            cv2.imwrite(f"crop/result-{i}.jpg", img)

    if i == 1:
        break

cv2.destroyAllWindows()
Adeel-Intizar commented 3 years ago

Can you test the script on your dataset to check if it is causing error just on my dataset?

hhk7734 commented 3 years ago

line 52 img = im[top:bottom, left:right, :]

import cv2
import numpy as np
import tensorflow as tf
from yolov4.tf import YOLOv4

yolo = YOLOv4()
yolo.classes = "/home/hhk7734/NN/coco.names"
yolo.input_size = (640, 480)
yolo.batch_size = 2

dataset = yolo.load_dataset(
    "/home/hhk7734/NN/val2017.txt",
    image_path_prefix="/home/hhk7734/NN/val2017",
    training=False,
)

for i, (images, gt) in enumerate(dataset):
    for j in range(len(images)):
        _candidates = []
        for candidate in gt:
            grid_size = candidate.shape[1:3]
            _candidates.append(
                tf.reshape(
                    candidate[j], shape=(1, grid_size[0] * grid_size[1] * 3, -1)
                )
            )
        candidates = np.concatenate(_candidates, axis=1)

        frame = images[j, ...] * 255
        im = np.copy(frame)
        height, width, _ = im.shape
        # frame = frame.astype(np.uint8)

        bboxes = yolo.candidates_to_pred_bboxes(candidates[0])
        # bboxes = yolo.fit_pred_bboxes_to_original(pred_bboxes, frame.shape)
        if bboxes.shape[-1] == 5:
            bboxes = np.concatenate(
                [bboxes, np.full((*bboxes.shape[:-1], 1), 2.0)], axis=-1
            )
        else:
            bboxes = np.copy(bboxes)

        for bbox in bboxes:
            c_x = int(bbox[0] * width)
            c_y = int(bbox[1] * height)
            half_w = int(bbox[2] * width / 2)
            half_h = int(bbox[3] * height / 2)
            left = c_x - half_w
            top = c_y - half_h
            right = c_x + half_w
            bottom = c_y + half_h
            img = im[top:bottom, left:right, :]
            cv2.imwrite(f"result-{i}.jpg", img)

    if i == 1:
        break

cv2.destroyAllWindows()
Adeel-Intizar commented 3 years ago

Thank you, Script is fine now :) 👍