def image_recognition(image_path,save_path):
    image = utils.read_image(image_path)
    model = core.Model()

    labels, boxes, scores = model.predict_top(image)

    fig, ax = plt.subplots(1)
    # If the image is already a tensor, convert it back to a PILImage
    # and reverse normalize it
    if isinstance(image, torch.Tensor):
        image = reverse_normalize(image)
        image = transforms.ToPILImage()(image)
    ax.imshow(image)

    # Show a single box or multiple if provided
    if boxes.ndim == 1:
        boxes = boxes.view(1, 4)

    if labels is not None and not _is_iterable(labels):
        labels = [labels]

    # Plot each box
    for i in range(boxes.shape[0]):
        box = boxes[i]
        width, height = (box[2] - box[0]).item(), (box[3] - box[1]).item()
        initial_pos = (box[0].item(), box[1].item())
        rect = patches.Rectangle(initial_pos, width, height, linewidth=1,
                                 edgecolor='r', facecolor='none')
        if labels:
            ax.text(box[0] + 5, box[1] - 5, '{}'.format(labels[i]), color='red')

        ax.add_patch(rect)

    plt.savefig(save_path)
Ejemplo n.º 2
0
def search():

    btn['state'] = NORMAL  #open image button is returened to enabled

    fracturedataset = core.Dataset(
        'trainingimagesgui/'
    )  #initializes the dataset the parameter is the folder in which to get the images for the dataset

    mymodel = core.Model([
        'crack'
    ])  #calls the neural network and it is looking for boxes labelled crack
    print("model imported"
          )  #print function to see if the neural network has been called

    mymodel.fit(fracturedataset)  #train the neuralnetwork on the dataset
    print("finished training custom dataset"
          )  #prints to show that the neural network has finished training
    #testing the neural network
    testimage = utils.read_image(
        filename
    )  #read the varible testimage which has the location of what is stored in variable filename
    print(filename)  #test to see the correct location is being pulled
    endresult = mymodel.predict(
        testimage)  #run the test image on the now trained neural network
    print(
        "prediction complete"
    )  #test to see that the test image has successfully ran on the neural network
    labels, boxes, scores = endresult  #all the parameters the test image will have
    print(labels)  #test to see if the correct label is printed
    print(boxes)  #this will print the coordiantes of the boxes
    print(scores)  #prints a score of how confident the neural network is
    visualize.show_labeled_image(testimage, boxes,
                                 labels)  #display the image on the screen
Ejemplo n.º 3
0
def get_object_box(image, object_name):
    model = core.Model()
    labels, boxes, _ = model.predict_top(image)
    if object_name in labels:
        idx = labels.index(object_name)
        return boxes[idx]
    else:
        return None
Ejemplo n.º 4
0
def predict(img):
    model = core.Model()

    predictions = model.predict_top(img)
    labels, boxes, scores = predictions

    l = labels
    s = []
    for i in scores:
        s.append(i.item())
    result = {"labels": l, "scores": s}

    return result
Ejemplo n.º 5
0
    def center_coordinates(self):
        model = core.Model()
        # print(model.predict_top(image))
        # person_is_found = 0
        try:
            labels, boxes, scores = model.predict_top(self.frame_input)
            min_x = 0
            min_y = 0
            max_x = 0
            max_y = 0

            for lbl, box in zip(
                    labels,
                    boxes):  # zip stops when the shorter list is finished
                if lbl is "person":
                    # print(type(box)) # finding the person's box
                    a, b, c, d = box  # a,b,c,d are "torch.Tensor" objects
                    min_x = int(a)
                    min_y = int(b)
                    max_x = int(c)
                    max_y = int(d)
                    # print(min_x, min_y, max_x, max_y)
                    # print("person HAS BEEN found")
                    break
        except:
            # if no person has been found
            print("&&&&&&&&&&&& NO person found &&&&&&&&&&&&")
            return (0, 0), 0

        # calculate main vector length, radius = (main vector length) * 0.2
        radius = (((max_x - min_x)**2 + (max_y - min_y)**2)**0.5) * 0.2
        radius = int(radius)
        # print(radius)

        center_x = int((max_x + min_x) / 2)
        center_y = int((max_y + min_y) / 2)
        # print(center_X, center_y)
        center_coordinates = (center_x, center_y)

        # visualize.show_labeled_image(image, boxes, labels)

        # CAN RETURN A CROPPED PERSON
        # img = cv2.imread(image_path)
        # crop_img = img[min_y:max_y, min_x:max_x]
        # cv2.imshow("cropped", crop_img)
        # cv2.waitKey(0)
        # return crop_img, center_coordinates

        cv2.circle(self.frame_output, center_coordinates, radius, (0, 0, 255),
                   10)
        return  # center_coordinates, radius
Ejemplo n.º 6
0
def draw_boxes(original_image, new_image):
    model = core.Model()
    labels, boxes, scores = model.predict_top(original_image)
    # print(labels, boxes, scores)

    for i, box in enumerate(boxes):
        if scores[i] > 0.6:
            x, y, x_low, y_low = box
            w = x_low - x
            h = y_low - y

            cv2.rectangle(new_image, (int(x), int(y)),
                          (int(x + w), int(y + h)), (255, 0, 0), 2)

    new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB)

    return new_image
Ejemplo n.º 7
0
    def trainMountingConfigClassifier(self,
                                      train_path,
                                      val_path,
                                      device=torch.device('cuda')):
        """
        This function uses Faster R-CNN ResNet50 FPN as the base network
        and as a transfer learning framework to train a model that performs
        object detection on the mounting configuration of solar arrays. It
        uses the training data to locate and classify mounting configuration
        of the solar installation. It uses the validation data to prevent
        overfitting and to test the prediction on the fly.

        Parameters
        -----------
        train_path: string
            This is the path to the folder that contains the training images
            Note that the directory must be structured in this format:
                    train_path/
                        ...images/
                            ......a_image_1.png
                            ......a_image_2.png
                        ...annotations/
                            ......b_image_1.xml
                            ......b_image_2.xml
        val_path: string
            This is the path to the folder that contains the validation images
            Note that the directory must be structured in this format:
                    val_path/
                        ...images/
                            ......a_image_1.png
                            ......a_image_2.png
                        ...annotations/
                            ......b_image_1.xml
                            ......b_image_2.xml
        device: string
            This argument is passed to the Model() class in Detecto.
            It determines how to run the model: either on GPU via Cuda
            (default setting), or on CPU. Please note that running the
            model on GPU results in significantly faster training times.

        Returns
        -----------
        model: detecto.core.Model object
            The final trained mounting configuration object detection
            model.
        """
        # Convert the data set combinations (png + xml) to a CSV record.
        val_labels_path = (val_path + '/annotations.csv')
        train_labels_path = (train_path + '/annotations.csv')
        utils.xml_to_csv(train_path + '/annotations/', train_labels_path)
        utils.xml_to_csv(val_path + '/annotations/', val_labels_path)
        # Custom oversampling to balance out our classes
        train_data = pd.read_csv(train_labels_path)
        class_count = pd.Series(train_data['class'].value_counts())
        train_data_resampled = train_data.copy()
        for index, count in class_count.iteritems():
            number_times_resample = class_count.max() - count
            # Randomly sample a class X times
            class_index_list = list(
                train_data[train_data['class'] == index].index)
            # Resample the list with with replacement
            idx_to_duplicate = choices(class_index_list,
                                       k=number_times_resample)
            for idx in idx_to_duplicate:
                dup = train_data.loc[idx]
                # Add to the dataframe
                train_data_resampled = \
                    train_data_resampled.append(dup, ignore_index=True)
        # Reindex after all of the duplicates have been added
        train_data_resampled = train_data_resampled.reset_index(drop=True)
        # Re-write the resampled data set
        train_data_resampled.to_csv(train_labels_path, index=False)
        custom_transforms = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize(800),
            transforms.ToTensor(),
            utils.normalize_transform()
        ])
        # Load in the training and validation data sets
        dataset = core.Dataset(train_labels_path,
                               train_path + '/images',
                               transform=custom_transforms)
        val_dataset = core.Dataset(val_labels_path, val_path + '/images')
        # Customize training options
        loader = core.DataLoader(dataset,
                                 batch_size=self.batch_size,
                                 shuffle=True)
        model = core.Model([
            "ground-fixed", "carport-fixed", "rooftop-fixed",
            "ground-single_axis_tracker"
        ],
                           device=device)
        losses = model.fit(loader,
                           val_dataset,
                           epochs=self.no_of_epochs,
                           learning_rate=self.learning_rate,
                           verbose=True)
        plt.plot(losses)
        plt.show()
        return model
