# -*- 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. appium 元素文件 -查找元素 封装思路和方法

    方法1. try: target="//android.widget.TextView[@text='立即體驗']" element = WebDriverWait(dr,5,0. ...

  2. Ajax的课外了解

    Ajax传入的数据的话,只能是字符串或数字,字段,其他形式的传参都不可以: Ajax只是跟后台交互也有同源策略的限制: 不是当前服务器叫跨域: Ajax也有同源策略的限制想做跨域处理,只能通过scri ...

  3. Jmeter(一)简介以及环境搭建

    刚刚在打扫卫生的时候,就一直在思考近一年以来所学知识及体系.知识太过于碎片化,整理的东西全写在笔记本上,日常工作不可能全部用到,所以复习很重要.因此开始准备将一些知识写在随笔里边,用于知识体系的重建, ...

  4. [UE4]删除动画:Remove from frame 5 to frame 18

    从当前帧开始删除到结尾的动画帧

  5. SQL-sqlHelper001

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. Windows安装启动MySQL

    Win安装MySQL数据库将下载下来的mysql解压到指定目录下,cmd切换到bin目录下>mysqld -install 安装服务>mysqld -remove 卸载服务>net ...

  7. Can't use Subversion command line client***Probably the path to Subversion executable is wrong. Fix

    新同事要入职,赶紧收拾一下电脑:再使用SVNupdate代码时,显示Nothing to show.androidstudio不能使用svn了: 是这样婶滴,报错信息 什么情况: 好了:问题叙述完毕开 ...

  8. 3-安装hive

    1.解压.修改权限 tar -zvxf apache-hive-1.2.1-bin.tar.gz -C /opt/app/ sudo chown -R hadoop:hadoop /opt/app/a ...

  9. Java7 新特性: try-with-resources

    Try-with-resources是java7中一个新的异常处理机制,它能够很容易地关闭在try-catch语句块中使用的资源. 利用Try-Catch-Finally管理资源(旧的代码风格)在ja ...

  10. 得到某个method所在类

    System.out.println(this.getClass().getMethod("testPrivate"));//public void mypss.MyTest.te ...