改进这段代码def face_recognize(i): # converted_results = [] # for result in results: # face align result = results[i] t0 = time.time() ret, align_img = face_landmark5_handle.rockx_face_align(frame, in_img_w, in_img_h, 使得运行with Pool(processes=4) as pool: results = pool.map(face_recognize, numbers)时不报错_pickle.PicklingError: Can't pickle <function face_recognize at 0x7f8f0ca950>: attribute lookup face_recognize on __main__ failed
时间: 2024-03-27 16:39:17 浏览: 130
android_mtcnn_insightface_face_recognize:使用mtcnn检测人脸并使用Insightface检测嵌入器
这个错误是由于使用 multiprocessing.Pool 时,函数 face_recognize 无法被 pickle 序列化导致的。解决方法有两个:
1. 把 face_recognize 函数定义在 main 函数内部,而不是全局作用域内。
2. 使用 multiprocessing.Pool.imap 或 multiprocessing.Pool.imap_unordered 代替 multiprocessing.Pool.map,因为这两个函数不需要对函数进行序列化。例如:
```
with Pool(processes=4) as pool:
results = pool.imap_unordered(face_recognize, numbers)
```
阅读全文