[英]If a car is recognized take a picture
針對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()
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()
更換:
if rects:
有:
if rects is not None :
或搭配:
if rects != None :
而且你會很黃金=)
我的意思是,您仍然無法檢測到汽車,但是至少錯誤會消失。 對於汽車檢測,我建議使用CNN(卷積神經網絡),對於“ YOLO CNN”或“ SSD CNN”使用google -已有許多檢測汽車的項目,您可以輕松地為自己找到一個良好的開端。
假設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
請注意,您的錯誤與大部分代碼無關。 確保嘗試將問題減少到最小的可重現的示例,該示例具有相同的問題。 通常,這樣做有助於解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.