|
| 1 | +from sklearn.preprocessing import LabelBinarizer |
| 2 | +from sklearn.model_selection import train_test_split |
| 3 | +from sklearn.metrics import classification_report |
| 4 | +from keras.models import load_model |
| 5 | +from Utils.ImageTools import ImageToArrayPreprocessor |
| 6 | +from PrePorcessor.Preprocessor import SimplePreprocessor |
| 7 | +from dataset.SimpleDatasetLoader import SimpleDatasetLoader |
| 8 | +from keras.optimizers import SGD |
| 9 | +from Model.IncludeNet import IncludeNet |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import numpy as np |
| 12 | +import argparse |
| 13 | +from imutils import paths |
| 14 | +import cv2 |
| 15 | + |
| 16 | + |
| 17 | +def show(image): |
| 18 | + # Figure size in inches |
| 19 | + plt.figure(figsize=(15, 15)) |
| 20 | + |
| 21 | + # Show image, with nearest neighbour interpolation |
| 22 | + plt.imshow(image, interpolation='nearest') |
| 23 | + plt.show() |
| 24 | + |
| 25 | + |
| 26 | +def ReadyToUseImage(im): |
| 27 | + image_blur = cv2.GaussianBlur(im, (7, 7), 0) |
| 28 | + image_blur_hsv = cv2.cvtColor(image_blur, cv2.COLOR_RGB2HSV) |
| 29 | + min_red = np.array([80, 60, 140]) |
| 30 | + max_red = np.array([255, 255, 255]) |
| 31 | + image_red1 = cv2.inRange(image_blur_hsv, min_red, max_red) |
| 32 | + big_contour, mask = find_biggest_contour(image_red1) |
| 33 | + overlay_mask(mask, im) |
| 34 | + moments = cv2.moments(mask) |
| 35 | + centre_of_mass = ( |
| 36 | + int(moments['m10'] / moments['m00']), |
| 37 | + int(moments['m01'] / moments['m00']) |
| 38 | + ) |
| 39 | + image_with_com = im.copy() |
| 40 | + # cv2.rectangle(image_with_com,10,(0,255,0),-1,cv2.LINE_AA) |
| 41 | + cv2.circle(image_with_com, centre_of_mass, 10, (0, 255, 0), -1, cv2.LINE_AA) |
| 42 | + # show(image_with_com) |
| 43 | + image_with_ellipse = im.copy() |
| 44 | + ellipse = cv2.fitEllipse(big_contour) |
| 45 | + # print(centre_of_mass) |
| 46 | + dst = cv2.bitwise_and(im, im, mask=mask) |
| 47 | + |
| 48 | + return (dst) |
| 49 | + |
| 50 | + |
| 51 | +def find_biggest_contour(image): |
| 52 | + image = image.copy() |
| 53 | + s, contours, hierarchy = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) |
| 54 | + biggest_contour = max(contours, key=cv2.contourArea) |
| 55 | + mask = np.zeros(image.shape, np.uint8) |
| 56 | + cv2.drawContours(mask, [biggest_contour], -1, 255, -1) |
| 57 | + return biggest_contour, mask |
| 58 | + |
| 59 | + |
| 60 | +def overlay_mask(mask, image): |
| 61 | + rgb_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) |
| 62 | + img = cv2.addWeighted(rgb_mask, 0.5, image, 0.5, 0) |
| 63 | + # show(img) |
| 64 | + |
| 65 | + |
| 66 | +ap = argparse.ArgumentParser() |
| 67 | +ap.add_argument("-d", "--dataset", required=True, help="halloo insert dataset") |
| 68 | +ap.add_argument("-m", "--model", required=True, help="path to output model") |
| 69 | +args = vars(ap.parse_args()) |
| 70 | +size = 50 |
| 71 | +classLabels = ["EOSINOPHIL", "LYMPHOCYTE", "MONOCYTE", "NEUTROPHIL"] |
| 72 | +print("[INFO] sampling images ...") |
| 73 | +imagePaths = np.array(list(paths.list_images(args["dataset"]))) |
| 74 | +idxs = np.random.randint(0, len(imagePaths), size=(10,)) |
| 75 | +imagePaths = imagePaths[idxs] |
| 76 | +sp = SimplePreprocessor(size, size) |
| 77 | +iap = ImageToArrayPreprocessor() |
| 78 | +sdl = SimpleDatasetLoader(preprocessors=[sp, iap]) |
| 79 | +(data, labels) = sdl.load(imagePaths) |
| 80 | +data = data.astype("float") / 255.0 |
| 81 | +print("[INFO] loading pre-trained network ...") |
| 82 | +model = load_model(args["model"]) |
| 83 | +print("[INFO] predicting ...") |
| 84 | +preds = model.predict(data, batch_size=size).argmax(axis=1) |
| 85 | +print(preds) |
| 86 | +for (i, imagePath) in enumerate(imagePaths): |
| 87 | + # load the example image, draw the prediction, and display it |
| 88 | + # to our screen |
| 89 | + image = cv2.imread(imagePath) |
| 90 | + # image=ReadyToUseImage(image) |
| 91 | + cv2.putText(image, "Label: {}".format(classLabels[preds[i]]), |
| 92 | + (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) |
| 93 | + cv2.imshow("Image", image) |
| 94 | + cv2.waitKey(0) |
0 commit comments