Example 3.2 not running

Hello,
I am trying to run example 3.2 and get the following error:

Traceback (most recent call last):
File “/home/user/catkin_ws/src/unit3_excercises/src/example_video.py”, line 84, in
load_video_object.video_detection()
File “/home/user/catkin_ws/src/unit3_excercises/src/example_video.py”, line 38, in video_detection
img_original = cv2.resize(frame,(300,200))
cv2.error: OpenCV(4.2.0) …/modules/imgproc/src/resize.cpp:4045: error: (-215:Assertion failed) !ssize.empty()in function ‘resize’

I am sure the path to the file is correct, as it is copied straight out of the example:
cap = cv2.VideoCapture(’/home/user/catkin_ws/src/opencv_for_robotics_images/Unit_3/Course_images/chris.mp4’)

Could you please assist?

Hello @nivostroff ,

Could you try the below code instead?

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
import numpy as np

class LoadVideo(object):

    def __init__(self):

        self.ctrl_c = False 
        self.bridge_object = CvBridge()

    def shutdownhook(self):

        # works better than the rospy.is_shutdown()
        self.ctrl_c = True

    def video_detection (self,):

        cap = cv2.VideoCapture("/home/user/catkin_ws/src/opencv_for_robotics_images/Unit_3/Course_images/chris.mp4")

        face_cascade = cv2.CascadeClassifier('/home/user/catkin_ws/src/unit3/haar_cascades/frontalface.xml')

        eyes_cascade = cv2.CascadeClassifier('/home/user/catkin_ws/src/unit3/haar_cascades/eye.xml')

        ScaleFactor = 1.2

        minNeighbors = 3

        while not self.ctrl_c:

            ret, frame = cap.read()            

            try:

                img_original = cv2.resize(frame,(300,200))
                img = cv2.resize(frame,(300,200))     

            except cv2.error as e:

                pass           

            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)        

            faces = face_cascade.detectMultiScale(gray, ScaleFactor, minNeighbors)

            for (x,y,w,h) in faces:

                cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)  

                roi = img[y:y+h, x:x+w]

                eyes = eyes_cascade.detectMultiScale(roi)

                for (ex,ey,ew,eh) in eyes:

                    cv2.rectangle(roi, (ex,ey),(ex+ew,ey+eh),(0,255,0),2)

            cv2.imshow('Face_original',img_original)

            cv2.imshow('Face',img)

            cv2.waitKey(1)

        cap.release()

if __name__ == '__main__':

    rospy.init_node('load_video_node', anonymous=True)

    load_video_object = LoadVideo()

    try:

        load_video_object.video_detection()

        rospy.oncespin()

    except rospy.ROSInterruptException:

        pass

    cv2.destroyAllWindows()