Detection

The next part of the code runs the image using the TensorFlow object detection API. It will provide the coordinates of the box as an array of the edge positions (top, left, bottom, and right). It will then crop and save the images based on the boxes. In order for us to crop the correct area we need to transform the coordinates from percentages to pixels by multiplying the values by the width and height:

def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
# --- Path to image to test, was: "test_image/image1.jpg"
TEST_IMAGE_PATH = 'image1.jpg'

After we have saved the image portions, we can pass each of them to Watson for classification. 

Notice the use of the variables we set previously (MAX_NUMBER_OF_BOXES and MINIMUM_CONFIDENCE) in the following code:

print('detecting...')
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
image = Image.open(TEST_IMAGE_PATH)
image_np = load_image_into_numpy_array(image)
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.
get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.
get_tensor_by_name('detection_scores:0')
num_detections = detection_graph.
get_tensor_by_name('num_detections:0')
# --- Actual detection.
(boxes, scores, num_detections) = sess.run([boxes, scores, num_detections], feed_dict={image_tensor: image_np_expanded})
# --- Create figure and axes and display the image
fig, ax = plt.subplots(1)
ax.imshow(image_np)
(height, width, x) = image_np.shape
for i in range(0, int(min(num_detections, MAX_NUMBER_OF_BOXES))):
score = np.squeeze(scores)[i]
# --- if the score is not greater than
# --- what we set the minimun score to be then
# --- exit the loop
if score < MINIMUM_CONFIDENCE:
break
box = np.squeeze(boxes)[i]
box_x = box[1] * width
box_y = box[0] * height
box_width = (box[3] - box[1]) * width
box_height = (box[2] - box[0]) * height
box_x2 = box[3] * width
box_y2 = box[2] * height
img2 = image.crop((box_x, box_y, box_x2, box_y2))
path = 'cropped/image1'
os.makedirs(path, exist_ok=True)
full_path = os.path.join(path, 'img{}.jpg'.format(i))
img2.save(full_path)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset