1. 程式人生 > >python DLib實時性不夠,通過多執行緒來解決

python DLib實時性不夠,通過多執行緒來解決

# 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:
[email protected]
: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,這個時候,體驗較流暢。