问题描述
我正在加载多张图像,这些图像会越过我的脸,但是我很难使图像越过用于创建脸部的正方形。我查看了很多资源,但是由于某些原因,尝试遵循其方法时出现错误。
每次这样做,我都会收到错误消息
ValueError:无法将输入数组从形状(334,334,3)广播到形状(234,234,3)
我认为这些图片可能太大,但是我尝试调整它们的大小以查看它们是否无济于事。
这是我的代码:
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
import os
import random
from timeit import default_timer as timer
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
#log.basicConfig(filename='webcam.log',level=log.INFO)
video_capture = cv2.VideoCapture(0)
anterior = 0
#s_img = cv2.imread("my.jpg")
increment = 0
for filename in os.listdir("Faces/"):
if filename.endswith(".png"):
FullFile = (os.path.join("Faces/",filename))
#ret,frame = video_capture.read()
frame = cv2.imread(FullFile,cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30) )
edges = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,9,9)
for (x,y,w,h) in faces:
roi_color = frame[y:( y ) + ( h ),x:x + w]
status = cv2.imwrite('export/faces_detected'+ str( increment ) +'.png',roi_color)
increment = increment + 1
else:
continue
masks = []
for filename in os.listdir("export/"):
if filename.endswith(".png"):
FullFile = (os.path.join("export/",filename))
s_img = cv2.imread(FullFile)
masks.append(s_img)
Start = timer()
End = timer()
MasksSize = len(masks)
nrand = random.randint(0,MasksSize -1 )
increment = 0
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# Capture frame-by-frame
ret,frame = video_capture.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,30)
)
edges = cv2.adaptiveThreshold(gray,9)
# Draw a rectangle around the faces
for (x,h) in faces:
if (End - Start) > 3:
Start = timer()
End = timer()
nrand = random.randint(0,MasksSize -1 )
# -75 and +20 added to fit my face
cv2.rectangle(frame,(x,y - 75),(x+w,y+h+20),(0,0),2)
s_img = masks[nrand]
increment = increment + 1
#maskresize = cv2.resize(s_img,(150,150))
#frame[y:y+s_img.shape[0],x:x+s_img.shape[1]] = s_img # problem occurs here with
# ValueError: Could not broadcast input array from shape (334,3) into shape (234,3)
# I assume I am inserting somethign too big?
End = timer()
if anterior != len(faces):
anterior = len(faces)
#log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.Now()))
# display the resulting frame
cv2.imshow('Video',frame)
#cv2.imshow('Video',cartoon)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# display the resulting frame
cv2.imshow('Video',frame)
# When everything is done,release the capture
video_capture.release()
cv2.destroyAllWindows()
解决方法
- 在以下行中,
frame[y:y+s_img.shape[0],x:x+s_img.shape[1]] = s_img
您正在尝试将 s_img分配给形状不同的frame [y:y + s_img.shape [0],x:x + s_img.shape [1]] 。 / p>
- 您可以通过打印形状来检查两者的形状(与错误中提到的形状相同)。
- 尝试将 s_img 重塑为相同的形状,然后尝试分配。
我使用此功能调整图像的大小以进行缩放。
def image_resize(image,width = None,height = None,inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h,w) = image.shape[:2]
# if both the width and height are None,then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r),height)
# otherwise,the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width,int(h * r))
# resize the image
resized = cv2.resize(image,dim,interpolation = inter)
# return the resized image
return resized
然后稍后致电
r= image_resize(s_img,height = h,width=w)
frame[y:y+r.shape[0],x:x+r.shape[1]] = r