私は/ランドマークワット顔認識を実行するためのOpenCVとDLIBを使用しています、ウェブカメラストリームから住んでいます。言語はPythonです。私のMacBookラップトップでは正常に動作しますが、デスクトップコンピュータから24時間365日稼働する必要があります。このコンピュータは、Debian Jessieが動作するPCインテル®Core™2 Quad CPU Q6600 @ 2.40GHz 32ビット版です。 パフォーマンスの低下は劇的です:処理のために10秒の遅延があります!OpenCVの/ Pythonの:ライブ顔認識のためのマルチスレッド
Iは、従って性能を得るためにマルチスレッドに見:
- Iは最初のOpenCVによるサンプルコードを試みたが、結果は素晴らしいです! 4つのコアすべてが100%ヒットし、パフォーマンスははるかに優れています。
- フレーム処理コードを自分のコードに置き換えても、パフォーマンスは向上しません。 1つのコアだけが100%をヒットし、他のコアは非常に低く保たれます。 私はそれがマルチスレッドではさらに悪いと思っています。
私はdlibサンプルコードから顔のランドマークコードを得ました。私はそれがおそらく最適化できることを知っていますが、私は理解したいなぜ私は(古い)コンピュータのフルパワーをマルチスレッドで使用できないのですか?
私は以下の私のコードをドロップします、おかげで読書のためにたくさん:)
from __future__ import print_function
import numpy as np
import cv2
import dlib
from multiprocessing.pool import ThreadPool
from collections import deque
from common import clock, draw_str, StatValue
import video
class DummyTask:
def __init__(self, data):
self.data = data
def ready(self):
return True
def get(self):
return self.data
if __name__ == '__main__':
import sys
print(__doc__)
try:
fn = sys.argv[1]
except:
fn = 0
cap = video.create_capture(fn)
#Face detector
detector = dlib.get_frontal_face_detector()
#Landmarks shape predictor
predictor = dlib.shape_predictor("landmarks/shape_predictor_68_face_landmarks.dat")
# This is where the facial detection takes place
def process_frame(frame, t0, detector, predictor):
# some intensive computation...
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe_image = clahe.apply(gray)
detections = detector(clahe_image, 1)
for k,d in enumerate(detections):
shape = predictor(clahe_image, d)
for i in range(1,68): #There are 68 landmark points on each face
cv2.circle(frame, (shape.part(i).x, shape.part(i).y), 1, (0,0,255), thickness=2)
return frame, t0
threadn = cv2.getNumberOfCPUs()
pool = ThreadPool(processes = threadn)
pending = deque()
threaded_mode = True
latency = StatValue()
frame_interval = StatValue()
last_frame_time = clock()
while True:
while len(pending) > 0 and pending[0].ready():
res, t0 = pending.popleft().get()
latency.update(clock() - t0)
draw_str(res, (20, 20), "threaded : " + str(threaded_mode))
draw_str(res, (20, 40), "latency : %.1f ms" % (latency.value*1000))
draw_str(res, (20, 60), "frame interval : %.1f ms" % (frame_interval.value*1000))
cv2.imshow('threaded video', res)
if len(pending) < threadn:
ret, frame = cap.read()
t = clock()
frame_interval.update(t - last_frame_time)
last_frame_time = t
if threaded_mode:
task = pool.apply_async(process_frame, (frame.copy(), t, detector, predictor))
else:
task = DummyTask(process_frame(frame, t, detector, predictor))
pending.append(task)
ch = cv2.waitKey(1)
if ch == ord(' '):
threaded_mode = not threaded_mode
if ch == 27:
break
cv2.destroyAllWindows()
すべてのコードを書く時間をとってくれてありがとう:)それは何らかの理由でうまくいきません。私は今週それをダイビングしようとしています。私はちょうどi7-2600K @ @ 3.40Ghzのためにコンピュータを変えました、そして、私はまだパフォーマンスの問題を抱えています!ボトルネックはどこか他の場所だと思います。 **おそらくOSXとDebianのアーキテクチャのどこかにあるのでしょうか?**すべてがOSXでうまく動作するからです!もう一度おねがいします。今週の結果についてお知らせします:) – Simon
コードやパフォーマンスはどういうものですか?コードは実行されていますが、まだ十分に速くないか、コードがまったく実行されていませんか?あなたが私にエラーを与えるなら、私はそれが何であるかを伝えるかもしれません。ありがとう –
コードは実行されますが、期待どおりに実行されません。私はこのファイルにログを記録しました:https://docs.google.com/document/d/1cs9Jpf03EjTPHommMLmJ_84PxoAT3ATtmFlvBEg6l6I/edit?usp=sharingもう一度、まだ深く掘り下げる時間はありませんでした。明日までに再度、感謝します :) – Simon