[英]How can use I use multithreading to speed this up?
下面的代碼遍歷我的 HDD 上的文件,這些文件有620,000 幀,我使用 OpenCV 的 DNN 人臉檢測器提取人臉。 它工作正常,但每幀大約需要 1 秒 = 172 小時。 所以我想使用多線程來加快速度,但不知道該怎么做。
注意:我的筆記本電腦上有 4 個 CPU 內核,我的 HDD 的讀寫速度約為 100 MB/s
文件路徑示例:/Volumes/HDD/frames/Fold1_part1/01/0/04541.jpg
frames_path = "/Volumes/HDD/frames"
path_HDD = "/Volumes/HDD/Data"
def filePath(path):
for root, directories, files in os.walk(path, topdown=False):
for file in files:
if (directories == []):
pass
elif (len(directories) > 3):
pass
elif (len(root) == 29):
pass
else:
# Only want the roots with /Volumes/HDD/Data/Fold1_part1/01
for dir in directories:
path_video = os.path.join(root, dir)
for r, d, f in os.walk(path_video, topdown=False):
for fe in f:
fullPath = r[:32]
label = r[-1:]
folds = path_video.replace("/Volumes/HDD/Data/", "")
finalPath = os.path.join(frames_path, folds)
finalImage = os.path.join(finalPath, fe)
fullImagePath = os.path.join(path_video, fe)
try :
if (os.path.exists(finalPath) == False):
os.makedirs(finalPath)
extractFaces(fullImagePath, finalImage)
except OSError as error:
print(error)
sys.exit(0)
def extractFaces(imageTest, savePath):
model = "/Users/yudhiesh/Downloads/deep-learning-face-detection/res10_300x300_ssd_iter_140000.caffemodel"
prototxt = "/Users/yudhiesh/Downloads/deep-learning-face-detection/deploy.prototxt.txt"
net = cv2.dnn.readNet(model, prototxt)
# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(imageTest)
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,(300, 300), (104.0, 177.0, 123.0))
print(f'Current file path {imageTest}')
# pass the blobs through the network and obtain the predictions
print("Computing object detections....")
net.setInput(blob)
detections = net.forward()
# Detect face with highest confidence
for i in range(0, detections.shape[2]):
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
confidence = detections[0, 0, i, 2]
# If confidence > 0.5, save it as a separate file
if (confidence > 0.5):
frame = image[startY:endY, startX:endX]
rect = dlib.rectangle(startX, startY, endX, endY)
image = image[startY:endY, startX:endX]
print(f'Saving image to {savePath}')
cv2.imwrite(savePath, image)
if __name__ == "__main__":
filePath(path_HDD)
設法將每張圖像的時間縮短到 0.09-0.1 秒。 感謝您使用 ProcessPoolExecutor 的建議。
frames_path = "/Volumes/HDD/frames"
path_HDD = "/Volumes/HDD/Data"
def filePath(path):
for root, directories, files in os.walk(path, topdown=False):
for file in files:
if (directories == []):
pass
elif (len(directories) > 3):
pass
elif (len(root) == 29):
pass
else:
# Only want the roots with /Volumes/HDD/Data/Fold1_part1/01
for dir in directories:
path_video = os.path.join(root, dir)
for r, d, f in os.walk(path_video, topdown=False):
for fe in f:
fullPath = r[:32]
label = r[-1:]
folds = path_video.replace("/Volumes/HDD/Data/", "")
finalPath = os.path.join(frames_path, folds)
finalImage = os.path.join(finalPath, fe)
fullImagePath = os.path.join(path_video, fe)
try :
if (os.path.exists(finalPath) == False):
os.makedirs(finalPath)
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(extractFaces(fullImagePath, finalImage))
except OSError as error:
print(error)
sys.exit(0)
def extractFaces(imageTest, savePath):
model = "/Users/yudhiesh/Downloads/deep-learning-face-detection/res10_300x300_ssd_iter_140000.caffemodel"
prototxt = "/Users/yudhiesh/Downloads/deep-learning-face-detection/deploy.prototxt.txt"
net = cv2.dnn.readNet(model, prototxt)
# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(imageTest)
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,(300, 300), (104.0, 177.0, 123.0))
print(f'Current file path {imageTest}')
# pass the blobs through the network and obtain the predictions
print("Computing object detections....")
net.setInput(blob)
detections = net.forward()
# Detect face with highest confidence
for i in range(0, detections.shape[2]):
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
confidence = detections[0, 0, i, 2]
# If confidence > 0.5, save it as a separate file
if (confidence > 0.5):
frame = image[startY:endY, startX:endX]
rect = dlib.rectangle(startX, startY, endX, endY)
image = image[startY:endY, startX:endX]
print(f'Saving image to {savePath}')
cv2.imwrite(savePath, image)
if __name__ == "__main__":
filePath(path_HDD)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.