Skip to content

Commit

Permalink
Update to 1.5.0
Browse files Browse the repository at this point in the history
1. Brand new auto mode based on KNN similarity
2. Size adjustments on some elements
  • Loading branch information
LevyHsu committed Apr 1, 2020
1 parent 717dd38 commit 8160356
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 76 deletions.
206 changes: 133 additions & 73 deletions Momus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,131 +3,191 @@

import os
import PySimpleGUI as sg
import numpy as np

import cv2
import random
import image_processing
from matplotlib import pyplot as plt
from PIL import Image

def img_processing_SIFT(original_img_path,counter,pixel_number,Salt_and_pepper_Noise_level,Random_Scalar_level,Random_Crop_Pixel):
def img_processing_SIFT(mode,auto_target_similarity,original_img_path,counter,pixel_number,Salt_and_pepper_Noise_level,Random_Scalar_level,Random_Crop_Pixel):
user_image = image_processing.usr_img(original_img_path)

shape = user_image.show_demo_1()
window_width = shape[1]
window_height = shape[0]

original_matches = user_image.show_demo_3()
#print("original_matches: "+ str(original_matches))
match_proportion = 100

sg.theme('DarkAmber')
layout = [
[sg.Multiline(default_text='开始图像处理\n', size=(window_width, 2),key = '-TEXT-',do_not_clear=True)],
[sg.Multiline(default_text='开始图像处理\n', size=(window_width, 2),key = '-TEXT-',do_not_clear=False)],
[sg.ProgressBar(100, orientation='h', size=(window_width, 20), key='progressbar')],
[sg.Image('demo.png',key = '-IMAGE-')],
]
window = sg.Window('Momus 图像反识别工具',layout, size=(window_width + 2, window_height + 30))
progress_bar = window['progressbar']
counter_current = 0

#Manual
if (mode == 0):
while counter_current < counter:
event, values = window.Read(timeout=1)
if event is None:
break

if(pixel_number>0):
user_image.keypoint_obscure(pixel_number)
if(Salt_and_pepper_Noise_level>0):
user_image.keypoint_white_black_salt(Salt_and_pepper_Noise_level)
if(Random_Scalar_level>0):
user_image.Random_Scalar_Draw(Random_Scalar_level,counter)

while counter_current < counter:
event, values = window.Read(timeout=1)
if event is None:
break
if(pixel_number>0):
user_image.keypoint_obscure(pixel_number)
if(Salt_and_pepper_Noise_level>0):
user_image.keypoint_white_black_salt(Salt_and_pepper_Noise_level)
if(Random_Scalar_level>0):
user_image.Random_Scalar_Draw(Random_Scalar_level,counter)
progress_bar.UpdateBar(counter_current/counter*100)
window.Element('-TEXT-').Update(value=("----------------------------\n第"+str(counter_current)+"次迭代\n"), append=False)
progress_bar.UpdateBar(counter_current/counter*100)
window.Element('-TEXT-').Update(value=("第"+str(counter_current)+"次迭代\n 匹配度: " + "%5.3f" % (match_proportion) + "%\n"), append=True)

if(counter_current % 3 == 0):
user_image.show_demo_1()
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="原图对比\n", append=True)

if(counter_current % 3 == 1):
user_image.show_demo_2()
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="更新特征值\n", append=True)
if(counter_current % 3 == 0):
user_image.show_demo_1()
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="原图对比\n", append=True)

if(counter_current % 3 == 1):
user_image.show_demo_2()
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="更新特征值\n", append=True)

if(counter_current % 3 == 2):
user_image.show_demo_3()
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="KNN匹配\n", append=True)
if(counter_current % 3 == 2):
match_proportion = user_image.show_demo_3()/original_matches *100
#print("match_proportion: "+ str(match_proportion))
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="KNN匹配\n", append=True)

counter_current += 1
#Auto
if (mode == 1):
while match_proportion > auto_target_similarity:
event, values = window.Read(timeout=1)
if event is None:
break
user_image.keypoint_obscure(1)
user_image.keypoint_white_black_salt(1)

