Exemplo n.º 1
0
def test_data_aug():

    # given
    data_path = DATA_DIR / "COVIDx"
    assert data_path.exists()
    img_path = data_path / 'test/0a51f668-b7b1-4d8d-9ab9-de1f702f071a.png'
    img = fv.open_image(img_path)

    # then brightness
    plt.close("all")
    fig, axs = plt.subplots(1,5,figsize=(12,4))
    for change, ax in zip([0.4, 0.5, 0.6, 1.0,1.1], axs):
        img = fv.open_image(img_path)
        fv.brightness(img, change).show(ax=ax, title=f'change={change:.1f}')
    plt.savefig("test_data_aug_bitghtness.jpg")

    # then rotation
    plt.close("all")
    tfm = [fv.rotate(degrees=(-10,10), p=0.75)]
    fig, axs = plt.subplots(1,5,figsize=(12,4))
    for ax in axs:
        img = fv.open_image(img_path)
        img = img.apply_tfms(tfm)
        title = f"Done, deg={tfm[0].resolved['degrees']:.1f}" if tfm[0].do_run else f'Not done'
        img.show(ax=ax, title=title)
    plt.savefig("test_data_aug_rotation.jpg")
Exemplo n.º 2
0
def input_fn(request_body, content_type=JPEG_CONTENT_TYPE):
    logger.info('Deserializing the input data.')
    # process an image uploaded to the endpoint
    if content_type == JPEG_CONTENT_TYPE:
        return open_image(io.BytesIO(request_body))
    # process a URL submitted to the endpoint
    if content_type == JSON_CONTENT_TYPE:
        img_request = requests.get(request_body['url'], stream=True)
        return open_image(io.BytesIO(img_request.content))
    raise Exception(
        'Requested unsupported ContentType in content_type: {}'.format(
            content_type))
Exemplo n.º 3
0
async def signature_compute(app: UploadFile = File(...),
                            device: UploadFile = File(...)):
    try:
        app_img = open_image(app.file)
    except:
        app_img = None
    try:
        device_img = open_image(device.file)
    except:
        device_img = None
    result = compare_sig(app_img, device_img)
    app.file.close()
    device.file.close()
    return result
Exemplo n.º 4
0
Arquivo: app.py Projeto: whb07/avp
async def infer():
    path = Path('data/')
    learn = load_learner(path)
    data = await request.get_data()
    img = open_image(BytesIO(data))
    pred_class, pred_idx, outputs = learn.predict(img)
    return str(pred_class)
Exemplo n.º 5
0
def is_hotdog(filepath: str) -> bool:
    """
    in current model, hotdog is 0 and not hotdog is 1
    """

    category, tensor, probs = model.predict(open_image(filepath))
    return False if model.data.c2i.get(str(category)) else True
Exemplo n.º 6
0
def write_prediction_with_score(result_file, learner, test_path, num_samples, class_names):
    """
    Writes the prediction to a file for the Stanford cars test dataset, including class name and confidence score.

    :param result_file: User-defined result file name, which will have the class id, class name and confidence score for prediction.
    :param learner: Model learner to classify images.
    :param test_path: Path to dir where test images as located.
    :param num_samples: Test samples = 8041, as provided by https://ai.stanford.edu/~jkrause/cars/car_dataset.html
    :param class_names: Class names for cars as provided by https://ai.stanford.edu/~jkrause/cars/car_dataset.html
    :return:
    """
    out = open(result_file, 'a')
    start = time.time()
    out.write('class_id, class_name, confidence\n')

    for i in range(num_samples):
        filename = os.path.join(test_path, '%05d.jpg' % (i + 1))
        img = open_image(filename)
        pred_class, pred_idx, confidence = learner.predict(img)
        out.write('{}, {}, {}\n'.format(pred_idx.item() + 1, class_names[pred_idx.item()][0][0],
                                        confidence[pred_idx.item()].item()))

    end = time.time()
    seconds = end - start
    print('avg fps: {}'.format(str(num_samples / seconds)))

    out.close()
