【问题标题】:CV2 is not saving imagesCV2 不保存图像
【发布时间】:2022-07-12 14:34:31
【问题描述】:

代码应该保存我使用检测到的对象的坐标设置的 roi。这部分没有发现错误,但是没有保存图片。

path = "C:\HelmetDetection"

dt = str(datetime.now().strftime("%Y%m%d-%H:%M:%S"))

overlapping = bool()

instance = None

def check_if_overlapping(x1, y1, trc1, blc1, x2, y2, trc2, blc2):
    check_instance(x1, y1, trc1, blc1, x2, y2, trc2, blc2)
    if instance == "ins1":
        global overlapping
        overlapping = True
    else:
        overlapping = False

def save_image(roi):
    status = cv2.imwrite(os.path.join(path, dt + '.jpg'), roi)
    print(status)

def check_instance(x1, y1, trc1, blc1, x2, y2, trc2, blc2):
    global instance
    if x1 < x2 and y1 > y2 and trc1 > trc2 and blc1 < blc2:
        instance = "ins1"

if label == "motorcycle":
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
    cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
    mcoords = []
    mcoords.append((x, y, x + w, y + h))
    if len(mcoords) == 1:
        x1, y1, trc1, blc1 = x, y, x + w, y + h
    else:
        x1, y1, trc1, blc1 = mcoords[0]
if label == "bicycle":
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
    cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
    x1, y1, trc1, blc1 = x, y, x + w, y + h
    bcoords = []
    bcoords.append((x, y, x + w, y + h))
    if len(bcoords) == 1:
        x1, y1, trc1, blc1 = x, y, x + w, y + h
    else:
        x1, y1, trc1, blc1 = bcoords[0]
if label == "person":
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
    cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
    hcoords = []
    hcoords.append((x, y, x + w, y + h))
    if len(hcoords) == 1:
         x2, y2, trc2, blc2 = x, y, x + w, y + h
    else:
         x2, y2, trc2, blc2 = hcoords[0]
if 'x1' and 'y1' and 'trc1' and 'blc1' and 'x2' and 'y2' and 'trc2' and 'blc2' in locals():
    check_if_overlapping(x1, y1, trc1, blc1, x2, y2, trc2, blc2)

!!!
if overlapping == True:
    check_instance()
    if instance == "ins1":
        if (y2 or blc2 or x1 or trc1) > 100: 
            roi = img[y2 - 100:blc2 + 100, x1 - 100:trc1 + 100]
            save_image(roi)
!!!

它返回 False,正如我所读,这意味着图像无法保存。我能得到一些容易理解的解决方案吗?

A/N:这些只是 sn-ps,这是一个漫长的项目,所以如果您需要有关所用代码的更多信息,请告诉我。

【问题讨论】:

    标签: python image save object-detection


    【解决方案1】:

    我一直喜欢做的事情是使用 open() 测试我的文件路径设置,因为它会引发更多有用的错误。

    在您的情况下,它会引发无效路径错误,如果您开始消除奇怪的字符,这是由于日期/时间格式中的“:##:”

    我建议您将格式切换为不包含冒号

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      相关资源
      最近更新 更多