def crack(self): result = [] # 加载数据 with open(os.path.join(current_dir, 'image_data.json'), 'r') as f: image_data = json.load(f) for letter in self.handle_split_image(): letter_vector = build_vector(letter) guess = [] for image in image_data: for x, y in image.items(): guess.append((distance_hanmming(y, letter_vector), x)) guess.sort() result.append(guess[0][1]) return ''.join(result)
def crack(self): # 装载训练数据集 with open(os.path.join(current_dir, 'imageset.dat'), 'rb') as f: imageset = pickle.load(f) # 对验证码图片进行切割 result = [] for letter in letters: final = self.im.crop((letter[0], 0, letter[1], self.im.size[1])) letter_vector = build_vector(final) guess = [] # 将切割得到的验证码小片段与每个训练片段进行比较 for image in imageset: for x, y in image.items(): guess.append((distance_cos(y, letter_vector), x)) guess.sort() result.append(guess[0][1]) return "".join(result)
def crack(self): result = [] # 加载数据 with open(os.path.join(current_dir, 'image_data.json'), 'rb') as f: image_data = json.load(f) for letter in self.handle_split_image(): letter_vector = build_vector(letter) guess = [] for image in image_data: for x, y in image.items(): guess.append((distance_hanmming(y, letter_vector), x)) guess.sort() neighbors = guess[:15] # 距离最近的十五个向量 class_votes = {} # 投票 for neighbor in neighbors: class_votes.setdefault(neighbor[-1], 0) class_votes[neighbor[-1]] += 1 sorted_votes = sorted(class_votes.items(), key=lambda x: x[1], reverse=True) result.append(sorted_votes[0][0]) return ''.join(result)
import os import json from PIL import Image from njupt.utils.captchas import build_vector if __name__ == "__main__": # 字符集 letters = ['2', '3', '4', '5', '6', '7', '8', '9'] # 将图像数据转为向量数据并保存 image_data = [] for letter in letters: for img in os.listdir('captcha_chars/{}/'.format(letter)): if img != "Thumbs.db" and img != ".DS_Store": temp = build_vector( Image.open("captcha_chars/{}/{}".format(letter, img))) image_data.append({letter: temp}) with open('image_data.json', 'w') as f: json.dump(image_data, f)
import json from io import BytesIO import requests from PIL import Image from njupt import Library from njupt.utils.captchas import build_vector from njupt.utils.captchas.library.libray_captcha import LibraryCaptcha if __name__ == "__main__": image_data = [] for i in range(10): r = requests.get(Library.URLs.CAPTCHA) im = Image.open(BytesIO(r.content)) im.show() captcha = LibraryCaptcha(image=im) letters = input() for letter, im in zip(letters, captcha.handle_split_image()): vector = build_vector(im) image_data.append({letter: vector}) with open('image_data.json', 'w') as f: json.dump(image_data, f)