Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,18 @@ from huggingface_hub import from_pretrained_keras
|
|
| 8 |
def resize_image(img_in,input_height,input_width):
|
| 9 |
return cv2.resize( img_in, ( input_width,input_height) ,interpolation=cv2.INTER_NEAREST)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def visualize_model_output(prediction, img):
|
| 12 |
unique_classes = np.unique(prediction[:,:,0])
|
| 13 |
rgb_colors = {'0' : [255, 255, 255],
|
|
@@ -48,7 +60,21 @@ def visualize_model_output(prediction, img):
|
|
| 48 |
added_image = cv2.addWeighted(img,0.5,output,0.1,0)
|
| 49 |
return added_image
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def do_prediction(model_name, img):
|
| 53 |
img_org = np.copy(img)
|
| 54 |
model = from_pretrained_keras(model_name)
|
|
@@ -77,12 +103,52 @@ def do_prediction(model_name, img):
|
|
| 77 |
img_width_model=model.layers[len(model.layers)-1].output_shape[2]
|
| 78 |
n_classes=model.layers[len(model.layers)-1].output_shape[3]
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if img.shape[0] < img_height_model:
|
| 81 |
img = resize_image(img, img_height_model, img.shape[1])
|
| 82 |
|
| 83 |
if img.shape[1] < img_width_model:
|
| 84 |
img = resize_image(img, img.shape[0], img_width_model)
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
marginal_of_patch_percent = 0.1
|
| 87 |
margin = int(marginal_of_patch_percent * img_height_model)
|
| 88 |
width_mid = img_width_model - 2 * margin
|
|
|
|
| 8 |
def resize_image(img_in,input_height,input_width):
|
| 9 |
return cv2.resize( img_in, ( input_width,input_height) ,interpolation=cv2.INTER_NEAREST)
|
| 10 |
|
| 11 |
+
def otsu_copy_binary(img):
|
| 12 |
+
img_r=np.zeros((img.shape[0],img.shape[1],3))
|
| 13 |
+
img1=img[:,:,0]
|
| 14 |
+
|
| 15 |
+
retval1, threshold1 = cv2.threshold(img1, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
|
| 16 |
+
|
| 17 |
+
img_r[:,:,0]=threshold1
|
| 18 |
+
img_r[:,:,1]=threshold1
|
| 19 |
+
img_r[:,:,2]=threshold1
|
| 20 |
+
|
| 21 |
+
return img_r
|
| 22 |
+
|
| 23 |
def visualize_model_output(prediction, img):
|
| 24 |
unique_classes = np.unique(prediction[:,:,0])
|
| 25 |
rgb_colors = {'0' : [255, 255, 255],
|
|
|
|
| 60 |
added_image = cv2.addWeighted(img,0.5,output,0.1,0)
|
| 61 |
return added_image
|
| 62 |
|
| 63 |
+
def return_num_columns(img):
|
| 64 |
+
model_classifier = from_pretrained_keras("SBB/eynollah-column-classifier")
|
| 65 |
+
img_1ch = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 66 |
|
| 67 |
+
img_1ch = img_1ch / 255.0
|
| 68 |
+
img_1ch = cv2.resize(img_1ch, (448, 448), interpolation=cv2.INTER_NEAREST)
|
| 69 |
+
img_in = np.zeros((1, img_1ch.shape[0], img_1ch.shape[1], 3))
|
| 70 |
+
img_in[0, :, :, 0] = img_1ch[:, :]
|
| 71 |
+
img_in[0, :, :, 1] = img_1ch[:, :]
|
| 72 |
+
img_in[0, :, :, 2] = img_1ch[:, :]
|
| 73 |
+
|
| 74 |
+
label_p_pred = model.predict(img_in, verbose=0)
|
| 75 |
+
num_col = np.argmax(label_p_pred[0]) + 1
|
| 76 |
+
return num_col
|
| 77 |
+
|
| 78 |
def do_prediction(model_name, img):
|
| 79 |
img_org = np.copy(img)
|
| 80 |
model = from_pretrained_keras(model_name)
|
|
|
|
| 103 |
img_width_model=model.layers[len(model.layers)-1].output_shape[2]
|
| 104 |
n_classes=model.layers[len(model.layers)-1].output_shape[3]
|
| 105 |
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
img_org = np.copy(img)
|
| 109 |
+
img_height_h = img_org.shape[0]
|
| 110 |
+
img_width_h = img_org.shape[1]
|
| 111 |
+
|
| 112 |
+
#model_region, session_region = self.start_new_session_and_model(self.model_region_dir_p_ens)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
if num_col_classifier == 1:
|
| 117 |
+
img_w_new = 1000
|
| 118 |
+
img_h_new = int(img_org.shape[0] / float(img_org.shape[1]) * img_w_new)
|
| 119 |
+
|
| 120 |
+
elif num_col_classifier == 2:
|
| 121 |
+
img_w_new = 1500
|
| 122 |
+
img_h_new = int(img_org.shape[0] / float(img_org.shape[1]) * img_w_new)
|
| 123 |
+
|
| 124 |
+
elif num_col_classifier == 3:
|
| 125 |
+
img_w_new = 2000
|
| 126 |
+
img_h_new = int(img_org.shape[0] / float(img_org.shape[1]) * img_w_new)
|
| 127 |
+
|
| 128 |
+
elif num_col_classifier == 4:
|
| 129 |
+
img_w_new = 2500
|
| 130 |
+
img_h_new = int(img_org.shape[0] / float(img_org.shape[1]) * img_w_new)
|
| 131 |
+
elif num_col_classifier == 5:
|
| 132 |
+
img_w_new = 3000
|
| 133 |
+
img_h_new = int(img_org.shape[0] / float(img_org.shape[1]) * img_w_new)
|
| 134 |
+
else:
|
| 135 |
+
img_w_new = 4000
|
| 136 |
+
img_h_new = int(img_org.shape[0] / float(img_org.shape[1]) * img_w_new)
|
| 137 |
+
img_resized = resize_image(img,img_h_new, img_w_new )
|
| 138 |
+
|
| 139 |
+
img = otsu_copy_binary(img_resized)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
if img.shape[0] < img_height_model:
|
| 144 |
img = resize_image(img, img_height_model, img.shape[1])
|
| 145 |
|
| 146 |
if img.shape[1] < img_width_model:
|
| 147 |
img = resize_image(img, img.shape[0], img_width_model)
|
| 148 |
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
marginal_of_patch_percent = 0.1
|
| 153 |
margin = int(marginal_of_patch_percent * img_height_model)
|
| 154 |
width_mid = img_width_model - 2 * margin
|