【发布时间】:2019-06-21 17:01:22
【问题描述】:
在下面的代码中,我尝试使用 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 数组返回的内容吗?这个不能直接用最后一个sn-p的代码转回RGB图吗?
【问题讨论】:
-
我想知道
"/home/.../assets/faces/"是否是正确的路径。你确定不是..? -
啊,对不起,Quang。让我删除那些,所以没有混淆......我的意思是那些点只是作为一个例子。路径一切正常
-
img = Image.fromarray()将尝试制作 PIL 图像,然后将其传递给cv2.imwrite()但 OpenCV 不适用于 PIL 图像 - 它需要 Numpy 数组。faceSamples[0]中已经有了你想要的东西 - OpenCV 可以使用的 Numpy 数组/图像。 -
你摇滚马克!我目前正在从我的图像中查看一张脸!感谢您的所有帮助,如果您想将此作为答案,我会接受它
标签: python arrays numpy opencv cv2