<jupyter_start><jupyter_text>Evaluate trained model<jupyter_code>%load_ext autoreload
%autoreload 1
import os
import cv2

import sys
from matplotlib import pyplot as plt
from matplotlib.pyplot import imshow
from PIL import Image


try:
    from labinet.io_util import load_label_map
    from labinet.io_util import load_model
    import labinet.object_detect
    import labinet.box
except ImportError:
    # This part is only required to run the notebook
    # directory when the module itself is not installed.
    #
    # If you have the module installed, just use "import labinet..."
    import os
    import inspect
    # the .travis.yml is coded so that we execute tests from within test subdir. Relative to 'test' the .py is found in ../source/dev
    cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0], "..")))
    if cmd_subfolder not in sys.path:
        sys.path.insert(0, cmd_subfolder)
    from labinet.io_util import load_label_map
    from labinet.io_util import load_model
    import labinet.object_detect
    import labinet.box

%aimport labinet.io_util
%aimport labinet.object_detect
%aimport labinet.box

%matplotlib inline<jupyter_output><empty_output><jupyter_text>change path, model name etc to your needs<jupyter_code>MODEL_NAME = 'inference_graph'   # the result from Step 6 Export Inference Graph
IMAGE_PATH = 'images/train'
IMAGE_NAME = '01-20190414173244-01.jpg'   # cmdline arg
# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
CWD_PATH = os.path.join(os.getcwd(),'..')  # should become gitbase
NUM_CLASSES = 1
# model
PATH_TO_MODEL = os.path.join(CWD_PATH, MODEL_NAME, 'frozen_inference_graph.pb')
# label map
LABEL_MAP = os.path.join(CWD_PATH, 'data', 'object-detection.pbtxt')
# path to image
PATH_TO_IMAGE = os.path.join(CWD_PATH, IMAGE_PATH, IMAGE_NAME)<jupyter_output><empty_output><jupyter_text>Load (frozen) Model into Memory<jupyter_code>detection_graph = load_model(PATH_TO_MODEL)<jupyter_output><empty_output><jupyter_text>load labels<jupyter_code>categories, category_index = load_label_map(LABEL_MAP, NUM_CLASSES)<jupyter_output><empty_output><jupyter_text>Run Inference : Object Detection _load the image and run on it the object detection_<jupyter_code># load image
image =  Image.open(PATH_TO_IMAGE)
#imshow(np.asarray(image))
#print(image.shape()

# inference
output_dict = labinet.object_detect.run_inference_for_single_image(image, detection_graph)<jupyter_output>tensor:0 for image.shape=(x=480,y=640)<jupyter_text>Visualize result<jupyter_code>image_with_boxes = labinet.object_detect.visualize_boxes_after_detection(image, output_dict, category_index)
# All the results have been drawn on image. Now display the image.
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_with_boxes)
print(IMAGE_NAME)
print(image_with_boxes.shape)<jupyter_output>01-20190414173244-01.jpg
(640, 480, 3)<jupyter_text>Write Result as XML_use the xml file to laod image in labelImg and check correct boxing_<jupyter_code>boxes = output_dict['detection_boxes']
scores = output_dict['detection_scores']
boxes_to_use = labinet.object_detect.get_boxes_to_use(boxes, scores)
# we need the box' coordinates relative to the image size
boxes_normed = []
for box in boxes_to_use:
    box_normed = labinet.box.get_normalized_coordinates(box, image.size)
    boxes_normed.append(box_normed)
%aimport labinet.box

xml = labinet.box.box_to_labelimg_xml(IMAGE_NAME, image.size, boxes_normed, imagepath=PATH_TO_IMAGE)
ET.dump(xml)
xmlfname = PATH_TO_IMAGE[:-4]+".xml"
xml.write(xmlfname)<jupyter_output><annotation><folder>images</folder><filename>01-20190414173244-01.jpg</filename><path>C:\Users\Carsten\Documents\python\object_detection\labinet\notebook\..\images\01-20190414173244-01.jpg</path><source><database>Unknown</database></source><size><width>480</width><height>640</height><depth>3</depth></size><segmented>0</segmented><object><name>dog</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>124</xmin><xmax>307</xmax><ymin>426</ymin><ymax>591</ymax></bndbox></object><object><name>dog</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>157</xmin><xmax>366</xmax><ymin>456</ymin><ymax>585</ymax></bndbox></object><object><name>dog</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>214</xmin><xmax>381</xmax><ymin>463</ymin><ymax>575</ymax></bndbox></object></annotation><jupyter_text>Do inference an all new files<jupyter_code>NEW_IMAGE_PATH = os.path.join(CWD_PATH, 'new_images')
files = os.listdir(NEW_IMAGE_PATH)
print(f"Infer image in: {NEW_IMAGE_PATH}")
print(files)
# # SLOW SLOW SLOW (but works)

