为Python和OpenCv运行此代码。我想要做的是,在我的数据集中保存/测试该工具正在检测的所有汽车的所有图像。使用运行我的代码
python3 car_detection y0d$ python3 build_car_dataset.py -c cars.xml -o dataset/test
因此,当我检测人脸并将矩形放在人脸上时,我创建了一个if函数,即如果人脸被识别并在图像上具有矩形,则请将该人脸的图片保存到我想要的输出。
if rects:
p = os.path.sep.join([args["output"], "{}.png".format(str(total).zfill(5))])
cv2.imwrite(p, orig)
total += 1
所以我得到的错误是:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我应该怎么做?提前谢谢你!
我的完整代码是:
# USAGE
# python3 build_car_dataset.py --cascade haarcascade_frontalface_default.xml --output dataset/test
# python3 build_face_dataset.py -c haarcascade_licence_plate_rus_16stages_original.xml -o dataset/test
#python3 build_face_dataset.py -c haarcascade_licence_plate_rus_16stages_original.xml -o dataset/test
#python3 build_car_dataset.py -c cars.xml -o dataset/test
from imutils.video import VideoStream
import argparse, imutils, time, cv2, os
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--cascade", required=True,
help = "path to where the face cascade resides")
ap.add_argument("-o", "--output", required=True,
help="path to output directory")
args = vars(ap.parse_args())
# load OpenCV's Haar cascade for face detection from disk
detector = cv2.CascadeClassifier(args["cascade"])
# initialize the video stream, allow the camera sensor to warm up and initialize the total number of example faces written to disk thus far
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
# vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
total = 0
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream, clone it, (just in case we want to write it to disk), and then resize the frame
# so we can apply face detection faster
frame = vs.read()
orig = frame.copy()
frame = imutils.resize(frame, width=400)
# detect faces in the grayscale frame
rects = detector.detectMultiScale(
cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30))
# loop over the face detections and draw them on the frame
for (x, y, w, h) in rects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if rects:
p = os.path.sep.join([args["output"], "{}.png".format(str(total).zfill(5))])
cv2.imwrite(p, orig)
total += 1
# show the output frame
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# do a bit of cleanup
print("[INFO] {} face images stored".format(total))
print("[INFO] cleaning up...")
cv2.destroyAllWindows()
vs.stop()
发布于 2018-07-08 13:12:20
替换:
if rects:
通过以下方式:
if rects is not None :
或通过以下方式:
if rects != None :
你将会是金色的=)
我的意思是,你仍然不能检测到汽车,但至少错误会消失。对于汽车检测,我建议使用CNN (卷积神经网络),google的"YOLO CNN“或"SSD CNN”--有很多已经存在的检测汽车的项目,你可能很容易给自己一个好的开始。
发布于 2018-07-08 09:26:10
比方说rects = [[1, 2, 3, 4], [3,4, 5, 6]]
for (x, y, w, h) in rects:
print("I got here:", x, y, w, h)
将打印:
I got here: 1 2 3 4
I got here: 3 4 5 6
但是如果是rects = None
,你会得到错误,如果是rects = []
,你没有得到输出,循环内部也没有运行。
基本上我要说的是,因为你的if rects
代码在一个通过rects
循环的循环中,你已经保证rects
在其中有信息,因为你的代码需要rects
是一个非空的迭代器才能做到这一点。
您可能真正想要做的是在循环遍历if rects
之前检查它。为了更好地理解,我们将请求宽恕而不是许可:
rects = None
try:
for (x, y, w, h) in rects:
print("I got here:", x, y, w, h)
except TypeError:
print("no rects")
# no rects
请注意,您的错误与您的大部分代码没有多大关系。请务必尝试将您的问题减少到具有相同问题的尽可能小的可重现的示例。通常,通过这样做,它有助于解决问题。
https://stackoverflow.com/questions/51227437
复制相似问题