Beispiel #1
0
def predict(image_path, model, topk=1, gpu=True, cat_to_name=None):
    """ Predict the class (or classes) of an image using a trained deep learning model.
    """
    model.to(get_device(gpu))
    img = process_image(image_path)
    img_torch = torch.from_numpy(img)
    img_torch = img_torch.unsqueeze_(0)
    img_torch = img_torch.float()

    with torch.no_grad():
        output = model.forward(img_torch.cuda())

    probabilities = F.softmax(output.data, dim=1).topk(topk)
    index_to_class = {val: key for key, val in model.class_to_idx.items()}

    probs = np.array(probabilities[0][0])
    classes = [cat_to_name[index_to_class[i]] for i in np.array(probabilities[1][0])] if cat_to_name is not None \
        else [index_to_class[i] for i in np.array(probabilities[1][0])]

    # Log the prediction results for the user to see them
    logging.info('\n'.join([
        'Class: {} with probability {}'.format(c, p)
        for c, p in zip(classes, probs)
    ]))

    return probs, classes
Beispiel #2
0
def main():
    args = parse_inputs(predict=True)

    # Load the model and process image
    # Define whether to use GPU or CPU and move model/img
    model = load_checkpoint(args.checkpoint, args.gpu)
    img = process_image(args.path)

    # Get prediction
    probs, classes = predict(img, model, args.top_k, args.gpu)

    # Load file with category names and classify image
    # Results are printed showing the category names and
    # probabilities
    gap = 40
    precision = 3
    with open(args.category_names, "r") as file:
        print("---- RESULTS ----")
        print("Flower name{}Prob(%)".format((gap - 11) * " "))
        print("-" * (gap + 8))

        flower_dict = json.load(file)
        if classes.ndim < 1:
            name = flower_dict[str(classes)]
            prob = str(round(probs * 100, precision))
            space = "." * (gap - len(name))
            print("{}{}{}%".format(name, space, prob))
        else:
            for idx, val in enumerate(classes.tolist()):
                name = flower_dict[str(val)]
                prob = str(round(probs[idx] * 100, precision))
                space = "." * (gap - len(name))
                print("{}{}{}%".format(name, space, prob))
        print("-" * (gap + 8), end='\n\n')
def detect_process():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)


        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            base_path = app_routes.root_path
            relative_path = app_config['heartscan'].get('upload_folder')
            # TODO: replace filename on UUID
            full_path = os.path.join(base_path, relative_path, filename)

            file.save(full_path)

            recognized_image, contour = process.process_image(full_path)
            b64img = b64encode(recognized_image).decode("utf-8")

            os.remove(full_path)
            return render_template("list.html", image=b64img)
    return render_template("upload.html")
def predict(image_path, model, top_k, category_names):
    '''
    Image object ==> top k classes predicted by model along with their probabilities 
    
    This function has three inputs: picture path, mode, and number of top k classes predicted by the model.
    It takes a picture from given path, applys necessary processing, uses the picture for prediction and returns 
    top k classes predicted by the model.
    '''
    my_model = tf.keras.models.load_model(
        model, custom_objects={'KerasLayer': hub.KerasLayer})

    im = Image.open(image_path)
    test_image = np.asarray(im)
    processed_test_image = process_image(test_image)

    ## Model accepts 4-dimension array. To satisfy this requirement we need to expand the processed image of 3 dims to 4 dims
    processed_test_image = np.expand_dims(processed_test_image, axis=0)
    print('Expanded processed image shape is:', processed_test_image.shape)

    ## Read Json file
    with open(category_names, 'r') as f:
        class_names = json.load(f)

    ## predict
    ps = my_model.predict(processed_test_image)

    ## Extract probaility and class label for top k classes with highest probability
    indeces = ps.argsort()[0][::-1][:top_k].tolist()
    probs = ps[0][indeces].tolist()
    classes = [str(i) for i in indeces]
    flowers_name = [
        class_names[str(x + 1)] for x in indeces
    ]  # add 1 to correspond the indexes from the prediction to the indexes in the class_names
    return probs, classes, flowers_name
Beispiel #5
0
    def process(self, inframe, outframe):

        im = inframe.getCvBGR()

        dbg, re, rc = process_image(im)

        # entry
        entry_area, entry_color, entry_h = re

        # cylinder
        cylinder_area, cylinder_color = rc

        entry_area = 0 if entry_area is None else entry_area
        cylinder_area = 0 if cylinder_area is None else cylinder_area

        params = dict(
            entry_color=color_to_rome_color(entry_color),
            cylinder_color=color_to_rome_color(cylinder_color),
            entry_height=int(entry_h),
            entry_area=int(entry_area),
            cylinder_area=int(cylinder_area),
        )

        frame = rome.Frame('jevois_tm_cylinder_cam', **params)
        data = frame.data()
        jevois.sendSerial(data)

        if outframe is not None:
            outframe.sendCvBGR(dbg)
