0
votes

I am getting an error in this code

def pre_processing(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = cv2.equalizeHist(img)
    img = img / 255
    return img
    
while True:
    success, img_original = cap.read()
    img = np.asarray(img_original)
    img = cv2.resize(img, (32, 32))
    img = pre_processing(img)
    cv2.imshow("Processsed Image", img)
    img = img.reshape(1, 32, 32, 1)

I get this error:

Traceback (most recent call last): File "C:/Users/PycharmProjects/test.py", line 27, in img = cv2.resize(img,(32,32)) TypeError: Expected Ptr<cv::UMat> for argument 'src'

1

1 Answers

0
votes

i dont know whats wrong with this coding

  • You need to analyze your code step-by-step

  • The first line of your code:

    • while True
      
    • I would suggest use cap.isOpened() where cap variable is your VideoCapture class object.

    • If your video won't open, then your code won't crash. Otherwise your code will throw error.

  • The second line of your code:

    • success, imgOriginal = cap.read()
      
    • Use success variable to check whether the current frame is returned.

    • if success:
          .
          .
      
  • The third line of your code:

    • img = np.asarray(img_original)
      
    • Use copy when you want to keep the original value.

    • img = img_original.copy()
      
  • The fourth line of your code:

    • img = pre_processing(img)  
      
    • You are applying histogram equalization and then normalizing the image.

    • If you normalize the image by img = img / 255, cv2.imshow will throw an error. Therefore you need to remove the img = img / 255 line.

  • The fifth line of your code:

    • cv2.imshow("Processsed Image", img)
      
    • You are trying to display the image, therefore you also need to use waitKey. The waitKey method takes delay as an integer. So you could do:

    • cv2.imshow("Processsed Image", img)
      cv2.waitKey(1)
      
    • But if you want to close during the display you could do:

      • key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
        
      • If you press q the code will terminate.

  • The last line of your code:

    • img = img.reshape(1, 32, 32, 1)
      
    • I assume you are trying to design your code for dataset, but you should reshape after the display.

  • Always release the VideoCapture after exit.


Code:


import cv2


def pre_processing(img_):
    img_ = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
    img_ = cv2.equalizeHist(img_)
    # img_ = img_ / 255
    return img_


if __name__ == '__main__':
    # Initialize VideoCature object for Webcam
    cap = cv2.VideoCapture(0)

    # While webcam opens
    while cap.isOpened():

        # Get the current frame
        success, imgOriginal = cap.read()

        # If the current frame returns successfully
        if success:

            # Copy the original-image
            img = imgOriginal.copy()

            # Resize for 32 x 32
            img = cv2.resize(img, (32, 32))

            # Convert to gray-scale and apply histogram equalization
            img = pre_processing(img)

            # Display the frame
            cv2.imshow("Processsed Image", img)
            key = cv2.waitKey(1) & 0xFF

            # If `q` is pressed then exit
            if key == ord("q"):
                break
            
    # Reshape
    img = img.reshape(1, 32, 32, 1)