Exemple #1
0
# loop over image pyramid
for image in image_pyramid(resized, scale=PYR_SCALE, min_size=ROI_SIZE):
    # loop over sliding window locations
    for x, y, roi in sliding_window(image, WIN_STEP, ROI_SIZE):
        # take ROI and pre-process it so we can classify region with Keras
        roi = img_to_array(roi)
        roi = np.expand_dims(roi, axis=0)
        roi = imagenet_utils.preprocess_input(roi)

        batch_ROIs.append(roi)
        batch_locs.append((x, y))
    
    if len(batch_ROIs) == BATCH_SIZE:
        batch_ROIs = np.vstack(batch_ROIs)
        # classify batch, then reset batch ROIs and (x, y)-coords
        labels = classify_batch(model, batch_ROIs, batch_locs, labels, min_prob=args['confidence'])

        # reset batch_ROIs and batch_locs
        batch_ROIs = []
        batch_locs = []

# check to see if there are any remaining ROIs that still need to be classified
if len(batch_ROIs) > 0:
    batch_ROIs = np.vstack(batch_ROIs)
    labels = classify_batch(model, batch_ROIs, batch_locs, labels, min_prob=args['confidence'])

# show how long detection preocess took
end = time.time()
print(f'[INFO] detections took {end-start:.4f} seconds')

# loop over labels for each of detected objects in image
Exemple #2
0
def main():
    """Run simple object detection
    """
    # construct the argument parse and parse the arguments
    args = argparse.ArgumentParser()
    args.add_argument("-i", "--image", required=True, help="path to the input image")
    args.add_argument(
        "-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections"
    )
    args = vars(args.parse_args())

    # load our the network weights from disk
    print("[INFO] loading network...")
    model = ResNet50(weights="imagenet", include_top=True)
    # initialize the object detection dictionary which maps class labels
    # to their predicted bounding boxes and associated probability
    labels = {}
    # load the input image from disk and grab its dimensions
    orig = cv2.imread(args["image"])
    # resize the input image to be a square
    resized = cv2.resize(orig, INPUT_SIZE, interpolation=cv2.INTER_CUBIC)
    # initialize the batch ROIs and (x, y)-coordinates
    batch_rois = None
    batch_locs = []
    # start the timer
    print("[INFO] detecting objects...")
    start = time.time()
    # loop over the image pyramid
    for image in image_pyramid(resized, scale=PYR_SCALE, min_size=ROI_SIZE):
        # loop over the sliding window locations
        for (x, y, roi) in sliding_window(image, WIN_STEP, ROI_SIZE):
            # take the ROI and pre-process it so we can later classify the region with Keras
            roi = img_to_array(roi)
            roi = np.expand_dims(roi, axis=0)
            roi = imagenet_utils.preprocess_input(roi)
            # if the batch is None, initialize it
            if batch_rois is None:
                batch_rois = roi
            # otherwise, add the ROI to the bottom of the batch
            else:
                batch_rois = np.vstack([batch_rois, roi])
            # add the (x, y)-coordinates of the sliding window to the batch
            batch_locs.append((x, y))
            # check to see if our batch is full
            if len(batch_rois) == BATCH_SIZE:
                # classify the batch, then reset the batch ROIs and (x, y)-coordinates
                labels = classify_batch(model, batch_rois, batch_locs, labels, min_probability=args["confidence"])
                # reset the batch ROIs and (x, y)-coordinates
                batch_rois = None
                batch_locs = []

    # check to see if there are any remaining ROIs that still need to be classified
    if batch_rois is not None:
        labels = classify_batch(model, batch_rois, batch_locs, labels, min_probability=args["confidence"])
    # show how long the detection process took
    end = time.time()
    print("[INFO] detections took {:.4f} seconds".format(end - start))
    # loop over the labels for each of detected objects in the image
    for k in labels.keys():
        # clone the input image so we can draw on it
        clone = resized.copy()
        # loop over all bounding boxes for the label and draw them on the image
        for (box, _) in labels[k]:
            (x, y, w, h) = box
            cv2.rectangle(clone, (x, y), (w, h), (0, 255, 0), 2)

        # show the image *without* apply non-maxima suppression
        cv2.imshow("Without NMS", clone)
        clone = resized.copy()
        # grab the bounding boxes and associated probabilities for each detection, then apply
        # non-maxima suppression to suppress weaker, overlapping detections
        boxes = np.array([p[0] for p in labels[k]])
        probability = np.array([p[1] for p in labels[k]])
        boxes = non_max_suppression(boxes, probability)
        # loop over the bounding boxes again, this time only drawing the
        # ones that were *not* suppressed
        for (x, y, w, h) in boxes:
            cv2.rectangle(clone, (x, y), (w, h), (0, 0, 255), 2)
        # show the output image
        print("[INFO] {}: {}".format(k, len(boxes)))
        cv2.imshow("With NMS", clone)
        cv2.waitKey(0)
Exemple #3
0
for image in image_pyramid(resized, scale=PYR_SCALE, minSize=ROI_SIZE):
    for (x, y, roi) in sliding_window(image, WIN_STEP, ROI_SIZE):
        roi = img_to_array(roi)
        roi = np.expand_dims(roi, axis=0)
        roi = imagenet_utils.preprocess_input(roi)

        if batchROIs is None:
            batchROIs = roi
        else:
            batchROIs = np.vstack([batchROIs, roi])

        batchLocs.append((x, y))

        if len(batchROIs) == BATCH_SIZE:
            labels = classify_batch(model, batchROIs, batchLocs, labels, minProb=args["confidence"])

            batchROIs = None
            batchLocs = []

