【问题标题】:writing pgm images with cv2.imwrite()用 cv2.imwrite() 写 pgm 图像
【发布时间】:2019-07-27 11:43:51
【问题描述】:

我想在 openCV 中使用 caffe 预训练模型编写检测到的图像,它适用于 jpg 或其他类似格式,但显示错误

SystemError: <built-in function imwrite> returned NULL without setting an error

这是我的代码

import os
import cv2
import numpy
from imutils import paths

# DIR_PATH = os.path.dirname(os.path.realpath('dataset/'))



DIR_PATH = (list(paths.list_images('dataset')))

print(DIR_PATH)

if not os.path.exists('Output'):
    os.makedirs('Output')

MODEL = cv2.dnn.readNetFromCaffe('deploy.prototxt', 'weights.caffemodel')


# print(DIR_PATH)
for file in DIR_PATH:
    filename, file_extension = os.path.splitext(file)

    if (file_extension in ['.png', '.jpg', '.pgm', '.jpeg']):
        image = cv2.imread(file)
        (h, w) = image.shape[:2]

        print("Proccess one started ")
        blob = cv2.dnn.blobFromImage(cv2.resize(
            image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123, 0))

        MODEL.setInput(blob)
        detections = MODEL.forward()  
        print("Proccess Two started ")
        COUNT = 0
        for i in range(0, detections.shape[2]):
            box = detections[0, 0, i, 3:7] * numpy.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")

            confidence = detections[0, 0, i, 2]

            if confidence > 0.165:
                cv2.rectangle(image, (startX, startY), 
                            (endX, endY), (0, 255, 0), 2)
                COUNT = COUNT + 1

        export_name = filename.split("\\")
        print(export_name)

        if file_extension == '.pgm' :
            cv2.imwrite('Output/'+export_name[1]+export_name[2], image, 0)
        else:
            cv2.imwrite('Output/'+export_name[1]+file_extension, image)




        print("Face detection complete for image " +
              file + " (" + str(COUNT) + ") faces found!")

我也检查了我的价值观。 PGM 格式的图像已加载,但检测到人脸,但人脸数量太多,用 cv2.imwrite 根本不写

这是确切的问题

  if file_extension == '.pgm' :
            cv2.imwrite('Output/'+export_name[1]+export_name[2], image, 0)
        else:
            cv2.imwrite('Output/'+export_name[1]+file_extension, image)

【问题讨论】:

    标签: python opencv conv-neural-network caffe pgm


    【解决方案1】:

    OpenCV 不会读取 OS 路径或 PathLib 格式的路径,它会读取一个字符串,因此请将您的代码更改为:

    if file_extension == '.pgm':
                fname = 'Output/{}{}'.format(export_name[1],export_name[2])
                cv2.imwrite(fname, image, 0)
            else:
                fname = 'Output/{}{}'.format(export_name[1],file_extension)
                cv2.imwrite(fname, image)
    

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2023-03-21
      • 2016-08-28
      • 2021-05-29
      相关资源
      最近更新 更多