python 人脸检测 +python 二维码检测

从官网下载opencv 目录结构如图

在samples中有丰富的示例

应为我的系统中已经安装好opepncv-python,可直接运行

会得到结果:

人脸检测代码如下

#!/usr/bin/env python

‘‘‘
face detection using haar cascades

USAGE:
    facedetect.py [--cascade <cascade_fn>] [--nested-cascade <cascade_fn>] [<video_source>]
‘‘‘

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2

# local modules
from video import create_capture
from common import clock, draw_str

def detect(img, cascade):
    rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
                                     flags=cv2.CASCADE_SCALE_IMAGE)
    if len(rects) == 0:
        return []
    rects[:,2:] += rects[:,:2]
    return rects

def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)

if __name__ == ‘__main__‘:
    import sys, getopt
    print(__doc__)

    args, video_src = getopt.getopt(sys.argv[1:], ‘‘, [‘cascade=‘, ‘nested-cascade=‘])
    try:
        video_src = video_src[0]
    except:
        video_src = 0
    args = dict(args)
    cascade_fn = args.get(‘--cascade‘, "../../data/haarcascades/haarcascade_frontalface_alt.xml")
    nested_fn  = args.get(‘--nested-cascade‘, "../../data/haarcascades/haarcascade_eye.xml")

    cascade = cv2.CascadeClassifier(cascade_fn)
    nested = cv2.CascadeClassifier(nested_fn)

    cam = create_capture(video_src, fallback=‘synth:bg=../data/lena.jpg:noise=0.05‘)

    while True:
        ret, img = cam.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        gray = cv2.equalizeHist(gray)

        t = clock()
        rects = detect(gray, cascade)
        vis = img.copy()
        draw_rects(vis, rects, (0, 255, 0))
        if not nested.empty():
            for x1, y1, x2, y2 in rects:
                roi = gray[y1:y2, x1:x2]
                vis_roi = vis[y1:y2, x1:x2]
                subrects = detect(roi.copy(), nested)
                draw_rects(vis_roi, subrects, (255, 0, 0))
        dt = clock() - t

        draw_str(vis, (20, 20), ‘time: %.1f ms‘ % (dt*1000))
        cv2.imshow(‘facedetect‘, vis)

        if cv2.waitKey(5) == 27:
            break
    cv2.destroyAllWindows()

其中训练好的分类器在

目录下

PS:在树莓派上使用时需要注意,USB接入的摄像头可使用opecv 调用,否则只能用picamera 来调起,

接着时二维码识别

这是资料地址

http://www.open-open.com/lib/view/open1464566856199.html

上面的这篇博客讲的非常详细和全面,不过没有代码,这里整理了一下方便结合视频的方式检测二维码,

 

def show(img, code=cv2.COLOR_BGR2RGB):
    cv_rgb = cv2.cvtColor(img, code)
    while (1):
     cv2.imshow(‘ckh‘,img)
     key = cv2.waitKey(10)
     c = chr(key & 255)
     if c in [‘B‘, ‘b‘, chr(27)]:
         break
def createLineIterator(P1, P2, img):
    """
    Produces and array that consists of the coordinates and intensities of each pixel in a line between two points

    Parameters:
        -P1: a numpy array that consists of the coordinate of the first point (x,y)
        -P2: a numpy array that consists of the coordinate of the second point (x,y)
        -img: the image being processed

    Returns:
        -it: a numpy array that consists of the coordinates and intensities of each pixel in the radii (shape: [numPixels, 3], row = [x,y,intensity])
    """
    #define local variables for readability
    imageH = img.shape[0]
    imageW = img.shape[1]
    P1X = P1[0]
    P1Y = P1[1]
    P2X = P2[0]
    P2Y = P2[1]

    #difference and absolute difference between points
    #used to calculate slope and relative location between points
    dX = P2X - P1X
    dY = P2Y - P1Y
    dXa = np.abs(dX)
    dYa = np.abs(dY)

    #predefine numpy array for output based on distance between points
    itbuffer = np.empty(shape=(np.maximum(dYa,dXa),3),dtype=np.float32)
    itbuffer.fill(np.nan)

    #Obtain coordinates along the line using a form of Bresenham‘s algorithm
    negY = P1Y > P2Y
    negX = P1X > P2X
    if P1X == P2X: #vertical line segment
        itbuffer[:,0] = P1X
        if negY:
            itbuffer[:,1] = np.arange(P1Y - 1,P1Y - dYa - 1,-1)
        else:
            itbuffer[:,1] = np.arange(P1Y+1,P1Y+dYa+1)
    elif P1Y == P2Y: #horizontal line segment
        itbuffer[:,1] = P1Y
        if negX:
            itbuffer[:,0] = np.arange(P1X-1,P1X-dXa-1,-1)
        else:
            itbuffer[:,0] = np.arange(P1X+1,P1X+dXa+1)
    else: #diagonal line segment
        steepSlope = dYa > dXa
        if steepSlope:
            slope = dX.astype(np.float32)/dY.astype(np.float32)
            if negY:
                itbuffer[:,1] = np.arange(P1Y-1,P1Y-dYa-1,-1)
            else:
                itbuffer[:,1] = np.arange(P1Y+1,P1Y+dYa+1)
            itbuffer[:,0] = (slope*(itbuffer[:,1]-P1Y)).astype(np.int) + P1X
        else:
            slope = dY.astype(np.float32)/dX.astype(np.float32)
            if negX:
                itbuffer[:,0] = np.arange(P1X-1,P1X-dXa-1,-1)
            else:
                itbuffer[:,0] = np.arange(P1X+1,P1X+dXa+1)
            itbuffer[:,1] = (slope*(itbuffer[:,0]-P1X)).astype(np.int) + P1Y

    #Remove points outside of image
    colX = itbuffer[:,0]
    colY = itbuffer[:,1]
    itbuffer = itbuffer[(colX >= 0) & (colY >=0) & (colX<imageW) & (colY<imageH)]

    #Get intensities from img ndarray
    itbuffer[:,2] = img[itbuffer[:,1].astype(np.uint),itbuffer[:,0].astype(np.uint)]

    return itbuffer
