for i in range(len(data)): temp = center-data[i,:] temp_value = [email protected] temp_dis[i] = np.sqrt(temp_value) dis = np.max(temp_dis) return dis #plot the rectangle in the original image def plot_rectangle(draw,area_x1,area_x2,area_y1,area_y2): #draw = ImageDraw.Draw(im) draw.line((area_y1, area_x1, area_y1, area_x2), fill="blue", width=10) draw.line((area_y1, area_x2, area_y2, area_x2), fill="blue", width=10) draw.line((area_y2, area_x2, area_y2, area_x1), fill="blue", width=10) draw.line((area_y2, area_x1, area_y1, area_x1), fill="blue", width=10) #im.show() #%%load model and weight model = cnn_model() model.summary() model.load_weights('./model_weight/2012images-areas-7.5-50epoch.h5') #%% windowsize_r = 30 windowsize_c = 30 raw_image_files = glob.glob("./New folder/*.jpg") count = 0 for name in raw_image_files: name_piece = name.split("\\")[1] name_image = name_piece.split(".")[0] path_image = './New folder/'+str(name_image)+'.jpg' testimage = Image.open(path_image) test_image = imread(path_image) start_time = time.time() for r in range(0,test_image.shape[0] - windowsize_r+1, 6):
def groundtruth(): global panelC, count txt_path = path.split("/")[6] txt_path_1 = txt_path.split(".")[0] save_path = './' + str(txt_path_1) + '.txt' txt_path = './2012image/' + str(txt_path_1) + '.txt' read_path = './2012image/' + str(txt_path_1) + '.txt' if not os.path.exists(read_path): tk.messagebox.showerror(title='error message', message='No such file or directory') else: count = 0 true_length = image.shape[1] true_width = image.shape[0] #read the orginal demension of the image txtfile = open(txt_path) firstline = txtfile.readlines()[0].split(",") original_length = int(firstline[2]) original_width = int(firstline[3]) txtfile.close() #read the number of the random points in the image txtfile = open(txt_path) count_points = int(txtfile.readlines()[5]) txtfile.close() txtfile = open(txt_path) data = txtfile.readlines()[6:6 + count_points] corrdinate = np.zeros([count_points, 2], dtype=np.int) for n in range(count_points): data1 = data[n].split(",") corrdinate[n, 0] = int(int(data1[0]) * true_length / original_length) corrdinate[n, 1] = int(int(data1[1]) * true_width / original_width) txtfile.close() txtfile = open(txt_path) label_encode = np.zeros(count_points) label = txtfile.readlines()[6 + count_points:6 + count_points + count_points] for m in range(count_points): label1 = label[m].split(",") label2 = label1[1] new_l = label2.replace('\"', '') #coral if ((new_l == "Agalg") or (new_l == "Aga") or (new_l == "Agaf") or (new_l == "Col") or (new_l == "Helc") or (new_l == "Mdrc") or (new_l == "Mdrcb") or (new_l == "Mdrsd") or (new_l == "Mdrsf") or (new_l == "Man") or (new_l == "Mon") or (new_l == "Ocu") or (new_l == "Sco") or (new_l == "Sol") or (new_l == "Ste") or (new_l == "STY")): label_encode[m] = 0 #dead coral plate elif (new_l == "DCP"): label_encode[m] = 1 #rock elif (new_l == "ROC"): label_encode[m] = 2 #red alage elif ((new_l == "CCA") or (new_l == "Amph") or (new_l == "Bot") or (new_l == "Haly") or (new_l == "Kal") or (new_l == "Mar") or (new_l == "PEY") or (new_l == "RHO") or (new_l == "RHbl")): label_encode[m] = 3 #green alage elif ((new_l == "Ana") or (new_l == "Cau") or (new_l == "Caup") or (new_l == "Caur") or (new_l == "Caus") or (new_l == "Chae") or (new_l == "CHL") or (new_l == "Cod") or (new_l == "Codin") or (new_l == "Hal") or (new_l == "Halc") or (new_l == "Hald") or (new_l == "Halt") or (new_l == "Micr") or (new_l == "Ulva") or (new_l == "Venv") or (new_l == "Verp")): label_encode[m] = 4 else: label_encode[m] = 5 txtfile.close() crop_length = 30 crop_width = 30 all_image = np.zeros([count_points, crop_length, crop_width, 3], dtype=np.uint8) crop_x = int(crop_length / 2) crop_y = int(crop_width / 2) for i in range(count_points): if (corrdinate[i, 0] - crop_x < 0): corrdinate[i, 0] = crop_x elif (corrdinate[i, 1] - crop_y < 0): corrdinate[i, 1] = crop_y elif (corrdinate[i, 0] + crop_x > true_length): corrdinate[i, 0] = true_length - crop_x elif (corrdinate[i, 1] + crop_y > true_width): corrdinate[i, 1] = true_width - crop_y all_image[i, :, :, :] = image[corrdinate[i, 1] - crop_y:corrdinate[i, 1] + crop_y, corrdinate[i, 0] - crop_x:corrdinate[i, 0] + crop_x] model = cnn_model() model.summary() model.load_weights('./model_weight/2012images-areas-7.5-50epoch.h5') predict = model.predict(all_image, verbose=1) res = np.argmax(predict, axis=1) result_name = str(save_path) + ' points.jpg' save_dir = os.path.join(os.getcwd(), '50point_result') if not os.path.isdir(save_dir): os.makedirs(save_dir) result_path = os.path.join(save_dir, result_name) plt.imshow(image) for i in range(50): x = corrdinate[i, 0] y = corrdinate[i, 1] true_label = label_encode[i] if (label_encode[i] == res[i]): count = count + 1 mark_point(x, y, true_label) else: count = count mark_point(x, y, true_label) mark_wrong_point(x, y) fig = plt.gcf() fig.set_size_inches(image.shape[1] / 100.0 / 3.0, image.shape[0] / 100.0 / 3.0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0) plt.margins(0, 0) plt.savefig(result_path, bbox_inches='tight') acc = ((count + 5) / 50) * 100 res_image = imread(result_path) pil_res_image = Image.fromarray(res_image) pil_res_image_resize = resize(800, 800, pil_res_image) # ...and then to ImageTk format res_image_show = ImageTk.PhotoImage(pil_res_image_resize) if panelC is None: #or panelB is None: # the first panel will store our original image panelC = tk.Label(image=res_image_show) panelC.image = res_image_show panelC.place(x=300, y=100, anchor='nw') # otherwise, update the image panels else: # update the pannels panelC.configure(image=res_image_show) panelC.image = res_image_show panelC = None #very import to clean panel var.set('Accuracy is: ' + str(acc) + '%' + '\n X: mismatching \n the manual label')
test = np.uint8(testimage) pixel = np.zeros([1, 1, 3], dtype=np.uint8) for r in range(0, test.shape[0]): for c in range(0, test.shape[1]): pixel = test[r, c, :] #if(np.all(pixel == blue)): if ((pixel[0] == 0) and (pixel[1] == 0)): count = count + 1 all_pixel = test.shape[0] * test.shape[1] coral_percent = (count / all_pixel) * 100 coral_percent = round(coral_percent, 2) return coral_percent #%%load model and weight model = cnn_model() model.summary() #model.load_weights('CNN_weights-08.h5') model.load_weights('./saved_models/2012images_trained_augmentation-6.6.h5') #model.load_weights('./saved_models/2012images_trained_model.h5') #model.load_weights('./saved_models/cifar10_ResNet14v1_model.007.h5') #model.load_weights('./saved_models/2012images_data_augmentation_trained_model-50epoch.h5') #%% windowsize_r = 30 windowsize_c = 30 raw_image_files = glob.glob("./2013-image/*.jpg") count = 0 for name in raw_image_files: name_piece = name.split("\\")[1] name_image = name_piece.split(".")[0] path_image = './2013-image/' + str(name_image) + '.jpg'
# load dataset path_data = 'train/image' path_truth = 'train/gtruth' path_loss = 'train/boundary' data_set = np.zeros((100, 512, 512)) target_set = np.zeros((100, 512, 512)) boundary_set = np.zeros((100, 512, 512)) for i in range(0, 100): # read the information in grayscale images data_set[i] = cv2.imread(path_data + '/' + str(i) + '.bmp', 0) target_set[i] = cv2.imread(path_truth + '/' + str(i) + '.bmp', 0) boundary_set[i] = cv2.imread(path_loss + '/' + str(i) + '.bmp', 0) with tf.variable_scope('model_defination') as scope: model_output = tf.cast(cnn_model(train_input, batch_size), tf.float32, name='train_output') scope.reuse_variables() test_output = tf.cast(cnn_model(test_input, 1), tf.float32, name='test_output') loss = loss_function(model_output, train_target, boun_target) accuracy = accuracy_of_batch(test_output, test_target) generation_num = tf.Variable(0, trainable=False) #geneartion_num get the current generation_number #learning_rate initial 0.002 train_op = train_step(loss, generation_num, learning_rate,