Skip to content

Instantly share code, notes, and snippets.

@DivyanshByte
Created February 15, 2021 15:43
Show Gist options
  • Save DivyanshByte/077d3ff7c81e78aa167ae5622e26fae8 to your computer and use it in GitHub Desktop.
Save DivyanshByte/077d3ff7c81e78aa167ae5622e26fae8 to your computer and use it in GitHub Desktop.
Visual Similarity With Clarifai
import json
from clarifai.rest import ClarifaiApp
from math import sqrt
from numpy import linalg
from numpy import array
from tkinter import *
from tkinter.scrolledtext import ScrolledText
from tkinter.ttk import *
from tkinter import messagebox
window = Tk()
window.title('Visual Similarity using Clarifai')
tabbed_pane = Notebook(window)
visual_similarity_tab = Frame(tabbed_pane)
num_images_to_predict = 3
all_image_input = []
label = Label(visual_similarity_tab,text="Clarifai API Key: ")
clarifai_api_input = Entry(visual_similarity_tab)
label.grid(row=0,column=0)
clarifai_api_input.grid(row=0,column=1)
for i in range(1,num_images_to_predict+1):
if i==1:
label = Label(visual_similarity_tab,text="Image to Predict URL: ")
else:
label = Label(visual_similarity_tab,text="Image "+str(i-1)+" URL: ")
input_ = Entry(visual_similarity_tab)
label.grid(row=i,column=0)
input_.grid(row=i,column=1)
all_image_input.append(input_)
def predict():
try:
app = ClarifaiApp(api_key=clarifai_api_input.get())
model = app.models.get("d02b4508df58432fbb84e800597b8959")
main_image = all_image_input[0].get()
images_to_predict = []
for image_input in all_image_input[1:]:
images_to_predict.append(image_input.get())
print(main_image)
print(images_to_predict)
except:
messagebox.showerror("An Error occurred", "An Error occurred While Handling Your Request")
def add_image():
global num_images_to_predict
label = Label(visual_similarity_tab,text="Image "+str(num_images_to_predict)+" URL: ")
input_ = Entry(visual_similarity_tab)
label.grid(row=num_images_to_predict+1,column=0)
input_.grid(row=num_images_to_predict+1,column=1)
num_images_to_predict += 1
all_image_input.append(input_)
add_button.grid(row=num_images_to_predict+1,column=0)
predict_button.grid(row=num_images_to_predict+2,column=0)
add_button = Button(visual_similarity_tab,command=add_image,text="Add Image")
predict_button = Button(visual_similarity_tab,command=predict,text="Predict")
add_button.grid(row=num_images_to_predict+1,column=0)
predict_button.grid(row=num_images_to_predict+2,column=0)
tabbed_pane.add(visual_similarity_tab, text='Visual Similarity')
tabbed_pane.pack(expand=1, fill="both")
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment