Skip to content
This script uses keras-yolo3 to detect the top left coordinates, width and height of the objects in the image.
Python
Branch: master
Clone or download
Latest commit 59258f0 Jun 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
keras_yolo3 initial commit Jun 17, 2019
model_data initial commit Jun 17, 2019
picture initial commit Jun 17, 2019
.gitignore initial commit Jun 17, 2019
LICENSE initial commit Jun 17, 2019
Pipfile initial commit Jun 17, 2019
Pipfile.lock
README.md Rewrite README.md Jun 17, 2019
example.py initial commit Jun 17, 2019
objects.py initial commit Jun 17, 2019
requirements.txt initial commit Jun 17, 2019

README.md

object-detection-keras-yolo3

license

This script uses qqwweee/keras-yolo3 to detect the top left coordinates, width and height of the objects in the image and return them as the list of dictionaries.

Install

  1. Download YOLOv3 weights from YOLO website.
  2. Convert the Darknet YOLO model to a Keras model.
python keras_yolo3/convert.py keras_yolo3/yolov3.cfg yolov3.weights model_data/yolo.h5

Usage

  1. Import YOLO class from keras.yolo3.
  2. Import get_objects_information function from objects.
  3. Create YOLO instance.
  4. Call get_objects_information with the YOLO instance and the image path as arguments.
  5. The function returns the list of object information dictionaries.
    If there is no object in the image, it returns an empty list.

Each dictionary has the following keys.

  • predicted_name Predicted name of the object.
  • x x-coordinate at the top left coordinate of the object.
  • y y-coordinate at the top left coordinate of the object.
  • width width of the object.
  • height height of the object.

Code Example

from PIL import Image
from keras_yolo3.yolo import YOLO
from objects import get_objects_information

if __name__ == '__main__':
    yolo = YOLO()
    image_path = "./picture/sample_picture.jpg"
    objects_info_list = get_objects_information(yolo, image_path)
    yolo.close_session()

    img = Image.open(image_path)
    count = 0
    for object_info in objects_info_list:
        class_name = object_info['predicted_name']
        x = object_info['x']
        y = object_info['y']
        width = object_info['width']
        height = object_info['height']
        cropped_img = img.crop((x, y, x + width, y + height))
        cropped_img.save("./picture/{}{}.jpg".format(class_name, count))
        count = count + 1

Input

Photo by rawpixel.com from Pexels sample_picture

output

person0.jpg
person0

person1.jpg
person1

You can’t perform that action at this time.