window.Element('-TEXT-').Update(value=("第"+str(counter_current)+"次迭代\n 匹配度: " + "%5.3f" % (match_proportion) + "%\n"), append=True)

counter_current += 1
if(counter_current % 3 == 0):
user_image.show_demo_1()
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="原图对比\n", append=True)

if(counter_current % 3 == 1):
user_image.show_demo_2()
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="更新特征值\n", append=True)

if(counter_current % 3 == 2):
match_proportion = user_image.show_demo_3()/original_matches *100
#print("match_proportion: "+ str(match_proportion))
window.Element('-IMAGE-').Update('demo.png')
window.Element('-TEXT-').Update(value="KNN匹配\n", append=True)

counter_current += 1
window.close()

if(Random_Crop_Pixel>0):
if(Random_Crop_Pixel>0 and mode == 0):
user_image.Random_Crop(Random_Crop_Pixel)
if(mode == 1):
user_image.Random_Crop(random.randint(2,5))
user_image.output()
sg.Popup('完成')
sg.Popup('完成 文件:'+ user_image.output_name)

def mainwindow():

sg.theme('DarkAmber')
sg.theme('DarkAmber')
auto_layout = [
[sg.Text(' ')],
[sg.Text(' ')],
[sg.Text(' ')],
[sg.Text('目标相似度(Target Similarity):')],
[sg.Text(' ')],
[sg.Text(' ')],
[sg.Slider(range=(10, 100), orientation='h', size=(40, 5), default_value=30)],
[sg.Text(' ')],
[sg.Text(' ')],
[sg.Text(' ')],
[sg.Text(' ')],
[sg.Text(' ')],
[sg.Text(' ')],
[sg.Submit(tooltip='Click to start',size=(20,1)), sg.Cancel(size=(20,1))],
]
manual_layout = [
[sg.Text('干扰手段:')],
[sg.Checkbox('近似值处理(Pixel Approximation) 密度:', size=(38,1),default=True)],
[sg.Slider(range=(1, 10), orientation='h', size=(40, 5), default_value=1)],
[sg.Checkbox('椒盐噪声(Salt-and-pepper Noise) 密度:', size=(38,1),default=True)],
[sg.Slider(range=(1, 10), orientation='h', size=(40, 5), default_value=1)],
[sg.Checkbox('随机线段(Random Scalar) 密度:', size=(38,1),default=True)],
[sg.Slider(range=(1, 10), orientation='h', size=(40, 5), default_value=1)],
[sg.Checkbox('随机裁剪(Random Crop) 裁剪像素:', size=(40,1),default=True)],
[sg.Slider(range=(1, 30), orientation='h', size=(40, 5), default_value=3)],
[sg.Text(' ')],
[sg.Text('迭代次数: (对于多次无法发送的图片 多迭代小干扰)')],
[sg.Slider(range=(1, 50), orientation='h', size=(40, 5), default_value=5)],
[sg.Submit(tooltip='Click to start',size=(20,1)), sg.Cancel(size=(20,1))],
]
layout = [
[sg.Text('要处理的图片:')],
[sg.Input(), sg.FileBrowse()],
[sg.Text(' ')],
[sg.Checkbox('反SIFT (Scale-Invariant Feature Transform) 匹配识别', size=(50,1),default=True)],
[sg.Checkbox('水印 (未开放)', size=(20,1),default=False)],
[sg.Text(' ')],
[sg.Text('干扰手段:')],
[sg.Checkbox('近似值处理(Pixel Approximation) 密度:', size=(38,1),default=True)],
[sg.Slider(range=(1, 10), orientation='h', size=(40, 5), default_value=1)],
[sg.Checkbox('椒盐噪声(Salt-and-pepper Noise) 密度:', size=(38,1),default=True)],
[sg.Slider(range=(1, 10), orientation='h', size=(40, 5), default_value=1)],
[sg.Checkbox('随机线段(Random Scalar) 密度:', size=(38,1),default=True)],
[sg.Slider(range=(1, 10), orientation='h', size=(40, 5), default_value=1)],
[sg.Checkbox('随机裁剪(Random Crop) 裁剪像素:', size=(40,1),default=True)],
[sg.Slider(range=(1, 30), orientation='h', size=(40, 5), default_value=3)],
[sg.Text(' ')],
[sg.Text('迭代次数: (对于多次无法发送的图片 多迭代小干扰)')],
[sg.Slider(range=(1, 50), orientation='h', size=(40, 5), default_value=5)],
[sg.Submit(tooltip='Click to submit this form'), sg.Cancel()],
[sg.TabGroup([[sg.Tab('自动(Auto)', auto_layout), sg.Tab('手动(Manual)', manual_layout)]])],
]


