cv_bridge opencv4 running slow
I have been able to built the package by adding this two lines into catkinws/src/visionopencv/cvbridge/CMakeLists.txt ``` set(PYTHONNUMPYINCLUDEDIR ~/.local/lib/python3.6/site-packages/numpy/core/include) set(PYTHONINCLUDERPATH /usr/include/python3.6) ``` But when I run my code it seems that cv_bridge is too slow for video streaming real time. Do you know how can I improve it? this is my code:
#!/usr/bin/env python3
import numpy as np
import cv2
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import sys
from cv_bridge import CvBridge bridge = CvBridge()
def cam_test():
cap = cv2.VideoCapture(-1)
pub = rospy.Publisher('chatter', Image, queue_size=1000)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(100)
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.circle(img,(320,240),15,(0,0,255),2)
image_message = bridge.cv2_to_imgmsg(img, encoding="passthrough")
rospy.loginfo(image_message)
pub.publish(image_message)
rate.sleep()
#cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
try:
cam_test()
except rospy.ROSInterruptException:
pass
Asked by subarashi on 2021-07-14 08:32:00 UTC
Answers
Hello subarashi,
For checking the, topic use Rviz: rosrun rviz rviz
and subscribe to chatter topic from rviz.
#!/usr/bin/env python3
import numpy as np
import cv2
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import sys
from cv_bridge import CvBridge
bridge = CvBridge()
def cam_test():
cap = cv2.VideoCapture(-1)
pub = rospy.Publisher('chatter', Image, queue_size=10)
rate = rospy.Rate(10)
while True:
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.circle(img,(320,240),15,(0,0,255),2)
image_message = bridge.cv2_to_imgmsg(img)
pub.publish(image_message)
if __name__ == '__main__':
try:
rospy.init_node('talker', anonymous=True)
cam_test()
except rospy.ROSInterruptException:
pass
Explanation about what was wrong,
You need these lines only if you are printing each frame using imshow.
k = cv2.waitKey(30) & 0xff if k == 27: break
cap.release() cv2.destroyAllWindows()
This should come in a while loop because of each frame you want to be published. In your code nothing will get publish it code will be stuck in a while loop.
image_message = bridge.cv2_to_imgmsg(img, encoding="passthrough") pub.publish(image_message)
If you are not clear or have some questions feel free to drop commet.
Asked by Ranjit Kathiriya on 2021-07-14 10:34:00 UTC
Comments
Hello! Thank you for your answer. I tried what you wrote but it is still too slow. I even modified the queue size to100 and the rospy.rate to 5, and it seems to be better but still has a delay of 3 seconds
Asked by subarashi on 2021-07-15 04:34:22 UTC
Can you please, tell me what hardware and camera are you using?
Asked by Ranjit Kathiriya on 2021-07-15 04:41:34 UTC
Jetson TX2 connected to a simple USB webcam.
Asked by subarashi on 2021-07-15 05:32:52 UTC
I don't know what is the cause of it. It should work and I have tried on my pc it is working fine.
Try out these steps: might be you can able to fix this.
- resize the video frame and publish. link
Try to keep in Gray image so there will be only one channel black and white it can increase the speed.
gray = cv2.cvtColor(frame, cv.COLOR_BGR2GRAY)
I think this could help you, but before that can you please! tell me the shape of the frame which you are getting from your camera as an output.
ret, img = cap.read()
print(img.shape)
Asked by Ranjit Kathiriya on 2021-07-15 06:02:27 UTC
Hi. the shape is (480, 640, 3)
Asked by subarashi on 2021-07-16 00:02:11 UTC
Yes, I am also having the same shape I did not see any delay in my frame. Can you please add time to your code and tell me how much time it shows to process single frame.
start = time.time() # After while loop
print(time.time() - start) # After publishing line
Asked by Ranjit Kathiriya on 2021-07-16 02:56:20 UTC
My Results are:
> rosrun ros_service abc.py
0.05800032615661621
0.001439809799194336
0.0011591911315917969
0.430736780166626
0.033577680587768555
0.03268933296203613
Asked by Ranjit Kathiriya on 2021-07-16 02:57:23 UTC
hello dear @Ranjit sorry for my late reply. Indeed, it works very well if I open rviz in jetson tx2 (ROS_slave) but when I am opening from another computer (ROS_master) there is a latency so I do not know what to do because the jetson tx2 is in a robot so I need to see what the robot is seeing...
Asked by subarashi on 2021-10-10 05:23:01 UTC
Comments