def process_images(path):
    files = os.listdir(path)
    images = []
    titles = []
    for file in files:
        if file[0:3] != "out":
            img = mpimg.imread(path + file)
            result = process_image(img)
            images.append(result)
            titles.append(file)
    show_images(images, 3, titles)
Beispiel #7
0
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$wordle'):
        msg = message.content[8:]
        txt = process.process_text(msg)
        im = process.process_image(txt)

        with BytesIO() as output:
            im.save(output, format="PNG")
            output.seek(0)
            await message.channel.send(file=discord.File(output, 'wordle.png'))
Beispiel #8
0
def run_prediction(model_path: str, base64_image: str) -> List[List[float]]:
    interpreter = tflite.Interpreter(model_path=model_path)
    interpreter.allocate_tensors()

    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    input_shape = input_details[0]['shape']
    input_data = process_image(base64_image, input_shape)
    interpreter.set_tensor(input_details[0]['index'], input_data)

    interpreter.invoke()

    output_data = interpreter.get_tensor(output_details[0]['index'])[0]
    return output_data.tolist()
def visualise_bounding_boxes(image, coordinates):
    print("visualising data {} with coordinates {}".format(image, coordinates))
    coordinate_object = json.loads(coordinates)
    frame = cv2.imread(image)
    h, w, _ = frame.shape
    # frame = cv2.resize(frame, (500, 375), interpolation= cv2.INTER_NEAREST )
    frame = process_image(frame)

    h1, w1 = frame.shape

    scale_h = h / h1
    scale_w = w / w1
    cv2.circle(frame, (round(coordinate_object[0][0] / scale_w),
                       round(coordinate_object[0][1] / scale_h)), 5,
               (255, 255, 255), 1)
    cv2.imshow("Display 1", frame)
    cv2.waitKey(0)
def predict(image_path, model, topk, device, class_to_idx, cat_to_name):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
 
    # TODO: Implement the code to predict the class from an image file
    model.to(device)
    im = torch.FloatTensor(process_image(image_path)).unsqueeze(0)
    im = im.to(device)
#     print(im)
#     with torch.no_grad():
#         outputs = model(im)
    model.eval()
    outputs = model.forward(im)
#     print(outputs.data)
    probs = torch.nn.functional.softmax(outputs.data)
    probs, idx = probs.topk(topk)
    idx_to_class = { v : k for k,v in class_to_idx.items()}
    top_class = [idx_to_class[x] for x in idx.tolist()[0]]
    names = [cat_to_name[k] for k in top_class]
    return probs.tolist()[0], names
Beispiel #11
0
def predict(image_path, model, topk, device, class_to_idx, cat_to_name):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    # TODO: Implement the code to predict the class from an image file
    image = process_image(image_path)
    image = Variable(torch.cuda.FloatTensor(image), requires_grad=True)
    image = image.unsqueeze(0)

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model.to(device)
    model.eval()

    with torch.no_grad():
        image = image.to(device)
        fc_out = model.forward(image)

    probs = torch.nn.functional.softmax(fc_out.data, dim=1)
    probs, idx = probs.topk(topk)
    idx_to_class = {v: k for k, v in class_to_idx.items()}
    top_class = [idx_to_class[x] for x in idx.tolist()[0]]
    names = [cat_to_name[k] for k in top_class]
    return probs.tolist()[0], names
Beispiel #12
0
def extract_and_update_csv(image_list):
    """ Upadtes a csv file with data extracted from NID image"""
    margin = 40
    recursion_num = 0
    faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades +
                                        "haarcascade_frontalface_default.xml")

    for image in image_list:
        count = 0
        image_name = os.path.basename(image)

        #Image.open(image).convert('L').save('temp.jpg')   #dont need img=
        image = cv2.imread(image)
        #image = correct_shape(image)
        best_angle, rotated = correct_skew(image, 2, 10)
        if best_angle != 0:
            image = rotated
        image = cv2.resize(image, (600, 400))

        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        face = faceCascade.detectMultiScale(gray,
                                            scaleFactor=1.3,
                                            minNeighbors=3,
                                            minSize=(30, 30))

        if len(face) == 0:
            print('no face detected on {}'.format(image_name))
            return "NID could not be detected. Please take the picture with good lighting conditions."
        print('{} is a NID image- processing for information extraction > > >'.
              format(image_name))

        nid_info_formatted = process_image(image, image_name, face, margin,
                                           count, recursion_num)
        # if nid_info_formatted is None:
        #     continue

        return nid_info_formatted  # possible bug location
import matplotlib.image as mpimg
import process
from moviepy.editor import VideoFileClip