Exemplo n.º 7
0
 def segment(self, input_volume, output_label):
     vol = sitk.ReadImage(input_volume)
     vol_nda = sitk.GetArrayFromImage(vol)
     original_shape = vol_nda.shape
     print("Original vol shape:", original_shape)
     stacked_labels = np.array([])
     for idx, slice in enumerate(vol_nda):
         im = Image.fromarray(slice).convert("L")
         with io.BytesIO() as slice_bytes:
             im.save(slice_bytes, format="PNG")
             img = open_image(slice_bytes)
         pred_class, pred_labels, losses = self.learner.predict(img)
         np_labels = pred_labels.numpy()
         if stacked_labels.size == 0:
             stacked_labels = np_labels
         else:
             stacked_labels = np.concatenate([stacked_labels, np_labels],
                                             axis=0)
     print("Prediction ranges:", stacked_labels.min(), stacked_labels.max())
     bone_labels = np.clip(stacked_labels - 1, 0, 1)
     print("Output volume shape:", bone_labels.shape)
     print("Output volume ranges:", bone_labels.min(), bone_labels.max())
     label_vol = sitk.GetImageFromArray(bone_labels.astype(np.uint8))
     label_vol.CopyInformation(vol)
     writer = sitk.ImageFileWriter()
     writer.Execute(label_vol, output_label, True)
Exemplo n.º 8
0
def predict_landcover_type(filepath: str) -> List[str]:
    """ Predict the landcover type form a Planet Skysat Image.

    Utilizes the Fasiai.vision land cover classification model in order to predict 
    a set of landcover types from 

    https://docs.fast.ai/vision.image.html

    Args:
        filepath [str]: Path to the image that will be run through the model 

    Returns:
        List[str]: A list of string labels resulting from a combination of the following land cover types: 
         {'agriculture', 'artisinal_mine', 'bare_ground', 'blooming', 'blow_down',
          'clear', 'cloudy', 'conventional_mine', 'cultivation',
          'habitation', 'haze', 'partly_cloudy', 'primary', 'road',
          'selective_logging', 'slash_burn', 'water'}

    """
    image = open_image(filepath)

    # https://forums.fast.ai/t/how-to-make-predictions-with-vision-learner-solved/34456/9
    model = load_learner('models/', 'fastai_planet_model.pkl')

    category, tensor, probs = model.predict(image)

    return str(category).split(';')
Exemplo n.º 9
0
def deploy(model_name):

    doc_ref = db.collection(u'Models').document(model_name)

    try:
        doc = doc_ref.get()
        labels = doc.to_dict()['labels']
    except:
        print(u'No such model exists!')
        return 'No such model exists!'

    if 'file' not in request.files:
        flash('No file part')
        return 'No File Sent'

    file = request.files['file']
    file.save("/home/bharathrajeevnair/dsfh-v2/api-backend/tmp/samp.jpeg")
    img = open_image(
        "/home/bharathrajeevnair/dsfh-v2/api-backend/tmp/samp.jpeg")

    learn = load_learner(
        '/home/bharathrajeevnair/dsfh-v2/api-backend/models/' + model_name)
    pred_class, _, _ = learn.predict(img)
    pred = int(pred_class)

    os.remove("/home/bharathrajeevnair/dsfh-v2/api-backend/tmp/samp.jpeg")
    data_foy = {
        u'prediction': labels[pred],
    }
    resy = json.dumps(data_foy)
    respons = json.loads(resy)
    return respons
Exemplo n.º 10
0
def authenticate():
    
    path = '/lib/Auth/RecFace/images/models/'
    root_models = [f for f in listdir(path) if isfile(join(path, f))]
    if 'tmp.pth' in root_models:
        root_models.remove('tmp.pth')
        
    classes = ["Test", "Train"]
    data = ImageDataBunch.single_from_classes('/lib/Auth/RecFace/images/', 
                                               classes, 
                                               ds_tfms=None, 
                                               size = 224)
    
    
    data.normalize(imagenet_stats)
    learn = cnn_learner(data, models.vgg16_bn)
    
    imgs = getFaces.getFaces()
    if len(imgs)==0:
        return False
    
    for img in imgs:
        img = resize(img, (224,224), interpolation = INTER_AREA)
        imwrite('temp.jpeg', img)
        img = open_image('temp.jpeg')
        for mod in root_models:
            if compare(mod.split('.')[0], img, learn):
                return True
    return False
