diff --git a/Result_ssd7_panel/Panel_Detector.ipynb b/Result_ssd7_panel/Panel_Detector.ipynb index feee2d8..1e34fa5 100644 --- a/Result_ssd7_panel/Panel_Detector.ipynb +++ b/Result_ssd7_panel/Panel_Detector.ipynb @@ -1695,7 +1695,7 @@ " current_axis.add_patch(plt.Rectangle((xmin, ymin), xmax-xmin, ymax-ymin, color=color, fill=False, linewidth=2))\n", " current_axis.text(xmin, ymin, label, size='x-large', color='white', bbox={'facecolor':color, 'alpha':1.0})\n", " \n", - " I = orig_images[0]\n", + " I = orig_images[0].copy()\n", " score = np.sum(I[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / ((ymax-ymin)*(xmax-xmin))\n", " \n", " boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score}\n", @@ -2037,7 +2037,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.6.5" } }, "nbformat": 4, diff --git a/Result_yolo3_panel/.gitignore b/Result_yolo3_panel/.gitignore index 75fc3d2..08a4b76 100644 --- a/Result_yolo3_panel/.gitignore +++ b/Result_yolo3_panel/.gitignore @@ -1,5 +1,9 @@ result_otros/ log_experimento_panel_gpu/ +Result_Prueba/ +Prueba/ +Otros_2/ +Otros/ *.jpg *.h5 diff --git a/config_full_yolo_panel_infer.json b/config_full_yolo_panel_infer.json new file mode 100644 index 0000000..ad43da3 --- /dev/null +++ b/config_full_yolo_panel_infer.json @@ -0,0 +1,49 @@ +{ + "model" : { + "min_input_size": 400, + "max_input_size": 400, + "anchors": [5,7, 10,14, 15, 15, 26,32, 45,119, 54,18, 94,59, 109,183, 200,21], + "labels": ["panel"], + "backend": "keras-yolo3-master/full_yolo_backend.h5" + }, + + "train": { + "train_image_folder": "Train&Test_A/Train/images/", + "train_annot_folder": "Train&Test_A/Train/anns/", + "cache_name": "Result_yolo3_panel/train_panel.pkl", + + "train_times": 1, + + "batch_size": 2, + "learning_rate": 1e-3, + "nb_epochs": 500, + "warmup_epochs": 15, + "ignore_thresh": 0.5, + "gpus": "0,1", + + "grid_scales": [1,1,1], + "obj_scale": 5, + "noobj_scale": 1, + "xywh_scale": 1, + "class_scale": 1, + + "tensorboard_dir": "Result_yolo3_panel/log_experimento_panel_gpu", + "saved_weights_name": "Result_yolo3_panel/yolo3_full_panel.h5", + "debug": true + }, + + "valid": { + "valid_image_folder": "Train&Test_A/Test/images/", + "valid_annot_folder": "Train&Test_A/Test/anns/", + "cache_name": "Result_yolo3_panel/val_panel.pkl", + + "valid_times": 1 + }, + "test": { + "test_image_folder": "Train&Test_A/Test/images/", + "test_annot_folder": "Train&Test_A/Test/anns/", + "cache_name": "Result_yolo3_panel/test_panel.pkl", + + "test_times": 1 + } +} diff --git a/keras-yolo3-master/utils/__pycache__/__init__.cpython-36.pyc b/keras-yolo3-master/utils/__pycache__/__init__.cpython-36.pyc index 9e1ed72..a8f73ec 100644 Binary files a/keras-yolo3-master/utils/__pycache__/__init__.cpython-36.pyc and b/keras-yolo3-master/utils/__pycache__/__init__.cpython-36.pyc differ diff --git a/keras-yolo3-master/utils/__pycache__/bbox.cpython-36.pyc b/keras-yolo3-master/utils/__pycache__/bbox.cpython-36.pyc index 7ed8610..e4a08e6 100644 Binary files a/keras-yolo3-master/utils/__pycache__/bbox.cpython-36.pyc and b/keras-yolo3-master/utils/__pycache__/bbox.cpython-36.pyc differ diff --git a/keras-yolo3-master/utils/__pycache__/colors.cpython-36.pyc b/keras-yolo3-master/utils/__pycache__/colors.cpython-36.pyc index 6f9979f..3318f9f 100644 Binary files a/keras-yolo3-master/utils/__pycache__/colors.cpython-36.pyc and b/keras-yolo3-master/utils/__pycache__/colors.cpython-36.pyc differ diff --git a/keras-yolo3-master/utils/__pycache__/utils.cpython-36.pyc b/keras-yolo3-master/utils/__pycache__/utils.cpython-36.pyc index b818292..fe277b7 100644 Binary files a/keras-yolo3-master/utils/__pycache__/utils.cpython-36.pyc and b/keras-yolo3-master/utils/__pycache__/utils.cpython-36.pyc differ diff --git a/panel_disconnect.py b/panel_disconnect.py new file mode 100644 index 0000000..6599f81 --- /dev/null +++ b/panel_disconnect.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Tue Mar 17 13:55:42 2020 + +@author: dlsaavedra +""" +import numpy as np + +def disconnect(image, boxes, obj_thresh = 0.5, area_min = 400, merge = 0): + + new_boxes = [] + for num, box in enumerate(boxes): + + xmin = box.xmin + merge + xmax = box.xmax - merge + ymin = box.ymin + merge + ymax = box.ymax - merge + + if xmin > 0 and ymin > 0 and xmax < image.shape[1] and ymax < image.shape[0] and box.classes[0] > obj_thresh: + + area = (ymax - ymin)*(xmax - xmin) + z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area + + if area > area_min: + + box.score = z_score + new_boxes.append(box) + #boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score, 'area' : area} + + mean_score = np.mean([box.score for box in new_boxes]) + sd_score = np.std([box.score for box in new_boxes]) + + new_boxes = [box for box in new_boxes if (box.score - mean_score)/sd_score > 2] + + for box in new_boxes: + + z_score = (box.score - mean_score)/sd_score + box.classes[0] = min((z_score-2)*0.5+ 0.5, 1) + + return new_boxes \ No newline at end of file diff --git a/predict_yolo3.py b/predict_yolo3.py index aff26b6..c0e9de2 100755 --- a/predict_yolo3.py +++ b/predict_yolo3.py @@ -16,7 +16,7 @@ import numpy as np def _main_(args): - + config_path = args.conf input_path = args.input output_path = args.output @@ -30,7 +30,7 @@ def _main_(args): # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster - obj_thresh, nms_thresh = 0.8, 0.3 + obj_thresh, nms_thresh = 0.5, 0.3 ############################### # Load the model diff --git a/predict_yolo3_disconnect.py b/predict_yolo3_disconnect.py index a2680e0..167f14e 100755 --- a/predict_yolo3_disconnect.py +++ b/predict_yolo3_disconnect.py @@ -15,8 +15,112 @@ from tqdm import tqdm import numpy as np -def _main_(args): +def disconnect(image, boxes, obj_thresh = 0.5, area_min = 400, merge = 0): + new_boxes = [] + for num, box in enumerate(boxes): + + xmin = box.xmin + merge + xmax = box.xmax - merge + ymin = box.ymin + merge + ymax = box.ymax - merge + + if xmin > 0 and ymin > 0 and xmax < image.shape[1] and ymax < image.shape[0] and box.classes[0] > obj_thresh: + + area = (ymax - ymin)*(xmax - xmin) + z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area + + if area > area_min: + + box.score = z_score + new_boxes.append(box) + #boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score, 'area' : area} + + mean_score = np.mean([box.score for box in new_boxes]) + sd_score = np.std([box.score for box in new_boxes]) + + new_boxes = [box for box in new_boxes if (box.score - mean_score)/sd_score > 2] + + for box in new_boxes: + + z_score = (box.score - mean_score)/sd_score + box.classes[0] = min((z_score-2)*0.5+ 0.5, 1) + + return new_boxes + + +def disconnect_plot(image, boxes, obj_thresh = 0.5, area_min = 400, merge = 0): + + new_boxes = [] + for num, box in enumerate(boxes): + + xmin = box.xmin + merge + xmax = box.xmax - merge + ymin = box.ymin + merge + ymax = box.ymax - merge + + if xmin > 0 and ymin > 0 and xmax < image.shape[1] and ymax < image.shape[0] and box.classes[0] > obj_thresh: + + area = (ymax - ymin)*(xmax - xmin) + z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area + + if area > area_min: + + box.score = z_score + new_boxes.append(box) + #boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score, 'area' : area} + + mean_score = np.mean([box.score for box in new_boxes]) + sd_score = np.std([box.score for box in new_boxes]) + + normal_score = ([box.score for box in new_boxes] - mean_score)/sd_score +# plt.figure() +# _ = plt.hist(normal_score, bins='auto') # arguments are passed to np.histogram +# plt.title("Histogram with 'auto' bins") +# plt.show() +# +# plt.figure() +# mean = np.mean([boxes_area_score[i]['area'] for i in boxes_area_score]) +# sd = np.std([boxes_area_score[i]['area'] for i in boxes_area_score]) +# normal = ([boxes_area_score[i]['area'] for i in boxes_area_score] - mean)/sd +# _ = plt.hist(normal, bins='auto') # arguments are passed to np.histogram +# plt.title("Histogram with 'auto' bins") +# plt.show() + + new_boxes = [box for box in new_boxes if (box.score - mean_score)/sd_score > 2] + + for box in new_boxes: + + z_score = (box.score - mean_score)/sd_score + box.classes[0] = min((z_score-2)*0.5+ 0.5, 1) + + + + + colors = plt.cm.brg(np.linspace(0, 1, 21)).tolist() + plt.figure(figsize=(10,6)) + plt.imshow(I,cmap = 'gray') + current_axis = plt.gca() + + for box in new_boxes: + + color = colors[2] + + #boxes_area_score[key]['score_norm'] = (boxes_area_score[key]['score'] - mean) / sd + #z_score = (box.score - mean_score) / sd_score + #z_score = (boxes_area_score[key]['area'] ) + + ### Escribe el z-score + #if z_score > 1: + current_axis.text((box.xmin + box.xmax)/2, + (box.ymin+ box.ymax)/2, + '%.2f' % box.classes[0], size='x-large', + color='white', bbox={'facecolor':color, 'alpha':1.0}) + + return new_boxes + +def _main_(args): + config_path = args.conf input_path = args.input output_path = args.output @@ -122,6 +226,7 @@ def _main_(args): times = [] for image_path in image_paths: + image = cv2.imread(image_path) print(image_path) start = time.time() @@ -130,10 +235,9 @@ def _main_(args): print('Elapsed time = {}'.format(time.time() - start)) times.append(time.time() - start) # draw bounding boxes on the image using labels - for box in boxes: - - draw_boxes(image, boxes, config['model']['labels'], obj_thresh) - + I = image.copy() + draw_boxes(I, boxes, config['model']['labels'], obj_thresh) + # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))