# -*- coding: utf-8 -*- """ Created on Mon Oct 1 09:32:37 2018 @author: Manuel """ 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 from time import gmtime, strftime import cv2 #逐帧获取图片 videoFile = 'py5.mp4' cap = cv2.VideoCapture(videoFile) numF=cap.get(cv2.CAP_PROP_FRAME_COUNT) fps=cap.get(cv2.CAP_PROP_FPS) #cap.set(cv2.CAP_PROP_FRAME_WIDTH,640) #cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480) 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) while(True): ret, frame = cap.read() if ret ==True: image = frame #img=rotate(frame,-90) #img=np.rot90(frame) #img=np.rot90(img) #img=np.rot90(img) cv2.imshow('my', image) f = strftime("%Y%m%d%H%M%S.jpg", gmtime()) cv2.imwrite(f, image) # image = imread("test.jpg")#fruits.png image=imread(f) 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 image= image[...,::-1] _,image=cv2.threshold(image,10,255,cv2.THRESH_BINARY) # image= cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2) # _,image=cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # cv2.imshow("image CR", cr1) cv2.imshow("Skin", image) # img0 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#将图片转换为灰度图片 f = strftime("%Y%m%d%H%M%S_.jpg", gmtime()) cv2.imwrite('2/'+ f, image) #if img.size == 0: # break if cv2.waitKey(200) & 0xFF == ord('q'): break cap.release cv2.destroyAllWindows() # 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.show() def createWidgets(self): ## The playing field self.draw = Canvas(self, width=640, height=480) self.im=Image.open('20190104030935.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()