Python Forum

Full Version: Problem starting second thread
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am new to python and while I am learning I clean up my code.
I have learned a bit about classes and while I changed my (messy) code to work with classes an error occurred when starting the camera thread.

All help would be much appreciated.

from picamera.array import PiRGBArray
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import tkinter as tk
import numpy as np
from PIL import Image
from PIL import ImageTk
from threading import Thread


class interface:
    t = None
    canvas = tk.Tk()
    canvas.geometry("800x600")
    canvas.title("Zelfrijdende auto interface")
    b = tk.Label(canvas)
    b.grid(row=0,column=1,rowspan=240,columnspan=320)

    #upSB = tk.Scrollbar(canvas,orient= tk.HORIZONTAL)


    #settings voor lijnherkenning
    upSBlabel=tk.Label(canvas,text="kleur sterkte")
    upSBlabel.grid(row=1,column=0,sticky='s')

    #bovenste grijswaarde
    upSBlabel2=tk.Label(canvas,text="upper grayscale")
    upSBlabel2.grid(row=2,column=0,sticky='s')
    upSB= tk.Scale(canvas, from_=0, to=255,orient=tk.HORIZONTAL)
    upSB.grid(row=3,column=0,columnspan = 1,sticky='ew')

    #laagste grijswaarde
    downSBlabel=tk.Label(canvas,text="lower grayscale")
    downSBlabel.grid(row=4,column=0,sticky='s')
    downSB = tk.Scale(canvas,from_=0,to=255,orient= tk.HORIZONTAL)
    downSB.grid(row=5,column=0,columnspan = 1,sticky='ew')

    #detectie hoogte
    detectionHlabel=tk.Label(canvas,text="Detection height")
    detectionHlabel.grid(row=7,column=0,sticky='s')
    detectionH = tk.Scale(canvas,from_=1,to=240,orient= tk.HORIZONTAL)
    detectionH.grid(row=8,column=0,columnspan = 1,sticky='ew')

    #detectie breedte
    detectionW = tk.Scale(canvas,from_=1,to=160,orient= tk.HORIZONTAL)
    detectionW.grid(row=10,column=0,columnspan = 1,sticky='ew')
    detectionWlabel=tk.Label(canvas,text="Detection width")
    detectionWlabel.grid(row=9,column=0,sticky='s')

    #actie label
    ACTION = "None"
    ActionLabel1=tk.Label(canvas,text="Action:")
    ActionLabel1.grid(row=0,column=322,sticky='nw')

    ActionLabel=tk.Label(canvas,text=ACTION)
    ActionLabel.grid(row=1,column=322,sticky='nw')


    #preview systeem van de settings
    checkedPreview = False
    def changeDISPLAYmode ():
        global checkedPreview
        if(checkedPreview == True):
            checkedPreview = False
        else:
            checkedPreview = True
    showPreview = tk.Button(canvas,text="Preview",command=changeDISPLAYmode)
    showPreview.grid(row=6,column=0)




    def TAKEFOTO():
        camera = PiCamera()
        camera.resolution = (320, 240)
        camera.framerate = 32
        rawCapture = PiRGBArray(camera, size=(320, 240))
 
    # allow the camera to warmup
        time.sleep(0.1)

    # capture frames from the camera
        for frame in camera.capture_continuous(rawCapture, format="rgb", use_video_port=True):
            image = frame.array

            (rows,columns,layers) = image.shape
            

            
            value = image.item(100,100,0)
            image.setflags(write=1)

            downValue = int(downSB.get())
            upValue = int(upSB.get())

            #get line intersections
            leftIntersection = False
            rightIntersection = False
            for y in range (0,int(detectionW.get())):
                dh = rows-detectionH.get()
                x = int(int(columns/2)-y)
                
                if(leftIntersection == False and image.item(dh,x,0) > downValue and image.item(dh,x,1) > downValue and image.item(dh,x,2) > downValue and image.item(dh,x,0) < upValue and image.item(dh,x,1) < upValue and image.item(dh,x,2) < upValue):
                    global dh
                    LEFTintersection = np.array([x,dh])
                    cv2.line(image,(LEFTintersection[0]-5,LEFTintersection[1]),(LEFTintersection[0]+5,LEFTintersection[1]),(255,255,255),5)
                    print("left_intersection")
                    leftIntersection = True
                xSEARCHright = int(columns/2)+y
                if(rightIntersection == False and image.item(dh,xSEARCHright,0) > downValue and image.item(dh,xSEARCHright,1) > downValue and image.item(dh,xSEARCHright,2) > downValue and image.item(dh,xSEARCHright,0) < upValue and image.item(dh,xSEARCHright,1) < upValue and image.item(dh,xSEARCHright,2) < upValue):
                    global dh
                    RIGHTintersection = np.array([xSEARCHright,dh])
                    cv2.line(image,(RIGHTintersection[0]-5,RIGHTintersection[1]),(RIGHTintersection[0]+5,RIGHTintersection[1]),(255,255,255),5)
                    print("right_intersection")
                    rightIntersection = True
                image[dh,x] = [0,0,0]
                image[dh,xSEARCHright] = [0,0,0]
            #intersecties gevonden, nu actie bepalen
            x = action()
            action = x.decideAction(leftinter=LEFTintersection,rightinter=RIGHTintersection)
            global ACTION
            ACTION = action
            
            if(checkedPreview == True):
                cv2.rectangle(image,(0,0),(int(columns/2),80),(downValue,downValue,downValue),cv2.FILLED)
                cv2.rectangle(image,(int(columns/2),0),(int(columns),80),(upValue,upValue,upValue),cv2.FILLED)
            
            
            key = cv2.waitKey(1) & 0xFF
            image = Image.fromarray(image)
            image = ImageTk.PhotoImage(image)
            b.configure(image = image)
            b.image = image
        
            

            rawCapture.truncate(0)
     
            # if the `q` key was pressed, break from the loop
            if key == ord("q"):
                break

    def startCamera():
        t = Thread (target=interface().TAKEFOTO)
        t.start()
    def stopCamera():
        t.join()
        
    startCam = tk.Button(canvas,command=startCamera,text="START CAMERA")
    startCam.grid(row=0,column=0)



        

    canvas.mainloop()
class action:
    def decideAction(leftinter,rightinter):
        mid = leftinter[0]-rightinter[0]
        if(mid>0):
            action = "left"
        if(mid<0):
            action = "right"
        return action

interface()
Error:
ERROR: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__ return self.func(*args) File "/home/pi/Desktop/zelfrijdendewagen/main.py", line 145, in startCamera t = Thread (target=interface().TAKEFOTO) NameError: name 'interface' is not defined