0

I am fairly new to both the raspberry pi and python. I have written a code to analyze an image, but I keep getting fatal python error: (pygame parachute) Segmentation Fault. It usually occurs between 15 minutes and an hour into my program. From what I have read, this is typically some sort of memory overreach problem. I haven't found any concrete answers that have been able to solve my problem. Here is my code. My memory usage never goes above 33% when I launch the program in the GUI. I also occasionally get a Fatal Python error: ceval: tstate mix-up. Please help. Any information on how to fix or debug this problem would be greatly appreciated.

import cv2
from SimpleCV import Image, Camera
import time
import os
import RPi.GPIO as GPIO

T = 1
z = 0

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(31, GPIO.OUT)
GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.output(31,GPIO.HIGH)
cam = Camera(prop_set={'width':320, 'height':240})
while(T):

    w = [0 in range(9)]

    if(GPIO.input(32)):
       start = 80
       pixel = 0
       end = 225
       time.sleep(0.32)
       img = cam.getImage()
       img.save('original.jpg')
       edges = cv2.imread('original.jpg')

       edges = cv2.Canny(edges,90,210)
       print("hola")
       for y in range (100,160):
           i = 0
           first = 0
           temp = 0
           last = 0
           for x in range (start, end):
               pixel = edges[y,x]

               if (pixel >=120 and first == 0):
                   first = x


               if (pixel >=120 and first != 0 and last < x):
                   last = x

           if last != 0:
               temp = last - first
               for x in range(start, end):
                   if ((x <= last) and (x >=first)):
                       edges[y,x] = 140;

       while(GPIO.input(32)):
           pass

I don't know what most of it means, but if I run the program in (gdb) and do a backtrace, I get this......

(gdb) bt
#0  0x000835f4 in PyEval_EvalFrameEx ()
#1  0x00081ca4 in PyEval_EvalCodeEx ()
#2  0x000c37c4 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)     

Today I found that if all that I do is import SimpleCV, image and camera, then open a camera and enter an infinite loop, I still get a segmentation fault

(gdb) bt
#0 0x00087024 in PyEval_EvalFrameEx ()
#1 0x000840cc in PyEval_EvalFrameEx ()
#2 0x000a4520 in ?? ()  

If I take the photo using pygame and do a backtrace, I get a segmentation fault with slightly more information

#0  0x0006de50 in PyErr_Format ()
#1  0x70952ee0 in v412_read_frame () from /usr/lib/python2.7/dist-packages/pygame/_camera.so
#2  0x7094ede0 in camera_get_image () from /usr/lib/python2.7/dist-packages/pygame/_camera.so
#3  0x0008360c in PyEval_EvalFrameEx ()
#4  0x00081ca4 in PyEval_EvalCodeEx ()
#5  0x000c37c4 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)  

I found a new function of gdb. I type info threads and I get...

 Id   Target ID         Frame
 2    Thread 0x72df9460 (LWP 601) "python2.7" 0x76e3ab80 in poll ()at ../sysdeps/unix/syscall-template.S:81
*1    Thread 0x76ff6000 (LWP 600) "python2.7" 0x0006de50 in PyErr_Format ()

then if I type list I get this

1        ../sysdeps/unix/syscall-template.S: No such file or directory

I believe that the asterisk means that is the thread that ended the program, but I am not certain. Does anybody know what to do from here? Or have any ideas of how to fix this or work around it??

  • Today, I tried taking and saving the picture with pygame and omitted simpleCV. It worked for about 2 hours and then gave me the dreaded Fatal Python error: (pygame parachute) Segmentation Fault – maddmaxamofo Feb 10 '17 at 20:52
  • I tried taking my picture with openCV and after about an hour I get VIDIOC_DQBUF: No such device error instead. – maddmaxamofo Feb 13 '17 at 17:55
  • I upgraded SimpleCV today and have the newest versions of pygame, and openCV. I am still getting the errors – maddmaxamofo Feb 15 '17 at 20:57
  • If I use openCV and reinitialize the camera every other picture, it seems to work out, but that takes more time and processing than I want to use. Does anybody have any other thoughts or suggestions? – maddmaxamofo Feb 21 '17 at 21:24
1

This is technically not an answer, but more of a work around. I have completely given up on SimpleCV. I don't know if there is an error in SimpleCV, pygame, or if it is just all over my head. I have found that if I use pygame to take the picture and reinitialize it every minute, I never get a segmentation fault.

import cv2
import numpy as np
import pygame
import pygame.camera

import time
import os
import RPi.GPIO as GPIO

T = 1
z = 0
buff = 0
flag = 0


pygame.camera.init()
pygame.camera.list_cameras()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(31, GPIO.OUT)
GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.output(31,GPIO.HIGH)

while(T):

    w = [0 in range(9)]
    if (flag == 0):
        print("Ready")
        flag = 1


    if (z == 0):
        z+=1
        camera = pygame.camera.Camera("/dev/video0",(320,240))
        camera.start()        

    if(GPIO.input(32) and (z>=1)):

       start = 80
       pixel = 0
       end = 225


       time.sleep(0.32)
       for buff in range(0,5):
           img = camera.get_image()

       pygame.image.save(img, "original.jpg")

       edges = cv2.imread("pic.jpg")

       edges = cv2.Canny(edges,90,210)
       np.edges = edges

       for y in range (100,160):
           i = 0
           first = 0
           temp = 0
           last = 0
           for x in range (start, end):
               pixel = np.edges[y,x]

               if (pixel >=120 and first == 0):
                   first = x


               if (pixel >=120 and first != 0 and last < x):
                   last = x

           if last != 0:
               temp = last - first
               for x in range(start, end):
                   if ((x <= last) and (x >=first)):
                       edges[y,x] = 140;

       z=0;
       camera.stop()

       while(GPIO.input(32)):
           pass

With this code as long as you take at least one picture every minute, it does not seem to segment out. I have tested this for approximately 25 hours on one occasion and 12 hours on another. If you do not need to take photos that frequently, you can stop the camera and reinitialize it next time it is needed. I hope this helps somebody, as it was a huge problem for me for several weeks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.