python opencv 拍照_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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值