Python 我可以使用所有的CPU和内核来处理图像吗?
我对用python编码是个新手 所以我有一个做人脸识别的项目,我有20个文件夹,每个文件夹包含4幅图像。但是当我运行代码时,编码过程花费了很多时间 这是我的代码:Python 我可以使用所有的CPU和内核来处理图像吗?,python,Python,我对用python编码是个新手 所以我有一个做人脸识别的项目,我有20个文件夹,每个文件夹包含4幅图像。但是当我运行代码时,编码过程花费了很多时间 这是我的代码: import face_recognition import cv2 import numpy as np import os import glob path = './dataset' folders = [f for f in glob.glob(path + '**/*', recursive=True)] known_
import face_recognition
import cv2
import numpy as np
import os
import glob
path = './dataset'
folders = [f for f in glob.glob(path + '**/*', recursive=True)]
known_face_encodings = []
known_face_names = []
for f in folders:
names = f.split('/')[2]
print('encoding file : {}'.format(names))
for images_f in glob.glob(f + '**/*.jpg'):
images = face_recognition.load_image_file(images_f)
location = face_recognition.face_locations(images)
images_encoding = face_recognition.face_encodings(images, known_face_locations = location)[0]
known_face_encodings.append(images_encoding)
known_face_names.append(names)
我能做些什么来加快编码过程吗?怎么做?提前感谢您可以使用此功能将处理分为不同的线程:
import threading
def face_recognition_function( f ) :
names = f.split('/')[2]
print('encoding file : {}'.format(names))
for images_f in glob.glob(f + '**/*.jpg'):
images = face_recognition.load_image_file(images_f)
location = face_recognition.face_locations(images)
images_encoding = face_recognition.face_encodings(images, known_face_locations = location)[0]
known_face_encodings.append(images_encoding)
known_face_names.append(names)
threads = []
for f in folders:
t = threading.Thread(target=face_recognition_function, args=(f,))
t.start()
threads.append( t )
# threads work now on your images
for t in threads :
t.join() # wait for the completion
当前版本的代码为每个文件夹启动一个新线程。如果这会导致您的计算机内存不足或导致无法预知的结果,您可能希望创建更少的并行线程。但是,对于20个文件夹,我认为这里不会发生任何错误。您可以使用此功能将处理划分为不同的线程:
import threading
def face_recognition_function( f ) :
names = f.split('/')[2]
print('encoding file : {}'.format(names))
for images_f in glob.glob(f + '**/*.jpg'):
images = face_recognition.load_image_file(images_f)
location = face_recognition.face_locations(images)
images_encoding = face_recognition.face_encodings(images, known_face_locations = location)[0]
known_face_encodings.append(images_encoding)
known_face_names.append(names)
threads = []
for f in folders:
t = threading.Thread(target=face_recognition_function, args=(f,))
t.start()
threads.append( t )
# threads work now on your images
for t in threads :
t.join() # wait for the completion
当前版本的代码为每个文件夹启动一个新线程。如果这会导致您的计算机内存不足或导致无法预知的结果,您可能希望创建更少的并行线程。但是,有20个文件夹,我认为这里不会发生任何错误。,我认为您必须澄清程序、IO或CPU计算的界限。如果IO是绑定的,则可以先预加载所有文件或进行批读取。如果是CPU计算,最好改进人脸识别过程。人脸编码扫描你检查你的CPU使用情况,以确定这是问题所在?每个图像大概要拍摄多长时间?也许这就是通常发生的事,谢谢!,我认为您必须澄清程序、IO或CPU计算的界限。如果IO是绑定的,则可以先预加载所有文件或进行批读取。如果是CPU计算,最好改进人脸识别过程。人脸编码扫描你检查你的CPU使用情况,以确定这是问题所在?每个图像大概要拍摄多长时间?也许这是正常情况,谢谢!