示例#1
0
    def init_model(self, model_name):
        model = self.models[model_name]

        # Model already initialized; return from method
        if model.get('predictor') is not None:
            return

        weights_file = rr.get_filename('package://cv/models/{}'.format(model['weights']), use_protocol=False)

        predictor = Model.load(weights_file, model['classes'])

        publisher_name = '{}/{}'.format(model['topic'], self.camera)
        publisher = rospy.Publisher(publisher_name, CVObject, queue_size=10)

        model['predictor'] = predictor
        model['publisher'] = publisher
示例#2
0
    def init_model(self, model_name):
        model = self.models[model_name]

        # Model already initialized; return from method
        if model.get('predictor') is not None:
            return

        path = os.path.dirname(__file__)
        weights_file = os.path.join(path, '../models', model['weights'])

        predictor = Model.load(weights_file, model['classes'])

        publisher_name = '{}/{}'.format(model['topic'], self.camera)
        publisher = rospy.Publisher(publisher_name, CVObject, queue_size=10)

        model['predictor'] = predictor
        model['publisher'] = publisher
# findFlag.py

from detecto.core import Model
import cv2  #Used for loading the image into memory

# First, let's load our trained model from the Training section
# We need to specify the label which we want to find (the same one from Classification and Training)
model = Model.load('model.pth', ['aboriginal_flag'])

# Now, let's load a sample image into memory
# Change the file name below if you want to test other potential samples
image = cv2.imread("samples/sample.jpg")

# model.predict() is the method we call with our image as an argument
# to try find our desired object in the sample image using our pre-trained model.
# It will do a bit of processing and then spit back some numbers.
# The numbers define what it thinks the bounding boxes are of potential matches.
# And the probability that the bounding box is recognizing the object (flag).
labels, boxes, scores = model.predict(image)

# Below we are just printing the results, predict() will
# give back a couple of arrays that represent the bounding box coordinates and
# probability that the model believes that the box is a match
# The coordinates are (xMin, yMin, xMax, yMax)
# Using this data, you could just open the original image in an image editor
# and draw a box around the printed coordinates
print(labels, boxes, scores)

# WARNING: You don't have to understand this part, I barely do.
# All this code does is draw rectangles around the model predictions above
# and outputs to the display for your viewing pleasure.
示例#4
0
from sys import argv
import json
#from DetectDigits import get_output_image
import cv2
import os
import ocr

from detecto.core import Model
from detecto.utils import read_image

model = Model.load('localization_model.pth', ['digits'])

path = argv[1]
image_name = argv[2]
filename = path + '\\' + image_name
data = {}

#cascade = cv2.CascadeClassifier(path + "\\Cascades\\cascade.xml")
#img = cv2.imread(filename)
img = read_image(filename)
top_preds = model.predict_top(img)
res = top_preds[1].numpy()[0]
#numbers = cascade.detectMultiScale(img, 1.1, 3)

x, y = int(res[0]), int(res[1])
w = int(res[2])
h = int(res[3])
w = w - x
h = h - y
img = img[y:y + h, x:x + w]
temp_path = "res.png"
示例#5
0
def detect(radar, img, file, locDat, sweep, detdir, vis, cint):
    model = Model.load('RASRmodl.pth', ['fall'])
    pred = model.predict(img)
    #print(max(pred[2]))
    for n in range(len(pred[1])):
        if (pred[2][n] > cint):
            bound = 0.5  # The unmapped location data is about 2x as far as it should be.
            # I don't know why this, and this is a temp solution.
            xdat, ydat = bound * 1000 * locDat[0], bound * 1000 * locDat[1]

            t = round(locDat[2], 2)
            name, date, btime, dtstr = stringed(file)
            atime = (datetime.strptime(btime, '%m/%d/%Y %H:%M:%S') +
                     timedelta(seconds=t))

            x0p, y0p, x1p, y1p = float(pred[1][n][0]), float(
                pred[1][n][1]), float(pred[1][n][2]), float(pred[1][n][3])
            xp, yp = (x1p + x0p) / 2, (y1p + y0p) / 2

            xdm = [np.amin(xdat), np.amax(xdat)]
            ydm = [np.amin(ydat), np.amax(ydat)]
            Xv = np.linspace(xdm[0], xdm[1], 2500)
            Yv = np.linspace(ydm[1], ydm[0], 2500)

            x, y = Xv[int(xp)], Yv[int(yp)]
            x0, y0 = Xv[int(x0p)], Yv[int(y0p)]
            x1, y1 = Xv[int(x1p)], Yv[int(y1p)]

            if (
                    vis == True
            ):  # Saves the image with a bounding box, detection type, and confidence level
                fig = plt.figure(figsize=(25, 25))
                ax = fig.add_axes([0, 0, 1, 1])
                ax.imshow(img)
                w, h = abs(x0p - x1p), abs(y0p - y1p)
                rect = patches.Rectangle((x0p, y0p),
                                         w,
                                         h,
                                         linewidth=1,
                                         edgecolor='r',
                                         facecolor='none')
                ax.add_patch(rect)
                detstr = pred[0][n] + ': ' + str(round(float(pred[2][n]), 2))
                plt.text(x0p + w / 2,
                         y0p - 5,
                         detstr,
                         fontsize=8,
                         color='red',
                         ha='center')
                imname = detdir + file + '_' + sweep + '_detected' + '.png'
                plt.savefig(imname, bbox_inches='tight')

            # Finding Geodetic coordinates from relative distance to site:
            z = np.sqrt(x**2 + y**2) * np.tan(np.radians(float(sweep)))
            sitealt, sitelon, sitelat = float(radar.altitude['data']), float(
                radar.longitude['data']), float(radar.latitude['data'])
            lon, lat = np.around(
                pyart.core.cartesian_to_geographic_aeqd(
                    x, y, sitelon, sitelat), 2)
            lon0, lat0 = np.around(
                pyart.core.cartesian_to_geographic_aeqd(
                    x0, y0, sitelon, sitelat), 3)
            lon1, lat1 = np.around(
                pyart.core.cartesian_to_geographic_aeqd(
                    x1, y1, sitelon, sitelat), 3)
            lon, lon0, lon1 = -lon, -lon0, -lon1
            alt = round(z + sitealt, 2)

            return [lat, lon, alt, atime], [lat0, lon0, lat1, lon1, alt, atime]
        else:
            pass