Пример #1
0
def predict():
    img_data = request.get_data()
    convert_image(img_data)
    X = imread('output.png', mode='L')
    X = np.invert(X)
    X = imresize(X, (28, 28))
    X = X.reshape(1, 28, 28, 1)

    with graph.as_default():
        out = model.predict(X)
        response = np.array_str(np.argmax(out))
        return str(CLASS_MAPPING[response])
Пример #2
0
def stylize(content_image, output_image, model, style_strength, cuda=0):

    # Select GPU if available
    device = torch.device('cuda' if cuda else 'cpu')

    # Load content image
    input_image = load_image(content_image)
    content_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Lambda(lambda x: x.mul(255))
    ])
    input_image = content_transform(input_image)
    input_image = input_image.unsqueeze(0).to(device)

    # Set requires_grad to False
    with torch.no_grad():
        style_model = TransformNet()
        state_dict = torch.load(model)

        # Load the model's learnt params
        style_model.load_state_dict(state_dict)
        style_model.to(device)

        # Output image
        output = style_model(input_image).cpu()

    # Make sure both images have same shapes
    content_image = match_size(input_image, output)
    weighted_output = output * style_strength + \
        (content_image * (1 - style_strength))
    return(convert_image(output_image, weighted_output[0]))
Пример #3
0
def run(args):
    model = build_model()

    (clf, class_names) = read_classifier(
        os.path.join(args.data_path, 'classifier.pickle'))
    # if classifier is none we only have one face
    if clf is None:
        verified_embedding, only_class = read_only_embedding(args.data_path)

    cap = cv2.VideoCapture(0)

    if (cap.isOpened() == False):
        print("Error opening video stream or file")

    face_detector = FaceDetector()

    while (cap.isOpened()):
        # Capture frame-by-frame
        ret, frame = cap.read()
        if ret == True:

            # Detect image and write it
            faces = face_detector.detect_faces(frame)
            for face in faces:
                x, y, w, h = face
                cropped = frame[y:y + h, x:x + w]
                cropped = cv2.resize(cropped, (96, 96))
                cropped = np.around(convert_image(cropped), decimals=12)
                embedding = model.predict(np.array([cropped]))

                if clf is None:
                    dist = np.linalg.norm(verified_embedding - embedding)
                    match = dist < 0.7
                    label = only_class if match else "Unknown"
                    if args.debug:
                        label += ' (d: {})'.format(round(dist, 2))
                else:
                    predictions = clf.predict_proba(embedding)
                    pred_class = np.argmax(predictions, axis=1)[0]
                    score = round(np.max(predictions) * 100, 2)
                    match = score > 70
                    name = class_names[pred_class]
                    label = '{} ({}%)'.format(name, score)

                color = (0, 255, 0) if match else (0, 0, 255)

                draw_bbox(frame, x, y, x + w, y + h, label=label, color=color)

            cv2.imshow('Frame', frame)

            # Press Q on keyboard to  exit
            if cv2.waitKey(25) & 0xFF == ord('q'):
                break

        else:
            break

    cap.release()
    cv2.destroyAllWindows()
Пример #4
0
def get_preview(mode):
    if mode == "window":
        eel.set_modal_eel("show")

    img = capture_instance.capture_mode(mode)
    send_image(utils.convert_image(img))

    eel.set_modal_eel("hide")
Пример #5
0
def hello():
    encoded_img = request.values['encoded_image']
    img = convert_image(encoded_img)

    result = grpc_infer(img)
    return json.dumps(
        {
            "code": 200,
            "result": result.tolist()
        }
    )
Пример #6
0
import numpy as np
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from utils import convert_image, chars_to_img, get_img_in_ascii, get_tile_img_dim

if __name__ == "__main__":
    c = input(
        "Choose what would you like to do:\nl - Load image to transform\nt - Translate you webcam\n"
    )
    if c == 'l':
        root = Tk()
        root.attributes("-topmost", True)
        root.withdraw()
        filename = askopenfilename(
            filetypes=[("Image files", "*.png, *.jpg"), ("All Files", "*.*")])
        img = convert_image(img_path=filename)
        img_in_ascii = get_img_in_ascii(img, get_tile_img_dim(img.shape))

        f = open("output.txt", 'w')
        for row in img_in_ascii:
            f.write(row + '\n')

        chars_to_img(img_in_ascii).save('sample-out.jpg')
    elif c == 't':
        # you should change frame rate based on how much time it takes to process img on your pc
        frame_rate = 7
        prev = 0
        cap = cv2.VideoCapture()
        cap.open(0)

        _, frame = cap.read()
    PSNRs = AverageMeter()
    SSIMs = AverageMeter()

    # Prohibit gradient computation explicitly because I had some problems with memory
    with torch.no_grad():
        # Batches
        for i, (lr_imgs, hr_imgs) in enumerate(test_loader):
            # Move to default device
            lr_imgs = lr_imgs.to(device)  # (batch_size (1), 3, w / 4, h / 4), imagenet-normed
            hr_imgs = hr_imgs.to(device)  # (batch_size (1), 3, w, h), in [-1, 1]

            # Forward prop.
            sr_imgs = model(lr_imgs)  # (1, 3, w, h), in [-1, 1]

            # Calculate PSNR and SSIM
            sr_imgs_y = convert_image(sr_imgs, source='[-1, 1]', target='y-channel').squeeze(
                0)  # (w, h), in y-channel
            hr_imgs_y = convert_image(hr_imgs, source='[-1, 1]', target='y-channel').squeeze(0)  # (w, h), in y-channel
            psnr = peak_signal_noise_ratio(hr_imgs_y.cpu().numpy(), sr_imgs_y.cpu().numpy(),
                                           data_range=255.)
            ssim = structural_similarity(hr_imgs_y.cpu().numpy(), sr_imgs_y.cpu().numpy(),
                                         data_range=255.)
            PSNRs.update(psnr, lr_imgs.size(0))
            SSIMs.update(ssim, lr_imgs.size(0))

    # Print average PSNR and SSIM
    print('PSNR - {psnrs.avg:.3f}'.format(psnrs=PSNRs))
    print('SSIM - {ssims.avg:.3f}'.format(ssims=SSIMs))

print("\n")