1

I'm trying to display video stream through an IP webcam.

import cv2
import sys
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture('http://192.168.43.1:8080/video')
while True:
# Capture frame-by-frame
  ret, frame = video_capture.read()

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

  faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.2,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.cv.CV_HAAR_SCALE_IMAGE
  )

# Draw a rectangle around the faces
  for (x, y, w, h) in faces:
      cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the resulting frame
  cv2.imshow('Video', frame)

  if cv2.waitKey(1) & 0xFF == ord('q'):
      break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

This code runs perfectly fine on the PC but when I run it on Raspberry it shows the following error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp, line 3737
Traceback (most recent call last):
  File "face_vid_detect.py", line 26, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

Could anyone tell me why this is happening?

  • Instead of editing the question it would have been better if it was answered. – NEO Dec 4 '16 at 13:55
1

This error means there is an empty frame and you cannot convert an empty frame to gray. So the your webcam is not able to start. Look for a proper way to run a webcam in Raspberry Pi. In general if you add cv2.VideoCapture(0) in the beginning, by default it should take your webcam as capturing device.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.