window = sg.Window('Momus 图像反识别工具', layout)
window = sg.Window('Momus 图像反识别工具', layout,size=(430,600))
event, values = window.read()
window.close()
return values
return values,event

def main():
parameter = mainwindow()
filepath = parameter[0]
filepath = parameter[0][0]

anti_SIFT = parameter[1]
watermark = parameter[2]
Gaussian_noise = parameter[3]
Gaussian_noise_level = parameter[4]
Salt_and_pepper_Noise = parameter[5]
Salt_and_pepper_Noise_level = parameter[6]
Random_Scalar = parameter[7]
Random_Scalar_level = parameter[8] * 8
Random_Crop = parameter[9]
Random_Crop_Pixel = parameter[10]
counter = parameter[11]

if(Gaussian_noise):
pass
else:
anti_SIFT = parameter[0][1]
watermark = parameter[0][2]
auto_target_similarity = parameter[0][3]
Gaussian_noise = parameter[0][4]
Gaussian_noise_level = parameter[0][5]
Salt_and_pepper_Noise = parameter[0][6]
Salt_and_pepper_Noise_level = parameter[0][7]
Random_Scalar = parameter[0][8]
Random_Scalar_level = parameter[0][9] * 8
Random_Crop = parameter[0][10]
Random_Crop_Pixel = parameter[0][11]
counter = parameter[0][12]

if(Gaussian_noise == False):
Gaussian_noise_level = 0
if(Salt_and_pepper_Noise):
pass
else:

if(Salt_and_pepper_Noise== False):
Salt_and_pepper_Noise_level = 0
if(Random_Scalar):
pass
else:

if(Random_Scalar== False):
Random_Scalar_level = 0
if(Random_Crop):
pass
else:

if(Random_Crop== False):
Random_Crop_Pixel = 0

if(parameter[1] == "Submit0"):
mode = 0
elif(parameter[1] == "Submit"):
mode = 1

if anti_SIFT :
img_processing_SIFT(filepath,counter,Gaussian_noise_level,Salt_and_pepper_Noise_level,Random_Scalar_level,Random_Crop_Pixel)
img_processing_SIFT(mode,auto_target_similarity,filepath,counter,Gaussian_noise_level,Salt_and_pepper_Noise_level,Random_Scalar_level,Random_Crop_Pixel)


if __name__ == "__main__":
Expand Down
9 changes: 6 additions & 3 deletions image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self,path):
self.height = self.img.shape[1]

class usr_img(image):

output_name = ''
not_png = False
_img_path = ''

Expand All @@ -71,7 +71,8 @@ def __init__(self,path):
def output(self):
if (self.not_png):
os.remove(self.img_path)
cv2.imwrite(os.path.splitext(self.img_path)[0] + "_output.png", self.img)
self.output_name = os.path.splitext(self.img_path)[0] + "_output.png"
cv2.imwrite(self.output_name, self.img)
os.remove("demo.png")


Expand Down Expand Up @@ -169,11 +170,13 @@ def show_demo_3(self):
matches = bf.knnMatch(des1,des2, k=2)
good = []
for m,n in matches:
if m.distance < 0.3*n.distance:
if m.distance < 0.8*n.distance:
good.append([m])
imgC = cv2.drawMatchesKnn(cv2.imread(self.img_path),kp1,self.img,kp2,good[:10000],None,flags=2)
#print("Pair of matches: " + str(len(good)))
cv2.imwrite('demo.png', imgC)
img_resize_to_GUI('demo.png')

return len(good)


0 comments on commit 8160356

Please sign in to comment.