多线程人脸检测opencv

multi-threaded face detection opencv

我有一个用于在照片中进行单线程顺序人脸检测的脚本,以及一个用于裁剪人脸的脚本。如何转换为多线程?这样图像就不是按顺序处理的,而是同时并行处理的。

import os
import cv2
import numpy as np

# Define paths
base_dir = os.path.dirname(__file__)
prototxt_path = os.path.join(base_dir + 'data/deploy.prototxt')
caffemodel_path = os.path.join(base_dir + 'data/weights.caffemodel')

# Read the model
model = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path)

# Create directory 'updated_images' if it does not exist
if not os.path.exists('updated_images'):
print("New directory created")
os.makedirs('updated_images')

# Loop through all images and save images with marked faces
for file in os.listdir(base_dir + 'images'):
file_name, file_extension = os.path.splitext(file)
if (file_extension in ['.png','.jpg']):
print("Image path: {}".format(base_dir + 'images/' + file))

image = cv2.imread(base_dir + 'images/' + file)

(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))

model.setInput(blob)
detections = model.forward()

# Create frame around face
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, show box around face
if (confidence > 0.5):
cv2.rectangle(image, (startX, startY), (endX, endY), (255, 255, 255), 2)

cv2.imwrite(base_dir + 'updated_images/' + file, image)
print("Image " + file + " converted successfully")

我试过把人脸检测和选择推到def里,然后通过pool和map监控并行流,但是我在这方面很弱,明显做错了。脚本刚刚停止工作。

我会这样做:

import os
import cv2
import numpy as np
import threading

base_dir = os.path.dirname(__file__)
prototxt_path = os.path.join(base_dir + 'data/deploy.prototxt')
caffemodel_path = os.path.join(base_dir + 'data/weights.caffemodel')

model = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path)

if not os.path.exists('updated_images'):
    print("New directory created")
    os.makedirs('updated_images')

def process(file, base_dir):
    print("Image path: {}".format(base_dir + 'images/' + file))
    image = cv2.imread(base_dir + 'images/' + file)
    blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
    model.setInput(blob)
    detections = model.forward()
    h, w = image.shape[:2]
    for i in range(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:
            cv2.rectangle(image, (startX, startY), (endX, endY), (255, 255, 255), 2)
    cv2.imwrite(base_dir + 'updated_images/' + file, image)
    print("Image " + file + " converted successfully")

for file in os.listdir(base_dir + 'images'):
    file_name, file_extension = os.path.splitext(file)
    if file_extension in ['.png','.jpg']:
        thread = threading.Thread(target=process, args=(file, base_dir))
        thread.start()

其中大部分与您的代码相同,除了一大块现在在一个函数中。我还冒昧地删除了一些冗余代码,例如如何不需要括号来解压可迭代对象,也不需要括号来执行 if 语句。

由于我没有你在代码中打开的文件,我无法对其进行测试,因此如果有任何问题,可能是我遗漏了什么,如果有的话请随时联系我发生了。