pupil-labs / apriltags

Python bindings for the apriltags3 library
https://pupil-apriltags.readthedocs.io/en/latest/index.html
Other
106 stars 29 forks source link

Segmentation Fault #34

Open mark-belbin opened 3 years ago

mark-belbin commented 3 years ago

I receive a segmentation fault when using this library on a Raspberry Pi 4 and python 3.7. When the entire script exits a seg fault pops up. I've attached the following gdb excerpt:

[Thread 0x9e2fc460 (LWP 4283) exited]
[Thread 0x9f2fd460 (LWP 4282) exited]
[Thread 0xa02fe460 (LWP 4281) exited]
[Thread 0xa12ff460 (LWP 4280) exited]
Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0xaaa33ae8 in quick_decode_uninit ()
   from /home/pi/.local/lib/python3.7/site-packages/pupil_apriltags/lib/libapriltag.so.3
(gdb) backtrace
#0  0xaaa33ae8 in quick_decode_uninit ()
    at /home/pi/.local/lib/python3.7/site-packages/pupil_apriltags/lib/libapriltag.so.3
#1  0xaaa34010 in apriltag_detector_clear_families ()
    at /home/pi/.local/lib/python3.7/site-packages/pupil_apriltags/lib/libapriltag.so.3
(gdb) 

The library works fine on windows with the same code, any ideas what would be causing this on a Pi 4?

aaronglennshepherd commented 3 years ago

Receiving same issue. Has there been a resolution?

Pi4 Python 3.7.3

Segfault appearing in this portion of code: at_detector = Detector(families='tag36h11', nthreads=2, quad_decimate=1.0, quad_sigma=0.0, refine_edges=1, decode_sharpening=0.25, debug=0)

Appears to be slightly more stable when nthreads=2 (skips my image if all tags are detected) and crashes if nthreads=1.

Thanks, Aaron

aaronglennshepherd commented 3 years ago

This is related to either filesize or ratio of resolution.

Attached is some minimum reproducible code. I tried as-is (file size 2028 x 1520 which maybe isn't a standard) and mostly consistently was able to get 10 loops before the segmentation error. I then reduced the resolution to 720x480 and was able to get 1000+ loops without issue.

The image I tested with is here: https://neoconinc-my.sharepoint.com/:i:/g/personal/ashepherd_neoconhfx_com/ETLrI41fyORAswLsMXFJPmYBRIDx9CtF4ZF-K4tqyGcSeQ?e=ph8BwU

`import cv2 from pupil_apriltags import Detector

for z in range(1,50):

image = cv2.imread('3333.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image,(720,480))
tag_id = 0
at_detector = Detector(families='tag36h11',
                       nthreads=1,
                       quad_decimate=1.0,
                       quad_sigma=0.0,
                       refine_edges=1,
                       decode_sharpening=0.25,
                       debug=0)

tags = at_detector.detect(image, estimate_tag_pose=False, camera_params=None, tag_size=None)

# loop over the AprilTag detection results
for r in tags:
    # extract the bounding box (x, y)-coordinates for the AprilTag
    # and convert each of the (x, y)-coordinate pairs to integers
    (ptA, ptB, ptC, ptD) = r.corners
    ptB = (int(ptB[0]), int(ptB[1]))
    ptC = (int(ptC[0]), int(ptC[1]))
    ptD = (int(ptD[0]), int(ptD[1]))
    ptA = (int(ptA[0]), int(ptA[1]))
    # draw the bounding box of the AprilTag detection
    cv2.line(image, ptA, ptB, (0, 255, 0), 2)
    cv2.line(image, ptB, ptC, (0, 255, 0), 2)
    cv2.line(image, ptC, ptD, (0, 255, 0), 2)
    cv2.line(image, ptD, ptA, (0, 255, 0), 2)
    # draw the center (x, y)-coordinates of the AprilTag
    (cX, cY) = (int(r.center[0]), int(r.center[1]))
    # if debugging is True:
    #    print ('tag center location is ' + str(cX) + ', ' + str(cY) + ' and ID # is ' + str(r.tag_id))
    cv2.circle(image, (cX, cY), 5, (0, 0, 255), -1)
    # draw the tag ID on the image
    cv2.putText(image, str(r.tag_id), (ptA[0], ptA[1] - 15),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

# show the image, provide window name first
cv2.imshow('step ' + str(z), image)

# add wait key. window waits until user presses a key
cv2.waitKey(1000)
# and finally destroy/close all open windows
cv2.destroyAllWindows()
print(str(z))

next

`

AbdelNabut commented 3 years ago

I'm on a Raspberry Pi 4 and my solution to fixing this was to optimize my code so as to not run anything unnecessarily in a while loop. For me this was setting at_detector once at the start. I also ran the optimizations mentioned above, but they didn't help me as much as refactoring my code.