# created at 2017-11-27
# updated at 2018-09-06

# Author:   coneypo
# Dlib:     http://dlib.net/
# Blog:     http://www.cnblogs.com/AdaminXie/
# Github:   https://github.com/coneypo/Dlib_examples

import dlib
import time, threading
import queue
#from skimage import io
import cv2
import time

imglist = queue.Queue(maxsize=1000)

url = 'http://admin:12345688@172.30.183.9:8081'
# 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector()

# Dlib 的 68点模型
predictor = dlib.shape_predictor("../face_lib/shape_predictor_68_face_landmarks.dat")

# 图片所在路径
#img = cv2.imread("../face_img/3.jpg")

# 生成 Dlib 的图像窗口
#win = dlib.image_window()
#win.set_image(img)
cap = cv2.VideoCapture(url)
# 使用 detector 检测器来检测图像中的人脸
#s = time.clock()
print(cap.isOpened())

#imglist = []
def get_img():
    while (cap.isOpened()):
        iss,frame = cap.read()
        frame = cv2.resize(frame,(640,320))
        imglist.put(frame)
        
def get_face():
    while True:
        if not imglist.empty():
            img = imglist.get()
            faces = detector(img, 1)
            for i, d in enumerate(faces):
                print("第", i+1, "个人脸的矩形框坐标:",
                      "left:", d.left(), "right:", d.right(), "top:", d.top(), "bottom:", d.bottom())
                cv2.rectangle(img,(d.left(),d.top()),(d.right(),d.bottom()),(0, 255, 0), 2)
                
                shape = predictor(img, faces[i])
                for i in range(68):
                    cv2.circle(img, (shape.part(i).x, shape.part(i).y), 2, (0, 255, 0), -1, 3)
        #      
            cv2.imshow('face2', img)
            cv2.waitKey(1)  
#        imglist.append(frame)
#        faces = detector(frame, 1)
    

#cv2.waitKey(0)
#cv2.waitKey(0)
#print(time.clock()-s)
    # 绘制面部轮廓
#    win.add_overlay(shape)
    
ts = threading.Thread(target=get_img,name='getimg')
td = threading.Thread(target=get_face,name='disimg')
ts.start()
td.start()

# 绘制矩阵轮廓
#win.add_overlay(faces)

# 保持图像
#dlib.hit_enter_to_continue()

多线程,单队列的思路来,充分利用CPU,这个时候,体验较流畅。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