如果识别出汽车拍照

时间:2018-07-07 22:40:18

标签: python opencv

针对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()

2 个答案:

答案 0 :(得分:1)

替换:

if rects:

具有:

if rects is not None :

或:

if rects != None :

你会很高兴=)

我的意思是,您仍然无法检测到汽车,但是至少错误会消失。对于汽车检测,我建议使用CNN(卷积神经网络),对于“ YOLO CNN”或“ SSD CNN”使用google -已有许多检测汽车的项目,您可以轻松地为自己找到一个良好的开端

答案 1 :(得分:0)

让我们说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,您会收到错误消息'NoneType' object is not iterable 如果rects = []没有输出,则循环内无任何运行。

基本上,我的意思是,由于您的if rects代码位于通过rects循环的循环中,因此您可以确保rects中包含信息,因为您的代码需要rects才能实现非空迭代。

您可能真正想做的是在循环之前检查if rects。要成为Pythonic,我们会请求宽恕而不是允许:

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

请注意,您的错误与大部分代码无关。确保尝试将问题减少到最小的可重现的示例,该示例具有相同的问题。通常,这样做有助于解决问题。