简体   繁体   English

CV2 不保存图像

[英]CV2 is not saving images

The code is supposed to save the roi I have set using the coordinates of detected objects.该代码应该保存我使用检测到的对象的坐标设置的 roi。 No errors were found on this part, but it doesnt save the image.这部分没有发现错误,但它不保存图像。

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

It returns False, which as I have read, means the image failed to save.它返回 False,正如我所读的,这意味着图像无法保存。 Can I get some easily understandable solutions?我能得到一些容易理解的解决方案吗?

A/N: These are just snippets, it is kind of a long project, so just tell me if you need more information about the codes used. A/N:这些只是片段,这是一个漫长的项目,所以如果您需要有关所用代码的更多信息,请告诉我。

Something I always like to do is to test my filepath setup using open() as it throws much more useful errors.我一直喜欢做的事情是使用 open() 测试我的文件路径设置,因为它会引发更多有用的错误。

In your case it throws an invalid path error, which if you start eliminating weird characters is due to the “:##:” in the date/time format在您的情况下,它会引发一个无效的路径错误,如果您开始消除奇怪的字符,这是由于日期/时间格式中的“:##:”

I would recommend switching your format to not contain colons我建议您将格式切换为不包含冒号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM