【问题标题】:Saving Metadata after object detection with opencv使用opencv进行对象检测后保存元数据
【发布时间】:2021-03-17 05:45:21
【问题描述】:

我对这个主题相当陌生,但想保护在我的视频中找到的“对象”计数,只需将每帧中对象的原始计数保存到文本文件中。我有以下运行良好的代码,但是 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

【问题讨论】:

    标签: python opencv computer-vision object-detection


    【解决方案1】:

    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')
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2021-08-29
      • 2013-05-05
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2010-12-03
      相关资源
      最近更新 更多