Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Last active March 5, 2022 19:04
Show Gist options
  • Select an option

  • Save kylekyle/a069cb51953aa7a715dd9785200a39b6 to your computer and use it in GitHub Desktop.

Select an option

Save kylekyle/a069cb51953aa7a715dd9785200a39b6 to your computer and use it in GitHub Desktop.
Minimal example of cropping and drawing bounding boxes around objects in images.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Generic Object Detection\n",
"\n",
"This is a minimal example of cropping and drawing bounding boxes around objects in images. It uses Tensorflow's excellent [mobile_object_localizer](https://tfhub.dev/google/object_detection/mobile_object_localizer_v1/1) model. The localizer works best with simple objects containing only a few objects. This code is based on snippets from the [ONNX ImageNet 1K Object Detector](https://github.com/ibaiGorordo/ONNX-ImageNet-1K-Object-Detector). \n",
"\n",
"To use, run this notebook from the same directory as the downloaded and unzipped model."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import tensorflow as tf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Image prep code taken from https://github.com/ibaiGorordo/ONNX-ImageNet-1K-Object-Detector\n",
"\n",
"Model from: https://tfhub.dev/google/object_detection/mobile_object_localizer_v1/1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# a utility method for displaying cv2 images in notebooks\n",
"import PIL \n",
"\n",
"from PIL.Image import fromarray\n",
"from IPython.display import display\n",
"\n",
"show_image = lambda img: display(fromarray(img))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:Saver not created because there are no variables in the graph to restore\n"
]
}
],
"source": [
"# load model\n",
"model_path = \"mobile_object_localizer_v1\"\n",
"model = tf.saved_model.load(model_path).signatures['default']\n",
"\n",
"# the dimensions the model expects\n",
"_, target_height, target_width, target_depth = model.inputs[0].shape"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"image_path = '/tf/data/uploaded/05d4a0a6-44c0-11ec-bd93-0242ac160002.jpg'\n",
"\n",
"# load the image\n",
"original = cv2.imread(image_path)\n",
"height, width, depth = original.shape\n",
"\n",
"# the model expects a tiny image\n",
"image = cv2.resize(original, (target_width, target_height))\n",
"\n",
"# convert to tensor\n",
"tensor = image[np.newaxis,:,:,:]\n",
"tensor = tf.convert_to_tensor(tensor.astype(np.float32))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.22 s, sys: 30.2 ms, total: 1.26 s\n",
"Wall time: 1.18 s\n"
]
}
],
"source": [
"%%time\n",
"predictions = model(tensor)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment