首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果识别出一辆车,就拍一张照片

如果识别出一辆车,就拍一张照片
EN

Stack Overflow用户
提问于 2018-07-08 06:40:18
回答 2查看 372关注 0票数 0

为Python和OpenCv运行此代码。我想要做的是,在我的数据集中保存/测试该工具正在检测的所有汽车的所有图像。使用运行我的代码

代码语言:javascript
复制
python3 car_detection y0d$ python3 build_car_dataset.py -c cars.xml -o dataset/test

因此,当我检测人脸并将矩形放在人脸上时,我创建了一个if函数,即如果人脸被识别并在图像上具有矩形,则请将该人脸的图片保存到我想要的输出。

代码语言:javascript
复制
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()我应该怎么做?提前谢谢你!

我的完整代码是:

代码语言:javascript
复制
# 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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-08 13:12:20

替换:

代码语言:javascript
复制
if rects:

通过以下方式:

代码语言:javascript
复制
if rects is not None :

或通过以下方式:

代码语言:javascript
复制
if rects != None :

你将会是金色的=)

我的意思是,你仍然不能检测到汽车,但至少错误会消失。对于汽车检测,我建议使用CNN (卷积神经网络),google的"YOLO CNN“或"SSD CNN”--有很多已经存在的检测汽车的项目,你可能很容易给自己一个好的开始。

票数 1
EN

Stack Overflow用户

发布于 2018-07-08 09:26:10

比方说rects = [[1, 2, 3, 4], [3,4, 5, 6]]

代码语言:javascript
复制
for (x, y, w, h) in rects:
    print("I got here:", x, y, w, h)

将打印:

代码语言:javascript
复制
I got here: 1 2 3 4
I got here: 3 4 5 6

但是如果是rects = None,你会得到错误,如果是rects = [],你没有得到输出,循环内部也没有运行。

基本上我要说的是,因为你的if rects代码在一个通过rects循环的循环中,你已经保证rects在其中有信息,因为你的代码需要rects是一个非空的迭代器才能做到这一点。

您可能真正想要做的是在循环遍历if rects之前检查它。为了更好地理解,我们将请求宽恕而不是许可:

代码语言:javascript
复制
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

请注意,您的错误与您的大部分代码没有多大关系。请务必尝试将您的问题减少到具有相同问题的尽可能小的可重现的示例。通常,通过这样做,它有助于解决问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51227437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档