Object Detection Tensorflow
To get started with object detection have a look at the following jupyter notebook:
Assuming that you have already setup your environment with tensorflow , in my case its a docker container . You need to still execute the following instructions .
One issue i was getting was that the jupyter notebook kept failing at the following line despite having followed all the instructions:
from object_detection.utils import ops as utils_ops
So i discovered that this was due to Python libraries not being availabe in PYTHONPATH:
# From tensorflow/models/research/ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
if despite having executed the above in your container or your tensorflow environment the problem still persists in your Jupyter notebook consider adding it directly as can be seen below :
====Extract Jupyter Notebook=============================
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from timeit import default_timer as timer
import cv2
sys.path.append('/notebooks/models/research') # point to your tensorflow dir
sys.path.append('/notebooks/models/research/slim') # point ot your slim dir
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from object_detection.utils import ops as utils_ops
if tf.__version__ < '1.4.0':
raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')
============================================================
Note that I have also changed the default method to use opencv for faster image IO and a timer to determine performance.
You install opencv using :
sudo apt-get install python-opencv
==============Extract ========================
for image_path in TEST_IMAGE_PATHS:
start = timer()
#image = Image.open(image_path)
image = cv2.imread(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
#image_np = load_image_into_numpy_array(image)
image_np = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
#image_np_expanded = np.expand_dims(image_np, axis=0)
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8)
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
end = timer()
duration = (end - start)
print('Image: {0} took {1} to be processed'.format(image_path,duration))
===========================================
No comments:
Post a Comment