|
11 | 11 | from cv_bridge import CvBridge, CvBridgeError
|
12 | 12 | import numpy as np
|
13 | 13 |
|
14 |
| -def write_buffer(filename, buffer): |
15 |
| - t = time.time() |
16 |
| - np.save(filename, buffer) |
17 |
| - #print("Saved in {}".format(time.time()-t), filename) |
18 |
| - |
19 | 14 | def main(args):
|
| 15 | + |
20 | 16 | rospy.init_node("camera", anonymous=True)
|
21 |
| - image_pub = rospy.Publisher("image",Image, queue_size=10) |
22 |
| - |
23 |
| - bridge = CvBridge() |
24 |
| - |
| 17 | + |
| 18 | + # Need to use the V4L2 backend on Linux to get the right format |
25 | 19 | cap = cv2.VideoCapture(0 + cv2.CAP_V4L2)
|
26 |
| - cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc(*"Y16 ")) |
27 |
| - cap.set(cv2.CAP_PROP_CONVERT_RGB, False) |
| 20 | + |
| 21 | + # Set fourcc code to Y16 and disable RGB conversion |
| 22 | + capture_raw = rospy.get_param('~raw_video', default_value=False) |
28 | 23 |
|
29 |
| - buffer_size = 100 |
30 |
| - buffer = np.zeros((buffer_size,512,640), dtype='uint16') |
| 24 | + if capture_raw: |
| 25 | + rospy.info("Raw (Y16) capture enabled.") |
| 26 | + cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc(*"Y16 ")) |
| 27 | + cap.set(cv2.CAP_PROP_CONVERT_RGB, False) |
| 28 | + else: |
| 29 | + rospy.info("RGB24 capture enabled.") |
31 | 30 |
|
32 |
| - i = 0 |
33 |
| - j = 0 |
| 31 | + queue_size = rospy.get_param('~queue_size', default_value=10) |
| 32 | + image_pub = rospy.Publisher("image",Image, queue_size=queue_size) |
34 | 33 |
|
35 |
| - while not rospy.is_shutdown(): |
36 |
| - t = time.time() |
37 |
| - res, image = cap.read() |
38 |
| - if res: |
39 |
| - image_pub.publish(bridge.cv2_to_imgmsg(image, encoding="passthrough")) |
40 |
| - #cv2.imwrite("/boson_ws/captures/image_{}.tiff".format(i), image) |
| 34 | + bridge = CvBridge() |
| 35 | + frame_count = 0 |
| 36 | + prev_capture = time.time() |
41 | 37 |
|
42 |
| - buffer[i] = image |
43 |
| - i += 1 |
| 38 | + while not rospy.is_shutdown(): |
44 | 39 |
|
45 |
| - if i == buffer_size: |
46 |
| - #print("Writing") |
47 |
| - #filename = "/boson_ws/captures/buffer_{}".format(j) |
48 |
| - #np.save(filename, buffer) |
49 |
| - #buffer, buffer_double = buffer_double, buffer |
50 |
| - i = 0 |
51 |
| - j += 1 |
| 40 | + capture_success, image = cap.read() |
52 | 41 |
|
53 |
| - #print "FPS", 1.0/(time.time()-t) |
| 42 | + if capture_success: |
| 43 | + image_pub.publish(bridge.cv2_to_imgmsg(image, encoding="passthrough")) |
| 44 | + frame_count += 1 |
| 45 | + rospy.logdebug("Frame %d", frame_count) |
| 46 | + else: |
| 47 | + rospy.logwarn(5, "Image capture failed") |
54 | 48 |
|
| 49 | + rospy.logdebug("FPS: %f", 1.0/(time.time()-prev_capture)) |
| 50 | + prev_capture = time.time() |
55 | 51 |
|
56 | 52 | cap.release()
|
57 | 53 |
|
|
0 commit comments