Hi, I'm doing a project including ROS and OpenCV. ROS kinetic is used for the communication between a camera (an external industry camera) and the computer (Ubuntu 16.04 LTS). I follow the guide/tutorial for Face detection with OpenCv and deep learning on https://www.pyimagesearch.com/2018/02/26/face-detection-with-opencv-and-deep-learning/
When I run the python script with the command
python detect_faces.py --image rooster.jpg --prototxt deploy.prototxt.txt --model res10_300x300_ssd_iter_140000.caffemodel
I get this error
[INFO] loading model...
[INFO] computing object detections...
[ INFO:0] Initialize OpenCL runtime...
OpenCV Error: Assertion failed (input.dims == 4 && (input.type() == 5 || input.type() == 6)) in finalize, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/dnn/src/layers/convolution_layer.cpp, line 78
Traceback (most recent call last):
File "detect_faces.py", line 41, in <module>
detections = net.forward()
cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/dnn/src/layers/convolution_layer.cpp:78: error: (-215) input.dims == 4 && (input.type() == 5 || input.type() == 6) in function finalize
The problem ONLY appears when I have this line included in my .bashrc-file in the home directory
# -------- ROS DEPENDENCIES -------------
source /opt/ros/kinetic/setup.bash
The setup.bash-file contains
#!/usr/bin/env bash
# generated from catkin/cmake/templates/setup.bash.in
CATKIN_SHELL=bash
# source setup.sh from same directory as this file
_CATKIN_SETUP_DIR=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)
. "$_CATKIN_SETUP_DIR/setup.sh"
I don't understand these files, it's quite new to me, but as it seems, the problem is that the script detect_faces.py
can't work when OpenCV is installed with ROS... does anyone understand the problem??? problem? What is missing from OpenCV with ROS for this to work? Should I download a new package or add something in the bashrc-file? Or use a an cv bridge?
Just to clearify, I haven't incorporated ROS with this script yet. I wanted to make it to work without reading from the camera, and only read a saved image in a folder to begin with.
.
.
.
.
.
I've found this package here https://github.com/yoshito-n-students/cv_dnn which says "A ROS package containing OpenCV's dnn module that is missing in OpenCV bundled with ROS". How do I do to try this package?
For better understanding, this is the code for detect_faces.py
from the tutorial
# USAGE
# python detect_faces.py --image rooster.jpg --prototxt deploy.prototxt.txt --model res10_300x300_ssd_iter_140000.caffemodel
# import the necessary packages
import numpy as np
import argparse
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-p", "--prototxt", required=True,
help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", required=True,
help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(args["image"])
#image = image[np.newaxis,:,:,:]
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
# pass the blob through the network and obtain the detections and
# predictions
print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()
# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with the
# prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
if confidence > args["confidence"]:
# compute the (x, y)-coordinates of the bounding box for the
# object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the bounding box of the face along with the associated
# probability
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(image, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
# show the output image
cv2.imshow("Output", image)
cv2.waitKey(0)
Thanks in advance!