• python案例(更新中)


    一. python实现图片识别

    '''
    1. 根据肤色数量判断
    2. 介绍YCbCr:YCBCR或是Y'CBCR,是色彩课件的一种,通常会用于影片中的影响连续处理,或是数字摄影系统中
    Y’是颜色的亮度成分,CB和CR是蓝色和蓝色的浓度偏移量成分,Y'和Y是不同的,Y表示光的浓度且为非线性。
    使用YCbCr可以做一些简易版的人练识别,肤色识别等功能
    3. 本脚本功能:识别seqing图片,当人体裸露身体部位到达整个画面一定比例时,则认为“黄图”
    '''
    
    from PIL import Image
    basedir=r'F:python实例\imag2'     # 只需要文件路径,不需要具体到文件名,否则报错NotADirectoryError: [WinError 267] 目录名称无效
    import os
    for filename in os.listdir(basedir):                # os.listdir获取basedir中包含的文件或文件夹名字的列表
        full_filename=os.path.join(basedir,filename)    # 拼接路径
        img = Image.open(full_filename).convert('YCbCr')  # convert PIL有9中模式:1,L,P,RGB,RGBA,CMYK,YCbCr,I,F
        w, h = img.size
        data = img.getdata()
    
        cnt = 0
        for i, ycbcr in enumerate(data):
            y, cb, cr = ycbcr
            print('------',ycbcr)
            if 86 <= cb <= 117 and 140 <= cr <= 168:
                cnt += 1
        print('%s is a porn image?:%s.'%(filename, 'Yes' if cnt > w * h * 0.3 else 'No'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2. 数字雨

    '''
    使用pygame模块,将字体渲染到画布上
    注意:字体ttf可自行下载,如果想显示中文,下载一个中文的
    '''
    
    import sys
    import random
    import pygame
    from pygame.locals import *
     
     
    # 屏幕大小
    WIDTH = 800
    HEIGHT = 600
    # 下落速度范围
    SPEED = [15, 30]
    # 字母大小范围
    SIZE = [5, 30]
    # CODE长度范围
    LEN = [1, 8]
     
     
    # 随机生成一个颜色
    def randomColor():
        return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    
    # 随机生成一个速度
    def randomSpeed():
        return random.randint(SPEED[0], SPEED[1])
     
    # 随机生成一个大小
    def randomSize():
        return random.randint(SIZE[0], SIZE[1])
     
    # 随机生成一个长度
    def randomLen():
        return random.randint(LEN[0], LEN[1])
     
    # 随机生成一个位置
    def randomPos():
        return (random.randint(0, WIDTH), -20)
     
    # 随机生成一个字符串
    def randomCode():
        return random.choice('qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890')
        # return '你大爷的'
     
    # 定义代码精灵类
    class Code(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.font = pygame.font.Font('happy.ttf', randomSize())    # 随机字体大小,此处可自行网上下载一个中文字体,否则中文容易不显示
            self.speed = randomSpeed()          # 随机速度
            self.code = self.getCode()          # 随机长度
            self.image = self.font.render(self.code, True, randomColor())   # 将一个随机数字(长度)选绕道画布上,且颜色随机
            self.image = pygame.transform.rotate(self.image, random.randint(87, 93))    # 讲图像随机旋转角度
            self.rect = self.image.get_rect()   # 获取图片在画布上的位置
            self.rect.topleft = randomPos()     # 随机位置
     
        def getCode(self):
            length = randomLen()
            code = ''
            for i in range(length):
                code += randomCode()
            return code
        def update(self):
            self.rect = self.rect.move(0,self.speed) 
            if self.rect.top > HEIGHT:
                self.kill()
     
     
    pygame.init()           # 初始函数,使用pygame的第一步
    screen = pygame.display.set_mode((WIDTH, HEIGHT))   #生成主屏幕screen;第一个参数是屏幕大小
    pygame.display.set_caption('数字雨')  # 窗口命名
     
    clock = pygame.time.Clock()                 # 初始化一个clock对象
    codesGroup = pygame.sprite.Group()          # 精灵组,一个简单的实体容器
    while True:
        clock.tick(24)                          # 控制游戏绘制的最大帧率为30
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
        screen.fill((0, 0, 0))                      # 填充背景颜色
     
        codeobject = Code()
        codesGroup.add(codeobject)              # 添加精灵对象
        codesGroup.update()
        codesGroup.draw(screen)
        pygame.display.update()
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    Java进阶(一) Java高效读取大文件,占内存少
    JAVA计算机毕业设计自行车在线租赁管理系统2021Mybatis+系统+数据库+调试部署
    Pytorch(一)——Pytorch基础知识
    【毕业设计】30-基于单片机矿井瓦斯_气体浓度_烟雾浓度报警设计(原理图+源代码+仿真+答辩论文+答辩PPT)
    el-form动态表单嵌套验证
    计算机网络——网络层
    国产etl 与 ODI
    Win10C盘满了怎么清理?如何清理电脑C盘?
    用frp搞个内网穿透
    XMLHttpRequest和Fetch API
  • 原文地址:https://blog.csdn.net/qq_43609939/article/details/127786236