Example of load model for detections

In [1]:
import time
import os
import argparse
import json
import cv2
import sys
sys.path += [os.path.abspath('../keras-yolo3-master')]

from utils.utils import get_yolo_boxes, makedirs
from utils.bbox import draw_boxes
from tensorflow.keras.models import load_model
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt

#detection_orto(infer_model, orto_image, div = [5,5], net_h = net_h, net_w = net_w, anchors
 #              obj_thresh = obj_thresh, nms_thresh = nms_thresh)
    

    
    
def detection_orto (infer_model, orto_image, div, net_h, net_w, anchors, obj_thresh, nms_thresh):
    
    div_h, div_w = div
    
    new_shape = [int(orto_image.shape[0] / div_h), int(orto_image.shape[1] / div_w)]
    
    final_boxes = []
    
    for h in range(div_h):
        for w in range(div_w):
            
            image = orto_image[new_shape[0]*h: new_shape[0]*(h + 1), new_shape[1]*w : new_shape[1]*(w + 1)]
            boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, anchors, obj_thresh, nms_thresh)[0]
            
            for box in boxes:
                box.xmin += new_shape[1]*w
                box.xmax += new_shape[1]*w
                box.ymin += new_shape[0]*h
                box.ymax += new_shape[0]*h
                final_boxes.append(box)
    return final_boxes

ORTO PHOTO

In [30]:
#image_path = 'odm_orthophoto.tif' 
image_path = 'ortomosaico.tif' 
image = cv2.imread(image_path)
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
cv2.imwrite(image_path, image)

div = (8,8) # divide row, column


fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

i = 2
sub_image = image[int(image.shape[0] / div[0]) * i : int(image.shape[0] / div[0]) * (i + 1),
                  int(image.shape[1] / div[1]) * i:int(image.shape[1] / div[1]) * (i + 1)]
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Path Image')
plt.imshow(sub_image, cmap='gray')
ax.axis('off') 
Out[30]:
(-0.5, 273.5, 575.5, -0.5)

Load Trained Model Soiling Fault

In [3]:
## Config of trained model, change this for use different trained model
config_path  = 'config_full_yolo_fault_1_infer.json' 

with open(config_path) as config_buffer:
        config = json.load(config_buffer)
                

###############################
#####   Load the model   ######
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
infer_model = load_model(config['train']['saved_weights_name'])

## Parameters of detection
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.5, 0.45


#infer_model.summary()
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.

Detection Soling Fault

In [4]:
image = cv2.imread(image_path)
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

start = time.time()
## predict the bounding boxes
#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
print('Elapsed time = {}'.format(time.time() - start))
## draw bounding boxes on the image using labels
draw_boxes(image, boxes, config['model']['labels'], obj_thresh)


## Show Detection Fault
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection Soiling Fault')
plt.imshow(image, cmap='gray')
ax.axis('off') 
#plt.imsave('Diode Fault_6.png', image)
Elapsed time = 102.51843619346619
Out[4]:
(-0.5, 2191.5, 4612.5, -0.5)

Load Trained Model Diode Fault

In [5]:
## Config of trained model, change this for use different trained model
config_path  = 'config_full_yolo_fault_4_infer.json' 

with open(config_path) as config_buffer:
        config = json.load(config_buffer)
                

###############################
#####   Load the model   ######
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
infer_model = load_model(config['train']['saved_weights_name'])

#infer_model.summary()

## Parameters of detection
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.5, 0.45
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.

Detection Diode Fault

In [6]:
image = cv2.imread(image_path)
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)

## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

start = time.time()
## predict the bounding boxes
#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
print('Elapsed time = {}'.format(time.time() - start))
## draw bounding boxes on the image using labels
draw_boxes(image, boxes, config['model']['labels'], obj_thresh)


## Show Detection Fault
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection Diode Fault')
plt.imshow(image, cmap='gray')
ax.axis('off') 
Elapsed time = 110.71979928016663
Out[6]:
(-0.5, 2191.5, 4612.5, -0.5)

Load Trained Model Cell Damage

In [7]:
## Config of trained model, change this for use different trained model
config_path  = 'config_full_yolo_fault_2_infer.json' 

with open(config_path) as config_buffer:
        config = json.load(config_buffer)
                

###############################
#####   Load the model   ######
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
infer_model = load_model(config['train']['saved_weights_name'])

#infer_model.summary()

## Parameters of detection
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.5, 0.45
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.

Detection Cell Damage Fault

In [8]:
image = cv2.imread(image_path)
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)

## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

start = time.time()
## predict the bounding boxes
#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
print('Elapsed time = {}'.format(time.time() - start))
## draw bounding boxes on the image using labels
draw_boxes(image, boxes, config['model']['labels'], obj_thresh)


## Show Detection Fault
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection Cell damage Fault')
plt.imshow(image, cmap='gray')
ax.axis('off') 
Elapsed time = 107.89967823028564
Out[8]:
(-0.5, 2191.5, 4612.5, -0.5)

Load Trained Model Panel Disconnect

In [9]:
## Config of trained model, change this for use different trained model
config_path  = 'config_full_yolo_panel_infer.json' 

with open(config_path) as config_buffer:
        config = json.load(config_buffer)
                

###############################
#####   Load the model   ######
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
infer_model = load_model(config['train']['saved_weights_name'])

#infer_model.summary()

## Parameters of detection
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.5, 0.3
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.

Detection Panel Disconnect

In [28]:
sys.path.insert(1, '../')
from panel_disconnect import disconnect

image = cv2.imread(image_path)
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)

## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

start = time.time()
## predict the bounding boxes
#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
boxes_image = [box for box in boxes if box.get_score() > obj_thresh]

boxes_disc = disconnect(image, boxes_panel, z_thresh = 1.8)
print('Elapsed time = {}'.format(time.time() - start))
## draw bounding boxes on the image using labels
draw_boxes(image, boxes_disc, config['model']['labels'], obj_thresh)


## Show Detection Fault
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection Fault')
plt.imshow(image, cmap='gray')
ax.axis('off') 
Elapsed time = 107.44442081451416
Out[28]:
(-0.5, 2191.5, 4612.5, -0.5)
In [24]:
sys.path.insert(1, '../')

from panel_disconnect import disconnect

boxes_image = [box for box in boxes if box.get_score() > obj_thresh]

print('Elapsed time = {}'.format(time.time() - start))

boxes_disc = disconnect(image, boxes_panel, z_thresh = 1.8)
Elapsed time = 506.6428759098053
../panel_disconnect.py:23: RuntimeWarning: invalid value encountered in true_divide
  z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area
In [26]:
len(boxes_disc)
Out[26]:
60
In [27]:
len(boxes_image)
Out[27]:
1709
In [ ]: