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


Messages In This Thread
OpenCV - Segmentation fault - by samtwilliams - Sep-15-2019, 07:39 PM
RE: OpenCV - Segmentation fault - by samtwilliams - Sep-15-2019, 08:50 PM
RE: OpenCV - Segmentation fault - by Larz60+ - Sep-16-2019, 12:45 AM
RE: OpenCV - Segmentation fault - by samtwilliams - Sep-17-2019, 07:43 PM
RE: OpenCV - Segmentation fault - by kinetic - Sep-17-2019, 08:05 PM
RE: OpenCV - Segmentation fault - by samtwilliams - Sep-17-2019, 08:18 PM
RE: OpenCV - Segmentation fault - by Larz60+ - Sep-18-2019, 12:01 AM

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