Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenCV - Segmentation fault
#1
Hey All,

I'm getting this error "Segmentation fault" and i'm guessing i'm trying to access something that doesnt exist but i can't find what. When i run this script on windows it doesnt give the error but does on ubuntu. Any ideas?

Code:
#
# 15/09/2019 Commented out cv2 and imshow
#
#

import numpy as np
import cv2
import sys
from openalpr import Alpr
import requests
import datetime

HA_ENDPOINT = 'http://192.168.1.68:8123/api/services/script/turn_on'
PLATES = ['TY19XVH']


RTSP_SOURCE  = 'rtsp://192.168.0.60/mpeg4'
WINDOW_NAME  = 'openalpr'
FRAME_SKIP   = 10


def open_cam_rtsp(uri):
    return cv2.VideoCapture(uri)

def main():
    alpr = Alpr('gb', '/srv/openalpr/openalpr.conf', '/srv/openalpr/runtime_data')
    if not alpr.is_loaded():
        print('Error loading OpenALPR')
        sys.exit(1)
    alpr.set_top_n(3)
    #alpr.set_default_region('new')

    cap = open_cam_rtsp(RTSP_SOURCE)
    if not cap.isOpened():
        alpr.unload()
        sys.exit('Failed to open video file!')
    #cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_AUTOSIZE)
    #cv2.setWindowTitle(WINDOW_NAME, 'OpenALPR video test')

    _frame_number = 0
    #declare a stamp 10 minutes ago, initialise variable when script starts
    STAMP = datetime.datetime.now() - datetime.timedelta(minutes=10)
    while True:
        ret_val, frame = cap.read()
        if not ret_val:
            print('VidepCapture.read() failed. Exiting...')
            break

        _frame_number += 1
        if _frame_number % FRAME_SKIP != 0:
            continue
        #cv2.imshow(WINDOW_NAME, frame)
        ret, enc = cv2.imencode("*.jpg", frame)
        results = alpr.recognize_array(enc.tobytes())

        #results = alpr.recognize_array(frame)
        for i, plate in enumerate(results['results']):
            best_candidate = plate['candidates'][0]
            print('Plate #{}: {:7s} ({:.2f}%)'.format(i, best_candidate['plate'].upper(), best_candidate['confidence']))
            
            #Does the plate match known plates
            if best_candidate['plate'].upper() in PLATES:

                #Has the gate fired recently? If not in the last 10 minutes then allow to fire again
                if datetime.datetime.now() > STAMP:
                    print("Recognised")
                    #If a plate is recongised set a timestamp to prevent it firing lots of times until time has expired
                    print(datetime.datetime.now())

                    #Open the gate
                    response = requests.post(
                        HA_ENDPOINT,
                        headers={'Content-Type': 'application/json', 'x-ha-access': 'w1ll1ams!' },
                        data='{"entity_id": "script.ANPR"}',
                    )
                    STAMP = STAMP = datetime.datetime.now() + datetime.timedelta(minutes=2)
                else:
                    print("Not firing as time has not exceeded")

        if cv2.waitKey(1) == 27:
            break

    cv2.destroyAllWindows()
    cap.release()
    alpr.unload()


if __name__ == "__main__":
    main()
Reply
#2
I'll add that the script runs fine, until a car number plate appears, it seems to take two readings and then fails with segmentation fault.
Reply
#3
Please show exact error traceback verbatim.
It contains valuable information, like where failing.
Reply
#4
Hi Larz60 sorry i'm not familiar with python so i'm hoping this is right;


