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

2024-10-01 19:17:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这个主题还不太熟悉,但我想确保在我的视频中找到的“对象”的计数是安全的,只是将每个帧中对象的原始计数保存到一个文本文件中。我有以下运行良好的代码,但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

Tags: 对象代码imgcountcv2计数capresources
1条回答
网友
1楼 · 发布于 2024-10-01 19:17:03

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

这就是问题所在

and是逻辑/布尔运算符,其优先级低于==。上面写的是什么意思

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

我想你不是那个意思

您可能需要&运算符,它是一个位运算符(对整数进行操作),并且比==具有更高的优先级

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

相关问题 更多 >

    热门问题