0

I'm using ImageAI and ResNet50 model to detect and extract pictures of people from photos. It all works very well, but I can't figure out how to disable the detection box from being drawn. Obviously, when multiple people are in the same shot the detection box from one then bleeds onto extracted images of other people.

Does anyone know how to do it? I've looked around and could only found one other question with the same issue and for some reason the answer is talking about OpenCV instead of imageai.

My code:

from pathlib import Path

import cv2
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications.resnet50 import ResNet50

from imageai.Detection import ObjectDetection

physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath('resnet50_coco_best_v2.1.0.h5')
detector.loadModel()
person = detector.CustomObjects(person=True)

source_folder = Path(r'd:/pics')
files = [f for f in source_folder.glob('*.jpg')]
output_folder = Path('c:/python/datasets/raw/extracted_images')

for idx, i in enumerate(files):
    print(f'Extracting images from: {i.name}')
    image = i 
    output_path = output_folder.joinpath(f'{idx}.jpg')
    detected_image, detections = detector.detectCustomObjectsFromImage(input_image=str(image),
                                                                       output_image_path=str(output_path),
                                                                       minimum_percentage_probability=20,
                                                                       input_type='file',
                                                                       output_type='file',
                                                                       display_percentage_probability=False,
                                                                       extract_detected_objects=True,
                                                                       custom_objects=person)

EDIT: I've found a hacky way to fix the issue - by going to the source of detectObjetcFromImage() function and commenting out this bit:

# image_copy = draw_boxes(image_copy, 
#                 box_points,
#                 display_box,
#                 label, 
#                 percentage_probability, 
#                 self.__box_color)

Is there a less intrusive way of dealing with this?

NotAName
  • 3,821
  • 2
  • 29
  • 44
  • What do you want plotted, just the name and the probability? – Tom McLean Jun 10 '21 at 11:30
  • Nothing, just want to extract the portion of the image inside the detection box but without drawing the detection boxes on the source image. @TomMcLean – NotAName Jun 10 '21 at 11:32
  • Then why do you need to plot anything? Could you just not plot the original image file? – Tom McLean Jun 10 '21 at 11:33
  • @TomMcLean, I'm not plotting anything but I guess the way `imageai` does this is it first draws detection boxes on the source image and *then* extracts detected regions, which happens to be with boxes drawn on them. – NotAName Jun 10 '21 at 11:34
  • you would have to check in doc if it has other methods to get only regions and then you could cut off images on your own. You could also check in source code how it works and eventually you could copy some part of code to create own function for cutting of image. – furas Jun 10 '21 at 14:21
  • in [documentation](https://github.com/OlafenwaMoses/ImageAI/tree/master/imageai/Detection#objectextraction) I see `eachObject["box_points"]` which suggests that you can get positions of detected objects and you could cut off them on your own. There is also `.detectObjectsFromImage( ..., extract_detected_objects=True)` that suggests that it can save detected object in separated files. – furas Jun 10 '21 at 14:26
  • frankly, I don't know what is the problem - you still have original image so you still have image without boxes. But if you think it would be better to stop drawing then maybe write to author to add option `draw_boxes=True` and `if draw_boxes: ...code which you commented out ...` – furas Jun 10 '21 at 14:31

0 Answers0