Exemplo n.º 11
0
def upload_file():
    """
    retrieve the image uploaded and make sure it is an image file
    """
    file = request.files['file']
    image_extensions = ['jpg', 'jpeg', 'png']

    if file.filename.split('.')[1] not in image_extensions:
        return jsonify('Please upload an appropriate image file')
    """
    Load the trained model in export.pkl 
    """
    learn = load_learner(path=".")
    """
    Perform prediction
    """
    #image_bytes = file.read()
    #img = Image.open(io.BytesIO(image_bytes))

    img = open_image(file)

    pred_class, pred_idx, outputs = learn.predict(img)
    i = pred_idx.item()
    classes = [
        'Domestic Medium Hair', 'Persian', 'Ragdoll', 'Siamese', 'Snowshoe'
    ]
    prediction = classes[i]

    return jsonify(f'Your cat is a {prediction}')
def get_vector(img, model): 
    sf = SaveFeatures(model.model[1][4]) # layer ke-empat sebelum softmax (_fc)
    model.
    _ = model.predict(open_image(img))
    vectors = sf.features
    sf.remove()
    return vectors[0]
def run_model(learn, image_path, info):

    # Get the image
    image = open_image(image_path)

    # Run the prediction
    __, __, outputs = learn.predict(image)

    # Convert the results to a list of predictions
    predictions = convert_to_predictions(outputs, learn.data.classes)
    prediction = predictions[0]

    if len(predictions) == 1:
        other_predictions = []
    else:
        del predictions[0]
        other_predictions = predictions

    # Get just the filename from the path
    __, filename = os.path.split(image_path)

    # copy to a result object
    r = Result(image_path, filename, info, prediction, other_predictions)

    return r
Exemplo n.º 14
0
def covid_func():
    model = load_learner('models')
    if request.method == 'GET':
        return render_template('index.html', value='We Got You!')

    if request.method == 'POST':
        print(request.files)
        if 'file' not in request.files:
            print('Image not uploaded')
            return
        file = request.files['file']
        img = open_image(file)

        #Getting the Best class
        pred_class_1, idx_1, pred_prob = model.predict(img)

        #Getting all best pred
        preds_sorted, idxs = pred_prob.sort(descending=True)

        #Getting  prediction
        pred_1_prob = np.round(100 * preds_sorted[0].item(), 2)

        return render_template('result.html',
                               label=pred_class_1,
                               category=pred_1_prob)
Exemplo n.º 15
0
def fetch_image(url):
    response = requests.get(url)
    img = open_image(BytesIO(response.content))
    #response = requests.get(url)
    #pil_img = PIL.Image.open(BytesIO(response.content))
    #display_img = np.asarray(pil_img)
    return img
Exemplo n.º 16
0
async def predict(request):
    form = await request.form()
    filename = form['file'].filename
    content = await form['file'].read()
    img = open_image(BytesIO(content))
    prediction = learn.predict(img)[0]
    return JSONResponse({'result': str(prediction)})
Exemplo n.º 17
0
    def handle_cli(self, args, func):
        parser = argparse.ArgumentParser()
        parser.add_argument("--input", required=True)
        parser.add_argument("-o",
                            "--output",
                            default="str",
                            choices=["str", "json", "yaml"])
        parsed_args = parser.parse_args(args)
        file_path = parsed_args.input

        verify_image_format_or_raise(file_path, self.accept_image_formats)
        if not os.path.isabs(file_path):
            file_path = os.path.abspath(file_path)

        image_array = open_image(
            fn=file_path,
            convert_mode=self.convert_mode,
            div=self.div,
            after_open=self.after_open,
            cls=self.cls or Image,
        )

        result = func(image_array)
        result = get_output_str(result, output_format=parsed_args.output)
        print(result)
Exemplo n.º 18
0
def submit():
    print(request.url)
    if 'file' not in request.files:
        flash('No file received')
        return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('No file selected.')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = file.filename
        img_path = (os.path.join(path, 'static', filename))
        file.save(img_path)
        print(img_path)
        img = open_image(img_path)
        _, pred_idx, pred_conf = learn.predict(img)
        pred_class = learn.data.train_ds.y.classes[pred_idx.data.numpy()]
        conf = pred_conf.max().numpy()
        output_string = f'{pred_class.__str__().title()} [ {conf*100:.4} % ]'
        return render_template('prediction.html',
                               image=filename,
                               prediction=output_string)
    else:
        flash('The file type is not allowed')
        return redirect(request.url)