Ejemplo n.º 8
0
            labels2.append(labels[i])
            if len(boxes2) == 0:
                boxes2 = boxes[i]
            else:
                boxes2 = torch.cat([boxes2,boxes[i]],-1)
            scores2.append(scores[i])
    boxes2 = boxes2.view(-1, 4)

    return labels2, boxes2, scores2
    
    
    
    from detecto import core, utils, visualize

image = utils.read_image('../Label Fotos/COMBINED_DATA/IMG_012674.jpg')
model = core.Model()

labels, boxes, scores = model.predict_top(image)

show_labeled_image(image, boxes, labels)


from detecto import core, utils, visualize

dataset = core.Dataset('/content/drive/My Drive/AEC Hackathon - LifeSavers/Label Fotos/COMBINED_DATA')
print(len(dataset))
#model = core.Model(['person','helmet','nohelmet','nowest'])
model = core.Model(['person'])
model.fit(dataset)
print("Training done")
from detecto import core, utils, visualize
from torchvision import transforms

augmentations = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomHorizontalFlip(0.5),
    transforms.ColorJitter(saturation=0.5),
    transforms.ToTensor(),
    utils.normalize_transform(),
])

val_dataset = core.Dataset('/dataset/validation_images/')
dataset = core.Dataset('//dataset/train_images/', transform=augmentations)
model = core.Model(['rust'])
loader = core.DataLoader(dataset, batch_size=2, shuffle=True)

losses = model.fit(loader,
                   val_dataset,
                   epochs=10,
                   learning_rate=0.001,
                   lr_step_size=5,
                   verbose=True)

model.save('model/model_weights.pth')
Ejemplo n.º 10
0
from detecto import core, utils, visualize

dataset = core.Dataset('images/')
model = core.Model(['fit_avocado', 'unfit_avocado'])
model.fit(dataset)

image = utils.read_image('images/10.jpg')
predictions = model.predict(image)

# predictions format: (labels, boxes, scores)
labels, boxes, scores = predictions
model.save('avocado_weights.pth')
Ejemplo n.º 11
0
import cv2
from detecto import core, utils, visualize
import numpy as np

dataset = core.Dataset(
    'C:/Users/ABC/Desktop/New folder/TDAI/Data/sample')  #load data
print(len(dataset))
model = core.Model(['top_left', 'top_right', 'bottom_left',
                    'bottom_right'])  #download model

losses = model.fit(dataset, epochs=30, verbose=True,
                   learning_rate=0.001)  #set parameter formodel and fit

model.save('id_card_4_conner.pth')

frame = ''
image = utils.read_image(frame)
labels, boxes, score = model.predict(image)
print(labels)
print(boxes)

##draw boxes
for i, box in enumerate(boxes):
    box = list(map(int, box))
    x_min, y_min, x_max, y_max = box
    cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
    cv2.putText(image, labels[i], (x_min, y_min), cv2.FONT_HERSHEY_COMPLEX,
                0.5, (0, 255, 0))


#nonmax suppression
Ejemplo n.º 12
0
])


dataset = core.Dataset('labels/',transform=augmentations)
model = core.Model(['baseball',
					  'beer',
					  'can_coke',
					  'can_pepsi',
					  'can_fanta',
					  'can_sprite',
					  'chips_can',
					  'coffee_box',
					  'cracker',
					  'cup',
					  'donut',
					  'fork',
					  'gelatin',
					  'meat',
					  'mustard',
					  'newspaper',
					  'orange',
					  'pear',
					  'plate',
					  'soccer_ball',
					  'soup',
					  'sponge',
					  'sugar',
					  'toy'])
model.fit(dataset)
print('Done training')
# image = utils.read_image('labels/bottle.png')
# predictions = model.predict(image)
Ejemplo n.º 13
0
# Getting all the train images
path = 'images/'
images = []
for file in os.listdir(path):
    if file.endswith(".png"):
        images.append(file)

# In[6]:

images

# In[7]:

