一段时间后OpenCV Python崩溃

时间:2019-06-28 16:43:45

标签: python opencv libpng

在我的代码中经过一定时间后(取决于PC或OS)崩溃。显示的错误是

libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data 

我的代码在来自网络摄像头的视频上吸引了ROI并拍摄了快照。在5-10分钟后删除ROI(我不确定为什么,这是错误),并且保存的图片没有尺寸,然后显示了上面的错误,但我没有找到错误。说明是,按“ c”停止视频并绘制ROI,再按“ c”拍照,按“ r”恢复视频,如果需要其他图片,请重复。 我在树莓派上的Windows 10和raspbian中测试了代码,时间不一样,但是两次程序都崩溃了。这是我的代码:

import cv2

# python PSI_Camera_test.py


class staticROI(object):
    def __init__(self):
        self.capture = cv2.VideoCapture(0)
        self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
        self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
        # Bounding box reference points and boolean if we are extracting coordinates
        self.image_coordinates = []
        self.extract = False
        self.selected_ROI = False
        self.img_counter = 0
        self.update()

    def update(self):
        while True:
            if self.capture.isOpened():
                # Read frame
                (self.status, self.frame) = self.capture.read()
                cv2.imshow('image', self.frame)
                key = cv2.waitKey(2)

                # Crop image
                if key == ord('c'):
                    self.clone = self.frame.copy()
                    cv2.namedWindow('image')
                    cv2.setMouseCallback('image', self.extract_coordinates)
                    while True:
                        key = cv2.waitKey(2)
                        cv2.imshow('image', self.clone)

                        # Crop and display cropped image
                        if key == ord('c'):
                            self.crop_ROI()
                            img_name = "Images\opencv_frame_{}.png".format(
                                self.img_counter)
                            cv2.imwrite(img_name, self.cropped_image)
                            print("{} written!".format(img_name))
                            self.img_counter += 1

                        # Resume video
                        if key == ord('r'):
                            break
                # Close program with keyboard 'esp'
                if key % 256 == 27:
                    cv2.destroyAllWindows()
                    exit(1)
            else:
                pass

    def extract_coordinates(self, event, x, y, flags, parameters):
        # Record starting (x,y) coordinates on left mouse button click
        if event == cv2.EVENT_LBUTTONDOWN:
            self.image_coordinates = [(x, y)]
            self.extract = True

        # Record ending (x,y) coordintes on left mouse bottom release
        elif event == cv2.EVENT_LBUTTONUP:
            self.image_coordinates.append((x, y))
            self.extract = False

            self.selected_ROI = True

            # Draw rectangle around ROI
            cv2.rectangle(
                self.clone, self.image_coordinates[0], self.image_coordinates[1], (0, 255, 0), 2)

        # Clear drawing boxes on right mouse button click
        elif event == cv2.EVENT_RBUTTONDOWN:
            self.clone = self.frame.copy()
            self.selected_ROI = False

    def crop_ROI(self):
        if self.selected_ROI:
            self.cropped_image = self.frame.copy()

            x1 = self.image_coordinates[0][0]
            y1 = self.image_coordinates[0][1]
            x2 = self.image_coordinates[1][0]
            y2 = self.image_coordinates[1][1]

            self.cropped_image = self.cropped_image[y1:y2, x1:x2]

            print('Cropped image: {} {}'.format(
                self.image_coordinates[0], self.image_coordinates[1]))
        else:
            print('Select ROI to crop before cropping')


# python PSI_Camera_test.py
if __name__ == '__main__':
    static_ROI = staticROI()

谢谢!

0 个答案:

没有答案