def main(req: func.HttpRequest) -> func.HttpResponse:
    try:
        image_file = req.files['petimage'].stream
    except:
        return func.HttpResponse("Failed to get image. Check the URL",
                                 status_code=400)

    global learn
    if learn is None:
        learn = load_model.load_fastai()

    # fetch image from uploaded file then predict
    try:
        img = open_image(BytesIO(image_file.read()))
        _, _, losses = learn.predict(img)
    except Exception as e:
        logging.error('predict image failed', exc_info=True)
        return func.HttpResponse("Failed to get image. Check the URL",
                                 status_code=400)

    res = sorted(zip(learn.data.classes, map(float, losses)),
                 key=lambda x: x[1],
                 reverse=True)
    result_class = res[0][0]

    return func.HttpResponse("Is your image {0} ?".format(result_class),
                             status_code=200)
Exemplo n.º 20
0
        def proc_file(f):
            im = open_image(os.path.join(path, f))
            if dir_qmap is not None:
                qmap = learn.predict_quality_map(im, [32, 32])

                name = os.path.basename(f).split('.')[0]

                qmap.plot()
                qmap.savefig(os.path.join(dir_qmap, name + '.jpg'))
                score = qmap.global_score

                if sz is not None:
                    height, width = qmap.img.size
                    new_width = sz  # 500
                    new_height = new_width * height // width
                    qmap.pil_image.resize(
                        (new_width, new_height), PIL_Image.ANTIALIAS).save(
                            os.path.join(dir_qmap, name + '_raw.jpg'))
                    qmap.blend(mos_range=(None, None)).resize(
                        (new_width, new_height), PIL_Image.ANTIALIAS).save(
                            os.path.join(dir_qmap, name + '_map.jpg'))
            else:
                score = learn.predict(im)[0].obj[0]
            del im
            del qmap
            return score
Exemplo n.º 21
0
    def handle_cli(self, args, func):
        parser = argparse.ArgumentParser()
        parser.add_argument("--input", required=True)
        parser.add_argument("-o",
                            "--output",
                            default="str",
                            choices=["str", "json", "yaml"])
        parsed_args = parser.parse_args(args)
        file_path = parsed_args.input

        check_file_format(file_path, self.accept_file_extensions)
        if not os.path.isabs(file_path):
            file_path = os.path.abspath(file_path)

        try:
            from fastai.vision import open_image, Image
        except ImportError:
            raise ImportError("fastai package is required to use")

        image_array = open_image(
            fn=file_path,
            convert_mode=self.convert_mode,
            div=self.div,
            after_open=self.after_open,
            cls=self.cls or Image,
        )

        result = func(image_array)
        result = get_output_str(result, output_format=parsed_args.output)
        print(result)
Exemplo n.º 22
0
def detect(path, model, img_cat_bin):
    '''
        Detect whether an image or a group of images are resistors or not.
    '''

    # setting cpu as default for inference
    defaults.device = torch.device('cpu')

    try:
        # load model
        learner = load_learner(path='.', file=model)

        # open image
        img = open_image(path)

    except FileNotFoundError as e:
        click.echo(e, err=True)
        return

    click.echo(f'Using model: {model}')
    click.echo('Starting prediction for:')

    # display image on the shell using imgcat from iterm2
    os.system(f'{img_cat_bin} {path}')

    # inference
    pred_class, pred_index, probs = learner.predict(img)

    click.echo(f'Data classes: {learner.data.classes} ')
    click.echo(f'prediction: {pred_class} ')
    click.echo(f'prediction index: {pred_index} ')
    click.echo(f'Probabilities: {probs}')
async def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Start my function')

    # fetch image from form data
    try:
        url = req.form['url']
    except:
        logging.error('Customer didn\'t pass URL', exc_info=True)
        return func.HttpResponse("Please pass URL for Prediction",
                                 status_code=400)

    # load model as global to cache
    global learn
    if learn is None:
        learn = load_model.load_fastai()

    # fetch image from URL then predict
    try:
        image = await utility.get_bytes(url)
        img = open_image(BytesIO(image))
        _, _, losses = learn.predict(img)
    except Exception as e:
        logging.error('predict image failed', exc_info=True)
        return func.HttpResponse("Failed to get image. Check the URL",
                                 status_code=400)

    res = sorted(zip(learn.data.classes, map(float, losses)),
                 key=lambda x: x[1],
                 reverse=True)
    result_class = res[0][0]

    return func.HttpResponse("Is your image {0} ?".format(result_class),
                             status_code=200)
Exemplo n.º 24
0
def get_image_by_url(url):
    """ Load img from url and save to path/filename locally """
    resp = requests.get(url)

    if resp.status_code == 200:
        # need convert to RGB to save in .jpeg further
        return open_image(BytesIO(resp.content))
