Table of Contents
Overview of Verification Types
Sliding Puzzle
Text selection
Word order click
Font recognition
Spatial Reasoning
Home Backend Development Python Tutorial How to implement the behavioral verification code verification function in Python

How to implement the behavioral verification code verification function in Python

Jun 03, 2023 am 09:31 AM
python

Overview of Verification Types

Sliding Puzzle

Innovative behavioral verification, easily complete the puzzle with one swipe, great experience, and pass the verification in seconds. Simple and efficient, it protects against machine risks while ensuring the ultimate user experience. Suitable for scenarios that pursue user experience.

# 生成背景图
  basemap1 = Image.open(bg).convert("RGBA")  # 背景图
  if basemap1.size != size:  # 需要裁切或拉伸
      basemap1 = Graphics.crop(basemap1, size[0], size[1])
  puzzle1 = Image.open(url_absolute(img)).convert("RGBA")  # 方块图,蒙板
  # 旋转角度
  if rotate == 2:
      angle = randint(0, 360)
  elif rotate == 1:
      angle = choice([0, 90, 180, 270])
  else:
      angle = 0
  # angle = 45
  if angle: puzzle1 = puzzle1.rotate(angle, resample=Image.Resampling.BILINEAR)

  puzzle1.putalpha(ImageEnhance.Brightness(puzzle1.split()[3]).enhance(alpha))  # 设置透明度,0-1之间
  # 产生随机位置
  img_size = puzzle1.size  # 滑动图片尺寸
  spacing = 0  # 滑动图片在底图位置四周间距,暂时使用0,小图片中的图案本身有20px边距
  # 随机位置
  x = randint(img_size[0] + spacing, size[0] - img_size[0] - spacing)
  y = randint(spacing, size[1] - img_size[1] - spacing)
  basemap1.paste(puzzle1, (x, y), puzzle1)  # 拷贝

  # 方块滑动图
  # basemap2 = Image.open(url_absolute(bg)).convert("RGBA")
  basemap2 = Image.open(bg).convert("RGBA")
  if basemap2.size != size:  # 需要裁切或拉伸
      basemap2 = Graphics.crop(basemap2, size[0], size[1])
  puzzle2 = Image.open(url_absolute(img)).convert("RGBA")
  if angle: puzzle2 = puzzle2.rotate(angle, resample=Image.Resampling.BILINEAR)  # 旋转

  basemap2 = basemap2.crop((x, y, x + img_size[0], y + img_size[1]))  # 裁切
  puzzle2.paste(basemap2, (0, 0), puzzle2)
  # 替换成长条形滑动块
  strip = Image.new('RGBA', (img_size[0], size[1]), (255, 255, 255, 0))
  strip.paste(puzzle2, (0, y), puzzle2)  # 拷贝
Copy after login

Text selection

Click on the text in the picture sequentially for a new behavioral verification, which is extremely secure and ensures verification security. While improving the difficulty of machine recognition, it ensures readability by real users. Suitable for business scenarios with high security requirements.

def random_character(self, length=None, type=[0, 1, 2, 3], repeat=False):
  """
  生成随机字符
  :param length: 生成的字符长度,几个字符
  :param type: [0] 数字,[1] 大写字母,[2]小写字母,[3] 特殊字符
  :param repeat: 是否允许重复字符
  :return [("A", 1, "大写字母"), ("8", 0, "数字"), ("a", 2, "小写字母"), ("", 3, "高跟鞋") ...]
  """
  if length is None: length = self.str_count
  # length = 10
  # type = [0]
  string = "".join(dict([(key, {
      0: "2345678923456789",
      1: "ABCDEFGHJKLMNQRTY",
      2: "abcdefghijkmnqrty",
      3: "",
  }[key]) for key in type]).values())

  r = []
  for i in range(length):
      if repeat:  # 允许重复
          s = choice(string)
          t = Inference.char_type(s)
          r.append((s, t[0], t[1]))
      else:
          anti = 0  # 防止死循环,尝试一定次数后允许字符重复
          while True:
              anti += 1
              s = choice(string)
              t = Inference.char_type(s)
              st = "".join([it[0] for it in r])
              if s not in st or anti > 30:
                  r.append((s, t[0], t[1]))
                  break
  # 替换 n 个字母为图形字符
  if 3 in type:
      index = sample([i for i in range(length)], randint(0, length))  # 随机一组索引值:[0, 3, 1]
      icon_char = sample(self.icon_str, len(index))  # 随机取出 n 组特殊字符
      x = 0
      for i in index:
          # r = Inference.char_replace(r, i, icon_char[x][1])
          r[i] = (icon_char[x][1], 3, icon_char[x][2])
          x += 1
  return r
