python - 如何使用我使用多线程来加快速度?
问题描述
下面的代码遍历我的 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)
推荐阅读
- amazon-web-services - 通过 AWS CLI 获取 cloudformation 堆栈的 JSON 模板
- javascript - kendodropdownlist 过滤具有字段的 json 响应
- java - 反转字符串 III java 运行时错误中的单词
- python - MDNavigationDrawer 中的按钮在发布时不会更改屏幕
- ios - 使用 SKProductDiscount 的 NumberFormatter (_unconditionallyBridgeFromObjectiveC) 崩溃
- python-3.x - 在 Tkinter 中重复按下按钮时替换 FigureCanvasTkAgg 内容
- sql - MariaDB 中的程序遇到问题
- javascript - Google Drive API:如何列出共享文件夹中的所有文件?
- html - CSS按类选择输入
- python-3.x - 从字符串中去除不需要的字符