File size: 2,077 Bytes
f760752
 
4b2d6fe
f760752
 
b3b9103
f760752
 
b3b9103
f760752
 
 
 
b3b9103
88e3082
b3b9103
 
 
01d8e10
1c2c506
8e826e2
30dace2
6dbc8f4
9a6b070
 
e6fd7bc
5f23b28
455b959
a65936d
455b959
 
a65936d
455b959
 
a65936d
8e826e2
66e93c9
e5843d9
8e826e2
e5843d9
 
 
b3b9103
f6210d9
b3b9103
 
 
 
c0b6df5
f760752
a993e6b
b3b9103
3ac8c37
b3b9103
97b4948
b3b9103
97b4948
3ac8c37
b3b9103
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import tensorflow as tf
from PIL import Image, ImageOps
import numpy as np
import os
import cv2

model_path = "IM_417_128.keras"

print("Loading Model...")
model = tf.keras.models.load_model(model_path)
model.summary()

labelled_images = [f"labelled/{i}.png" for i in range(1, 418)]

# Define regression function
def predict_regression(image):
    # Preprocess image

    print(f" {type(image)} image layers ===> { len(image['layers']) }")
    image = np.squeeze(image["layers"][0][:, :, -1:], axis=-1)
    
    image = np.clip(image, 0, 255).astype('uint8')

    resized_image = cv2.resize(image, (128, 128), interpolation=cv2.INTER_CUBIC)
    
    # Convert to grayscale
    # gray_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)

    # Create a mask for values greater than 10
    mask = resized_image > 10
    
    # Use the mask to assign values
    gray_image = np.where(mask, 255, 0)

    print(f"gray_image image TEST :: {gray_image.shape}")
    
    # Convert the image to a NumPy array
    image_array = np.array(gray_image)
    # Invert the colors
    inverted_image_array = 255 - image_array
    
    # Predict
    predictions = model.predict(inverted_image_array[None, ...])  # Assuming single regression value
    predicted_output = np.argmax(predictions, axis=1)
    predicted_label = f"labelled/{str(predicted_output[0])}.png"
    print(f"prediction IM-417 character :: {predicted_label} :: source {predicted_output}")
    output_data = cv2.imread(predicted_label, cv2.COLOR_BGR2GRAY)
    return cv2.resize(output_data, (128, 128)) , image_array

# Create Gradio interface
output_image = gr.Image(height=150, width=150)
i_0 = gr.Image(height=150, width=150)

# sketchpad = gr.Sketchpad(flatten=True) 
interface = gr.Interface(fn=predict_regression,
             inputs="sketchpad",
             outputs=[output_image, i_0],
             live=True,
             description="A simple mlp classification model for image classification using the sign list from 'The Indus Scripts: Texts, Concordance and Tables'"
)


interface.launch()