forked from saurabhcharde/Pixible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixi.py
105 lines (86 loc) · 3.23 KB
/
pixi.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16
import tensorflow as tf
import sys
import traceback
import threading
#sound api
from playsound import playsound
#wikipedia api
import wikipedia as wiki
#google search api
from googlesearch import search
#PyQT UI elements
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QFileDialog
graph = tf.get_default_graph()
model = VGG16()
class PredictorThread(threading.Thread):
def __init__(self,myapp,path):
threading.Thread.__init__(self)
self.myapp=myapp
self.path=path
def run(self):
global graph,model
with graph.as_default():
image = load_img(self.path, target_size=(224, 224))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)
pred = model.predict(image)
label = decode_predictions(pred)
#print(label)
label = label[0][0]
self.myapp.Display_Title(label[1])
#print('%s (%.2f%%)' % (label[1], label[2]*100))
wiki_content=''
try:
info_page=wiki.page(label[1])
img_content=info_page.content
#print('------Wiki------')
#print(img_content[:200]+'....')
wiki_content=img_content[:350]+'....'
except:
tb = traceback.format_exc()
print(tb)
s_result=''
#print('------Related----------')
s=search(label[1],num=3,stop=1)
for j in s:
s_result=s_result+j+"\n"
info = [label[1],wiki_content,s_result]
self.myapp.Display(info)
playsound(label[1]+".mp3")
qtCreatorFile = "pix.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.path=''
self.upload.clicked.connect(self.Decode)
#self.label.clicked.connect(self.Reload)
#self.go.clicked.connect(self.Decode)
def Decode(self):
self.path=QFileDialog.getOpenFileName()[0]
if(len(self.path)>1):
self.upload.setStyleSheet("border-image: url"+"("+self.path+")")
predictor_thread = PredictorThread(self,self.path)
predictor_thread.start()
#def Decode(self):
# self.upload.setStyleSheet("border-image: url"+"("+self.path+")")
def Display(self,info):
self.prediction.setText(info[0])
self.wiki_holder.setText(info[1])
self.search_holder.setText(info[2])
def Display_Title(self,info):
self.prediction.setText(info)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())