【问题标题】:multi-threaded face detection opencv多线程人脸检测opencv
【发布时间】:2021-07-17 18:05:45
【问题描述】:

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

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监控并行流,但是我这方面很薄弱,显然做错了什么。脚本刚刚停止工作。

【问题讨论】:

    标签: python multithreading opencv face-recognition


    【解决方案1】:

    我会这样做:

    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 语句。

    由于我没有您在代码中打开的文件,因此我无法对其进行测试,因此如果有任何问题,可能是我遗漏了一些东西,所以如果发生这种情况,请随时联系我。

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2016-05-05
      • 2012-02-04
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2012-04-15
      • 2016-09-09
      相关资源
      最近更新 更多