I am currently working on a project on NiryoOne which requires facial detection of a person through NiryoOne camera (ELP SONY IMX). The code that I am using works on my laptop running ubuntu. However, I am currently getting an assertion error, which suggests that the file path has not been properly specified, but have tried various file path and none seem to be the solution. Not certain if I am missing some dependencies or packages on NiryoOne. Any help regarding the use of OpenCV would be greatly appreciated.
For both laptop and NiryoOne
Ubuntu: 16.04 Python: 2.7.13 OpenCV: 3.3.1-dev
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import os, sys
import cv2
import numpy as np
import rospy
from std_msgs.msg import Int32
Start = False
rospy.init_node('my_third_node')
pub = rospy.Publisher('camera',Int32,queue_size=10)
#initialise cascade type
face_cascade = cv2.CascadeClassifier('/haarcascade_frontalface_alt2.xml')
# Open the device at the ID 1 (USB)
cap = cv2.VideoCapture(0)
#To set the resolution
#remove cv.cv depeding on version
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
#REPLACE WITH VOICE LATER
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #convert to grey for haarcascade as per documentation
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) #1.5, 5 standard values ; scaling factor increase for higher accuracy
# x,y = x,y coordinates, h,w = height, weight unit pixel
for (x, y, w, h) in faces:
print(x,y,w,h)
pub.publish(x)
pub.publish(y)
pub.publish(w)
pub.publish(h)
#roi = region of interest
roi_gray = gray[y:y+h+35, x:x+w+20]
#roi_color = frame[y:y+h+35, x:x+w+20]
img_item = "saved-img.png"
cv2.imwrite(img_item, roi_gray)
#bounding rectangle
stroke = 2
end_cord_x = x + w
end_cord_y = y + w
#arg 1;2;3: frame to draw, start coord, end coord
cv2.rectangle(frame, (x,y), (end_cord_x,end_cord_y), (255, 0, 0))
# Start == True
# if Start == True:
# cap.release()
# cv2.destroyAllWindows()
cv2.imshow('frame', frame)
#Check whether user selected camera is opened successfully.
if not (cap.isOpened()): print('Could not open video device')
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Display More
Error
niryo@niryo-desktop:~/sepra6$ python camhaar.py OpenCV Error: Assertion failed (!empty()) in detectMultiScale, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/objdetect/src/cascadedetect.cpp, line 1698 Traceback (most recent call last): File "camhaar.py", line 31, in faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) #1.5, 5 standard values ; scaling factor increase for higher accuracy cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/objdetect/src/cascadedetect.cpp:1698: error: (-215) !empty() in function detectMultiScale