SVM标记学习

# -*- coding: utf-8 -*-
"""
Created on Mon Oct  1 09:32:37 2018

@author: 
"""
import numpy as np
from tkinter import *
#import tkinter
from PIL import Image, ImageTk
from scipy.misc import imread
import matplotlib.pyplot as plt
from sklearn.svm import SVC

class SVM_Classifier(Frame):
    def __init__(self, master=None):
        self.root = Tk()#tkinter.TK()
        Frame.__init__(self, master)
        Pack.config(self)
        self.menus()
        self.createWidgets()
        self.after(10, self.callback)
        self.state=0
        global X_list
        X_list=[]
        global y_list
        y_list=[]
    def menus(self):
        allmenu = Menu(self.root)#tkinter.Menu
        # 添加子菜单
        menu1 = Menu(allmenu, tearoff=0)
        menu2=Menu(allmenu, tearoff=0)
        # 添加选项卡
        menu1.add_command(label='前景', command=self.target)
        menu1.add_command(label='背景', command=self.background)
        allmenu.add_cascade(label='样本标注', menu=menu1) 
        menu2.add_command(label='SVM学习并显示结果', command=self.processing)
        allmenu.add_cascade(label='分析处理', menu=menu2)
        self.root.config(menu=allmenu)
    def target(self):
        self.state=1
    def background(self):
        self.state=2
    def processing(self):
        self.state=0
        X=np.array(X_list)
        y=np.array(y_list)
        print(X)
        print(y)
        #将X,Y写入txt文件
#        np_X=[]
#        np_y=[]
#        np_X.append(X)
#        np_y.append(y)
        svm_x='svm_x.txt'
        svm_y='svm_y.txt'
        X1_string = '\n'.join(str(x) for x in X)
        with open(svm_x,'w') as svm_file:
            svm_file.write(X1_string)
        y1_string = '\n'.join(str(x) for x in y)
        with open(svm_y,'w') as svm_file:
            svm_file.write(y1_string)
        
        #支持向量机学习
        clf=SVC(kernel="linear", C=0.025)#SVC(gamma=2, C=1)
        clf.fit(X, y)#SVM学习
        score = clf.score(X,y)
        print('score=',score)
        image = imread("WIN_20190110_16_56_25_Pro.jpg")#fruits.png
        XX=[]
        for i in range(image.shape[0]):
            for j in range(image.shape[1]):
                XX.append([image[i,j,0],image[i,j,1],image[i,j,2]])
        Z=clf.decision_function(XX)
        ZZ=np.array(Z)
        ZZ=ZZ.reshape(image.shape[0],image.shape[1])
        for i in range(image.shape[0]):
            for j in range(image.shape[1]):
                if ZZ[i,j]<0:
                    image[i,j,0]=0
                    image[i,j,1]=0
                    image[i,j,2]=0
#        for i in range(image.shape[0]):
#            for j in range(image.shape[1]):
#                Z = clf.decision_function([[image[i,j,0],image[i,j,1],image[i,j,2]]])
#                if Z[0]<0:
#                    image[i,j,0]=0
#                    image[i,j,1]=0
#                    image[i,j,2]=0
        plt.imshow(image)
        plt.axis('off')
        plt.title('SVM')
        plt.show()

    def createWidgets(self):
        ## The playing field
        self.draw = Canvas(self, width=640, height=480)
        self.im=Image.open('WIN_20190110_16_56_25_Pro.jpg')#fruits.png
        self.tkimg=ImageTk.PhotoImage(self.im)
        self.myImg=self.draw.create_image(10,10,anchor=NW,image=self.tkimg)
        
        self.draw.pack(side=LEFT)
    def mouse_pick(self,event):
        rgb=self.im.getpixel((event.x-10,event.y-10))
        print("clicked at:x=", event.x-10,'y=',event.y-10,' r=',rgb[0],'g=',rgb[1],'b=',rgb[2])
        X_list.append([np.uint8(rgb[0]),np.uint8(rgb[1]),np.uint8(rgb[2])])
        if self.state==1:
            self.pick_points = self.draw.create_oval((event.x - 2),(event.y - 2),(event.x + 2),(event.y + 2),fill="red")
            y_list.append(1)#添加入前景标签
        if self.state==2:
            self.pick_points = self.draw.create_oval((event.x - 2),(event.y - 2),(event.x + 2),(event.y + 2),fill="green")
            y=y_list.append(-1)#添加入背景标签
    def callback(self, *args):
        self.draw.tag_bind(self.myImg, "<Button-1>", self.mouse_pick)
       
game = SVM_Classifier()
game.mainloop()

猜你喜欢

转载自www.cnblogs.com/Manuel/p/10430049.html