Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!pip install pyyaml
|
| 2 |
+
!pip install 'git+https://github.com/facebookresearch/detectron2.git'
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
|
| 6 |
+
CUDA_VERSION = torch.version.cuda
|
| 7 |
+
print("torch:", TORCH_VERSION, "; cuda:", CUDA_VERSION)
|
| 8 |
+
|
| 9 |
+
# Instale Detectron2 que coincida con la versi贸n de PyTorch anterior
|
| 10 |
+
# Ver https://detectron2.readthedocs.io/tutorials/install.html para instrucciones
|
| 11 |
+
!pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/$CUDA_VERSION/torch$TORCH_VERSION/index.html
|
| 12 |
+
|
| 13 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 14 |
+
# Importaci贸n de bibliotecas
|
| 15 |
+
import pandas as pd
|
| 16 |
+
import os, cv2, sys
|
| 17 |
+
import matplotlib.pyplot as plt
|
| 18 |
+
import ipywidgets as widgets
|
| 19 |
+
# %matplotlib auto
|
| 20 |
+
|
| 21 |
+
# Importaci贸n de los paquetes de Detectron2
|
| 22 |
+
from detectron2 import model_zoo
|
| 23 |
+
from detectron2.engine import DefaultPredictor
|
| 24 |
+
from detectron2.config import get_cfg
|
| 25 |
+
from detectron2.utils.visualizer import Visualizer
|
| 26 |
+
from detectron2.data import catalog
|
| 27 |
+
|
| 28 |
+
# instalar colab_zirc_dims
|
| 29 |
+
!pip install colab_zirc_dims==1.0.10
|
| 30 |
+
|
| 31 |
+
#!python -m pip install 'git+https://github.com/MCSitar/colab_zirc_dims.git@v1_0_10'
|
| 32 |
+
|
| 33 |
+
# importar m贸dulos colab_zirc_dims
|
| 34 |
+
from colab_zirc_dims import save_load
|
| 35 |
+
from colab_zirc_dims import alc_notebook_fxns
|
| 36 |
+
from colab_zirc_dims import zirc_dims_GUI
|
| 37 |
+
from colab_zirc_dims import gen_notebook_fxns
|
| 38 |
+
from colab_zirc_dims import gen_filename_fxns
|
| 39 |
+
from colab_zirc_dims import expl_vis
|
| 40 |
+
from colab_zirc_dims import non_std_cfgs
|
| 41 |
+
# opcionalmente importar algunas funciones al IPython principal
|
| 42 |
+
# kernel para compatibilidad local (no Colab)
|
| 43 |
+
try:
|
| 44 |
+
from google.colab import output
|
| 45 |
+
except ModuleNotFoundError:
|
| 46 |
+
from colab_zirc_dims.jupyter_colab_compat import output_local as output
|
| 47 |
+
|
| 48 |
+
from PIL import Image
|
| 49 |
+
import skimage.io
|
| 50 |
+
|
| 51 |
+
!pip install gradio
|
| 52 |
+
|
| 53 |
+
import gradio as gr
|
| 54 |
+
|
| 55 |
+
from google.colab import drive
|
| 56 |
+
drive.mount('/content/gdrive')
|
| 57 |
+
|
| 58 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 59 |
+
def generate_medition(sample_img,Escala):
|
| 60 |
+
#sample_img = im.convert('RGB')
|
| 61 |
+
#print(sample_img.shape)
|
| 62 |
+
#input()
|
| 63 |
+
|
| 64 |
+
from PIL import Image
|
| 65 |
+
im = Image.fromarray(sample_img)
|
| 66 |
+
im.save("sample_image.jpg")
|
| 67 |
+
fn = 'sample_image.jpg'
|
| 68 |
+
filepath = '/content/sample_image.jpg'
|
| 69 |
+
#Escala = 3
|
| 70 |
+
selected_samples = []
|
| 71 |
+
sample_img
|
| 72 |
+
loaded_data_dict = {fn: {fn: {'img_file': filepath, 'Align_file': '', 'rel_file': fn, 'scale_factor': Escala, 'scale_from': 'default (1.0)'}}}
|
| 73 |
+
#print(loaded_data_dict)
|
| 74 |
+
#input()
|
| 75 |
+
alc_notebook_fxns.select_samples_fxn(loaded_data_dict, selected_samples)
|
| 76 |
+
|
| 77 |
+
model_lib_loc = 'default'
|
| 78 |
+
current_model_dict = {}
|
| 79 |
+
alc_notebook_fxns.select_download_model_interface(current_model_dict, model_lib_loc)
|
| 80 |
+
|
| 81 |
+
grain_metadata = catalog.Metadata(name='grain_meta', thing_classes=['grain'])
|
| 82 |
+
predictor = non_std_cfgs.smart_load_predictor(
|
| 83 |
+
current_model_dict['selected_config_yaml'],
|
| 84 |
+
current_model_dict['selected_model_weights'],
|
| 85 |
+
use_cpu = True,
|
| 86 |
+
adj_nms_thresh='auto',
|
| 87 |
+
adj_thresh_test='auto')
|
| 88 |
+
print('Predictor loaded')
|
| 89 |
+
|
| 90 |
+
ROOT_DIR = "/content/drive/MyDrive"
|
| 91 |
+
Try_contrast_enhanced_subimage = True
|
| 92 |
+
Try_Otsu_thresholding = True
|
| 93 |
+
Save_polygons = True
|
| 94 |
+
save_polys_bool = Save_polygons
|
| 95 |
+
alt_methods = [Try_contrast_enhanced_subimage,
|
| 96 |
+
Try_Otsu_thresholding]
|
| 97 |
+
full_auto_str = ''
|
| 98 |
+
# %matplotlib auto
|
| 99 |
+
plt.switch_backend('Agg')
|
| 100 |
+
run_dir = gen_notebook_fxns.full_auto_proc(ROOT_DIR, selected_samples, loaded_data_dict,
|
| 101 |
+
predictor, save_polys_bool, alt_methods,
|
| 102 |
+
full_auto_str, stream_output=False)
|
| 103 |
+
|
| 104 |
+
filepath = run_dir + '/mask_images/' + fn + '/' + fn + '.png'
|
| 105 |
+
im = Image.open(filepath)
|
| 106 |
+
rgb_im = im.convert('RGB')
|
| 107 |
+
rgb_im.save('imagef.jpg')
|
| 108 |
+
imagef = '/content/imagef.jpg'
|
| 109 |
+
from google.colab.patches import cv2_imshow
|
| 110 |
+
img = cv2.imread(imagef, cv2.COLOR_BGR2RGB)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# Carga de los datos de medici贸n del fragmento de ripios de perforaci贸n
|
| 114 |
+
file = run_dir + '/grain_dimensions/' + fn + '_grain_dimensions.csv'
|
| 115 |
+
print(file)
|
| 116 |
+
df = pd.read_csv(file)
|
| 117 |
+
df = df.head()
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
return img, df
|
| 122 |
+
|
| 123 |
+
demo = gr.Interface(fn=generate_medition, inputs=[gr.Image(label="Imagen"), gr.Radio([1,2,3,4,5,6], label="Escala de la imagen")], outputs=[gr.Image(label="Fragmento medido"), gr.Dataframe(label="Tabla de datos")], title="MEDICI脫N DE IM脕GENES DE RIPIOS DE PERFORACI脫N",
|
| 124 |
+
description='La siguiente interfaz medir谩 de forma autom谩tica fragmentos en im谩genes de ripios de perforaci贸n. El usuario deber谩 ingresar en la parte de la izquierda la imagen a ser procesada y su escala en um/px, mientras que en la parte de la derecha se mostrar谩 la imagen con el fragmento ya medido y una tabla con los par谩metros medidos y calculados. Se recomienda ingresar im谩genes sin ning煤n tipo de mediciones o s铆mbolos ya que esto podr铆a afectar en la predicci贸n del modelo.',
|
| 125 |
+
article='Nota: En el caso de ingresar im谩genes que no tengan relaci贸n a muestras de ripios de perforaci贸n, los autores de esta aplicaci贸n no se hacen responsables por los resultados de estas, el modelo de medici贸n de ripios de perforaci贸n est谩 entrenado para dar un resultado.')
|
| 126 |
+
demo.launch()
|