Exemplo n.º 25
0
async def classify_url(request):
    bytes = await get_bytes(request.query_params["url"])
    img = open_image(BytesIO(bytes))
    _, _, losses = learner.predict(img)
    return JSONResponse({
        "predictions": sorted(zip(learner.data.classes, map(float, losses)), key=lambda p: p[1], reverse=True)
    })
def test_model_to_learner(tmp):
    model = models.resnet18

    # Test if the function loads an ImageNet model (ResNet) trainer
    learn = model_to_learner(model(pretrained=True))
    assert len(learn.data.classes) == 1000  # Check Image net classes
    assert isinstance(learn.model, models.ResNet)

    # Test if model can predict very simple image
    IM_URL = "https://cvbp.blob.core.windows.net/public/images/cvbp_cup.jpg"
    imagefile = os.path.join(tmp, "cvbp_cup.jpg")
    urllib.request.urlretrieve(IM_URL, imagefile)

    category, ind, predict_output = learn.predict(
        open_image(imagefile, convert_mode="RGB"))
    assert learn.data.classes[ind] == str(category) == "coffee_mug"

    # Test if .predict() yield the same output when use .get_preds()
    one_data = (
        ImageList.from_folder(tmp).split_none().label_const(
        )  # cannot use label_empty because of fastai bug: # https://github.com/fastai/fastai/issues/1908
        .transform(
            tfms=None,
            size=IMAGENET_IM_SIZE).databunch(bs=1).normalize(imagenet_stats))
    learn.data.train_dl = one_data.train_dl
    get_preds_output = learn.get_preds(ds_type=DatasetType.Train)

    assert np.all(
        np.isclose(
            np.array(get_preds_output[0].tolist()
                     [0]),  # Note, get_preds() produces a batch (list) output
            np.array(predict_output.tolist()),
            rtol=1e-05,
            atol=1e-08,
        ))
Exemplo n.º 27
0
def UploadPhoto(request):

    image_object = Image()
    context = {}
    uploaded = False
    learn = load_learner('classifier')

    if request.method == "POST":

        form = PictureForm(request.POST, request.FILES, instance=image_object)
        if form.is_valid():
            image_object = form.save(commit=False)
            image_object.save()
            uploaded = True

            context['image_url'] = image_object.image.url
            image = request.FILES['image']
            # image = PIL.Image.open(image)

            image = open_image(image)
            prediction = learn.predict(image)
            print(prediction)
            context['prediction'] = prediction[0]

    form = PictureForm(instance=image_object)

    context['form'] = form
    context['uploaded'] = uploaded

    return render(request,
                  template_name='classifier/photo_upload.html',
                  context=context)
Exemplo n.º 28
0
def index(request):
    result = False
    if request.method == 'POST' and request.FILES:
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')

        form = MainForm({'ip': ip}, request.FILES)
        if form.is_valid():
            model = form.save()
            img = open_image(settings.BASE_DIR + model.photo.url)
            learn = load_learner(os.path.join(settings.BASE_DIR, 'fastai'))
            pred_class, pred_idx, outputs = learn.predict(img)
            scores = sorted(zip(classes, outputs.data.tolist()),
                            key=lambda kv: kv[1],
                            reverse=True)
            model.scores = dict(scores)
            model.save(update_fields=["scores"])
            result = list(scores[0]) + [_(scores[0][0])]
            print(result)
    else:
        form = MainForm()

    return render(request, 'web/index.html', {
        'form': form,
        'result': result,
        'labels': labels
    })
Exemplo n.º 29
0
 def get_preds(img_path):
     img = open_image(img_path)
     _, _, losses = self.LEARNER.predict(img)
     return [
         x
         for x in zip(self.LEARNER.data.classes, map(float, losses))
         if x[0] in self.LABELS
     ]
Exemplo n.º 30
0
def predict_single(img_file):
    '''function to take image and return prediction'''
    logging.debug('Funcao para pegar uma imagem e retornar a prediction')

    prediction = learn.predict(open_image(img_file))
    probs_list = prediction
    logging.debug('Antes da lista de probabilidades')
    return probs_list
Exemplo n.º 31
0
async def analyze(request):
    data = await request.form()
    img_bytes = await (data['file'].read())
    img = open_image(BytesIO(img_bytes))
    prediction = learn.predict(img)[0]
    return JSONResponse({'result': str(prediction)})