def infer_on_all_files_slow(files):
    for fname in files:
        if fname.endswith('jpg'):
            image_file = os.path.join(NEW_IMAGE_PATH, fname)
            print(f'inference on: {fname}')
            image =  Image.open(image_file)
            output_dict = labinet.object_detect.run_inference_for_single_image(image, detection_graph)
            boxes = output_dict['detection_boxes']
            scores = output_dict['detection_scores']
            boxes_to_use = labinet.object_detect.get_boxes_to_use(boxes, scores)
            boxes_normed = []
            print(f'found {len(boxes_to_use)} objects')
            for box in boxes_to_use:
                box_normed = labinet.box.get_normalized_coordinates(box, image.size)
                boxes_normed.append(box_normed)
                print(box_normed)
            xml = labinet.box.box_to_labelimg_xml(fname, image.size, boxes_normed, imagepath=image_file)
            xmlfname = image_file[:-4]+".xml"
            #xml.write(xmlfname)
            break<jupyter_output><empty_output><jupyter_text>```inference on: 01-20190414173236-00.jpgfound 2 objects{'xmin': 216, 'xmax': 367, 'ymin': 392, 'ymax': 513}{'xmin': 421, 'xmax': 480, 'ymin': 452, 'ymax': 618}```<jupyter_code>import numpy as np
#print(image_np_exp.shape)
import tensorflow as tf
image_width=480
image_height=640
#image_np_exp = np.expand_dims(labinet.io_util.load_image_into_numpy_array(image), axis=0) 
#image_np_exp.shape  # 1, 640, 480, 3

tensor_dict = labinet.object_detect.get_tensor_dict_with_masks(image_width, image_height, detection_graph)
image_tensor = tensor_dict['image_tensor']
config = tf.ConfigProto()
config.gpu_options.allow_growth=True

with tf.Session(graph=detection_graph, config=config) as sess:
    for fname in files:
        if fname.endswith('jpg'):
            # load and converte image
            image_file = os.path.join(NEW_IMAGE_PATH, fname)
            print(f'inference on: {fname}')
            image =  Image.open(image_file)
            image_np_exp = np.expand_dims(labinet.io_util.load_image_into_numpy_array(image), axis=0) 
            
            # inference
            output_dict = sess.run(tensor_dict, feed_dict={image_tensor: image_np_exp})
            labinet.object_detect.convert_output_dict(output_dict)

            # boxes to xml
            boxes = output_dict['detection_boxes']
            scores = output_dict['detection_scores']
            boxes_to_use = labinet.object_detect.get_boxes_to_use(boxes, scores)
            boxes_normed = []
            print(f'found {len(boxes_to_use)} objects')
            for box in boxes_to_use:
                box_normed = labinet.box.get_normalized_coordinates(box, image.size)
                boxes_normed.append(box_normed)
                print(box_normed)
            xml = labinet.box.box_to_labelimg_xml(fname, image.size, boxes_normed, imagepath=image_file)
            # xml to disk
            xmlfname = image_file[:-4]+".xml"
            xml.write(xmlfname)
            #break - comment in to test for 1 file only

# for everything like video stream or so... do 
# labinet.object_detect.get_tensor_dict_with_masks(...)
# with tf.Session(graph=detection_graph) as sess:
#   ....
#   output_dict = sess.run(tensor_dict, feed_dict={image_tensor: image_np_exp})
#   ....
#import cv2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11638837353123067, 'fraction_numerical': 0.02585034013605442, 'mean_word_length': 4.545953360768175, 'pattern_counts': {'":': 0, '<': 116, '<?xml version=': 0, '>': 116, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '234380', 'license': 'MIT', 'n_tokens_mistral': 2959, 'n_tokens_neox': 2752, 'n_words': 494, 'path': 'notebook/object_detect_one_image.ipynb', 'repo_name': 'Carstig/labinet', 'size': '8085'}