在下面的代码中,我试图使用CV2输出一个单独的人脸(从一张较大的图像中裁剪出来):
def machine_pst():
mlimg = request.files.get("mlimg")
fname = mlimg.filename
filepath = "/home/assets/faces/"
mlimg.save(filepath + fname, overwrite = True)
full_path = filepath + fname
cascPath = "/home/assets/haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(cascPath)
faceSamples=[]
pilImage=Image.open(full_path).convert('L')
imageNp=np.array(pilImage,'uint8')
faces=detector.detectMultiScale(imageNp)
for (x,y,w,h) in faces:
faceSamples.append(imageNp[y:y+h,x:x+w])
img = Image.fromarray(faceSamples[0], 'RGB')
cv2.imwrite("/home/assets/faces/read.png", img)
source = "/static/faces/read.png"
return template("home/machineout", source = source)
使用source作为参数传递到img src="{{source}}
如果我返回一个包含3张脸的图像中的脸部数量,我会获得“3”,所以这似乎很好用,如果我返回faceSamples的任何索引(例如faceSamples [0]),我也会得到数据返回,但是当我尝试将该人脸样本转换为图像时,出现了问题...
img = Image.fromarray(faceSamples[0], 'RGB')
我收到了一个ValueError,说“图像数据不足”
我知道(从之前的答案中得知),detectMultiScale返回的是矩形而不是图像,但是通过我的额外的Numpy代码,这仍然是个问题吗? 我是否仍然没有完全理解faceSamples数组返回的内容? 最后一段代码不能直接将其转换回RGB图像吗?
"/home/.../assets/faces/"
是否是正确的路径。你确定不是..
吗? - Quang Hoangimg = Image.fromarray()
会尝试创建一个 PIL 图像,然后将其传递给cv2.imwrite()
,但是 OpenCV 不支持 PIL 图像 - 它期望的是 Numpy 数组。你已经在faceSamples[0]
中拥有了你想要的 - 一个 OpenCV 可以使用的 Numpy 数组/图像。 - Mark Setchell