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
#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')
## 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()
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)
## 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
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')
## 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
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')
## 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
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')
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)
len(boxes_disc)
len(boxes_image)