Spaces:
Sleeping
Sleeping
| 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() |