if batchROIs is not None:
    labels = classify_batch(model, batchROIs, batchLocs, labels, minProb=args["confidence"])

end = time.time()
print("[INFO] detections took {:.4f} seconds".format(end - start))

for k in labels.keys():
    clone = resized.copy()

    for (box, prob) in labels[k]:
        (xA, yA, xB, yB) = box
Exemple #4
0
def plant_growth_health_tracking():
    # define the paths to the Keras deep learning model
    MODEL_PATH = "plantgrowthv2.model"

    #INPUT_SIZE: the width and height of our input to whcih the image is resized prior to being fed into the CNN
    INPUT_SIZE = (400, 400)
    #PYR_SCALE: the scale of our image pyramid
    PYR_SCALE = 1.5
    #WIN_STEP: step of our sliding window
    WIN_STEP = 8
    #ROI_SIZE: input ROI size to our CNN as if we were perforimg classification
    ROI_SIZE = (96, 96)
    #BATCH_SIZE: size of batch to be passed through the CNN
    BATCH_SIZE = 32

    # load the model
    print("loading model...")
    model = load_model(MODEL_PATH)

    #initialize the object detection dictionary which maps class labels to their predicted bounding boxes and associated probability
    labels = {
        "Futterkohl_Gruner_Ring": [],
        "Lollo_Rossa": [],
        "Mangold_Lucullus": [],
        "Pak_Choi": [],
        "Tatsoi": []
    }

    # initialize the video stream and allow the camera sensor to warm up
    print("starting video stream...")
    #vs = VideoStream(src=0).start()
    vs = VideoStream(usePiCamera=True).start()
    time.sleep(2.0)
    fps = FPS().start()

    # loop over the frames from the video stream
    while fps._numFrames < 100:
        # grab the frame from the threaded video stream and resize it
        # to be a square
        frame = vs.read()
        (h, w) = frame.shape[:2]
        frame = cv2.resize(frame, INPUT_SIZE, interpolation=cv2.INTER_CUBIC)

        #initialize the batch ROIs and (x,y)-coordinates
        batchROIs = None
        batchLocs = []

        #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        #a typical green will have still have some red and blue
        #we will get the lower-light mixes of all colors still
        lower_green = np.array([30, 100, 100])
        upper_green = np.array([90, 255, 255])

        #create a mask for a specific range
        #pixels in the range will be converted to pure white
        #pixels outside the range will be converted to black
        mask = cv2.inRange(hsv, lower_green, upper_green)

        #restore 'green-ness' by running a bitwise operation
        #show color when there is the frame AND the mask
        res = cv2.bitwise_and(frame, frame, mask=mask)

        date_yyyymmdd = time.strftime("%Y-%m-%d")
        #saves 5 color filtered images in JPG format in the working directory for post
        for index in range(1, 6):
            cv2.imwrite(
                str(date_yyyymmdd) + "_pic_" + str(index) + ".jpg", res)

        #start the timer
        print("detecting plants...")

        # loop over the image pyramid
        for image in image_pyramid(frame, scale=PYR_SCALE, minSize=ROI_SIZE):
            # loop over the sliding window locations
            for (x, y, roi) in sliding_window(image, WIN_STEP, ROI_SIZE):
                #take the ROI and preprocess it so we can classify the region with Keras
                roi = img_to_array(roi)
                roi = np.expand_dims(roi, axis=0)

                #if batchROIs is None, initialize it
                if batchROIs is None:
                    batchROIs = roi

                #otherwise add the ROI to the bottom of the batch
                else:
                    batchROIs = np.vstack([batchROIs, roi])

                # append the (x,y)-coordinates of the sliding window to the batch
                batchLocs.append((x, y))

        #check to see if the batch is full
        if len(batchROIs) == BATCH_SIZE:
            #classify the batch, then reset the batch ROIs and (x,y)-coordinates
            labels = classify_batch(model,
                                    batchROIs,
                                    batchLocs,
                                    labels,
                                    minProb=0.5)

        #reset the batch ROIs and (x,y) -coordinates
        batchROIs = None
        batchLocs = []

        #check to see if there are any remaining ROIs that still need to be classified
        if batchROIs is not None:
            labels = classify_batch(model,
                                    batchROIs,
                                    batchLocs,
                                    labels,
                                    minProb=0.5)

        #loop over all the class labels for each detected object in the image
        for classLabel in labels.keys():
            #grab the bounding boxes and associated probabilities for each detection and apply non-maxima suppression
            #to suppress weaker overlapping detections
            boxes = np.array(p[0] for p in labels[classLabel])
            proba = np.array(p[1] for p in labels[classLabel])
            boxes = non_max_suppression(boxes, proba)

            #loop over the bounding boxes again, this time only drawing the ones that were not suppressed
            for (xA, yA, xB, yB) in boxes:
                title = "{}: {:.2f}%".format(classLabel, proba * 100)

                #if str(classLabel) == "Lollo_Rossa":
                cv2.rectangle(frame, (xA, yA), (xB, yB), (255, 0, 255), 2)
                frame = cv2.putText(frame, title, (xA, yB),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                                    (255, 0, 255), 2)

                #elif str(classLabel) == "Tatsoi":
                #    cv2.rectangle(frame, (xA, yA), (xB, yB), (255,0,0), 2)
                #    frame = cv2.putText(frame, title, (xA, yB), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0), 2)

            if len(labels[classLabel]) > 5:
                # send a text notification to firebase for app to receive
                triggered("Young seedling has matured. Time to transplant" +
                          str(classLabel))

        # show the output frame
        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break

        fps.update()

    # do a bit of cleanup
    print("cleaning up...")
    fps.stop()
    cv2.destroyAllWindows()
    vs.stop()