Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open_cv ros2 node. #5

Open
wants to merge 1 commit into
base: humble
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions open_cv_node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Integrating-Open-cv-with-ROS-2
This package shows an integration of opencv (cv_bridge) with ROS 2 ,also face-dectection using ROS 2.
# OPENCV ROS2 (FACE DETECTION).

This package helps you integrate Opencv with ROS 2 and also face-detection.
Install all dependecies:
```
sudo apt install ros-<ros2-distro>-cv-bridge
```
using the pip command:
```
pip install opencv-python
```
NOTE: python3 has to be installed before this nodes can work.

To run this Node.

Create a workspace and clone the package into your src folder.

Then:
```
ros2 run ros2_cv face_detection
```

Open rviz and visualize the image from your webcam.
```
ros2 run rviz2 rviz2
```
set the image topic to "/webcam"
You will see as Video displayed below:


[Screencast from 03-16-2023 12:54:55 AM.webm](https://user-images.githubusercontent.com/97457075/225474915-bcefc1c0-0e42-40a6-988c-16b32089fa94.webm)

You can integrate this package in simulation and also can be used in a pysical robot with Opencv.



[Screencast from 03-17-2023 02:15:13 AM.webm](https://user-images.githubusercontent.com/97457075/225787575-08a740c3-6f32-426f-b019-a367211019d9.webm)

[Screencast from 03-17-2023 08:20:00 AM.webm](https://user-images.githubusercontent.com/97457075/225844009-1fcbccce-c65d-4a09-836b-0634536294e8.webm)


1 change: 1 addition & 0 deletions open_cv_node/build/.built_by
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
colcon
Empty file.
Empty file.
51 changes: 51 additions & 0 deletions open_cv_node/build/ros2_cv/build/lib/ros2_cv/face_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import rclpy
import cv2
from rclpy.node import Node
from geometry_msgs.msg import Twist #A Twist messgage type to drive the robot.
from sensor_msgs.msg import Image
from cv_bridge import CvBridge,CvBridgeError

#store the cvBridge, which is a type of the cv_bridge package.
#create a class that stores all the object and the methods we will use.
class program(Node):
def __init__(self):
super().__init__('face_detection')
self.pub_camera = self.create_publisher(Image,'/webcam',100) #publish the camera image data to the topic(Note:Make sure the topic name is not the same as your robot topic , so you can use the webcam)
self.image = cv2.VideoCapture(0) #reading from the webcam.
self.face_cascade = cv2.CascadeClassifier('/home/magnum/opencv_ws/src/ros2_cv/ros2_cv/facedetection.xml') #Add the path to your open_cv face detection.xml classifier.
print(self.image.isOpened())
self.store_bridge = CvBridge()


#create a method that executes the code.
def show(self):

if not self.image.isOpened():
print("This camera can't be opened")

while True:
__ret,frame = self.image.read()
self.reading =self.face_cascade.detectMultiScale(frame)
cv2.imshow('frame_name',self.reading)
self.msg = self.store_bridge.cv2_to_imgmsg(self.reading,"bgr8") #This line is very important.
self.pub_camera.publish(self.msg) #publish the message over to ros2.
#cv2.imshow("frame_name",frame)


#if statement that tells what key to press to stop the node.
if cv2.waitKey(20) and 0xFF == ord('q'):
break

self.image.release()
#cv2.destroyAllWindows()


def main(args=None):
rclpy.init(args=args)
store = program() #passing in an object.
# store.show()
rclpy.spin(store)


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import rclpy
import cv2
from rclpy.node import Node
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Image
from cv_bridge import CvBridge,CvBridgeError

#store the cvBridge, which is a type of the cv_bridge package.
#create a class that stores all the object and the methods we will use.
class program(Node):
def __init__(self):
super().__init__('human_tracking')
self.pub_camera = self.create_publisher(Image,'/webcam',100) #publish the camera image data to the topic(Note:Make sure the topic name is not the same as your robot topic , so you can use the webcam)
self.image = cv2.VideoCapture(0) #reading from our webcam.
print(self.image.isOpened())
self.store_bridge = CvBridge()


#create a method that executes the code.
def show(self):

if not self.image.isOpened():
print("This camera can't be opened")

while True:
__ret,frame = self.image.read()
cv2.imshow("frame_name",frame)
msg = self.store_bridge.cv2_to_imgmsg(frame,"bgr8") #This line is very important.
self.pub_camera.publish(msg) #publish the message over to ros2.
# cv2.imshow("frame_name",frame)


#if statement that tells what key to press to stop the node.
if cv2.waitKey(20) and 0xFF == ord('q'):
break

self.image.release()
# cv2.destroyAllWindows()


def main(args=None):
rclpy.init(args=args)
store = program() #passing in an object.
store.show()
rclpy.spin(store)


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions open_cv_node/build/ros2_cv/colcon_build.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# generated from colcon_core/shell/template/command_prefix.sh.em
77 changes: 77 additions & 0 deletions open_cv_node/build/ros2_cv/colcon_command_prefix_setup_py.sh.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
AMENT_PREFIX_PATH=/home/magnum/opencv_ws/install/ros2_cv:/home/magnum/simuate_ws/install/robot:/home/magnum/simuate_ws/install/drive_robot:/opt/ros/foxy
CHROME_DESKTOP=code-url-handler.desktop
CMAKE_PREFIX_PATH=/home/magnum/simuate_ws/install/robot
COLCON=1
COLCON_PREFIX_PATH=/home/magnum/opencv_ws/install:/home/magnum/simuate_ws/install
COLORTERM=truecolor
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
DESKTOP_SESSION=ubuntu
DISPLAY=:1
GDK_BACKEND=x11
GDMSESSION=ubuntu
GIO_LAUNCHED_DESKTOP_FILE_PID=3038
GIT_ASKPASS=/usr/share/code/resources/app/extensions/git/dist/askpass.sh
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_SHELL_SESSION_MODE=ubuntu
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
GTK_MODULES=gail:atk-bridge
HOME=/home/magnum
IM_CONFIG_PHASE=1
INVOCATION_ID=a486f43dbf9d499c98348f9f361804e3
JOURNAL_STREAM=8:52967
LANG=en_US.UTF-8
LANGUAGE=en_NG:en
LC_ALL=en_US.UTF-8
LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/gazebo-11/plugins:/opt/ros/foxy/opt/yaml_cpp_vendor/lib:/opt/ros/foxy/opt/rviz_ogre_vendor/lib:/opt/ros/foxy/lib/x86_64-linux-gnu:/opt/ros/foxy/lib
LESSCLOSE=/usr/bin/lesspipe %s %s
LESSOPEN=| /usr/bin/lesspipe %s
LOGNAME=magnum
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
MANAGERPID=2449
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
NO_AT_BRIDGE=1
OLDPWD=/home/magnum/opencv_ws/src/ros2_cv
ORIGINAL_XDG_CURRENT_DESKTOP=ubuntu:GNOME
PATH=/opt/ros/foxy/bin:/home/magnum/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PWD=/home/magnum/opencv_ws/src/build/ros2_cv
PYTHONPATH=/home/magnum/opencv_ws/install/ros2_cv/lib/python3.8/site-packages:/home/magnum/simuate_ws/install/drive_robot/lib/python3.8/site-packages:/opt/ros/foxy/lib/python/site-packages:/opt/ros/foxy/lib/python3.8/site-packages
QT_ACCESSIBILITY=1
QT_IM_MODULE=ibus
QT_QPA_PLATFORMTHEME=qt5ct
QT_STYLE_OVERRIDE=kvantum
ROS_DISTRO=foxy
ROS_LOCALHOST_ONLY=0
ROS_PYTHON_VERSION=3
ROS_VERSION=2
SESSION_MANAGER=local/robostek:@/tmp/.ICE-unix/2693,unix/robostek:/tmp/.ICE-unix/2693
SHELL=/bin/bash
SHLVL=2
SSH_AGENT_PID=2620
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
TERM=xterm-256color
TERMINATOR_DBUS_NAME=net.tenshu.Terminator23558193cd9818af7fe4d2c2f5bd9d00f
TERMINATOR_DBUS_PATH=/net/tenshu/Terminator2
TERMINATOR_UUID=urn:uuid:235e12da-744e-45c0-a1fd-dbd7bcb002ec
TERM_PROGRAM=vscode
TERM_PROGRAM_VERSION=1.74.2
TURTLEBOT3_MODEL=burger
USER=magnum
USERNAME=magnum
VSCODE_GIT_ASKPASS_EXTRA_ARGS=--ms-enable-electron-run-as-node
VSCODE_GIT_ASKPASS_MAIN=/usr/share/code/resources/app/extensions/git/dist/askpass-main.js
VSCODE_GIT_ASKPASS_NODE=/usr/share/code/code
VSCODE_GIT_IPC_HANDLE=/run/user/1000/vscode-git-9919a58158.sock
VTE_VERSION=6003
WINDOWPATH=2
XAUTHORITY=/run/user/1000/gdm/Xauthority
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
XDG_CURRENT_DESKTOP=Unity
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
XDG_MENU_PREFIX=gnome-
XDG_RUNTIME_DIR=/run/user/1000
XDG_SESSION_CLASS=user
XDG_SESSION_DESKTOP=ubuntu
XDG_SESSION_TYPE=x11
XMODIFIERS=@im=ibus
_=/usr/bin/colcon
17 changes: 17 additions & 0 deletions open_cv_node/build/ros2_cv/install.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv/__init__.py
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv/open_cv_integration.py
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv/face_detection.py
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv/__pycache__/__init__.cpython-38.pyc
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv/__pycache__/open_cv_integration.cpython-38.pyc
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv/__pycache__/face_detection.cpython-38.pyc
/home/magnum/opencv_ws/src/install/ros2_cv/share/ament_index/resource_index/packages/ros2_cv
/home/magnum/opencv_ws/src/install/ros2_cv/share/ros2_cv/package.xml
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv-0.0.0-py3.8.egg-info/SOURCES.txt
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv-0.0.0-py3.8.egg-info/entry_points.txt
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv-0.0.0-py3.8.egg-info/PKG-INFO
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv-0.0.0-py3.8.egg-info/top_level.txt
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv-0.0.0-py3.8.egg-info/zip-safe
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv-0.0.0-py3.8.egg-info/dependency_links.txt
/home/magnum/opencv_ws/src/install/ros2_cv/lib/python3.8/site-packages/ros2_cv-0.0.0-py3.8.egg-info/requires.txt
/home/magnum/opencv_ws/src/install/ros2_cv/lib/ros2_cv/face_detection
/home/magnum/opencv_ws/src/install/ros2_cv/lib/ros2_cv/integration
Binary file not shown.
3 changes: 3 additions & 0 deletions open_cv_node/build/ros2_cv/prefix_override/sitecustomize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sys
sys.real_prefix = sys.prefix
sys.prefix = sys.exec_prefix = '/home/magnum/opencv_ws/src/install/ros2_cv'
10 changes: 10 additions & 0 deletions open_cv_node/build/ros2_cv/ros2_cv.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Metadata-Version: 1.2
Name: ros2-cv
Version: 0.0.0
Summary: TODO: Package description
Home-page: UNKNOWN
Maintainer: magnum
Maintainer-email: [email protected]
License: TODO: License declaration
Description: UNKNOWN
Platform: UNKNOWN
17 changes: 17 additions & 0 deletions open_cv_node/build/ros2_cv/ros2_cv.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package.xml
setup.cfg
setup.py
../build/ros2_cv/ros2_cv.egg-info/PKG-INFO
../build/ros2_cv/ros2_cv.egg-info/SOURCES.txt
../build/ros2_cv/ros2_cv.egg-info/dependency_links.txt
../build/ros2_cv/ros2_cv.egg-info/entry_points.txt
../build/ros2_cv/ros2_cv.egg-info/requires.txt
../build/ros2_cv/ros2_cv.egg-info/top_level.txt
../build/ros2_cv/ros2_cv.egg-info/zip-safe
resource/ros2_cv
ros2_cv/__init__.py
ros2_cv/face_detection.py
ros2_cv/open_cv_integration.py
test/test_copyright.py
test/test_flake8.py
test/test_pep257.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions open_cv_node/build/ros2_cv/ros2_cv.egg-info/entry_points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[console_scripts]
face_detection = ros2_cv.face_detection:main
integration = ros2_cv.open_cv_integration:main

1 change: 1 addition & 0 deletions open_cv_node/build/ros2_cv/ros2_cv.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setuptools
1 change: 1 addition & 0 deletions open_cv_node/build/ros2_cv/ros2_cv.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ros2_cv
1 change: 1 addition & 0 deletions open_cv_node/build/ros2_cv/ros2_cv.egg-info/zip-safe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions open_cv_node/install/.colcon_install_layout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
isolated
Empty file.
Loading