使用 opencv 进行对象检测后保存元数据

Saving Metadata after object detection with opencv

我对这个主题还很陌生,但想将在我的视频中找到的“对象”计数保存到一个文本文件中,只需将每个帧中对象的原始计数保存下来。我有以下运行良好的代码,但 1) 它不会将对象作为图像保存到我的文件夹中,我不明白这一点,并且 2) 它不会/或对象作为元数据保存在何处。在这方面有什么帮助吗?

代码如下:

import cv2

#############################################
frameWidth = 640
frameHeight = 480
nPlateCascade = cv2.CascadeClassifier("Resources/haarcascades/haarcascade_fullbody.xml")
minArea = 200
color = (255, 0, 255)
###############################################

cap = cv2.VideoCapture("Resources/nyc.mp4")
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 150)
count = 0

while True:
    success, img = cap.read()
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    numberPlates = nPlateCascade.detectMultiScale(imgGray, 1.1, 10)
    for (x, y, w, h) in numberPlates:
        area = w * h
        if area > minArea:
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)
            cv2.putText(img, "Object", (x, y - 5),
                    cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color, 2)
            imgRoi = img[y:y + h, x:x + w]
            cv2.imshow("ROI", imgRoi)

cv2.imshow("Result", img)

if cv2.waitKey(1) and 0xFF == ord('s'):
    cv2.imwrite("Resources/Output/" + str(count) + ".jpg", imgRoi)
    cv2.rectangle(img, (0, 200), (640, 300), (0, 255, 0), cv2.FILLED)
    cv2.putText(img, "Scan Saved", (150, 265), cv2.FONT_HERSHEY_DUPLEX,
                2, (0, 0, 255), 2)
    cv2.imshow("Result", img)
    cv2.waitKey(500)
    count += 1

if cv2.waitKey(1) and 0xFF == ord('s')

这就是问题所在。

and 是一个 logical/boolean 运算符,优先级低于 ==。上面写的是什么意思

cv2.waitKey(1) and (0xFF == ord('s'))

我想你不是那个意思。

您可能需要 & 运算符,它是按位运算符(对整数进行运算)并且优先级高于 ==

cv2.waitKey(1) & 0xFF == ord('s') # is equivalent to
(cv2.waitKey(1) & 0xFF) == ord('s')