Copy after login

Word order click

According to Chinese semantics, click on the text in the picture in order, and the semantic understanding ability is combined with the behavioral trajectory. Suitable for business scenarios with high security requirements.

The following is an example of the production of interference points and interference lines:

# 噪线
  for i in range(line_count):
      x1 = randint(0, size[0])
      x2 = randint(0, size[0])
      y1 = randint(0, size[1])
      y2 = randint(0, size[1])
      draw.line((x1, y1, x2, y2), fill=Word.get_random_color())

  # 噪点
  for i in range(point_count):
      draw.point([randint(0, size[0]), randint(0, size[1])], fill=Word.get_random_color())
      x = randint(0, size[0])
      y = randint(0, size[1])
      draw.arc((x, y, x + 4, y + 4), 0, 90, fill=Word.get_random_color())
Copy after login

Font recognition

When clicking on text in a different font from other characters, the user only needs one click, that is Security verification is possible. Suitable for business scenarios with extremely high security requirements.

# 字体识别
if type in (10, 11, 12):  # 789生成成语/固定字符
    str_count = 1
    str_inter = numeric(str_inter, 2, 20)  # 干扰字符不能少于2
    v_font = sample(ttf, 2)  # 随机选出两种字体

string = []
for i in range(str_count + str_inter):

    if type in (10, 11, 12):  # 字体识别,只使用两种字体
        font_file = v_font[0] if i == 0 else v_font[1]
    else:  # 随机字体
        font_file = choice(ttf)

    font = ImageFont.truetype(url_absolute(font_file), size=font_size)

    # 成语/使用固定字符,前n个字符使用成语字符
    random_char = idiom[i:i+1] if idiom else ""

    # 随机字符串及补充固定字符时追加干扰字符
    if random_char == "":
        head = randint(0xb0, 0xf7)
        body = randint(0xa1, 0xfe)
        random_char = bytes.fromhex(f'{head:x} {body:x}').decode("gb18030")

    # print(random_char, font_file)

    # 随机位置
    anti = 0  # 防止字体设置过大或者图片设置过小,导致死循环,尝试一定次数后允许字符重叠
    while True:  # 防止文字重叠
        anti += 1
        x = randint(0, size[0] - font_size)
        y = randint(0, size[1] - font_size)
        find = True
        for s in string:
            if abs(x - s[1]) < font_size and abs(y - s[2]) < font_size:
                find = False
                break
        if find or not string or anti > 20: break

    # 创建文字图片,可旋转
    str_bg = Image.new("RGBA", (font_size, font_size), (255, 255, 255, 0))  # 文字用空白图层
    str_draw = ImageDraw.Draw(str_bg)
    str_draw.text((0, 0), random_char, Word.get_random_color(), font=font)  # 添加文字
    angle = randint(-75, 75) if rotate else 0  # 是否随机角度
    str_bg = str_bg.rotate(angle, resample=Image.Resampling.BILINEAR, expand=0)  # 随机旋转

    basemap.paste(str_bg, (x, y), str_bg)  # 图片与文字合并

    # 保存随机字符及位置
    string.append([random_char, x, y, -angle])  # 字符、x、y、角度(正负转换,转用CSS顺时针旋转形式)
Copy after login

Spatial Reasoning

According to the prompts, click on the corresponding element. Logical problem-solving ability combined with the ability to recognize graphic symbols and other elements. Suitable for business scenarios with extremely high security requirements.

The following are some examples of verification methods:

