【发布时间】:2021-06-22 01:08:09
【问题描述】:
我有以下功能,
def detect_face(image, return_val):
frame = Image.open('input/' + image + '.jpg')
face_boxes = face_recognition.face_locations(np.array(frame), model='cnn')
if len(face_boxes) > 1:
print("-----2 faces detected in {} image-----".format(image))
return_val.append(None)
return
elif len(face_boxes) == 0:
print("-----No face detected in {} image-----".format(image))
return_val.append(None)
return
top, right, bottom, left = np.squeeze(face_boxes)
frameCropped = frame.crop(box=(left, top, right, bottom + 15))
frame_resized = np.array(frameCropped.resize((224, 224)))
Image.fromarray(frame_resized).save('input/cropped_' + image + '.jpg')
preprocess_image = np.expand_dims(np.array(frame_resized, dtype=np.float64), 0)
preprocess_image = preprocess_input(np.array(preprocess_image), version=1)
fig,ax = plt.subplots(nrows=1, ncols=3, figsize=(5,12))
ax[0].imshow(np.array(frame))
ax[0].axis('off')
ax[1].imshow(np.array(frame_resized))
ax[1].axis('off')
ax[2].imshow(np.array(preprocess_image[0]))
ax[2].axis('off')
return_val.append(preprocess_image)
return preprocess_image
manager = multiprocessing.Manager()
return_val = manager.list()
preprocess_image = Process(target=detect_face, args=('ammar', return_val))
preprocess_image.start()
preprocess_image.join()
print(return_val)
return_val 总是一个空列表 如何使用多处理模块返回我的 numpy 数组 我试图寻找答案,但似乎我做错了什么
【问题讨论】:
-
您的函数显式返回 nothing,并且您的代码也不会从中收到任何内容。为什么你会期望有一个返回值?您是否知道将列表传递给进程,就像
return_val参数/参数一样,只会发送其更改未反映在原始副本中的副本? -
我已经编辑了我的代码以返回 preprocess_image,因为我希望从我的函数 @MisterMiyagi 中返回 numpy 数组
标签: python numpy multiprocessing