<jupyter_start><jupyter_text>Session 15: Detecting objects with Distant Viewing ToolkitWe extend our approach to detect new types of objects.<jupyter_code>%pylab inline

import numpy as np
import scipy as sp
import pandas as pd
import sklearn
from sklearn import linear_model
import urllib
import keras

import os
from os.path import join
import matplotlib.pyplot as plt
import matplotlib.patches as patches

plt.rcParams["figure.figsize"] = (12,12)
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"<jupyter_output><empty_output><jupyter_text>Distant Viewing ToolkitLet's download the distant viewing toolkit:    pip install keras-retinanet    pip install dvt    Which we can load with the following:<jupyter_code>import dvt<jupyter_output><empty_output><jupyter_text>Bewitched againLet's load and look at the the bewitch corpus. It contains still imagesfrom two episodes of the sitcom Bewitched.<jupyter_code>df = pd.read_csv(join("..", "data", "bewitched.csv"))
df.head()<jupyter_output><empty_output><jupyter_text>Location objectsThe distant viewing toolkit canbe used to detect objects withinan image. We start by creating an object detector:<jupyter_code>odrn = dvt.annotate.object.ObjectDetectRetinaNet()<jupyter_output><empty_output><jupyter_text>Let's try to find objects in the following images:<jupyter_code>img_path = join('..', 'images', 'bewitched', df.filename[200])
img = imread(img_path)
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>Here is how we detect objects in the image:<jupyter_code>objs = odrn.detect(img)
objs<jupyter_output><empty_output><jupyter_text>As with the faces, we can show these within the image. We'll addsome labels.<jupyter_code>fig,ax = plt.subplots(1,1)
plt.imshow(img)
n, m, d = img.shape
for obj in objs:
    rect = plt.Rectangle((obj['left'], obj['top']),
                          obj['right'] - obj['left'],
                          obj['bottom'] - obj['top'],
                         edgecolor='orange', linewidth=2, facecolor='none')
    ax.add_patch(rect)
    
    plt.text(obj['left'], obj['top'] - 12, obj['class'],
             fontsize=12,
             bbox=dict(facecolor='orange'))
    
plt.axis('off')<jupyter_output><empty_output><jupyter_text>Your turnNow, let's try to find a couple of other images that you might be interested in detecting objects. Keep in mind that the algorithmby default is only able to detect a limited set of (mostly modern)things.Please put these images in the test directory.<jupyter_code>img = imread(join('..', 'images', 'test', 'dog.jpg'))
objs = odrn.detect(img)
fig,ax = plt.subplots(1,1)
plt.imshow(img)
n, m, d = img.shape
for obj in objs:
    rect = plt.Rectangle((obj['left'], obj['top']),
                          obj['right'] - obj['left'],
                          obj['bottom'] - obj['top'],
                         edgecolor='orange', linewidth=2, facecolor='none')
    ax.add_patch(rect)
    
    plt.text(obj['left'], obj['top'] - 12, obj['class'],
             fontsize=12,
             bbox=dict(facecolor='orange'))
    
plt.axis('off')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12232316677482155, 'fraction_numerical': 0.007462686567164179, 'mean_word_length': 4.129783693843594, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '461750', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1031, 'n_tokens_neox': 1002, 'n_words': 265, 'path': 'nb/session15-object-detection.ipynb', 'repo_name': 'statsmaths/hilt2019-image-analysis', 'size': '3082'}