# -*- 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()

SVM标记学习的更多相关文章

  1. 多标记学习--Learning from Multi-Label Data

    传统分类问题,即多类分类问题是,假设每个示例仅具有单个标记,且所有样本的标签类别数|L|大于1,然而,在很多现实世界的应用中,往往存在单个示例同时具有多重标记的情况. 而在多分类问题中,每个样本所含标 ...

  2. SVM个人学习总结

    SVM个人学习总结 如题,本文是对SVM学习总结,主要目的是梳理SVM推导过程,以及记录一些个人理解. 1.主要参考资料 [1]Corres C. Support vector networks[J] ...

  3. 菜鸟之路——机器学习之SVM分类器学习理解以及Python实现

    SVM分类器里面的东西好多呀,碾压前两个.怪不得称之为深度学习出现之前表现最好的算法. 今天学到的也应该只是冰山一角,懂了SVM的一些原理.还得继续深入学习理解呢. 一些关键词: 超平面(hyper ...

  4. include动作标记和include指令标记学习笔记

    我的jsp学习参考书是耿祥义,张跃平编著的jsp大学使用教程这本书,我也向大家推荐这本书,我觉得这本书适合我的学习方式,知识的讲解透彻易懂. include指令标记                   ...

  5. Ibatis.Net <![CDATA[ ]]>标记学习(九)

    当Sql语句中包含特殊字符时,例如: <select id="SelectOnePerson" resultMap="PersonModel"> s ...

  6. [matlab]机器学习及SVM工具箱学习笔记

    机器学习与神经网络的关系: 机器学习是目的,神经网络是算法.神经网络是实现机器学习的一种方法,平行于SVM. 常用的两种工具:svm tool.libsvm SVM分为SVC和SVR,svc是专门用来 ...

  7. 机器学习之支持向量机(SVM)学习笔记

    支持向量机是一种二分类算法,算法目的是找到一个最佳超平面将属于不同种类的数据分隔开.当有新数据输入时,判断该数据在超平面的哪一侧,继而决定其类别. 具体实现思路: 训练过程即找到最佳的分隔超平面的过程 ...

  8. 学习笔记TF045:人工智能、深度学习、TensorFlow、比赛、公司

    人工智能,用计算机实现人类智能.机器通过大量训练数据训练,程序不断自我学习.修正训练模型.模型本质,一堆参数,描述业务特点.机器学习和深度学习(结合深度神经网络). 传统计算机器下棋,贪婪算法,Alp ...

  9. 1. SVM简介

    从这一部分开始,将陆续介绍SVM的相关知识,主要是整理以前学习的一些笔记内容,梳理思路,形成一套SVM的学习体系. 支持向量机(Support Vector Machine)是Cortes和Vapni ...

随机推荐

  1. sass之为什么要使用预处理器

    使用预处理器主要目的就是编写出可读性更好.更易于维护的css. 以sass为例,sass中提供了@import可以在sass文件中导入其他sass文件,或在选择器中按需导入所需要的某个属性样式: @i ...

  2. PAT 甲级 1011 World Cup Betting (20)(20 分)

    1011 World Cup Betting (20)(20 分)提问 With the 2010 FIFA World Cup running, football fans the world ov ...

  3. springMVC的执行流程和完整代码

    一.什么是 Spring MVC Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 里面,是一个强大灵活的 Web 框架.Spring ...

  4. zabbix_agentd.conf配置文件详解

    Alias key的别名,例如 Alias=ttlsa.userid:vfs.file.regexp[/etc/passwd,^ttlsa:.:([0-9]+),,,,\1], 或者ttlsa的用户I ...

  5. MySQL 索引建立原则及注意事项

    一.索引建立的几大原则: 1) 最左前缀匹配原则,非常重要的原则,mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹配,比如a = 1 and b = 2 ...

  6. C# 公共类

    https://github.com/Jimmey-Jiang/Common.Utility/tree/master/Utility%E5%9F%BA%E7%A1%80%E7%B1%BB%E5%A4% ...

  7. Windows Server 2012 R2 无法启用Microsoft .NET Framework 3.5 功能

    1 在新windows 2012 R2 上安装SQL 2014 ,提示需要安装 .NET Framework 3.5 2 在添加角色和功能--功能--.NET Framework 3.5,然后失败 3 ...

  8. Http跨域

    一.传统 ajax跨域访问是一个老问题了,解决方法很多,比较常用的是JSONP方法,JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全. 即使使用jQuery的json ...

  9. SqlServer存储过程输出参数

    if exists(select 1 from sysobjects where name='P_PreOrderInfo') drop Procedure P_PreOrderInfo go Cre ...

  10. 17 RAID与mdadm管理命令

    在"14 磁盘及文件系统管理详解"中,我们详细介绍了磁盘的工作原理,但是,有一点我们一定要明白,作为现在存储数据的主要设备,机械磁盘早就是上个世纪的产品,而它的读写速度与内存.CP ...