Skip to content

Instantly share code, notes, and snippets.

@protortyp
Last active March 17, 2023 09:15
Show Gist options
  • Save protortyp/614e027facb735f9bcf21ffc9e3ca906 to your computer and use it in GitHub Desktop.
Save protortyp/614e027facb735f9bcf21ffc9e3ca906 to your computer and use it in GitHub Desktop.
shit
#!/usr/bin/env python
import rospy
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from PyCapture2 import *
def image_publisher():
rospy.init_node('epiphan_image_publisher', anonymous=True)
pub = rospy.Publisher('image_topic', Image, queue_size=10)
bridge = CvBridge()
# Connect to Epiphan frame grabber
bus = BusManager()
cam = None
numCams = bus.getNumOfCameras()
if not numCams:
rospy.logerr("No camera detected!")
return
for i in range(numCams):
camInfo = bus.getCameraFromIndex(i)
if camInfo.modelName.find("DVI2USB") != -1:
cam = Camera()
cam.connect(camInfo.serialNumber)
break
if not cam:
rospy.logerr("Could not connect to the camera!")
return
# Start capturing video
cam.startCapture()
while not rospy.is_shutdown():
try:
# Get the next image from the camera
image = cam.retrieveBuffer()
# Convert the image to OpenCV format
img_cv = image.convert(PyCapture2.PIXEL_FORMAT.BGR)
# Convert OpenCV image to ROS image message
ros_img = bridge.cv2_to_imgmsg(img_cv.getData(), "bgr8")
ros_img.header.stamp = rospy.Time.now()
# Publish the image
pub.publish(ros_img)
except Exception as e:
rospy.logerr("Error capturing image: %s" % str(e))
break
# Stop capturing and disconnect from camera
cam.stopCapture()
cam.disconnect()
if __name__ == '__main__':
try:
image_publisher()
except rospy.ROSInterruptException:
pass
# Get the height and width of the image
height, width = img.shape[:2]
# Find the center of the image
center_x, center_y = int(width/2), int(height/2)
# Find the coordinates to crop the image
x = center_x - 300
y = center_y - 300
w = 600
h = 600
# Crop the image
crop_img = img[y:y+h, x:x+w]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment