├── 1) Python Basics ├── save_string.txt ├── x_y.png ├── data_pickle.pkl ├── data.txt └── pythonBasics.py ├── 4) Skin Cancer Classification Project with Tkinter ├── result1.txt ├── result2.txt ├── dx_type.pdf ├── referance.txt └── skin_cancer_classification_project_with_Tkinter.py ├── 6) Clustering Project with PyQt5 ├── cluster_result.txt ├── cluster_figure.jpg ├── data.csv └── Clustering_project_with_pyqt5.py ├── 7) Digit Classification Project with PyQt5 ├── Output.txt ├── icon1.png ├── input.png ├── model1.h5 ├── model2.h5 ├── images │ ├── 0.png │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png ├── input_img.png └── digit_classification_project_w_pyqt5.py ├── 2) Tkinter Widgets ├── cell.jpg ├── frame_labelFrame_panedWindow.py ├── geometry_manager.py ├── open_image_plot_mouseEvent.py ├── button_label_entry_messageBox.py ├── radioButton_comboBox_checkButton.py └── menu_tabs_treeview_listbox_textEditor_scroll.py ├── 5) PyQt5 widgets ├── icon1.png ├── menubar.py ├── radioButton_comboBox_checkButton_progressBar.py ├── button_label_entry_messageBox.py ├── horizontal_vertical_formLayout_qsplitterFrame_groupboxGridLayout.py └── tab_list_text editor_slider_table_spinbox_images_calendar.py └── 3) Forex Trading Project with Tkinter ├── news_EURGBR.txt ├── news_EURUSD.txt └── trading_analysis_project_w_tkinter.py /1) Python Basics/save_string.txt: -------------------------------------------------------------------------------- 1 | hello world -------------------------------------------------------------------------------- /4) Skin Cancer Classification Project with Tkinter/result1.txt: -------------------------------------------------------------------------------- 1 | D:/codes/course/HAM10000_images_part_1/ISIC_0024318.jpg--bcc -------------------------------------------------------------------------------- /4) Skin Cancer Classification Project with Tkinter/result2.txt: -------------------------------------------------------------------------------- 1 | D:/codes/course/HAM10000_images_part_1/ISIC_0024317.jpg--bcc -------------------------------------------------------------------------------- /6) Clustering Project with PyQt5/cluster_result.txt: -------------------------------------------------------------------------------- 1 | Cluster1: 40 (black) -- Cluster2: 39 (red) -- Cluster3: 121 (cyan) -- -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/Output.txt: -------------------------------------------------------------------------------- 1 | real entry: 2, model name: model2, noise value: 59, Predicted class: 2 -------------------------------------------------------------------------------- /1) Python Basics/x_y.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/1) Python Basics/x_y.png -------------------------------------------------------------------------------- /2) Tkinter Widgets/cell.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/2) Tkinter Widgets/cell.jpg -------------------------------------------------------------------------------- /5) PyQt5 widgets/icon1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/5) PyQt5 widgets/icon1.png -------------------------------------------------------------------------------- /1) Python Basics/data_pickle.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/1) Python Basics/data_pickle.pkl -------------------------------------------------------------------------------- /6) Clustering Project with PyQt5/cluster_figure.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/6) Clustering Project with PyQt5/cluster_figure.jpg -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/icon1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/icon1.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/input.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/model1.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/model1.h5 -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/model2.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/model2.h5 -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/0.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/1.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/2.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/3.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/4.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/5.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/6.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/7.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/8.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/images/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/images/9.png -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/input_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/7) Digit Classification Project with PyQt5/input_img.png -------------------------------------------------------------------------------- /4) Skin Cancer Classification Project with Tkinter/dx_type.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dataiteam/Python-GUI-Tkinter-PyQt5-with-Real-World-Python-Projects/HEAD/4) Skin Cancer Classification Project with Tkinter/dx_type.pdf -------------------------------------------------------------------------------- /1) Python Basics/data.txt: -------------------------------------------------------------------------------- 1 | ,country,capital,area,population 2 | 0,Turkey,Ankara,5.5,80 3 | 1,Russia,Moscow,17.1,150 4 | 2,France,Paris,4.1,80 5 | 3,China,Beijing,9.2,1500 6 | 4,South Africa,Pretoria,1.5,50 7 | 5,Spain,Madrid,4.0,70 8 | -------------------------------------------------------------------------------- /3) Forex Trading Project with Tkinter/news_EURGBR.txt: -------------------------------------------------------------------------------- 1 | News 2 | Euro/GBR 3 | Euro/GBR 4 | Euro/GBR 5 | Euro/GBR 6 | Euro/GBR 7 | Euro/GBR 8 | Euro/GBR 9 | Euro/GBR 10 | Euro/GBR 11 | Euro/GBR 12 | Euro/GBR 13 | Euro/GBR 14 | Euro/GBR 15 | Euro/GBR 16 | -------------------------------------------------------------------------------- /3) Forex Trading Project with Tkinter/news_EURUSD.txt: -------------------------------------------------------------------------------- 1 | News 2 | Euro/usd 3 | Euro/usd 4 | Euro/usd 5 | Euro/usd 6 | Euro/usd 7 | Euro/usd 8 | Euro/usd 9 | Euro/usd 10 | Euro/usd 11 | Euro/usd 12 | Euro/usd 13 | Euro/usd 14 | Euro/usd 15 | Euro/usd 16 | -------------------------------------------------------------------------------- /4) Skin Cancer Classification Project with Tkinter/referance.txt: -------------------------------------------------------------------------------- 1 | [2] Tschandl, P., Rosendahl, C. & Kittler, H. The HAM10000 dataset, a large collection of multi-source dermatoscopic images of common pigmented skin lesions. Sci. Data 5, 180161 doi:10.1038/sdata.2018.161 (2018). -------------------------------------------------------------------------------- /5) PyQt5 widgets/menubar.py: -------------------------------------------------------------------------------- 1 | from PyQt5.QtWidgets import * 2 | 3 | 4 | class Window(QMainWindow): 5 | 6 | def __init__(self): 7 | super().__init__() 8 | self.setGeometry(50,50,520,320) 9 | self.setWindowTitle("Menu App") 10 | 11 | self.menubar() 12 | self.show() 13 | 14 | def menubar(self): 15 | 16 | bar = self.menuBar() 17 | 18 | file = bar.addMenu("File") 19 | file.addAction("New") 20 | 21 | save = QAction("Save",self) 22 | save.setShortcut("Ctrl+S") 23 | file.addAction(save) 24 | 25 | data = file.addMenu("Data") 26 | data.addAction("Export") 27 | data.addAction("Import") 28 | 29 | file.triggered[QAction].connect(self.progressTrig) 30 | 31 | 32 | def progressTrig(self, q): 33 | print(q.text()) 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | window = Window() 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /2) Tkinter Widgets/frame_labelFrame_panedWindow.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import ttk 3 | 4 | window = tk.Tk() # high level window 5 | 6 | ## frame 7 | #frame_left = tk.Frame(window, width = 540, height = 640,bg = "red") 8 | #frame_left.grid(row = 0, column = 0, padx = 10, pady = 10) 9 | # 10 | #frame_right = tk.Frame(window, width = 540, height = 640, bg = "green") 11 | #frame_right.grid(row = 0, column = 1, padx = 10, pady = 10) 12 | # 13 | #frame1 = tk.LabelFrame(frame_left, text = "frame1", width = 540, height = 500,bg = "yellow") 14 | #frame1.grid(row = 0, column = 0, padx = 10, pady = 10) 15 | # 16 | #frame2 = tk.LabelFrame(frame_left, text = "frame2", width = 540, height = 140,bg = "yellow") 17 | #frame2.grid(row = 1, column = 0, padx = 10, pady = 10) 18 | # 19 | #label1 = tk.Label(frame2, text="label in frame2") 20 | #label1.grid(row=1,column=0, padx = 10, pady = 10) 21 | 22 | # paned window 23 | 24 | pw = ttk.Panedwindow(window, orient = tk.HORIZONTAL) 25 | pw.pack(fill = tk.BOTH, expand = True) 26 | 27 | m2 = ttk.Panedwindow(pw, orient = tk. VERTICAL) 28 | 29 | frame2 = ttk.Frame(pw, width = 720, height = 400, relief = tk.RIDGE) 30 | frame3 = ttk.Frame(pw, width = 720, height = 240, relief = tk.RAISED) 31 | m2.add(frame2) 32 | m2.add(frame3) 33 | 34 | frame1 = ttk.Frame(pw, width = 360, height = 640, relief = tk.GROOVE) 35 | pw.add(m2) 36 | pw.add(frame1) 37 | 38 | 39 | 40 | window.mainloop() -------------------------------------------------------------------------------- /2) Tkinter Widgets/geometry_manager.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import ttk 3 | 4 | window = tk.Tk() 5 | window.title("Geometry Manager") 6 | 7 | 8 | ## pack 9 | #red = tk.Button(window, text = "Red",fg= "red") 10 | #red.pack(side = tk.LEFT) 11 | # 12 | #green = tk.Button(window, text = "green",fg= "green") 13 | #green.pack(side = tk.LEFT) 14 | # 15 | #blue = tk.Button(window, text = "blue",fg= "blue") 16 | #blue.pack(side = tk.LEFT) 17 | # 18 | #black = tk.Button(window, text = "black",fg= "black") 19 | #black.pack(side = tk.BOTTOM) 20 | # 21 | #brown = tk.Button(window, text = "brown",fg= "brown") 22 | #brown.pack(side = tk.BOTTOM) 23 | # 24 | #cyan = tk.Button(window, text = "cyan",fg= "cyan") 25 | #cyan.pack(side = tk.TOP, fill = tk.BOTH, expand = True) 26 | 27 | ## grid manager 28 | #for r in range(5): 29 | # for c in range(5): 30 | # label = tk.Label(window, text = 'R%s/C%s'%(r,c),borderwidth = 2) 31 | # label.grid(row = r, column = c, padx = 3, pady = 3) 32 | 33 | # place 34 | 35 | label1 = tk.Label(window, text = "place1") 36 | label1.place(x = 15, y = 15) 37 | 38 | label2 = tk.Label(window, text = "place2") 39 | label2.place(x = 30, y = 30) 40 | 41 | label3 = tk.Label(window, text = "place3") 42 | label3.place(x = 45, y = 45) 43 | 44 | label4 = tk.Label(window, text = "place4") 45 | label4.place(relx = 0, rely = 0) 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | window.mainloop() 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /2) Tkinter Widgets/open_image_plot_mouseEvent.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import filedialog 3 | from PIL import ImageTk, Image 4 | 5 | from matplotlib.figure import Figure 6 | from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 7 | import numpy as np 8 | 9 | window = tk.Tk() 10 | window.geometry("500x800") 11 | 12 | 13 | # file dialog 14 | def openFile(): 15 | file_name = filedialog.askopenfilename(initialdir = "D:\codes\course", title = "select a file...") 16 | print(file_name) 17 | img = Image.open(file_name) 18 | img = ImageTk.PhotoImage(img) 19 | 20 | label = tk.Label(window, image = img) 21 | label.image = img 22 | label.pack(padx = 15, pady = 15) 23 | 24 | button = tk.Button(window, text = "open file", command = openFile) 25 | button.pack() 26 | 27 | # plot 28 | fig = Figure(figsize = (5,4), dpi = 50) 29 | data = np.arange(0,3,0.1) 30 | fig.add_subplot(111).plot(data,data*2+10) 31 | 32 | canvas = FigureCanvasTkAgg(fig,master = window) 33 | canvas.draw() 34 | canvas.get_tk_widget().pack() 35 | 36 | # mouse event 37 | def leftClick(event): 38 | tk.Label(window,text = "left").pack() 39 | 40 | def middleClick(event): 41 | tk.Label(window,text = "middle").pack() 42 | 43 | def rightClick(event): 44 | tk.Label(window,text = "right").pack() 45 | 46 | 47 | window.bind("",leftClick) 48 | window.bind("",middleClick) 49 | window.bind("",rightClick) 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 | window.mainloop() -------------------------------------------------------------------------------- /2) Tkinter Widgets/button_label_entry_messageBox.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import ttk 3 | from tkinter import messagebox 4 | 5 | window = tk.Tk() # 6 | window.geometry("500x450") 7 | window.title("Welcome to first app") 8 | 9 | def buttonFunction(): 10 | print("Push button") 11 | 12 | # label 13 | label.config(text = "hello world", 14 | fg = "black", 15 | bg = "red", 16 | font = "Times 25") 17 | # entry 18 | value = entry.get() 19 | print(value) 20 | label.configure(text = value) 21 | entry.configure(state = "disabled") 22 | 23 | # message 24 | # message_box = messagebox.showinfo(title = "info", message = "information") 25 | # message_box = messagebox.askretrycancel(title = "info", message = "information") 26 | # message_box = messagebox.askquestion(title = "info", message = "information") 27 | # message_box = messagebox.askyesnocancel(title = "info", message = "information") 28 | message_box = messagebox.showerror(title = "info", message = "information") 29 | print(message_box) 30 | 31 | # button 32 | button = tk.Button(window, text = "First button", activebackground = "red", 33 | bg = "black", fg = "white", activeforeground = "black", 34 | height = 15, width = 50, 35 | command = buttonFunction) 36 | 37 | button.pack() 38 | 39 | #label 40 | label = tk.Label(window, text = "Hi World", font = "Times 16", fg = "white", bg = "black", 41 | wraplength = 150) 42 | 43 | label.pack(side = tk.RIGHT, padx = 25) 44 | 45 | # entry 46 | 47 | entry = tk.Entry(window, width = 50) 48 | entry.insert(string = "write something only one time",index = 0) 49 | entry.pack(side = tk.LEFT, padx = 25) 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | window.mainloop() -------------------------------------------------------------------------------- /2) Tkinter Widgets/radioButton_comboBox_checkButton.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import ttk 3 | 4 | window = tk.Tk()# top level window 5 | window.geometry("500x450") # window size x*y 6 | 7 | def buttonFunction(): 8 | print("here") 9 | # radio button 10 | m = method.get() 11 | if m =="1": 12 | print("method1") 13 | elif m =="2": 14 | print("method2") 15 | else: 16 | print("method1 & method2") 17 | 18 | # combobox 19 | print(problem.get()) 20 | 21 | # checkButton 22 | value = save_var.get() 23 | if value == 1: 24 | print("Save__") 25 | else: 26 | print("not save") 27 | 28 | button = tk.Button(window, text = "button", activebackground = "red", bg = "black", 29 | fg = "white", activeforeground = "black", 30 | height = 15, width = 50, command = buttonFunction) 31 | 32 | button.grid(row = 0, column = 0, pady = 15) 33 | 34 | # radio button 35 | method = tk.StringVar() 36 | tk.Radiobutton(window, text = "method1: ", value = "1", activebackground = "red", 37 | bg = "green",height = 5, width = 5,borderwidth = 15,variable = method).grid(row=1,column=0) 38 | 39 | tk.Radiobutton(window, text = "method2: ", value = "2", variable = method).grid(row=1,column=1, pady=15) 40 | 41 | # comboBox 42 | problem = tk.StringVar() 43 | comboBox = ttk.Combobox(window, textvariable = problem, values = ("1","2","3"), state= "readonly") 44 | comboBox.grid(row=2,column=0, pady=15) 45 | 46 | def checkButtonFunction(): 47 | print("save") 48 | 49 | # checkButton 50 | save_var = tk.IntVar() 51 | save_var.set(0) 52 | c_button = tk.Checkbutton(window, text = "Save", variable = save_var, font = "Times 25", activebackground = "green", 53 | activeforeground = "white", 54 | bg = "yellow", command = checkButtonFunction) 55 | c_button.grid(row=2,column=1, pady=15) 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | window.mainloop() -------------------------------------------------------------------------------- /2) Tkinter Widgets/menu_tabs_treeview_listbox_textEditor_scroll.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import ttk 3 | 4 | window = tk.Tk() 5 | window.geometry("800x600") 6 | 7 | 8 | # menu 9 | def fileFunction(): 10 | print("here") 11 | 12 | menubar = tk.Menu(window) 13 | window.config(menu = menubar) 14 | 15 | file = tk.Menu(menubar) 16 | edit = tk.Menu(menubar) 17 | 18 | menubar.add_cascade(label = "file", menu = file) 19 | menubar.add_cascade(label = "edit", menu = edit) 20 | 21 | file.add_command(label = "new file", command =fileFunction ) 22 | edit.add_command(label = "undo", command =fileFunction ) 23 | 24 | 25 | # tabs 26 | tabs = ttk.Notebook(window, width = 540, height = 300) 27 | tabs.place(x = 25, y = 25) 28 | 29 | tab1 = ttk.Frame(tabs, width = 50, height = 50) 30 | tab2 = ttk.Frame(tabs, width = 50, height = 50) 31 | tab3 = ttk.Frame(tabs, width = 50, height = 50) 32 | 33 | tk.Label(tab1, text = "tab1").pack() 34 | tk.Label(tab2, text = "tab2").pack() 35 | #tk.Label(tab3, text = "tab3").grid() 36 | 37 | tabs.add(tab1, text = "treeview") 38 | tabs.add(tab2, text = "list box") 39 | tabs.add(tab3, text = "text editor") 40 | 41 | # tree view 42 | treeview = ttk.Treeview(tab1) 43 | treeview.place(x = 5, y = 5) 44 | 45 | treeview.insert("","0","item1", text = "Spain") 46 | treeview.insert("item1","1","item2", text = "Madrid") 47 | treeview.insert("","2","item3", text = "France") 48 | treeview.insert("item3","3","item4", text = "Paris") 49 | 50 | def callback(event): 51 | item = treeview.identify("item",event.x,event.y) 52 | print(item) 53 | 54 | treeview.bind("",callback) 55 | 56 | # listbox 57 | listBox = tk.Listbox(tab2, selectmode = tk.MULTIPLE) 58 | listBox.insert(0,"Spain") 59 | listBox.insert(1,"France") 60 | listBox.insert(2,"China") 61 | 62 | listBox.place(x = 5, y = 5) 63 | 64 | def getItem(): 65 | index_list = listBox.curselection() 66 | print(index_list) 67 | for each in index_list: 68 | print(listBox.get(each)) 69 | 70 | button = tk.Button(tab2, text = "button", command = getItem) 71 | button.place(x = 150, y = 5) 72 | 73 | # text editor 74 | textEditor = tk.Text(tab3, width = 25, height = 25, wrap = tk.WORD) 75 | textEditor.grid(row = 0, column = 0, padx = 10, pady = 10) 76 | 77 | def textFunction(): 78 | print(textEditor.get(1.0, tk.END)) 79 | 80 | button = tk.Button(tab3, text = "save", command = textFunction) 81 | button.grid(row = 0, column = 2, padx = 10, pady = 10) 82 | 83 | 84 | # scroll 85 | scroll = tk.Scrollbar(tab3, orient = tk.VERTICAL, command = textEditor.yview) 86 | scroll.grid(row = 0, column = 1, sticky = tk.N + tk.S) 87 | textEditor.config(yscrollcomman = scroll.set) 88 | 89 | 90 | window.mainloop() 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /5) PyQt5 widgets/radioButton_comboBox_checkButton_progressBar.py: -------------------------------------------------------------------------------- 1 | from PyQt5.QtWidgets import * 2 | from PyQt5.QtCore import QTimer 3 | 4 | class Window(QWidget): 5 | 6 | def __init__(self): 7 | super().__init__() 8 | 9 | self.setGeometry(50,50,1080, 640) 10 | self.setWindowTitle("PYQT5 App") 11 | 12 | self.radioButton() 13 | self.comboBox() 14 | self.checkBox() 15 | self.progressBar() 16 | self.show() 17 | 18 | def radioButton(self): 19 | 20 | self.method1 = QRadioButton("method1",self) 21 | self.method2 = QRadioButton("method2",self) 22 | self.method3 = QRadioButton("method3",self) 23 | 24 | self.method1.move(50,40) 25 | self.method2.move(50,60) 26 | self.method3.move(50,80) 27 | 28 | self.method1.setChecked(True) 29 | 30 | button = QPushButton("Radio Button",self) 31 | button.move(50,100) 32 | button.clicked.connect(self.radioButtonFunction) 33 | 34 | def radioButtonFunction(self): 35 | 36 | if self.method1.isChecked(): 37 | print("method1") 38 | elif self.method2.isChecked(): 39 | print("method2") 40 | elif self.method3.isChecked(): 41 | print("method3") 42 | else: 43 | print("choose") 44 | 45 | def comboBox(self): 46 | 47 | self.combo = QComboBox(self) 48 | self.combo.move(150,40) 49 | self.combo.addItem("method1") 50 | self.combo.addItem("method2") 51 | self.combo.addItems(["method3","method4"]) 52 | 53 | button = QPushButton("Combo Box",self) 54 | button.move(150,70) 55 | button.clicked.connect(self.comboFunction) 56 | 57 | def comboFunction(self): 58 | print(self.combo.currentText()) 59 | 60 | 61 | def checkBox(self): 62 | self.save = QCheckBox("Save",self) 63 | self.save.move(250,40) 64 | 65 | button = QPushButton("Save",self) 66 | button.move(250,70) 67 | button.clicked.connect(self.checkFunction) 68 | 69 | def checkFunction(self): 70 | if self.save.isChecked(): 71 | print("save") 72 | else: 73 | print("not save") 74 | 75 | def progressBar(self): 76 | self.pbar = QProgressBar(self) 77 | self.pbar.setGeometry(400,40, 200, 25) 78 | self.pbar.setValue(0) 79 | 80 | self.timer = QTimer() 81 | self.timer.timeout.connect(self.handleTimer) 82 | self.timer.start(500) 83 | 84 | def handleTimer(self): 85 | value = self.pbar.value() 86 | step = 5 87 | if value < 100: 88 | value = value + step 89 | self.pbar.setValue(value) 90 | else: 91 | self.timer.stop() 92 | 93 | window = Window() 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /5) PyQt5 widgets/button_label_entry_messageBox.py: -------------------------------------------------------------------------------- 1 | from PyQt5.QtWidgets import * 2 | from PyQt5.QtGui import QFont 3 | 4 | class Window(QWidget): 5 | 6 | def __init__(self): 7 | super().__init__() 8 | 9 | self.setGeometry(50,50,1080, 640) 10 | self.setWindowTitle("PYQT5 App") 11 | 12 | self.button() 13 | self.label() 14 | self.entry() 15 | self.messageBox() 16 | self.font() 17 | self.show() 18 | 19 | # button 20 | def button(self): 21 | button = QPushButton("Hello World",self) 22 | button.setToolTip("This is a hello world button") 23 | button.resize(100,50) 24 | button.move(50,50) 25 | button.clicked.connect(self.buttonFunction) 26 | 27 | def buttonFunction(self): 28 | print("hello world") 29 | 30 | # label 31 | def label(self): 32 | 33 | text1 = QLabel("hello",self) 34 | self.text2 = QLabel("world",self) 35 | 36 | # geometry manager 37 | text1.move(170,50) 38 | self.text2.move(170,70) 39 | 40 | button1 = QPushButton("Change",self) 41 | button1.move(170,100) 42 | button1.clicked.connect(self.button1Function) 43 | 44 | def button1Function(self): 45 | self.text2.setText("Hello World") 46 | self.text2.resize(200,25) 47 | self.text2.setFont(QFont("Arial", 25, QFont.Bold)) 48 | 49 | def entry(self): 50 | 51 | self.textBox = QLineEdit(self) 52 | self.textBox.setPlaceholderText("place holder") 53 | self.textBox.move(300,50) 54 | 55 | button1 = QPushButton("Save",self) 56 | button1.move(300,75) 57 | button1.clicked.connect(self.saveFunction) 58 | 59 | def saveFunction(self): 60 | 61 | txt = self.textBox.text() 62 | 63 | if txt != "": 64 | print(txt) 65 | else: 66 | print("write something") 67 | 68 | def messageBox(self): 69 | 70 | button1 = QPushButton("message",self) 71 | button1.move(500,50) 72 | button1.clicked.connect(self.messageFunction) 73 | 74 | button2 = QPushButton("message2",self) 75 | button2.move(500,75) 76 | button2.clicked.connect(self.messageFunction2) 77 | 78 | def messageFunction(self): 79 | m_box = QMessageBox.question(self,"Question","Did you enjoy the course?", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.No) 80 | 81 | if m_box == QMessageBox.Yes: 82 | print("yes") 83 | elif m_box == QMessageBox.No: 84 | print("no") 85 | else: 86 | print("cancel") 87 | 88 | def messageFunction2(self): 89 | m_box = QMessageBox.information(self,"Information","Enjor your course") 90 | 91 | 92 | def font(self): 93 | self.label = QLabel("Hello world",self) 94 | self.label.move(700,100) 95 | 96 | button2 = QPushButton("choose font",self) 97 | button2.move(700,50) 98 | button2.clicked.connect(self.setfont) 99 | 100 | def setfont(self): 101 | font, ok = QFontDialog.getFont() 102 | 103 | if ok: 104 | self.label.setFont(font) 105 | self.label.resize(200,75) 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | # 127 | window = Window() 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /6) Clustering Project with PyQt5/data.csv: -------------------------------------------------------------------------------- 1 | CustomerID,Genre,Age,Annual Income (k$),Spending Score (1-100) 2 | 0001,Male,19,15,39 3 | 0002,Male,21,15,81 4 | 0003,Female,20,16,6 5 | 0004,Female,23,16,77 6 | 0005,Female,31,17,40 7 | 0006,Female,22,17,76 8 | 0007,Female,35,18,6 9 | 0008,Female,23,18,94 10 | 0009,Male,64,19,3 11 | 0010,Female,30,19,72 12 | 0011,Male,67,19,14 13 | 0012,Female,35,19,99 14 | 0013,Female,58,20,15 15 | 0014,Female,24,20,77 16 | 0015,Male,37,20,13 17 | 0016,Male,22,20,79 18 | 0017,Female,35,21,35 19 | 0018,Male,20,21,66 20 | 0019,Male,52,23,29 21 | 0020,Female,35,23,98 22 | 0021,Male,35,24,35 23 | 0022,Male,25,24,73 24 | 0023,Female,46,25,5 25 | 0024,Male,31,25,73 26 | 0025,Female,54,28,14 27 | 0026,Male,29,28,82 28 | 0027,Female,45,28,32 29 | 0028,Male,35,28,61 30 | 0029,Female,40,29,31 31 | 0030,Female,23,29,87 32 | 0031,Male,60,30,4 33 | 0032,Female,21,30,73 34 | 0033,Male,53,33,4 35 | 0034,Male,18,33,92 36 | 0035,Female,49,33,14 37 | 0036,Female,21,33,81 38 | 0037,Female,42,34,17 39 | 0038,Female,30,34,73 40 | 0039,Female,36,37,26 41 | 0040,Female,20,37,75 42 | 0041,Female,65,38,35 43 | 0042,Male,24,38,92 44 | 0043,Male,48,39,36 45 | 0044,Female,31,39,61 46 | 0045,Female,49,39,28 47 | 0046,Female,24,39,65 48 | 0047,Female,50,40,55 49 | 0048,Female,27,40,47 50 | 0049,Female,29,40,42 51 | 0050,Female,31,40,42 52 | 0051,Female,49,42,52 53 | 0052,Male,33,42,60 54 | 0053,Female,31,43,54 55 | 0054,Male,59,43,60 56 | 0055,Female,50,43,45 57 | 0056,Male,47,43,41 58 | 0057,Female,51,44,50 59 | 0058,Male,69,44,46 60 | 0059,Female,27,46,51 61 | 0060,Male,53,46,46 62 | 0061,Male,70,46,56 63 | 0062,Male,19,46,55 64 | 0063,Female,67,47,52 65 | 0064,Female,54,47,59 66 | 0065,Male,63,48,51 67 | 0066,Male,18,48,59 68 | 0067,Female,43,48,50 69 | 0068,Female,68,48,48 70 | 0069,Male,19,48,59 71 | 0070,Female,32,48,47 72 | 0071,Male,70,49,55 73 | 0072,Female,47,49,42 74 | 0073,Female,60,50,49 75 | 0074,Female,60,50,56 76 | 0075,Male,59,54,47 77 | 0076,Male,26,54,54 78 | 0077,Female,45,54,53 79 | 0078,Male,40,54,48 80 | 0079,Female,23,54,52 81 | 0080,Female,49,54,42 82 | 0081,Male,57,54,51 83 | 0082,Male,38,54,55 84 | 0083,Male,67,54,41 85 | 0084,Female,46,54,44 86 | 0085,Female,21,54,57 87 | 0086,Male,48,54,46 88 | 0087,Female,55,57,58 89 | 0088,Female,22,57,55 90 | 0089,Female,34,58,60 91 | 0090,Female,50,58,46 92 | 0091,Female,68,59,55 93 | 0092,Male,18,59,41 94 | 0093,Male,48,60,49 95 | 0094,Female,40,60,40 96 | 0095,Female,32,60,42 97 | 0096,Male,24,60,52 98 | 0097,Female,47,60,47 99 | 0098,Female,27,60,50 100 | 0099,Male,48,61,42 101 | 0100,Male,20,61,49 102 | 0101,Female,23,62,41 103 | 0102,Female,49,62,48 104 | 0103,Male,67,62,59 105 | 0104,Male,26,62,55 106 | 0105,Male,49,62,56 107 | 0106,Female,21,62,42 108 | 0107,Female,66,63,50 109 | 0108,Male,54,63,46 110 | 0109,Male,68,63,43 111 | 0110,Male,66,63,48 112 | 0111,Male,65,63,52 113 | 0112,Female,19,63,54 114 | 0113,Female,38,64,42 115 | 0114,Male,19,64,46 116 | 0115,Female,18,65,48 117 | 0116,Female,19,65,50 118 | 0117,Female,63,65,43 119 | 0118,Female,49,65,59 120 | 0119,Female,51,67,43 121 | 0120,Female,50,67,57 122 | 0121,Male,27,67,56 123 | 0122,Female,38,67,40 124 | 0123,Female,40,69,58 125 | 0124,Male,39,69,91 126 | 0125,Female,23,70,29 127 | 0126,Female,31,70,77 128 | 0127,Male,43,71,35 129 | 0128,Male,40,71,95 130 | 0129,Male,59,71,11 131 | 0130,Male,38,71,75 132 | 0131,Male,47,71,9 133 | 0132,Male,39,71,75 134 | 0133,Female,25,72,34 135 | 0134,Female,31,72,71 136 | 0135,Male,20,73,5 137 | 0136,Female,29,73,88 138 | 0137,Female,44,73,7 139 | 0138,Male,32,73,73 140 | 0139,Male,19,74,10 141 | 0140,Female,35,74,72 142 | 0141,Female,57,75,5 143 | 0142,Male,32,75,93 144 | 0143,Female,28,76,40 145 | 0144,Female,32,76,87 146 | 0145,Male,25,77,12 147 | 0146,Male,28,77,97 148 | 0147,Male,48,77,36 149 | 0148,Female,32,77,74 150 | 0149,Female,34,78,22 151 | 0150,Male,34,78,90 152 | 0151,Male,43,78,17 153 | 0152,Male,39,78,88 154 | 0153,Female,44,78,20 155 | 0154,Female,38,78,76 156 | 0155,Female,47,78,16 157 | 0156,Female,27,78,89 158 | 0157,Male,37,78,1 159 | 0158,Female,30,78,78 160 | 0159,Male,34,78,1 161 | 0160,Female,30,78,73 162 | 0161,Female,56,79,35 163 | 0162,Female,29,79,83 164 | 0163,Male,19,81,5 165 | 0164,Female,31,81,93 166 | 0165,Male,50,85,26 167 | 0166,Female,36,85,75 168 | 0167,Male,42,86,20 169 | 0168,Female,33,86,95 170 | 0169,Female,36,87,27 171 | 0170,Male,32,87,63 172 | 0171,Male,40,87,13 173 | 0172,Male,28,87,75 174 | 0173,Male,36,87,10 175 | 0174,Male,36,87,92 176 | 0175,Female,52,88,13 177 | 0176,Female,30,88,86 178 | 0177,Male,58,88,15 179 | 0178,Male,27,88,69 180 | 0179,Male,59,93,14 181 | 0180,Male,35,93,90 182 | 0181,Female,37,97,32 183 | 0182,Female,32,97,86 184 | 0183,Male,46,98,15 185 | 0184,Female,29,98,88 186 | 0185,Female,41,99,39 187 | 0186,Male,30,99,97 188 | 0187,Female,54,101,24 189 | 0188,Male,28,101,68 190 | 0189,Female,41,103,17 191 | 0190,Female,36,103,85 192 | 0191,Female,34,103,23 193 | 0192,Female,32,103,69 194 | 0193,Male,33,113,8 195 | 0194,Female,38,113,91 196 | 0195,Female,47,120,16 197 | 0196,Female,35,120,79 198 | 0197,Female,45,126,28 199 | 0198,Male,32,126,74 200 | 0199,Male,32,137,18 201 | 0200,Male,30,137,83 -------------------------------------------------------------------------------- /5) PyQt5 widgets/horizontal_vertical_formLayout_qsplitterFrame_groupboxGridLayout.py: -------------------------------------------------------------------------------- 1 | from PyQt5.QtWidgets import * 2 | from PyQt5.QtCore import Qt 3 | 4 | class Window(QWidget): 5 | 6 | def __init__(self): 7 | super().__init__() 8 | 9 | self.setGeometry(50,50,1080, 640) 10 | self.setWindowTitle("PYQT5 App") 11 | 12 | # self.horizontal() 13 | # self.vertical() 14 | # self.verticalAndHorizontal() 15 | # self.formlayout() 16 | # self.qsplitter() 17 | self.gridLayout() 18 | self.show() 19 | 20 | def horizontal(self): 21 | 22 | hbox = QHBoxLayout() 23 | 24 | # widgets 25 | button1 = QPushButton("yes",self) 26 | text1 = QLabel("hello",self) 27 | button2 = QPushButton("no",self) 28 | text2 = QLabel("world",self) 29 | button3 = QPushButton("1",self) 30 | text3 = QLabel("2",self) 31 | 32 | # add widgets into hbox 33 | hbox.addStretch() 34 | hbox.addWidget(button1) 35 | hbox.addWidget(text1) 36 | hbox.addWidget(button2) 37 | hbox.addWidget(text2) 38 | hbox.addWidget(button3) 39 | hbox.addWidget(text3) 40 | hbox.addStretch() 41 | 42 | self.setLayout(hbox) 43 | 44 | def vertical(self): 45 | 46 | vbox = QVBoxLayout() 47 | 48 | # widgets 49 | button1 = QPushButton("yes",self) 50 | text1 = QLabel("hello",self) 51 | button2 = QPushButton("no",self) 52 | text2 = QLabel("world",self) 53 | button3 = QPushButton("1",self) 54 | text3 = QLabel("2",self) 55 | 56 | # add widgets into hbox 57 | # vbox.addStretch() 58 | vbox.addWidget(button1) 59 | vbox.addWidget(text1) 60 | vbox.addWidget(button2) 61 | vbox.addWidget(text2) 62 | vbox.addWidget(button3) 63 | vbox.addWidget(text3) 64 | vbox.addStretch() 65 | 66 | self.setLayout(vbox) 67 | 68 | def verticalAndHorizontal(self): 69 | 70 | mainlayout = QHBoxLayout() 71 | 72 | leftlayout = QVBoxLayout() 73 | midlayout = QVBoxLayout() 74 | rightlayout = QVBoxLayout() 75 | 76 | mainlayout.addLayout(leftlayout) 77 | mainlayout.addLayout(midlayout) 78 | mainlayout.addLayout(rightlayout) 79 | 80 | # widgets 81 | button1 = QPushButton("left") 82 | button2 = QPushButton("mid") 83 | button3 = QPushButton("r1") 84 | button4 = QPushButton("r2") 85 | 86 | leftlayout.addWidget(button1) 87 | 88 | midlayout.addWidget(button2) 89 | 90 | rightlayout.addStretch() 91 | rightlayout.addWidget(button3) 92 | rightlayout.addWidget(button4) 93 | rightlayout.addStretch() 94 | 95 | self.setLayout(mainlayout) 96 | 97 | def formlayout(self): 98 | 99 | hbox1= QHBoxLayout() 100 | 101 | hbox1.addWidget(QPushButton("1")) 102 | hbox1.addWidget(QPushButton("2")) 103 | hbox1.addStretch() 104 | 105 | formLayout = QFormLayout() 106 | formLayout.addRow(QLabel("Push 1 or 2"), hbox1) 107 | 108 | self.setLayout(formLayout) 109 | 110 | def qsplitter(self): 111 | 112 | hbox = QHBoxLayout(self) 113 | 114 | topleft = QFrame(self) 115 | topleft.setFrameShape(QFrame.StyledPanel) 116 | 117 | topright = QFrame(self) 118 | topright.setFrameShape(QFrame.StyledPanel) 119 | 120 | bottom = QFrame(self) 121 | bottom.setFrameShape(QFrame.StyledPanel) 122 | 123 | splitter1 = QSplitter(Qt.Horizontal) 124 | splitter1.addWidget(topleft) 125 | splitter1.addWidget(topright) 126 | 127 | splitter2 = QSplitter(Qt.Vertical) 128 | splitter2.addWidget(splitter1) 129 | splitter2.addWidget(bottom) 130 | 131 | hbox.addWidget(splitter2) 132 | self.setLayout(hbox) 133 | 134 | 135 | def createButton(self): 136 | 137 | groupBox = QGroupBox("Buttons") 138 | button1 = QPushButton("1") 139 | button2 = QPushButton("2") 140 | button3 = QPushButton("3") 141 | 142 | vbox = QVBoxLayout() 143 | vbox.addWidget(button1) 144 | vbox.addWidget(button2) 145 | vbox.addWidget(button3) 146 | vbox.addStretch() 147 | 148 | groupBox.setLayout(vbox) 149 | 150 | return groupBox 151 | 152 | def gridLayout(self): 153 | 154 | grid = QGridLayout() 155 | 156 | grid.addWidget(self.createButton(),0,0) 157 | grid.addWidget(self.createButton(),1,0) 158 | 159 | 160 | self.setLayout(grid) 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | window = Window() 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /5) PyQt5 widgets/tab_list_text editor_slider_table_spinbox_images_calendar.py: -------------------------------------------------------------------------------- 1 | from PyQt5.QtWidgets import * 2 | from PyQt5.QtCore import Qt, QDate 3 | from PyQt5.QtGui import QPixmap 4 | import numpy as np 5 | from datetime import datetime 6 | import calendar 7 | 8 | class Window(QWidget): 9 | 10 | def __init__(self): 11 | super().__init__() 12 | 13 | self.setGeometry(50,50,1080, 640) 14 | self.setWindowTitle("PYQT5 App") 15 | 16 | # self.tabs() 17 | # self.listW() 18 | # self.textEditor() 19 | # self.slider() 20 | # self.table() 21 | # self.spinbox() 22 | # self.image() 23 | self.calendar() 24 | self.show() 25 | 26 | def tabs(self): 27 | 28 | mainLayout = QVBoxLayout() 29 | 30 | self.tab = QTabWidget() 31 | 32 | self.tab1 = QWidget() 33 | self.tab2 = QWidget() 34 | self.tab3 = QWidget() 35 | 36 | vbox = QVBoxLayout() 37 | hbox = QHBoxLayout() 38 | hbox2 = QHBoxLayout() 39 | 40 | self.button1 = QPushButton("First tab") 41 | self.button2 = QPushButton("Second tab") 42 | self.button3 = QPushButton("Third tab") 43 | 44 | vbox.addWidget(self.button1) 45 | hbox.addWidget(self.button2) 46 | hbox2.addWidget(self.button3) 47 | 48 | self.tab1.setLayout(vbox) 49 | self.tab2.setLayout(hbox) 50 | self.tab3.setLayout(hbox2) 51 | 52 | self.tab.addTab(self.tab1, "First") 53 | self.tab.addTab(self.tab2, "Second") 54 | self.tab.addTab(self.tab3, "Third") 55 | 56 | mainLayout.addWidget(self.tab) 57 | 58 | self.setLayout(mainLayout) 59 | 60 | def listW(self): 61 | 62 | self.list = QListWidget(self) 63 | 64 | c = 2019 65 | 66 | for i in range(10): 67 | self.list.addItem(str(c-i)) 68 | 69 | def textEditor(self): 70 | 71 | self.editor = QTextEdit(self) 72 | self.editor.move(50,50) 73 | 74 | button = QPushButton("Text Editor",self) 75 | button.move(50,25) 76 | button.clicked.connect(self.textEditorFunction) 77 | 78 | def textEditorFunction(self): 79 | text = self.editor.toPlainText() 80 | print(text) 81 | 82 | def slider(self): 83 | 84 | vbox = QVBoxLayout() 85 | 86 | self.slider = QSlider(Qt.Horizontal) 87 | self.slider.setMinimum(0) 88 | self.slider.setMaximum(100) 89 | self.slider.setTickInterval(10) 90 | self.slider.setTickPosition(QSlider.TicksBelow) 91 | 92 | self.slider.valueChanged.connect(self.sliderFunction) 93 | 94 | vbox.addWidget(self.slider) 95 | vbox.addStretch() 96 | self.setLayout(vbox) 97 | 98 | def sliderFunction(self): 99 | print(self.slider.value()) 100 | 101 | def table(self): 102 | 103 | vbox = QVBoxLayout() 104 | 105 | self.table = QTableWidget() 106 | self.table.setRowCount(2) 107 | self.table.setColumnCount(2) 108 | self.table.setHorizontalHeaderItem(0, QTableWidgetItem("method1")) 109 | self.table.setHorizontalHeaderItem(1, QTableWidgetItem("method2")) 110 | 111 | arr = np.array([[1,2],[3,4]]) 112 | for r in range(arr.shape[0]): 113 | for c in range(arr.shape[1]): 114 | self.table.setItem(r,c,QTableWidgetItem(str(arr[r,c]))) 115 | 116 | button = QPushButton("Table") 117 | button.clicked.connect(self.getValue) 118 | 119 | vbox.addWidget(self.table) 120 | vbox.addWidget(button) 121 | self.setLayout(vbox) 122 | 123 | def getValue(self): 124 | for item in self.table.selectedItems(): 125 | print("Value: {}, row: {}, column: {}".format(item.text(),item.row(),item.column())) 126 | 127 | def spinbox(self): 128 | 129 | self.spinbox = QSpinBox(self) 130 | self.spinbox.move(50,50) 131 | self.spinbox.setRange(30,40) 132 | self.spinbox.setSingleStep(1) 133 | self.spinbox.setSuffix(" $") 134 | 135 | button = QPushButton("spin button",self) 136 | button.move(50,25) 137 | button.clicked.connect(self.spinFunction) 138 | 139 | def spinFunction(self): 140 | print(self.spinbox.value()) 141 | 142 | def image(self): 143 | 144 | self.image = QLabel(self) 145 | self.image.setPixmap(QPixmap("icon1.png")) 146 | self.image.move(50,50) 147 | 148 | def calendar(self): 149 | 150 | self.calendar = QCalendarWidget(self) 151 | self.calendar.move(20,20) 152 | self.calendar.setGridVisible(True) 153 | 154 | self.calendar.clicked.connect(self.printDateInfo) 155 | 156 | def printDateInfo(self, qDate): 157 | print("{}/{}/{}".format(qDate.month(),qDate.day(),qDate.year())) 158 | 159 | 160 | window = Window() 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /6) Clustering Project with PyQt5/Clustering_project_with_pyqt5.py: -------------------------------------------------------------------------------- 1 | import warnings 2 | warnings.filterwarnings("ignore") 3 | 4 | from PyQt5.QtWidgets import * 5 | 6 | import numpy as np 7 | import pandas as pd 8 | from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 9 | from matplotlib.figure import Figure 10 | 11 | class Window(QMainWindow): 12 | 13 | def __init__(self): 14 | super().__init__() 15 | 16 | self.left = 50 17 | self.top = 50 18 | self.width = 1080 19 | self.height = 640 20 | self.title = "Clustering" 21 | 22 | self.setWindowTitle(self.title) 23 | self.setGeometry(self.left, self.top ,self.width, self.height ) 24 | 25 | self.k = 1 26 | self.save_txt = "" 27 | 28 | self.widgets() 29 | self.tabWidget() 30 | self.layouts() 31 | self.prepareData() 32 | self.show() 33 | 34 | def tabWidget(self): 35 | self.tabs = QTabWidget() 36 | self.setCentralWidget(self.tabs) 37 | self.tab1 = QWidget() 38 | self.tabs.addTab(self.tab1,"Main") 39 | 40 | def widgets(self): 41 | 42 | # plot 43 | self.p = PlotCanvas(self,width = 5, height = 5) 44 | 45 | # label K 46 | self.k_number_text = QLabel("Choose K:") 47 | 48 | # spin box 49 | self.k_number = QSpinBox(self) 50 | self.k_number.setMinimum(1) 51 | self.k_number.setMaximum(9) 52 | self.k_number.setSingleStep(1) 53 | self.k_number.valueChanged.connect(self.k_numberFunction) 54 | 55 | # radio button 56 | self.text_save = QRadioButton("Save text",self) 57 | self.plot_save = QRadioButton("Save plot",self) 58 | self.text_plot_save = QRadioButton("Save text and plot",self) 59 | self.text_plot_save.setChecked(True) 60 | 61 | # button 62 | self.cluster = QPushButton("Cluster",self) 63 | self.cluster.clicked.connect(self.clusterFunction) 64 | 65 | # list 66 | self.result_list = QListWidget(self) 67 | 68 | def prepareData(self): 69 | 70 | self.p.clear() 71 | 72 | data = pd.read_csv("data.csv") 73 | 74 | self.f1 = data.iloc[:,3].values 75 | self.f2 = data.iloc[:,4].values 76 | 77 | X = np.array(list(zip(self.f1,self.f2))) 78 | 79 | self.C_x = np.random.randint(0, np.max(X) - 20, size = self.k) 80 | self.C_y = np.random.randint(0, np.max(X) - 20, size = self.k) 81 | 82 | self.p.plot(self.f1,self.f2,"black",7) 83 | self.p.plot(self.C_x,self.C_y,"red",200,"*") 84 | 85 | def k_numberFunction(self): 86 | self.k = self.k_number.value() 87 | self.prepareData() 88 | 89 | def dist(self,a,b): 90 | return np.linalg.norm(a - b, axis = 1) 91 | 92 | def kMeansClustering(self,f1,f2,C_x,C_y,k): 93 | 94 | X = np.array(list(zip(f1,f2))) 95 | 96 | C = np.array(list(zip(C_x,C_y))) 97 | 98 | clusters = np.zeros(len(X)) 99 | 100 | for z in range(10): 101 | 102 | for i in range(len(X)): 103 | 104 | distances = self.dist(X[i], C) 105 | cluster = np.argmin(distances) 106 | clusters[i] = cluster 107 | 108 | for i in range(k): 109 | 110 | points = [ X[j] for j in range(len(X)) if clusters[j] == i] 111 | C[i] = np.mean(points, axis = 0) 112 | colors = ['black', 'red', 'cyan','magenta', 'blue', 'yellow',"darkgreen","silver","indigo","maroon"] 113 | 114 | for i in range(k): 115 | 116 | points = np.array([ X[j] for j in range(len(X)) if clusters[j] == i]) 117 | self.p.plot(points[:,0],points[:,1],colors[i],7) 118 | self.p.plot(C[:,0],C[:,1],"red",200,"*") 119 | 120 | result_txt = "Cluster"+str(i+1)+": "+str(len(points)) + " ("+colors[i]+")" 121 | self.result_list.addItems([result_txt]) 122 | 123 | self.save_txt = self.save_txt + result_txt + " -- " 124 | 125 | def clusterFunction(self): 126 | 127 | self.result_list.clear() 128 | self.p.clear() 129 | 130 | self.kMeansClustering(self.f1, self.f2, self.C_x, self.C_y, self.k) 131 | 132 | # radio button 133 | if self.text_save.isChecked(): 134 | path_name = "cluster_result.txt" 135 | text_file = open(path_name,"w") 136 | text_file.write(self.save_txt) 137 | text_file.close() 138 | if self.plot_save.isChecked(): 139 | self.p.fig.savefig("cluster_figure.jpg") 140 | 141 | if self.text_plot_save.isChecked(): 142 | path_name = "cluster_result.txt" 143 | text_file = open(path_name,"w") 144 | text_file.write(self.save_txt) 145 | text_file.close() 146 | 147 | self.p.fig.savefig("cluster_figure.jpg") 148 | 149 | def layouts(self): 150 | 151 | # layout 152 | self.mainlayout = QHBoxLayout() 153 | self.leftlayout = QFormLayout() 154 | self.middlelayout = QFormLayout() 155 | self.rightlayout = QFormLayout() 156 | 157 | # left 158 | self.leftlayoutGroupBox = QGroupBox("Plot") 159 | self.leftlayout.addRow(self.p) 160 | self.leftlayoutGroupBox.setLayout(self.leftlayout) 161 | 162 | # middle 163 | self.middlelayoutGroupBox = QGroupBox("Clustering") 164 | self.middlelayout.addRow(self.k_number_text) 165 | self.middlelayout.addRow(self.k_number) 166 | self.middlelayout.addRow(self.text_save) 167 | self.middlelayout.addRow(self.plot_save) 168 | self.middlelayout.addRow(self.text_plot_save) 169 | self.middlelayout.addRow(self.cluster) 170 | self.middlelayoutGroupBox.setLayout(self.middlelayout) 171 | 172 | # right 173 | self.rightlayoutGroupBox = QGroupBox("Result") 174 | self.rightlayout.addRow(self.result_list) 175 | self.rightlayoutGroupBox.setLayout(self.rightlayout) 176 | 177 | # main -> tab 178 | self.mainlayout.addWidget(self.leftlayoutGroupBox,50) 179 | self.mainlayout.addWidget(self.middlelayoutGroupBox,25) 180 | self.mainlayout.addWidget(self.rightlayoutGroupBox,25) 181 | self.tab1.setLayout(self.mainlayout) 182 | 183 | 184 | class PlotCanvas(FigureCanvas): 185 | 186 | def __init__(self, parent = None, width = 5, height = 5, dpi = 100): 187 | 188 | self.fig = Figure(figsize=(width,height), dpi = dpi) 189 | 190 | FigureCanvas.__init__(self,self.fig) 191 | 192 | 193 | def plot(self, x,y,c,s, m = "."): 194 | 195 | self.ax = self.figure.add_subplot(111) 196 | self.ax.scatter(x,y,c = c, s = s, marker = m) 197 | self.ax.set_title("K-Means Clustering") 198 | self.ax.set_xlabel("Income") 199 | self.ax.set_ylabel("Number of Transaction") 200 | self.draw() 201 | 202 | def clear(self): 203 | self.fig.clf() 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | w = Window() 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | -------------------------------------------------------------------------------- /1) Python Basics/pythonBasics.py: -------------------------------------------------------------------------------- 1 | print("Hello World1!") 2 | print("Hello World2!") 3 | print("Hello World3!") 4 | print("Hello World4!") 5 | 6 | # %% 7 | print("Hello World5!") 8 | print("Hello World6!") 9 | 10 | # %% 11 | 12 | # section3, this is my first coding experience 13 | # *************************************************************** !! 14 | print("Hello World7!") 15 | print("Hello World8!") 16 | 17 | # %% data types 18 | 19 | a = 4 # integer 20 | b = 8 21 | c = 3.14 # float 22 | type(a) 23 | 24 | d = a+b+c 25 | print(d) 26 | 27 | # string 28 | string1 = "Hello" 29 | 30 | string2 = "World" 31 | 32 | print(string1) 33 | print(string2) 34 | 35 | string3 = string1 + " " + string2 36 | print(string3) 37 | 38 | string3[4] # Hello World 39 | 40 | string3[0:5] 41 | 42 | bool1 = True 43 | bool2 = False 44 | 45 | #%% lists ve dictionaries 46 | 47 | list1= ["milk","orange", "fish", "water"] 48 | 49 | list1[0] 50 | list1[-1] 51 | list1[1:3] 52 | 53 | list1[1] = "coffee" 54 | 55 | len(list1) 56 | 57 | list1.append("orange") 58 | 59 | list1.remove("orange") 60 | 61 | 62 | list1.insert(1,"orange") 63 | 64 | 65 | list1.pop(0) 66 | 67 | list2 = [1,5,6,7,23,4,5,5,5] 68 | list2.sort() 69 | 70 | list2.count(5) 71 | 72 | # dictionary 73 | 74 | dict1 = {"country":"Spain", 75 | "city":"Madrid", 76 | "year":1964} 77 | 78 | x = dict1["city"] 79 | y = dict1.get("city") 80 | 81 | dict1["city"] = "Barcelona" 82 | 83 | len(dict1) 84 | 85 | dict1.values() 86 | dict1.keys() 87 | 88 | 89 | dict1["food"] = "pizza" 90 | 91 | dict1.pop("city") 92 | 93 | # %% if/else, for and while loops 94 | 95 | a = 23 96 | b = 55 97 | 98 | if ab: 101 | print("a is bigger than b") 102 | else: 103 | print("a == b ") 104 | 105 | 106 | c = 33 107 | 108 | if a 100 359 | data2 = data1[filter1] 360 | 361 | # numpy 362 | n = data1.as_matrix() 363 | 364 | n = data1.values 365 | 366 | # save pickle 367 | data.to_pickle("data_pickle.pkl") 368 | data_p = pd.read_pickle("data_pickle.pkl") 369 | 370 | # list comprehension 371 | data.population = [ i + 10 for i in data.population] 372 | 373 | # drop 374 | df = data.drop(["area"],axis = 1) 375 | 376 | # https://www.kaggle.com/kanncaa1/data-sciencetutorial-for-beginners 377 | 378 | 379 | # %% matplotlib 380 | 381 | import matplotlib.pyplot as plt 382 | import numpy as np 383 | 384 | x = np.linspace(0,10,100) 385 | y = np.linspace(-5,5,100) 386 | 387 | fig = plt.figure() 388 | ax = fig.add_subplot(111) 389 | ax.plot(x,y, color = "red", linewidth = 1) 390 | ax.scatter(x,y, color = "green", marker = "*") 391 | 392 | plt.xlabel("x") 393 | plt.ylabel("y") 394 | plt.title("x vs y") 395 | plt.savefig("x_y.png") 396 | plt.show() 397 | 398 | # seaborn 399 | 400 | import seaborn as sns 401 | a = np.random.randint(5,size = 1000) 402 | 403 | plt.figure() 404 | sns.countplot(a) 405 | 406 | # https://www.kaggle.com/kanncaa1/seaborn-tutorial-for-beginners 407 | 408 | # %% random 409 | import random 410 | 411 | random.seed(2) 412 | 413 | a = random.random() 414 | b = random.uniform(1,10) 415 | c = random.randint(1,10) 416 | d = random.randrange(0,101,2) 417 | 418 | # %% i/o 419 | 420 | save_txt = "hello world" 421 | text_file = open("save_string.txt","w") 422 | text_file.write(save_txt) 423 | text_file.close() 424 | 425 | # load 426 | load_file = open("save_string.txt","r") 427 | print(load_file.read()) 428 | 429 | # %% common mistakes 430 | """ 431 | 1) import 432 | 2) array creation 433 | 3) return 434 | 4) indentation 435 | 5) remember figure() 436 | """ 437 | 438 | #if True: 439 | # print("12") 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | -------------------------------------------------------------------------------- /4) Skin Cancer Classification Project with Tkinter/skin_cancer_classification_project_with_Tkinter.py: -------------------------------------------------------------------------------- 1 | # libraries 2 | import tkinter as tk 3 | from tkinter import ttk 4 | from tkinter import filedialog 5 | from tkinter import messagebox 6 | 7 | from PIL import ImageTk, Image 8 | import matplotlib.pyplot as plt 9 | import pandas as pd 10 | import numpy as np 11 | import seaborn as sns 12 | 13 | from keras.models import Sequential 14 | from keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense 15 | from keras.optimizers import Adam 16 | 17 | from keras.utils.np_utils import to_categorical 18 | from keras.models import load_model 19 | 20 | #%% 21 | skin_df = pd.read_csv("HAM10000_metadata.csv") 22 | 23 | skin_df.head() 24 | skin_df.info() 25 | sns.countplot(x = "dx", data = skin_df) 26 | 27 | # %% preprocess 28 | data_folder_name = "HAM10000_images_part_1/" 29 | ext = ".jpg" 30 | #"HAM10000_images_part_1\ISIC_0027419.jpg" 31 | #data_folder_name + image_id[i] + ext 32 | skin_df["path"] = [ data_folder_name + i + ext for i in skin_df["image_id"]] 33 | skin_df["image"] = skin_df["path"].map( lambda x: np.asarray(Image.open(x).resize((100,75)))) 34 | plt.imshow(skin_df["image"][0]) 35 | skin_df["dx_idx"] = pd.Categorical(skin_df["dx"]).codes 36 | skin_df.to_pickle("skin_df.pkl") 37 | 38 | # %% load pkl 39 | skin_df = pd.read_pickle("skin_df.pkl") 40 | 41 | # %% stardardization 42 | x_train = np.asarray(skin_df["image"].tolist()) 43 | x_train_mean = np.mean(x_train) 44 | x_train_std = np.std(x_train) 45 | x_train = (x_train - x_train_mean)/x_train_std 46 | 47 | # one hot encoding 48 | y_train = to_categorical(skin_df["dx_idx"], num_classes = 7) 49 | 50 | #%% CNN 51 | 52 | input_shape = (75,100,3) 53 | num_classes = 7 54 | 55 | model = Sequential() 56 | model.add(Conv2D(32, kernel_size = (3,3), activation = "relu", padding = "Same", input_shape = input_shape)) 57 | model.add(Conv2D(32, kernel_size = (3,3), activation = "relu", padding = "Same")) 58 | model.add(MaxPool2D(pool_size = (2,2))) 59 | model.add(Dropout(0.25)) 60 | 61 | model.add(Conv2D(64, kernel_size = (3,3), activation = "relu", padding = "Same")) 62 | model.add(Conv2D(64, kernel_size = (3,3), activation = "relu", padding = "Same")) 63 | model.add(MaxPool2D(pool_size = (2,2))) 64 | model.add(Dropout(0.5)) 65 | 66 | model.add(Flatten()) 67 | model.add(Dense(128,activation="relu")) 68 | model.add(Dropout(0.5)) 69 | model.add(Dense(num_classes,activation="softmax")) 70 | model.summary() 71 | 72 | optimizer = Adam(lr = 0.001) 73 | model.compile(optimizer = optimizer, loss = "categorical_crossentropy", metrics = ["accuracy"]) 74 | 75 | epochs = 5 76 | batch_size = 25 77 | 78 | history = model.fit(x = x_train, y = y_train, batch_size = batch_size, epochs = epochs, verbose = 1, shuffle = True) 79 | 80 | model.save("my_model1.h5") 81 | 82 | # %% load 83 | model1 = load_model("my_model1.h5") 84 | model2 = load_model("my_model2.h5") 85 | 86 | # %% prediction 87 | index = 5 88 | y_pred = model1.predict(x_train[index].reshape(1,75,100,3)) 89 | y_pred_class = np.argmax(y_pred, axis = 1) 90 | 91 | # %% Skin Cancer Classification GUI 92 | 93 | window = tk.Tk() 94 | window.geometry("1080x640") 95 | window.wm_title("Skin Cancer Classification") 96 | 97 | ## global variables 98 | img_name = "" 99 | count = 0 100 | img_jpg = "" 101 | 102 | ## frames 103 | frame_left = tk.Frame(window, width = 540, height = 640, bd = "2") 104 | frame_left.grid(row = 0, column = 0) 105 | 106 | frame_right = tk.Frame(window, width = 540, height = 640, bd = "2") 107 | frame_right.grid(row = 0, column = 1) 108 | 109 | frame1 = tk.LabelFrame(frame_left, text = "Image", width = 540, height = 500) 110 | frame1.grid(row = 0, column = 0) 111 | 112 | frame2 = tk.LabelFrame(frame_left, text = "Model and Save", width = 540, height = 140) 113 | frame2.grid(row = 1, column = 0) 114 | 115 | frame3 = tk.LabelFrame(frame_right, text = "Features", width = 270, height = 640) 116 | frame3.grid(row = 0, column = 0) 117 | 118 | frame4 = tk.LabelFrame(frame_right, text = "Result", width = 270, height = 640) 119 | frame4.grid(row = 0, column = 1, padx = 10) 120 | 121 | 122 | # frame1 123 | def imageResize(img): 124 | 125 | basewidth = 500 126 | wpercent = (basewidth/float(img.size[0])) # 1000 *1200 127 | hsize = int((float(img.size[1])*float(wpercent))) 128 | img = img.resize((basewidth,hsize),Image.ANTIALIAS) 129 | return img 130 | 131 | def openImage(): 132 | 133 | global img_name 134 | global count 135 | global img_jpg 136 | 137 | count += 1 138 | if count != 1: 139 | messagebox.showinfo(title = "Warning", message = "Only one image can be opened") 140 | else: 141 | img_name = filedialog.askopenfilename(initialdir = "D:\codes",title = "Select an image file") 142 | 143 | img_jpg = img_name.split("/")[-1].split(".")[0] 144 | # image label 145 | tk.Label(frame1, text =img_jpg, bd = 3 ).pack(pady = 10) 146 | 147 | # open and show image 148 | img = Image.open(img_name) 149 | img = imageResize(img) 150 | img = ImageTk.PhotoImage(img) 151 | panel = tk.Label(frame1, image = img) 152 | panel.image = img 153 | panel.pack(padx = 15, pady = 10) 154 | 155 | # image feature 156 | data = pd.read_csv("HAM10000_metadata.csv") 157 | cancer = data[data.image_id == img_jpg] 158 | 159 | for i in range(cancer.size): 160 | x = 0.4 161 | y = (i/10)/2 162 | tk.Label(frame3, font = ("Times",12), text = str(cancer.iloc[0,i])).place(relx = x, rely = y) 163 | 164 | menubar = tk.Menu(window) 165 | window.config(menu = menubar) 166 | file = tk.Menu(menubar) 167 | menubar.add_cascade(label = "File",menu = file) 168 | file.add_command(label = "Open", command = openImage) 169 | 170 | # frame3 171 | def classification(): 172 | 173 | if img_name != "" and models.get() != "": 174 | 175 | # model selection 176 | if models.get() == "Model1": 177 | classification_model = model1 178 | else: 179 | classification_model = model2 180 | 181 | z = skin_df[skin_df.image_id == img_jpg] 182 | z = z.image.values[0].reshape(1,75,100,3) 183 | 184 | # 185 | z = (z - x_train_mean)/x_train_std 186 | h = classification_model.predict(z)[0] 187 | h_index = np.argmax(h) 188 | predicted_cancer = list(skin_df.dx.unique())[h_index] 189 | 190 | for i in range(len(h)): 191 | x = 0.5 192 | y = (i/10)/2 193 | 194 | if i != h_index: 195 | tk.Label(frame4,text = str(h[i])).place(relx = x, rely = y) 196 | else: 197 | tk.Label(frame4,bg = "green",text = str(h[i])).place(relx = x, rely = y) 198 | 199 | if chvar.get() == 1: 200 | 201 | val = entry.get() 202 | entry.config(state = "disabled") 203 | path_name = val + ".txt" # result1.txt 204 | 205 | save_txt = img_name + "--" + str(predicted_cancer) 206 | 207 | text_file = open(path_name,"w") 208 | text_file.write(save_txt) 209 | text_file.close() 210 | else: 211 | print("Save is not selected") 212 | else: 213 | messagebox.showinfo(title = "Warning", message = "Choose image and Model First!") 214 | tk.Label(frame3, text = "Choose image and Model First!" ).place(relx = 0.1, rely = 0.6) 215 | 216 | columns = ["lesion_id","image_id","dx","dx_type","age","sex","localization"] 217 | for i in range(len(columns)): 218 | x = 0.1 219 | y = (i/10)/2 220 | tk.Label(frame3, font = ("Times",12), text = str(columns[i]) + ": ").place(relx = x, rely = y) 221 | 222 | classify_button = tk.Button(frame3, bg = "red", bd = 4, font = ("Times",13),activebackground = "orange",text = "Classify",command = classification) 223 | classify_button.place(relx = 0.1, rely = 0.5) 224 | # frame 4 225 | labels = skin_df.dx.unique() 226 | 227 | for i in range(len(columns)): 228 | x = 0.1 229 | y = (i/10)/2 230 | tk.Label(frame4, font = ("Times",12), text = str(labels[i]) + ": ").place(relx = x, rely = y) 231 | # frame 2 232 | # combo box 233 | model_selection_label = tk.Label(frame2, text = "Choose classification model: ") 234 | model_selection_label.grid(row = 0, column = 0, padx = 5) 235 | 236 | models = tk.StringVar() 237 | model_selection = ttk.Combobox(frame2,textvariable = models, values = ("Model1","Model2"), state = "readonly") 238 | model_selection.grid(row = 0, column = 1, padx = 5) 239 | 240 | # check box 241 | chvar = tk.IntVar() 242 | chvar.set(0) 243 | xbox = tk.Checkbutton(frame2, text = "Save Classification Result", variable = chvar) 244 | xbox.grid(row = 1, column =0 , pady = 5) 245 | 246 | # entry 247 | entry = tk.Entry(frame2, width = 23) 248 | entry.insert(string = "Saving name...",index = 0) 249 | entry.grid(row = 1, column =1 ) 250 | 251 | window.mainloop() 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | -------------------------------------------------------------------------------- /3) Forex Trading Project with Tkinter/trading_analysis_project_w_tkinter.py: -------------------------------------------------------------------------------- 1 | # libraries 2 | import tkinter as tk 3 | from tkinter import ttk 4 | from tkinter import messagebox 5 | 6 | from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 7 | import matplotlib.pyplot as plt 8 | 9 | import pandas as pd 10 | import numpy as np 11 | 12 | window = tk.Tk() 13 | window.geometry("1080x640") 14 | window.wm_title("Trading") 15 | 16 | pw = ttk.PanedWindow(window, orient = tk.HORIZONTAL) 17 | pw.pack(fill = tk.BOTH, expand = True) 18 | 19 | w2 = ttk.PanedWindow(pw, orient = tk.VERTICAL) 20 | 21 | frame1 = ttk.Frame(pw, width = 360, height = 640, relief = tk.SUNKEN) 22 | frame2 = ttk.Frame(pw, width = 720, height = 400, relief = tk.SUNKEN) 23 | frame3 = ttk.Frame(pw, width = 720, height = 240, relief = tk.SUNKEN) 24 | 25 | w2.add(frame2) 26 | w2.add(frame3) 27 | 28 | pw.add(w2) 29 | pw.add(frame1) 30 | 31 | # frame1: treeview, open trade button 32 | 33 | item = "" 34 | def callback(event): 35 | global item 36 | item = treeview.identify("item", event.x,event.y) 37 | # print("Clicked: ",item) 38 | 39 | # treeview 40 | treeview = ttk.Treeview(frame1) 41 | treeview.grid(row = 0, column = 1, padx = 25 , pady = 25) 42 | treeview.insert("", "0", "Major", text = "Major") 43 | treeview.insert("Major", "1", "EUR/USD", text ="EUR/USD") 44 | treeview.insert("", "2", "Minor", text = "Minor") 45 | treeview.insert("Minor", "3", "EUR/GBR", text ="EUR/GBR") 46 | treeview.bind("",callback) 47 | 48 | def readNews(item): 49 | 50 | if item == "EUR/USD": 51 | news = pd.read_csv("news_EURUSD.txt") 52 | elif item == "EUR/GBR": 53 | news = pd.read_csv("news_EURGBR.txt") 54 | textBox.insert(tk.INSERT, news) 55 | 56 | def openTrade(): 57 | global data, future, line, canvas, data_close_array, future_array, ax1,line2,canvas2,ax2,line3,canvas3,ax3,line4,canvas4,ax4 58 | print("openTrade") 59 | if item != "": 60 | print("Chosen item: ",item ) 61 | 62 | if item == "EUR/USD": 63 | # button setting 64 | open_button.config(state = "disabled") 65 | start_button.config(state = "normal") 66 | 67 | # read data 68 | data = pd.read_csv("eurusd.csv") 69 | 70 | # split data 71 | future = data[-1000:] 72 | data = data[:len(data)-1000] 73 | data_close_array = data.close1.values 74 | future_array = list(future.close1.values) 75 | 76 | # line 77 | fig1 = plt.Figure(figsize =(5,4), dpi = 100) 78 | ax1 = fig1.add_subplot(111) 79 | line, = ax1.plot(range(len(data)),data.close1,color = "blue") 80 | 81 | canvas = FigureCanvasTkAgg(fig1, master = tab1) 82 | canvas.draw() 83 | canvas.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = 1) 84 | 85 | # scatter 86 | fig2 = plt.Figure(figsize =(5,4), dpi = 100) 87 | ax2 = fig2.add_subplot(111) 88 | line2 = ax2.scatter(range(len(data)),data.close1,s = 1, alpha = 0.5, color = "blue") 89 | 90 | canvas2 = FigureCanvasTkAgg(fig2, master = tab2) 91 | canvas2.draw() 92 | canvas2.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = 1) 93 | 94 | # read news 95 | readNews(item) 96 | 97 | elif item == "EUR/GBR": 98 | 99 | # button setting 100 | open_button.config(state = "disabled") 101 | start_button.config(state = "normal") 102 | 103 | # read data 104 | data = pd.read_csv("eurgbr.csv") 105 | 106 | # split data 107 | future = data[-1000:] 108 | data = data[:len(data)-1000] 109 | data_close_array = data.close1.values 110 | future_array = list(future.close1.values) 111 | 112 | # line 113 | fig3 = plt.Figure(figsize =(5,4), dpi = 100) 114 | ax3 = fig3.add_subplot(111) 115 | line3, = ax3.plot(range(len(data)),data.close1,color = "blue") 116 | 117 | canvas3 = FigureCanvasTkAgg(fig3, master = tab1) 118 | canvas3.draw() 119 | canvas3.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = 1) 120 | 121 | # scatter 122 | fig4 = plt.Figure(figsize =(5,4), dpi = 100) 123 | ax4 = fig4.add_subplot(111) 124 | line4 = ax4.scatter(range(len(data)),data.close1,s = 1, alpha = 0.5, color = "blue") 125 | 126 | canvas4 = FigureCanvasTkAgg(fig4, master = tab2) 127 | canvas4.draw() 128 | canvas4.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = 1) 129 | 130 | # read news 131 | readNews(item) 132 | 133 | else: 134 | messagebox.showinfo(title = "Warning", message = "Double click to choose currency pair") 135 | else: 136 | messagebox.showinfo(title = "Warning", message = "Double click to choose currency pair") 137 | 138 | # button 139 | open_button = tk.Button(frame1, text = "Open Trading", command = openTrade) 140 | open_button.grid(row = 2, column = 1, padx = 5, pady = 5) 141 | 142 | # frame3: text editor (fundamental analysis), scroll bar 143 | 144 | textBox = tk.Text(frame3, width = 70, height = 10, wrap = "word") 145 | textBox.grid(row = 0, column = 0, padx =25, pady = 25) 146 | scroll = tk.Scrollbar(frame3, orient = tk.VERTICAL, command = textBox.yview) 147 | scroll.grid(row = 0, column = 1, sticky = tk.N + tk.S, pady=10) 148 | textBox.config(yscrollcommand = scroll.set) 149 | 150 | 151 | # frame2: tab view, radio button, button, result(labelframe), plot 152 | 153 | tabs = ttk.Notebook(frame2, width = 540, height = 300) 154 | tabs.place(x = 25, y= 25) 155 | 156 | tab1 = ttk.Frame(tabs, width = 50, height = 50) 157 | tab2 = ttk.Frame(tabs) 158 | 159 | tabs.add(tab1, text = "Line") 160 | tabs.add(tab2, text = "Scatter", compound = tk.LEFT) 161 | 162 | # radio button 163 | method = tk.StringVar() 164 | tk.Radiobutton(frame2, text = "m1: ", value = "m1", variable = method).place(x = 580, y= 100) 165 | tk.Radiobutton(frame2, text = "m2: ", value = "m2", variable = method).place(x = 580, y= 125) 166 | 167 | # label frame: result 168 | label_frame = tk.LabelFrame(frame2, text = "Result", width = 100, height = 150) 169 | label_frame.place(x = 580, y = 25) 170 | tk.Label(label_frame, text = "Buy: ", bd = 3).grid(row = 0, column = 0) 171 | tk.Label(label_frame, text = "Sell: ", bd = 3).grid(row = 1, column = 0) 172 | 173 | # buy sell labels 174 | buy_value = tk.Label(label_frame, text = "1", bd = 3) 175 | buy_value.grid(row = 0, column = 1) 176 | sell_value = tk.Label(label_frame, text = "0", bd = 3) 177 | sell_value.grid(row = 1, column = 1) 178 | 179 | def moving_average(a, n = 50): 180 | ret = np.cumsum(a, dtype= float) 181 | ret[n:] = ret[n:] - ret[:-n] 182 | return ret[n-1:]/n 183 | 184 | def update(): 185 | global data_close_array, ax1,ax2,ax3,ax4 186 | 187 | spread = 0.0002 188 | buy_value.config(text = str((data_close_array[-1]-spread).round(5))) 189 | sell_value.config(text = str((data_close_array[-1]+spread).round(5))) 190 | 191 | window.after(500, update) 192 | 193 | data_close_array = np.append(data_close_array,future_array.pop(0)) 194 | 195 | if method.get() == "m1": 196 | if item == "EUR/USD": 197 | # line 198 | ax1.set_xlim(0,len(data_close_array) + 10) 199 | line.set_ydata(data_close_array) 200 | line.set_xdata(range(len(data_close_array))) 201 | 202 | # scatter 203 | ax2.set_xlim(0,len(data_close_array) + 10) 204 | ax2.scatter(range(len(data_close_array)), data_close_array, s = 1, alpha = 0.5, color = "blue") 205 | 206 | # moving average 207 | n = 50 208 | mid_rolling = moving_average(data_close_array,n) 209 | ax1.plot(range(n-1,len(data_close_array)),mid_rolling,linestyle = "--", color = "red") 210 | ax2.plot(range(n-1,len(data_close_array)),mid_rolling,linestyle = "--", color = "red") 211 | 212 | canvas.draw() 213 | canvas2.draw() 214 | 215 | elif item == "EUR/GBR": 216 | # line 217 | ax3.set_xlim(0,len(data_close_array) + 10) 218 | line3.set_ydata(data_close_array) 219 | line3.set_xdata(range(len(data_close_array))) 220 | 221 | # scatter 222 | ax4.set_xlim(0,len(data_close_array) + 10) 223 | ax4.scatter(range(len(data_close_array)), data_close_array, s = 1, alpha = 0.5, color = "blue") 224 | 225 | # moving average 226 | n = 50 227 | mid_rolling = moving_average(data_close_array,n) 228 | ax3.plot(range(n-1,len(data_close_array)),mid_rolling,linestyle = "--", color = "red") 229 | ax4.plot(range(n-1,len(data_close_array)),mid_rolling,linestyle = "--", color = "red") 230 | 231 | canvas3.draw() 232 | canvas4.draw() 233 | 234 | elif method.get() == "m2": 235 | if item == "EUR/USD": 236 | # line 237 | ax1.set_xlim(0,len(data_close_array) + 10) 238 | line.set_ydata(data_close_array) 239 | line.set_xdata(range(len(data_close_array))) 240 | 241 | # scatter 242 | ax2.set_xlim(0,len(data_close_array) + 10) 243 | ax2.scatter(range(len(data_close_array)), data_close_array, s = 1, alpha = 0.5, color = "blue") 244 | 245 | # moving average 246 | n = 200 247 | long_rolling = moving_average(data_close_array,n) 248 | ax1.plot(range(n-1,len(data_close_array)),long_rolling,linestyle = "--", color = "green") 249 | ax2.plot(range(n-1,len(data_close_array)),long_rolling,linestyle = "--", color = "green") 250 | 251 | canvas.draw() 252 | canvas2.draw() 253 | 254 | elif item == "EUR/GBR": 255 | # line 256 | ax3.set_xlim(0,len(data_close_array) + 10) 257 | line3.set_ydata(data_close_array) 258 | line3.set_xdata(range(len(data_close_array))) 259 | 260 | # scatter 261 | ax4.set_xlim(0,len(data_close_array) + 10) 262 | ax4.scatter(range(len(data_close_array)), data_close_array, s = 1, alpha = 0.5, color = "blue") 263 | 264 | # moving average 265 | n = 200 266 | long_rolling = moving_average(data_close_array,n) 267 | ax3.plot(range(n-1,len(data_close_array)),long_rolling,linestyle = "--", color = "green") 268 | ax4.plot(range(n-1,len(data_close_array)),long_rolling,linestyle = "--", color = "green") 269 | 270 | canvas3.draw() 271 | canvas4.draw() 272 | else: 273 | if item == "EUR/USD": 274 | # line 275 | ax1.set_xlim(0,len(data_close_array) + 10) 276 | line.set_ydata(data_close_array) 277 | line.set_xdata(range(len(data_close_array))) 278 | 279 | # scatter 280 | ax2.set_xlim(0,len(data_close_array) + 10) 281 | ax2.scatter(range(len(data_close_array)), data_close_array, s = 1, alpha = 0.5, color = "blue") 282 | 283 | canvas.draw() 284 | canvas2.draw() 285 | elif item == "EUR/GBR": 286 | # line 287 | ax3.set_xlim(0,len(data_close_array) + 10) 288 | line3.set_ydata(data_close_array) 289 | line3.set_xdata(range(len(data_close_array))) 290 | 291 | # scatter 292 | ax4.set_xlim(0,len(data_close_array) + 10) 293 | ax4.scatter(range(len(data_close_array)), data_close_array, s = 1, alpha = 0.5, color = "blue") 294 | 295 | canvas3.draw() 296 | canvas4.draw() 297 | 298 | # button 299 | def startTrading(): 300 | window.after(0,update) 301 | print("startTrading") 302 | 303 | start_button = tk.Button(frame2, text = "Start Trading", command = startTrading) 304 | start_button.place(x=580, y = 150) 305 | start_button.config(state = "disabled") 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | window.mainloop() 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | -------------------------------------------------------------------------------- /7) Digit Classification Project with PyQt5/digit_classification_project_w_pyqt5.py: -------------------------------------------------------------------------------- 1 | # libraries 2 | from PyQt5.QtWidgets import * 3 | from PyQt5.QtGui import QIcon, QPixmap, QImage, QPainter, QPen # icon and load image 4 | from PyQt5.QtCore import Qt, QPoint 5 | 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | import matplotlib.image as mpimg # load image 9 | 10 | import cv2 # open-cv use for image resize 11 | 12 | ## Convolutional Neural Network 13 | import keras 14 | from keras.datasets import mnist 15 | from keras.models import Sequential, load_model 16 | from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D 17 | 18 | #%% preprocess 19 | 20 | (x_train, y_train),(x_test, y_test) = mnist.load_data() 21 | 22 | plt.figure() 23 | i = 55 24 | plt.imshow(x_train[i], cmap = "gray") 25 | print("Label: ",y_train[i]) 26 | plt.axis("off") 27 | plt.grid(False) 28 | 29 | img_rows = 28 30 | img_cols = 28 31 | x_train = x_train.reshape( x_train.shape[0],img_rows,img_cols,1) 32 | x_test = x_test.reshape( x_test.shape[0],img_rows,img_cols,1) 33 | input_shape = (img_rows,img_cols,1) 34 | 35 | # normalization 36 | 37 | x_train = x_train.astype("float32") 38 | x_test = x_test.astype("float32") 39 | 40 | x_train /= 255 # x_train = x_train/255 41 | x_test /= 255 42 | 43 | num_classes = 10 44 | y_train = keras.utils.to_categorical(y_train,num_classes) 45 | y_test = keras.utils.to_categorical(y_test,num_classes) 46 | 47 | # %% CNN 48 | 49 | model_list = [] 50 | score_list = [] 51 | 52 | batch_size = 256 53 | epochs = 5 54 | 55 | filter_numbers = np.array([[16,32,64],[8,16,32]]) 56 | 57 | for i in range(2): 58 | 59 | print(filter_numbers[i]) 60 | model = Sequential() 61 | model.add(Conv2D(filter_numbers[i,0], kernel_size = (3,3),activation = "relu",input_shape = input_shape)) 62 | model.add(Conv2D(filter_numbers[i,1], kernel_size = (3,3),activation = "relu")) 63 | model.add(MaxPooling2D(pool_size = (2,2))) 64 | model.add(Dropout(0.25)) 65 | 66 | model.add(Flatten()) 67 | model.add(Dense(filter_numbers[i,2],activation = "relu")) 68 | model.add(Dropout(0.5)) 69 | model.add(Dense(num_classes,activation = "softmax")) 70 | 71 | model.compile(loss = keras.losses.categorical_crossentropy, optimizer = keras.optimizers.Adadelta(), 72 | metrics = ["accuracy"]) 73 | 74 | history = model.fit(x_train, y_train, batch_size = batch_size, epochs = epochs, verbose = 1,validation_data = (x_test,y_test)) 75 | 76 | score = model.evaluate(x_test, y_test, verbose = 0) 77 | print("Model {} Test Loss: {}".format(i+1, score[0])) 78 | print("Model {} Test Accuracy: {}".format(i+1, score[1])) 79 | model_list.append(model) 80 | score_list.append(score) 81 | 82 | model.save("model"+str(i+1)+".h5") 83 | 84 | # %% 85 | model1 = load_model("model1.h5") 86 | model2 = load_model("model2.h5") 87 | #%% Canvas 88 | class Canvas(QMainWindow): 89 | 90 | def __init__(self): 91 | 92 | super().__init__() 93 | 94 | self.width = 400 95 | self.height = 400 96 | self.setWindowTitle("Draw Digit App") 97 | self.setGeometry(50,100,self.width, self.height) 98 | 99 | self.image = QImage(self.size(),QImage.Format_RGB32) 100 | self.image.fill(Qt.black) 101 | 102 | self.lastPoint = QPoint() 103 | self.drawing = False 104 | 105 | # image_array 106 | self.im_np = np.zeros([self.width, self.height]) 107 | 108 | button1 = QPushButton("Ok!", self) 109 | button1.move(2,2) 110 | button1.clicked.connect(self.enterFunction) 111 | 112 | self.show() 113 | 114 | def paintEvent(self,event): 115 | canvasPainter = QPainter(self) 116 | canvasPainter.drawImage(self.rect(),self.image, self.image.rect()) 117 | 118 | def enterFunction(self): 119 | ptr = self.image.constBits() 120 | ptr.setsize(self.image.byteCount()) 121 | 122 | self.im_np = np.array(ptr).reshape(self.width, self.height,4) 123 | self.im_np = self.im_np[:,:,0] 124 | self.im_np = self.im_np/255.0 125 | 126 | if np.sum(self.im_np) ==0: 127 | print("Please write a digit") 128 | else: 129 | plt.figure(figsize = (1,1),dpi = 200) 130 | plt.imshow(self.im_np, cmap = "gray") 131 | plt.axis("off") 132 | plt.grid(False) 133 | plt.savefig("input_img.png") 134 | 135 | self.close() 136 | 137 | def mousePressEvent(self,event): 138 | 139 | if event.button() == Qt.LeftButton: 140 | self.lastPoint = event.pos() 141 | self.drawing = True 142 | print(self.lastPoint) 143 | 144 | def mouseMoveEvent(self,event): 145 | if (event.buttons() == Qt.LeftButton) & self.drawing: 146 | painter = QPainter(self.image) 147 | painter.setPen(QPen(Qt.white,20,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin)) 148 | painter.drawLine(self.lastPoint, event.pos()) 149 | self.lastPoint = event.pos() 150 | self.update() 151 | 152 | def mouseReleaseEvent(self,event): 153 | if event.button == Qt.LeftButton: 154 | self.drawing = False 155 | 156 | 157 | 158 | #c = Canvas() 159 | 160 | 161 | #%% GUI 162 | 163 | class Window(QMainWindow): 164 | 165 | def __init__(self): 166 | super().__init__() 167 | 168 | # main window 169 | self.width = 1080 170 | self.height = 640 171 | 172 | self.setWindowTitle("Digit Classification") 173 | self.setGeometry(50,100,self.width, self.height) 174 | self.setWindowIcon(QIcon("icon1.png")) 175 | 176 | self.create_canvas = Canvas() 177 | 178 | self.tabWidget() 179 | self.widgets() 180 | self.layouts() 181 | self.show() 182 | 183 | def tabWidget(self): 184 | self.tabs = QTabWidget() 185 | self.setCentralWidget(self.tabs) 186 | 187 | self.tab1 = QWidget() 188 | self.tab2 = QWidget() 189 | 190 | self.tabs.addTab(self.tab1, "Classification") 191 | self.tabs.addTab(self.tab2, "Parameters") 192 | 193 | def widgets(self): 194 | 195 | # tab1 left 196 | self.drawCanvas = QPushButton("Draw Canvas") 197 | self.drawCanvas.clicked.connect(self.drawCanvasFunction) 198 | 199 | self.openCanvas = QPushButton("Open Canvas") 200 | self.openCanvas.clicked.connect(self.openCanvasFunction) 201 | 202 | self.inputImage = QLabel(self) 203 | self.inputImage.setPixmap(QPixmap("input.png")) 204 | 205 | self.searchText = QLabel("Real number: ") 206 | 207 | self.searchEntry = QLineEdit() 208 | self.searchEntry.setPlaceholderText("Which number do you write?") 209 | 210 | # tab1 left middle 211 | self.methodSelection = QComboBox(self) 212 | self.methodSelection.addItems(["model1","model2"]) 213 | 214 | self.noiseText = QLabel("Add Noise: % " + "0") 215 | self.noiseSlider = QSlider(Qt.Horizontal) 216 | self.noiseSlider.setMinimum(0) 217 | self.noiseSlider.setMaximum(100) 218 | self.noiseSlider.setTickPosition(QSlider.TicksBelow) 219 | self.noiseSlider.setTickInterval(1) 220 | self.noiseSlider.valueChanged.connect(self.noiseSliderFunction) 221 | 222 | self.remember = QCheckBox("Save Result",self) 223 | 224 | self.predict = QPushButton("Predict") 225 | self.predict.clicked.connect(self.predictionFunction) 226 | 227 | # tab1 right middle 228 | self.outputImage = QLabel(self) 229 | self.outputImage.setPixmap(QPixmap("icon1.png")) 230 | 231 | self.outputLabel = QLabel("",self) 232 | self.outputLabel.setAlignment(Qt.AlignCenter) 233 | 234 | # tab1 right 235 | self.resultTable = QTableWidget() 236 | self.resultTable.setColumnCount(2) 237 | self.resultTable.setRowCount(10) 238 | self.resultTable.setHorizontalHeaderItem(0, QTableWidgetItem("Label(Class)")) 239 | self.resultTable.setHorizontalHeaderItem(1, QTableWidgetItem("Probability")) 240 | 241 | # tab2 method1 242 | self.parameter_list1 = QListWidget(self) 243 | self.parameter_list1.addItems(["batch_size = 256","epochs = 5","img_rows = 28", 244 | "img_cols = 28","Filter # = [16,32,64]","Activation Function = Relu", 245 | "loss = categorical cross entropy", 246 | "optimizer = Adadelta","metrics = accuracy"]) 247 | 248 | # tab2 method2 249 | self.parameter_list2 = QListWidget(self) 250 | self.parameter_list2.addItems(["batch_size = 256","epochs = 5","img_rows = 28", 251 | "img_cols = 28","Filter # = [8,16,32]","Activation Function = Relu", 252 | "loss = categorical cross entropy", 253 | "optimizer = Adadelta","metrics = accuracy"]) 254 | 255 | 256 | def predictionFunction(self): 257 | save_string = "" 258 | 259 | real_entry = self.searchEntry.text() 260 | save_string = save_string + " real entry: "+str(real_entry) + ", " 261 | 262 | # CNN model selection 263 | model_name = self.methodSelection.currentText() 264 | 265 | if model_name == "model1": 266 | model = load_model("model1.h5") 267 | elif model_name == "model2": 268 | model = load_model("model2.h5") 269 | else: 270 | print("Error") 271 | 272 | save_string = save_string + "model name: "+str(model_name) + ", " 273 | 274 | # slider noise 275 | 276 | noise_val = self.noiseSlider.value() 277 | 278 | if noise_val != 0: 279 | noise_array = np.random.randint(0,noise_val,(28,28))/100 280 | else: 281 | noise_array = np.zeros([28,28]) 282 | 283 | save_string = save_string + "noise value: " + str(noise_val) + ", " 284 | print(save_string) 285 | 286 | # load image as numpy 287 | img_array = mpimg.imread("input_img.png")[26:175,26:175,0] 288 | 289 | resized_img_array = cv2.resize(img_array, dsize=(28,28),interpolation = cv2.INTER_CUBIC) 290 | 291 | # add noise 292 | resized_img_array = resized_img_array + noise_array 293 | 294 | # vis noise image 295 | plt.imshow(resized_img_array, cmap = "gray") 296 | plt.title("image after adding noise and resize") 297 | 298 | # predict 299 | result = model.predict(resized_img_array.reshape(1,28,28,1)) 300 | QMessageBox.information(self,"Information","Classification is completed.") 301 | predicted_class = np.argmax(result) 302 | print("Prediction: ",predicted_class) 303 | 304 | save_string = save_string + "Predicted class: "+str(predicted_class) 305 | 306 | # save result 307 | if self.remember.isChecked(): 308 | text_file = open("Output.txt","w") 309 | text_file.write(save_string) 310 | text_file.close() 311 | else: 312 | print("not save") 313 | 314 | self.outputImage.setPixmap(QPixmap("images\\"+str(predicted_class)+".png")) 315 | self.outputLabel.setText("Real: "+str(real_entry)+ " and Predicted: "+str(predicted_class)) 316 | 317 | 318 | # set result 319 | for row in range(10): 320 | self.resultTable.setItem(row,0,QTableWidgetItem(str(row))) 321 | self.resultTable.setItem(row,1,QTableWidgetItem(str(np.round(result[0][row],5)))) 322 | 323 | def drawCanvasFunction(self): 324 | self.create_canvas.show() 325 | 326 | def openCanvasFunction(self): 327 | self.inputImage.setPixmap(QPixmap("input_img.png")) 328 | 329 | def noiseSliderFunction(self): 330 | val = self.noiseSlider.value() 331 | self.noiseText.setText("Add Noise: % " + str(val)) 332 | 333 | def layouts(self): 334 | 335 | # tab1 layout 336 | self.mainLayout = QHBoxLayout() 337 | self.leftLayout = QFormLayout() 338 | self.leftMiddleLayout = QFormLayout() 339 | self.rightMiddleLayout = QFormLayout() 340 | self.rightLayout = QFormLayout() 341 | 342 | # left 343 | self.leftLayoutGroupBox = QGroupBox("Input Image") 344 | self.leftLayout.addRow(self.drawCanvas) 345 | self.leftLayout.addRow(self.openCanvas) 346 | self.leftLayout.addRow(self.inputImage) 347 | self.leftLayout.addRow(self.searchText) 348 | self.leftLayout.addRow(self.searchEntry) 349 | self.leftLayoutGroupBox.setLayout(self.leftLayout) 350 | 351 | # left middle 352 | self.leftMiddleLayoutGroupBox = QGroupBox("Settings") 353 | self.leftMiddleLayout.addRow(self.methodSelection) 354 | self.leftMiddleLayout.addRow(self.noiseText) 355 | self.leftMiddleLayout.addRow(self.noiseSlider) 356 | self.leftMiddleLayout.addRow(self.remember) 357 | self.leftMiddleLayout.addRow(self.predict) 358 | self.leftMiddleLayoutGroupBox.setLayout(self.leftMiddleLayout) 359 | 360 | # right middle 361 | self.rightMiddleLayoutGroupBox = QGroupBox("Output") 362 | self.rightMiddleLayout.addRow(self.outputImage) 363 | self.rightMiddleLayout.addRow(self.outputLabel) 364 | self.rightMiddleLayoutGroupBox.setLayout(self.rightMiddleLayout) 365 | 366 | # right 367 | self.rightLayoutGroupBox = QGroupBox("Result") 368 | self.rightLayout.addRow(self.resultTable) 369 | self.rightLayoutGroupBox.setLayout(self.rightLayout) 370 | 371 | # tab1 main layout 372 | self.mainLayout.addWidget(self.leftLayoutGroupBox,25) 373 | self.mainLayout.addWidget(self.leftMiddleLayoutGroupBox,25) 374 | self.mainLayout.addWidget(self.rightMiddleLayoutGroupBox,25) 375 | self.mainLayout.addWidget(self.rightLayoutGroupBox,25) 376 | self.tab1.setLayout(self.mainLayout) 377 | 378 | # tab2 layout 379 | self.tab2Layout = QHBoxLayout() 380 | self.tab2Method1Layout = QFormLayout() 381 | self.tab2Method2Layout = QFormLayout() 382 | 383 | # tab2 Method1 Layout 384 | self.tab2Method1LayoutGroupBox = QGroupBox("Method1") 385 | self.tab2Method1Layout.addRow(self.parameter_list1) 386 | self.tab2Method1LayoutGroupBox.setLayout(self.tab2Method1Layout) 387 | 388 | # tab2 Method2 Layout 389 | self.tab2Method2LayoutGroupBox = QGroupBox("Method2") 390 | self.tab2Method2Layout.addRow(self.parameter_list2) 391 | self.tab2Method2LayoutGroupBox.setLayout(self.tab2Method2Layout) 392 | 393 | # tab2 main layout 394 | self.tab2Layout.addWidget(self.tab2Method1LayoutGroupBox, 50) 395 | self.tab2Layout.addWidget(self.tab2Method2LayoutGroupBox, 50) 396 | self.tab2.setLayout(self.tab2Layout) 397 | 398 | 399 | w = Window() 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | --------------------------------------------------------------------------------