File size: 1,919 Bytes
6c29b84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7dd880
6c29b84
 
 
 
 
 
 
 
 
 
 
 
c5c6077
6c29b84
 
 
 
 
 
 
 
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
import os
import similarity_search
import gradio as gr

# Path to the directory containing similar images
# similar_images_dir = ".\gallery"

# def read_image(image_file):
#     img = cv2.imread(
#         image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION
#     )
#     img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#     if img is None:
#         raise ValueError('Failed to read {}'.format(image_file))
#     return img

def get_similar_images(image):
    similar_image_ids = similarity_search.find(image)
    return similar_image_ids


# def get_image_paths(prod_ids):
#     csv_path = './gallery.csv'  # Replace with the actual path
#     data = pd.read_csv(csv_path)
#     image_paths = []

#     for i, prod_id in enumerate(prod_ids):
#         row = data[data['seller_img_id'] == prod_id]
        
#         if not row.empty:
#             image_path = './' + row.iloc[0]['img_path']
#             print(image_path)
#             image_paths.append(image_path)

#     return image_paths


def predict(image):

    similar_image_ids = get_similar_images(image)

    return {"similar_image_ids" : similar_image_ids}

# Create title, description and article strings
title = "Visual Product Recognition"
description = "A model to find the similar images in the e-commerce platform"
article = "Created by Thenujan Nagaratnam for Data Science Project at UoM"

# Create examples list from "examples/" directory
example_list = [["examples/" + example] for example in os.listdir("examples")]

# Create the Gradio demo
demo = gr.Interface(fn=predict, # mapping function from input to output
                    inputs=gr.Image(type="pil"), # what are the inputs?
                    outputs=gr.JSON(label="Predictions"),
                    examples=example_list, 
                    title=title,
                    description=description,
                    article=article)


# Launch the demo!
demo.launch()