Output:
Starting program: /usr/bin/python3 readstream.py [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [New Thread 0x7ffff444d700 (LWP 129)] [Thread 0x7ffff444d700 (LWP 129) exited] [New Thread 0x7ffff444d700 (LWP 132)] [New Thread 0x7fffc7382700 (LWP 133)] Thread 1 "python3" received signal SIGSEGV, Segmentation fault. 0x00007fffe37f5568 in tesseract::LTRResultIterator::WordFontAttributes(bool*, bool*, bool*, bool*, bool*, bool*, int*, int*) const () from /usr/lib/x86_64-linux-gnu/libtesseract.so.4 (gdb) backtrace #0 0x00007fffe37f5568 in tesseract::LTRResultIterator::WordFontAttributes(bool*, bool*, bool*, bool*, bool*, bool*, int*, int*) const () from /usr/lib/x86_64-linux-gnu/libtesseract.so.4 #1 0x00007fffe40615e2 in alpr::TesseractOcr::recognize_line(int, alpr::PipelineData*) () from /usr/lib/libopenalpr.so.2 #2 0x00007fffe4062f91 in alpr::OCR::performOCR(alpr::PipelineData*) () from /usr/lib/libopenalpr.so.2 #3 0x00007fffe402591a in alpr::AlprImpl::analyzeSingleCountry(cv::Mat, cv::Mat, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >) () from /usr/lib/libopenalpr.so.2 #4 0x00007fffe40243e0 in alpr::AlprImpl::recognizeFullDetails(cv::Mat, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >) () from /usr/lib/libopenalpr.so.2 #5 0x00007fffe4026dd7 in alpr::AlprImpl::recognize(cv::Mat, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >) () from /usr/lib/libopenalpr.so.2 #6 0x00007fffe4026cbc in alpr::AlprImpl::recognize(cv::Mat) () from /usr/lib/libopenalpr.so.2 #7 0x00007fffe40263ef in alpr::AlprImpl::recognize(std::vector<char, std::allocator<char> >) () from /usr/lib/libopenalpr.so.2 #8 0x00007fffe401ea6c in alpr::Alpr::recognize(std::vector<char, std::allocator<char> >) () from /usr/lib/libopenalpr.so.2 #9 0x00007fffe437719f in recognizeArray () from /usr/lib/libopenalprpy.so #10 0x00007ffff1402dae in ffi_call_unix64 () from /usr/lib/x86_64-linux-gnu/libffi.so.6 #11 0x00007ffff140271f in ffi_call () from /usr/lib/x86_64-linux-gnu/libffi.so.6 #12 0x00007ffff16165d4 in _ctypes_callproc () from /usr/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so #13 0x00007ffff1616d64 in ?? () from /usr/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so #14 0x00000000005a95fc in _PyObject_FastCallKeywords () #15 0x0000000000509ad3 in ?? () #16 0x000000000050b4a9 in _PyEval_EvalFrameDefault () #17 0x0000000000508c69 in ?? () #18 0x000000000050999d in ?? () #19 0x000000000050b4a9 in _PyEval_EvalFrameDefault () #20 0x0000000000508c69 in ?? () #21 0x000000000050999d in ?? () #22 0x000000000050b4a9 in _PyEval_EvalFrameDefault () #23 0x0000000000507125 in ?? () #24 0x000000000050a3b3 in PyEval_EvalCode () #25 0x00000000006349e2 in ?? () #26 0x0000000000634a97 in PyRun_FileExFlags () #27 0x000000000063824f in PyRun_SimpleFileExFlags () #28 0x0000000000638df1 in Py_Main () #29 0x00000000004b0de0 in main () (gdb)

I have also added a fault handler and the output is this;

Output:
# python3 -Xfaulthandler readstream.py Plate #0: 0909E (81.66%) Plate #0: 0909E (81.66%) Fatal Python error: Segmentation fault Current thread 0x00007f3ad58e1740 (most recent call first): File "/usr/local/lib/python3.6/dist-packages/openalpr/openalpr.py", line 174 in recognize_array File "readstream.py", line 54 in main File "readstream.py", line 89 in <module> Segmentation fault
Not quite sure what the issue is though, it's always seem to process two plates before faulting.
Reply
#5
Looks like its failing on the following command...

results = alpr.recognize_array(enc.tobytes())

is it worth checking what is returned by cv2.imencode on line 53...

enc = cv2.imencode("*.jpg", frame)
Reply
#6
didn't really expose much, I removed that line and went with ndarray;

#cv2.imshow(WINDOW_NAME, frame)
#ret, enc = cv2.imencode("*.jpg", frame)
results = alpr.recognize_ndarray(frame)
print(results)
this is the output

Output:
root@a6d9d4883914:/anpr# python3 readstream.py {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751373946, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 109.541199, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751374089, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 124.764801, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751374244, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 80.463402, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751374989, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 84.739304, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751375821, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 68.119301, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751376661, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 91.671898, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751377485, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 86.435303, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751378316, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 74.5457, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751379148, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 85.642502, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751379980, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 76.310799, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751380813, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 95.191399, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751381644, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 109.487602, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751382476, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 105.507004, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751383305, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 108.853699, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751384140, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 118.1707, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751384973, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 106.439697, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751385804, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 79.0336, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} {'version': 2, 'data_type': 'alpr_results', 'epoch_time': 1568751386636, 'img_width': 1280, 'img_height': 720, 'processing_time_ms': 105.540497, 'regions_of_interest': [{'x': 0, 'y': 0, 'width': 1280, 'height': 720}], 'results': []} Fatal Python error: Segmentation fault Current thread 0x00007f76c0bcc740 (most recent call first): File "/usr/local/lib/python3.6/dist-packages/openalpr/openalpr.py", line 192 in recognize_ndarray File "readstream.py", line 54 in main File "readstream.py", line 88 in <module> Segmentation fault
Reply
#7
I've been out all day, and just getting around to answering posts.

you are correct as to line 54 being the last statement executed.
I can't say what actually caused the error, and this may be a difficult one to find.
I have seen this type of error in 'C' code when a pointer was corrupted, and registers got mucked up a result.
perhaps send the link of this thread to the author [email protected] and ask for his thoughts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  make: *** [Makefile:29: all] Segmentation fault Anldra12 2 1,814 May-01-2022, 06:17 PM
Last Post: Anldra12
  Segmentation fault (core dumped) hobbyist 1 10,352 Jun-07-2021, 12:56 PM
Last Post: supuflounder
  Segmentation fault with large files kusal1 3 2,695 Oct-01-2019, 07:32 AM
Last Post: Gribouillis
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 2,797 Mar-21-2019, 08:54 PM
Last Post: mmoelle1
  Segmentation fault when connecting to modbus device with Libmodbus alice 0 2,418 Dec-18-2018, 04:03 PM
Last Post: alice
  Debugging a seg fault tidymax 1 2,822 Oct-31-2018, 02:58 PM
Last Post: ichabod801
  calling python function in c++ callback getting segmentation fault error Jotirling 3 7,123 Oct-26-2017, 08:55 AM
Last Post: Larz60+
  Compiler fault or some kind of weird referencing bug? Joseph_f2 11 9,064 May-09-2017, 08:50 PM
Last Post: volcano63
  Thai Text Segmentation Module draems 4 8,139 Feb-03-2017, 03:29 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020