ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Running non-ros python programs with rosrun

asked 2019-08-22 13:50:26 -0500

avi8or gravatar image

updated 2019-08-24 06:42:41 -0500

gvdhoorn gravatar image

I apologize if this isn't the right place for my question. I followed this guide https://www.pyimagesearch.com/2018/02/26/face-detection-with-opencv-and-deep-learning/ for face detection, and can run it using

python detect_faces_video.py --prototxt deploy.prototxt.txt \
    --model res10_300x300_ssd_iter_140000.caffemodel

My goal now is to be able to run the detect_faces_video.py program using rosrun (I've included it at the end of this post). I will list the steps I have taken so far.

I began by creating a workspace named opencv_ros and a src, and in opencv_ros/src running catkin_create_pkg facescan rospy.

These are the files from the original folder I downloaded, and I copied these into opencv_ros/src/facescan/src, followed by running catkin_make at the base of the workspace:

sriram@sriram-ThinkPad-E580:~$ cd deep-learning-face-detection/
sriram@sriram-ThinkPad-E580:~/deep-learning-face-detection$ ls
deploy.prototxt.txt    iron_chic.jpg
detect_faces.py        res10_300x300_ssd_iter_140000.caffemodel
detect_faces_video.py  rooster.jpg

In opencv_ros/src/facescan/src, I ran chmod +x for all the files in the folder, then added #!/usr/bin/env python to the top of the python program.

I then ran another catkin_make, along with source ~/opencv_ros/devel/setup.bash.

This shows my attempt to run the program using rosrun:

sriram@sriram-ThinkPad-E580:~/opencv_ros/src/facescan/src$ workon cv
(cv) sriram@sriram-ThinkPad-E580:~/opencv_ros/src/facescan/src$ rosrun facescan detect_faces_video.py --prototxt deploy.prototxt.txt --model res10_300x300_ssd_iter_140000.caffemodel
from: can't read /var/mail/imutils.video
import-im6.q16: not authorized `np' @ error/constitute.c/WriteImage/1037.
import-im6.q16: not authorized `argparse' @ error/constitute.c/WriteImage/1037.
import-im6.q16: not authorized `imutils' @ error/constitute.c/WriteImage/1037.
import-im6.q16: not authorized `time' @ error/constitute.c/WriteImage/1037.
import-im6.q16: not authorized `cv2' @ error/constitute.c/WriteImage/1037.
/home/sriram/opencv_ros/src/facescan/src/detect_faces_video.py: line 13: syntax error near unexpected token `('
/home/sriram/opencv_ros/src/facescan/src/detect_faces_video.py: line 13: `ap = argparse.ArgumentParser()'

The imports are not authorized, and I feel like I need to change the rights to read | write for these imports, but I am not certain. I could also be completely wrong and need to be using cv_bridge for this. I am using Ubuntu 18.04 with ROS Melodic and opencv 3.4. Any help would be greatly appreciated; I tried learning about rosrun from textbooks, but was not able to find the appropriate solution.

Main script code:

#!/usr/bin/env python
# USAGE
# python detect_faces_video.py --prototxt deploy.prototxt.txt --model res10_300x300_ssd_iter_140000.caffemodel

# import the necessary packages
from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
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"])

# initialize the video ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-08-24 06:40:14 -0500

gvdhoorn gravatar image

There is a good chance that your detect_faces_video.py doesn't have a shebang line. That would make it work when started directly with python .., but rosrun doesn't start a node/script like that.

Verify the first line of detect_faces_video.py is something like #!/usr/bin/env python. If there isn't such a line, that would most likely be your problem.

The imports are not authorized, and I feel like I need to change the rights to read | write for these imports

that has most likely nothing to do with it. Your shell is just using the wrong program to interprete the Python script, leading to all sorts of problems when that interpreter is trying to make sense of your Python statements.

edit flag offensive delete link more

Comments

Thank you! I added #!/usr/bin/env python to the detect_faces_video.py file in the original download folder, not my workspace. I am now able to run with rosrun.

avi8or gravatar image avi8or  ( 2019-08-24 14:05:56 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-08-22 13:50:26 -0500

Seen: 453 times

Last updated: Aug 24 '19