base = "/home/veon/edu/udacity/CarND-Advanced-Lane-Lines/test_images/"
# vid1 = [base + "vid1/021.jpg", base + "vid1/025.jpg", base + "vid1/026.jpg"]
# hard = [base + "vid2/hard009.jpg", base + "vid2/hard016.jpg", base + "vid2/hard017.jpg",
#         base + "vid2/hard018.jpg", base + "vid2/hard025.jpg", base + "vid2/hard029.jpg",
#         base + "vid2/hard030.jpg", base + "vid2/hard032.jpg", base + "vid2/hard040.jpg",
#         base + "vid2/hard046.jpg", base + "vid2/hard050.jpg",
#         base + "vid3/cachal047.jpg"]
# tests = [base + "straight_lines1.jpg", base + "test1.jpg", base + "test2.jpg", base + "test3.jpg", base + "test4.jpg",
#          base + "test5.jpg"]

image = False
process._debug = image

if image:
    o = mpimg.imread(base + "straight_lines1.jpg")
    process.process_image(o)
else:
    clip1 = VideoFileClip("./project_video.mp4")
    video_clip = clip1.fl_image(process.process_image)
    video_clip.write_videofile("./project_out.mp4", codec='mpeg4', audio=False)
Beispiel #14
0
import socket
from process import process_image

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = ("127.0.0.1", 7777)

client_socket.connect(addr)


def recvall(sock):
    BUFFER = 4096
    data = b''

    try:
        while True:
            part = sock.recv(BUFFER)
            data += part
            if len(part) < BUFFER:
                break

    except socket.error as msg:
        print("Socket Error: %s" % msg)

    finally:
        sock.close()

    return data


process_image(recvall(client_socket).decode(
    "utf-8"))  # Use decode to convert bytes to str
Beispiel #15
0
for i in range(int(net_data)):
    py1 = 0

    py2 = area

    for h in range(int(net_height)):
        x_pad = 0
        if x1 - padding >= 0:
            x_pad = padding
        y_pad = 0
        if py1 - padding >= 0:
            y_pad = padding

        crop = im[py1 - y_pad:py2, x1 - x_pad:x2]
        img = process_image(crop)
        image_data = preprocess_input(img)
        data_set = np.ndarray(shape=(1, 500, 500, 3), dtype=np.float32)

        data_set[0, :, :, :] = image_data
        prediction = model.predict(data_set)
        coordinate = []
        for value in prediction[0]:
            coordinate.append(round(float(value) + 10))

        lis = np.array_split(coordinate, 3)

        cord = detect_shape(img)

        for l in lis:
            r = 30
def create_dataset(dict_of_image_names_with_its_gcp_cordinates):

    data_image_height = 500
    data_image_width = 500
    data_image_depth = 3
    max_num_of_gcp = 3

    print(len(dict_of_image_names_with_its_gcp_cordinates))
    is_real_data_required_in_data_set = False
    length_of_dataset = len(dict_of_image_names_with_its_gcp_cordinates)
    if is_real_data_required_in_data_set is True:
        length_of_dataset = length_of_dataset * 2
    if data_image_depth is None:
        data_set = np.ndarray(shape=(length_of_dataset, data_image_height,
                                     data_image_width),
                              dtype=np.float32)
    else:
        data_set = np.ndarray(shape=(length_of_dataset, data_image_height,
                                     data_image_width, data_image_depth),
                              dtype=np.float32)

    target_set = np.ndarray(shape=(length_of_dataset, 2 * max_num_of_gcp),
                            dtype=np.float32)

    for index, image_file in enumerate(
            dict_of_image_names_with_its_gcp_cordinates):
        image_coordinate = dict_of_image_names_with_its_gcp_cordinates[
            image_file]
        image_data = cv2.imread(image_file)
        h, w, _ = image_data.shape
        print(image_data.shape)
        processed_image = process_image(image_data, image_file)
        processed_image = preprocess_input(processed_image)
        print(processed_image.shape)
        data_set[index, :, :] = processed_image
        if is_real_data_required_in_data_set is True:
            gray = cv2.cvtColor(image_data, cv2.COLOR_BGR2GRAY)
            image_data = preprocess_input(gray)
            data_set[index + len(dict_of_image_names_with_its_gcp_cordinates
                                 ), :, :, :] = image_data
        coordinates = []
        for coordinate in image_coordinate:
            coordinates.append([
                round((coordinate[0] / w) * data_image_width),
                round((coordinate[1] / h) * data_image_height)
            ])

        while len(coordinates) < max_num_of_gcp:
            coordinates.append([0, 0])

        coordinates = sum(coordinates, [])
        target_set[index, :] = coordinates
        if is_real_data_required_in_data_set is True:
            target_set[index + len(dict_of_image_names_with_its_gcp_cordinates
                                   ), :] = coordinates

    data_set = data_set[0:length_of_dataset, :, :]
    target_set = target_set[0:length_of_dataset, :]
    print(target_set.shape)
    print(data_set.shape)
    return data_set, target_set