def isTimingPattern(line):
    # 除去开头结尾的白色像素点
    while line[0] != 0:
        line = line[1:]
    while line[-1] != 0:
        line = line[:-1]
    # 计数连续的黑白像素点
    c = []
    count = 1
    l = line[0]
    for p in line[1:]:
        if p == l:
            count = count + 1
        else:
            c.append(count)
            count = 1
        l = p
    c.append(count)
    # 如果黑白间隔太少,直接排除
    if len(c) < 5:
        return False
    # 计算方差,根据离散程度判断是否是 Timing Pattern
    threshold = 5
    return np.var(c) < threshold
def cv_distance(P, Q):
    return int(np.math.sqrt(pow((P[0] - Q[0]), 2) + pow((P[1] - Q[1]), 2)))
def check(a, b,path):
    # 存储 ab 数组里最短的两点的组合
    s1_ab = ()
    s2_ab = ()
    # 存储 ab 数组里最短的两点的距离,用于比较
    s1 = np.iinfo(‘i‘).max
    s2 = s1
    for ai in a:
        for bi in b:
            d = cv_distance(ai, bi)
            if d < s2:
                if d < s1:
                    s1_ab, s2_ab = (ai, bi), s1_ab
                    s1, s2 = d, s1
                else:
                    s2_ab = (ai, bi)
                    s2 = d

    a1, a2 = s1_ab[0], s2_ab[0]
    b1, b2 = s1_ab[1], s2_ab[1]

    a1 = (a1[0] + np.int0((a2[0]-a1[0])*1/14), a1[1] + np.int0((a2[1]-a1[1])*1/14))
    b1 = (b1[0] + np.int0((b2[0]-b1[0])*1/14), b1[1] + np.int0((b2[1]-b1[1])*1/14))
    a2 = (a2[0] + np.int0((a1[0]-a2[0])*1/14), a2[1] + np.int0((a1[1]-a2[1])*1/14))
    b2 = (b2[0] + np.int0((b1[0]-b2[0])*1/14), b2[1] + np.int0((b1[1]-b2[1])*1/14))
    img = cv2.imread(path)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    th, bi_img = cv2.threshold(img_gray, 100, 255, cv2.THRESH_BINARY)
    # 将最短的两个线画出来
    #cv2.line(draw_img, a1, b1, (0,0,255), 3)
    #cv2.line(draw_img, a2, b2, (0,0,255), 3)
    lit1 = createLineIterator(a1,b1,bi_img)
    lit2 = createLineIterator(a2,b2,bi_img)
    if isTimingPattern(lit1[:,2]):
        return True
    elif isTimingPattern(lit2[:,2]):
        return True
    else:
        return False
def RunImg(path):
    img = cv2.imread(path)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_gb = cv2.GaussianBlur(img_gray, (5, 5), 0)
    edges = cv2.Canny(img_gray, 100, 200)
    img_fc, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    hierarchy = hierarchy[0]
    found = []
    for i in range(len(contours)):
        k = i
        c = 0
        while hierarchy[k][2] != -1:
            k = hierarchy[k][2]
            c = c + 1  # count hierarchy
        if c >= 5:
            found.append(i)  # store index
            # 对图像进行二值化
    th, bi_img = cv2.threshold(img_gray, 100, 255, cv2.THRESH_BINARY)
    draw_img = img.copy()
    boxes = []
    for i in found:
        rect = cv2.minAreaRect(contours[i])
        box = np.int0(cv2.boxPoints(rect))
        #    cv2.drawContours(draw_img,[box], 0, (0,0,255), 2)
        # box = map(tuple, box)
        box = [tuple(x) for x in box]
        boxes.append(box)
        # show(draw_img)
        # print("Length of Boxes is ",len(boxes))
    valid = set()
    for i in range(len(boxes)):
        for j in range(i + 1, len(boxes)):
            if check(boxes[i], boxes[j],path):
                valid.add(i)
                valid.add(j)
    contour_all = []
    while len(valid) > 0:
        c = contours[found[valid.pop()]]
        for sublist in c:
            for p in sublist:
                contour_all.append(p)
    rect = cv2.minAreaRect(np.array(contour_all))
    box = np.array([cv2.boxPoints(rect)], dtype=np.int0)
    cv2.polylines(draw_img, box, True, (0, 0, 255), 3)
    show(draw_img)

if __name__ == ‘__main__‘:
    RunImg(‘er.jpg‘)

效果很好。

以上

时间: 2024-10-16 16:50:15

python 人脸检测 +python 二维码检测的相关文章

python生成带参数二维码

#coding:utf8 import urllib2 import urllib import json import string import random class WebChat(object): def __init__(self, appid=None, secret=None, code=None): self.appid = appid self.secret = secret self.code = code def token_url(self): return "htt

超实用python小项目--基于python的手机通讯录二维码生成网站--1、项目介绍和开发环境

这个项目是我做完整的第一个python web项目,对于新手来说,这个项目绝对是一个特别好的练手项目. 起名还是困难,但是自己确实比较烦输入这么长的名字(手机通讯录二维码生成网站)去定义这个网站,所以还是给这个项目起个名字吧,叫什么呢?就叫 "鹅日通讯录"吧(Earth address list). --------------------------------------------------------------------------------------------我是

Python使用QRCode生成二维码

PIL和QRCode下载地址: http://www.pythonware.com/products/pil/ https://pypi.python.org/pypi/qrcode/5.1 #你可能需要的setuptools: https://pypi.python.org/pypi/setuptools/ 参考文档: http://www.cnblogs.com/linjiqin/p/4140455.html http://www.cnblogs.com/ryuham/p/4455954.h

python三步生成二维码

本次使用python 2.7.13 下 安装qrcode模块三步生成二维码; qcrode官方说明https://pypi.python.org/pypi/qrcode/ qcrode模块通过pip install qrcode 安装即可 pip命令没有请参照http://dyc2005.blog.51cto.com/270872/1940870安装 以下是生成二维码过程: 代码如下: import qrcode mk = qrcode.make("http://www.pkey.cn"

Python将文本生成二维码

#coding:utf-8 ''' Python生成二维码 v1.0 主要将文本生成二维码图片 测试一:将文本生成白底黑字的二维码图片 测试二:将文本生成带logo的二维码图片 ''' __author__ = 'Xue' import qrcode from PIL import Image import os #生成二维码图片 def make_qr(str,save): qr=qrcode.QRCode( version=4, #生成二维码尺寸的大小 1-40 1:21*21(21+(n-

Python qrcode 生成一个二维码

借助第三方库qrcode实现. 二维码图片生成借助pillow qrcode的安装 在命令行中输入 pip install qrcode[pil]   用法: 1.在命令行中输入 qr "Some text" > test.png 2.在python中输入 import qrcode img = qrcode.make('Some data here') 高级用法: 使用QRCode类 import qrcode qr = qrcode.QRCode( version=1, er

1行Python代码制作动态二维码

原文地址 https://blog.csdn.net/m0_38106923/article/details/100603516 GitHub网站参见:https://github.com/sylnsfar/qrcode 原文地址:https://www.cnblogs.com/php-linux/p/11843357.html

OpenCV和Zbar两个Python模块实现二维码和条形码识别

在我们的日常生活中,处处可见条形码和二维码. 在以前,我们去逛书店时,或者你现在随手拿起你身边的一本书,你肯定能看到书本的封页后面印有一排黑色线条组成的标签,也就是条形码:你去你们学校的自助机上借书还书时识别的也是条形码:哦,对了,你还记得每次大型考试答题卡上都会贴上监考老师分发给你的那个标签吗?还是条形码:甚至现在你随随便便逛个超市或便利店,收银员或者自助机也都是通过扫商品条形码给你计价的.条形码在我们的日常生活中真的是随处可见. 到了后来,2016年之后,二维码也渐渐开始普及起来,现在二维码

python应用:生成简单二维码

概述 \(\quad\)第一篇python的应用就打算写一写用python生成简单的二维码啦.因为二维码在日常生活中越来越常用了,部分博客也用二维码来用作打赏的工具.但是要提醒大家的是,千万不要乱扫街上的二维码,很多都是包含了恶意软件,或者把你重定向到别的网页去了. \(\quad\)那么闲话不多说,开始干活~.默认大家会用python且会下载第三方库咯,生成二维码我们使用的是python的第三方库MyQR,调用方式也是极其简单,上代码: from MyQR import myqr words