Ask Your Question

Revision history [back]

Facing assertion error

here is the error i'm facing OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp, line 10638 Traceback (most recent call last): File "facedatasets.py", line 23, in <module> gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY) cv2.error: /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor

Here is the code i'm trying to run

Import OpenCV2 for image processing

import cv2

Start capturing video

vid_cam = cv2.VideoCapture(0)

Detect object in video stream using Haarcascade Frontal Face

face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

For each person, one face id

face_id = 5

Initialize sample face image

count = 0

Start looping

while(True):

# Capture video frame
_, image_frame = vid_cam.read()

# Convert frame to grayscale
gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY)

# Detect frames of different sizes, list of faces rectangles
faces = face_detector.detectMultiScale(gray, 1.3, 5)

# Loops for each faces
for (x,y,w,h) in faces:

    # Crop the image frame into rectangle
    cv2.rectangle(image_frame, (x,y), (x+w,y+h), (255,0,0), 2)

    # Increment sample face image
    count += 1

    # Save the captured image into the datasets folder
    cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])

    # Display the video frame, with bounded rectangle on the person's face
    cv2.imshow('frame', image_frame)

# To stop taking video, press 'q' for at least 100ms
if cv2.waitKey(100) & 0xFF == ord('q'):
    break

# If image taken reach 100, stop taking video
elif count>100:
    break

Stop video

vid_cam.release()

Close all started windows

cv2.destroyAllWindows()

Facing assertion error

here is the error i'm facing OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp, line 10638 Traceback (most recent call last): File "facedatasets.py", line 23, in <module> gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY) cv2.error: /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor

Here is the code i'm trying to run

# Import OpenCV2 for image processing

processing import cv2

cv2 # Start capturing video

video vid_cam = cv2.VideoCapture(0)

cv2.VideoCapture(0) # Detect object in video stream using Haarcascade Frontal Face

Face face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # For each person, one face id

id face_id = 5

5 # Initialize sample face image

image count = 0

0 # Start looping

while(True):

looping
while(True):
# Capture video frame
 _, image_frame = vid_cam.read()
 # Convert frame to grayscale
 gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY)
 # Detect frames of different sizes, list of faces rectangles
 faces = face_detector.detectMultiScale(gray, 1.3, 5)
 # Loops for each faces
 for (x,y,w,h) in faces:
 # Crop the image frame into rectangle
  cv2.rectangle(image_frame, (x,y), (x+w,y+h), (255,0,0), 2)
 # Increment sample face image
 count += 1
 # Save the captured image into the datasets folder
  cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])
 # Display the video frame, with bounded rectangle on the person's face
 cv2.imshow('frame', image_frame)
 # To stop taking video, press 'q' for at least 100ms
 if cv2.waitKey(100) & 0xFF == ord('q'):
 break
 # If image taken reach 100, stop taking video
 elif count>100:
 break

# Stop video

vid_cam.release()

video vid_cam.release() # Close all started windows

cv2.destroyAllWindows()

windows cv2.destroyAllWindows()

Facing assertion error

here is the error i'm facing OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp, line 10638 Traceback (most recent call last): File "facedatasets.py", line 23, in <module> gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY) cv2.error: /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor

Here is the code i'm trying to run

# Import OpenCV2 for image processing
import cv2

# Start capturing video 
vid_cam = cv2.VideoCapture(0)

# Detect object in video stream using Haarcascade Frontal Face
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# For each person, one face id
face_id = 5

# Initialize sample face image
count = 0

# Start looping
while(True):

    # Capture video frame
    _, image_frame = vid_cam.read()

    # Convert frame to grayscale
    gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY)

    # Detect frames of different sizes, list of faces rectangles
    faces = face_detector.detectMultiScale(gray, 1.3, 5)

    # Loops for each faces
    for (x,y,w,h) in faces:

        # Crop the image frame into rectangle
        cv2.rectangle(image_frame, (x,y), (x+w,y+h), (255,0,0), 2)

        # Increment sample face image
        count += 1

        # Save the captured image into the datasets folder
        cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])

        # Display the video frame, with bounded rectangle on the person's face
        cv2.imshow('frame', image_frame)

    # To stop taking video, press 'q' for at least 100ms
    if cv2.waitKey(100) & 0xFF == ord('q'):
        break

    # If image taken reach 100, stop taking video
    elif count>100:
        break

# Stop video
vid_cam.release()

# Close all started windows
cv2.destroyAllWindows()