def send_color2differ(self):
        """ 请点击一个颜色不一样的字符 """
        color = self.color_name(2)  # 获取 2 组带中文名称的颜色 [(&#39;蓝色&#39;, &#39;#0000FF&#39;), ]
        data = []
        for i in range(self.str_count):
            # data/在图片上生成的数据
            data.append({
                "str": self.string[i][0],  # 字符内容
                "X": self.coord[i][0],  # x 位置
                "Y": self.coord[i][1],  # y 位置
                "color": color[0][1] if i == 0 else color[1][1],
                "angle": self.angle[i],
                "icon": True if self.string[i][1] == 3 else False,  # 是否为图形字符
            })
        # hint/操作说明文字
        hint = f&#39;请点击一个 <i>颜色不一样</i> 的 <i>{self.string[0][2]}</i>&#39;
        str = [(data[0]["str"], data[0]["X"], data[0]["Y"], data[0]["angle"]), ]
        return {"data": data, "str": str, "hint": hint}

def send_color2capital(self):
        """ 请点击蓝色字母对应的大写 """
        direc = choice([1, 2])  # 随机一种方式,大写 to 小写/小写 to 大写
        color = self.color_name()  # 获取 n 组带中文名称的颜色 [(&#39;蓝色&#39;, &#39;#0000FF&#39;), ]
        self.string = self.random_character(type=[direc])
        data = []
        for i in range(self.str_count):
            # data/在图片上生成的数据
            data.append({
                "str": self.string[i][0],  # 字符内容
                "X": self.coord[i][0],  # x 位置
                "Y": self.coord[i][1],  # y 位置
                "color": color[i][1],
                "angle": self.angle[i],
                "icon": True if self.string[i][1] == 3 else False,  # 是否为图形字符
            })

        data[0]["str"] = data[1]["str"].swapcase()
        # hint/操作说明文字
        hint = f&#39;请点击 <i>{color[0][0]}字母</i> 对应的 <i>{"大写" if direc == 1 else "小写"}</i>&#39;
        str = [(data[1]["str"], data[1]["X"], data[1]["Y"], data[1]["angle"]), ]
        return {"data": data, "str": str, "hint": hint}
Copy after login

The above is the detailed content of How to implement the behavioral verification code verification function in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to view server version of Redis How to view server version of Redis Apr 10, 2025 pm 01:27 PM

Question: How to view the Redis server version? Use the command line tool redis-cli --version to view the version of the connected server. Use the INFO server command to view the server's internal version and need to parse and return information. In a cluster environment, check the version consistency of each node and can be automatically checked using scripts. Use scripts to automate viewing versions, such as connecting with Python scripts and printing version information.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

How to set the Redis memory size according to business needs? How to set the Redis memory size according to business needs? Apr 10, 2025 pm 02:18 PM

Redis memory size setting needs to consider the following factors: data volume and growth trend: Estimate the size and growth rate of stored data. Data type: Different types (such as lists, hashes) occupy different memory. Caching policy: Full cache, partial cache, and phasing policies affect memory usage. Business Peak: Leave enough memory to deal with traffic peaks.

What are the Redis memory configuration parameters? What are the Redis memory configuration parameters? Apr 10, 2025 pm 02:03 PM

**The core parameter of Redis memory configuration is maxmemory, which limits the amount of memory that Redis can use. When this limit is exceeded, Redis executes an elimination strategy according to maxmemory-policy, including: noeviction (directly reject write), allkeys-lru/volatile-lru (eliminated by LRU), allkeys-random/volatile-random (eliminated by random elimination), and volatile-ttl (eliminated by expiration time). Other related parameters include maxmemory-samples (LRU sample quantity), rdb-compression

What is the impact of Redis persistence on memory? What is the impact of Redis persistence on memory? Apr 10, 2025 pm 02:15 PM

Redis persistence will take up extra memory, RDB temporarily increases memory usage when generating snapshots, and AOF continues to take up memory when appending logs. Influencing factors include data volume, persistence policy and Redis configuration. To mitigate the impact, you can reasonably configure RDB snapshot policies, optimize AOF configuration, upgrade hardware and monitor memory usage. Furthermore, it is crucial to find a balance between performance and data security.

Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

See all articles