# Testing out the pre-trained model to see if it detects anything in the screenshot
image = utils.read_image(path + images[0])
model = core.Model()

labels, boxes, scores = model.predict_top(image)
visualize.show_labeled_image(image, boxes, labels)

# The pre-trained model failed as expected. Though it did detect one of the tiles as a 'book'.

# << Label screenshots with labelImg >>

# In[10]:

# Train model with custom labels
dataset = core.Dataset(path)
model = core.Model(['tile'])
model.fit(dataset)
Ejemplo n.º 14
0
# prediction.setModelPath( "C:\\Users\\cw263\\OneDrive\\Desktop\\NSO_LIFE\\resnet50_weights_tf_dim_ordering_tf_kernels.h5")

# prediction.loadModel()





# predictions, percentage_probabilities = prediction.predictImage("C:\\Users\\cw263\\OneDrive\\Desktop\\image.jpg", result_count=5)

# for index in range(len(predictions)):

#  print(predictions[index] , " : " , percentage_probabilities[index])
#import torch
# from detecto import core, utils, visualize

# image = utils.read_image('C:\\Users\\cw263\\OneDrive\\Desktop\\image.jpg')
# model = core.Model()

# labels, boxes, scores = model.predict_top(image)
# visualize.show_labeled_image(image, boxes, labels)
#print(torch.cuda.is_available())
from detecto import core, utils, visualize
dataset = core.Dataset('images/')
model = core.Model(['chris'])
model.fit(dataset)
image = utils.read_image('images/christopher.jpg')
predictions = model.predict(image)
labels, boxes, scores = predictions
print(labels) 
Ejemplo n.º 15
0
from detecto import core, utils, visualize

dataset = core.Dataset('images/')
model = core.Model(['un_logo'])

model.fit(dataset)

model.save('un_logo_model.pth')
Ejemplo n.º 16
0
 def train(self):
     dataset = core.Dataset('images/')
     model = core.Model(['fit_avocado', 'unfit_avocado'])
     model.fit(dataset)
     self.model = model
Ejemplo n.º 17
0
from __future__ import print_function, division
import pandas as pd
import torch
from detecto import core, utils, visualize
import cv2
import datetime
from urllib.request import urlopen
import numpy as np
device = torch.device("cuda")
model = core.Model(['figure'])
model.load('figure_model_weights_latest4.pth', ['figure'])
index = ['color', 'color_name', 'hex', 'R', 'G', 'B']
df = pd.read_csv('colors.csv', names=index, header=None)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
url = "http://192.168.43.42:80"
CAMERA_BUFFER_SIZE = 4096
stream = urlopen(url + "/stream.jpg")
bts = b''
while True:
    k = cv2.waitKey(1)
    bts += stream.read(CAMERA_BUFFER_SIZE)
    jpghead = bts.find(b'\xff\xd8')
    jpgend = bts.find(b'\xff\xd9')
    figure = 1
    if jpghead > -1 and jpgend > -1:
        jpg = bts[jpghead:jpgend + 2]
        bts = bts[jpgend + 2:]
        frame = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                             cv2.IMREAD_UNCHANGED)
        frame = cv2.resize(frame, (640, 480))
Ejemplo n.º 18
0
from detecto import core, utils, visualize

dataset = core.Dataset('jpegs/')
model = core.Model('Drone')

model.fit(dataset)
Ejemplo n.º 19
0
def run_detect(image_name):
    image = utils.read_image('static/' + image_name)
    model = core.Model()
    labels, boxes, scores = model.predict_top(image)
    return visualize.show_labeled_image(image, boxes, labels)
Ejemplo n.º 20
0
def detecto_m(pic):
    image = utils.read_image(pic)
    model = core.Model()
    labels, boxes, scores = model.predict_top(image)
    result = visualize.show_labeled_image(image, boxes, labels)
    return result
Ejemplo n.º 21
0
from detecto import core, utils, visualize
image = utils.read_image('cat.jpg')
model = core.Model()
labels, boxes, scores = model.predict_top(image)
visualize.show_labeled_image(image, boxes, labels)
from detecto import core, utils, visualize
import matplotlib.pyplot as plt

dataset = core.Dataset('frames/')
model = core.Model(['enemy'])

model.fit(dataset, verbose=True)
model.save('model.pth')