Scanning from a laser line and usb cam
V Python fill up memory to 1.6G and crash the system
Smaller Scan works fine
is there a better plotting module or a way of getting past V Python
memory block?
The plot must happen in color xyz rgb.

import thread
import sys
import time
import CV
import visual as PLOT

from Geom_3D import *
from Laser_Dots import *
from Ply_Save import *
from PLOT import *

global linelist,linelist_2,PlotNumber,captureR
global image,Red,Picsize,LessData,Stepper

Stepper = 1
Cam = 1
Plot = 1
Liniar,Rotary = 0,1
Picsize = 320,240
Picsize = 640,480

LessData = 20
Picnum = 342
red,green,blue,Value = 1,1,1,1
PlotNumber,PicNum,dot = 1,1,1
CenterLine = 250
linelist = []
linelist_2 = []
Plot_Data = []
Red = red

deviceR = Cam
CamSizeX,CamSizeY = Picsize

LessData = (CamSizeX/(100/LessData))

Win_Name  = 'Get_Laser_Dots'
cv.NamedWindow(Win_Name, 1)
cv.MoveWindow(Win_Name, 450, 10)
CamSizeX,CamSizeY = Picsize
deviceR = Cam
captureR = cv.CreateCameraCapture(deviceR)
cv.SetCaptureProperty (captureR,cv.CV_CAP_PROP_FRAME_HEIGHT,
                       CamSizeY)
cv.SetCaptureProperty (captureR,cv.CV_CAP_PROP_FRAME_WIDTH,
                       CamSizeX)
image = cv.QueryFrame(captureR)
cv.ShowImage(Win_Name, image)
time.sleep(3)
image = cv.QueryFrame(captureR)
cv.ShowImage(Win_Name, image)

Cam = captureR

def on_mouse(event,x, y, flags, param):
    global image,Red,PlotNumber,Value
    if event == cv.CV_EVENT_MOUSEMOVE:
        pass
    if event == cv.CV_EVENT_LBUTTONDOWN:    
        x,y =   int(x) , int(y)
        Pixel = cv.Get2D (image, y, x )
        blue, green, red,N = Pixel
        if cv.CV_EVENT_MBUTTONDOWN:
            PicNum = 1
            dotend = 1
            dot = 1
            PlotNumber = 1
            Red = red
            print Red
            Value = (green + blue)/2
            Value = red = Value
cv.SetMouseCallback('Get_Laser_Dots', on_mouse)


def main():
    thread.start_new_thread(FimdRed,())
def FimdRed():
    global linelist,linelist_2,PlotNumber,image
    global dot
    global image,Red,CenterLine,LessData
    
    while PlotNumber < Picnum:
        
        if Red > 50 and PlotNumber == 1:
            Cam_Stepper()

        if Red > 50:
            image = cv.QueryFrame(captureR)
            PlotNumber = PlotNumber + 1
            linelist = []    
            linelist,image_Return,dot = Laser.FindDot(image,Picnum,linelist,\
                                                      Cam,Red,Value,Picsize)
    
            N,linelist = geo.Get_Geometry(Picnum,linelist,Liniar,\
                                    Rotary,Picsize,CenterLine)
    
            if Plot == 1:
                plot.Plot_3D(linelist,LessData)
            cv.ShowImage(Win_Name, image_Return)
    
            linelist_2 = linelist_2 + linelist
            linelist = []            


if __name__ == "__main__":

    Laser = Get_Laser_Dots(image,Picnum,linelist,Cam,red,Value,Picsize)    
    file = Saving(dot,linelist)    
    geo = Calc_Geometry(Picnum,linelist_2,Liniar,Rotary,Picsize,CenterLine)
    plot = Plot_Visual(linelist,LessData)
    file.formatPLY()    
    main()

    while True:
        if PlotNumber >= Picnum:
            if Red < 50 and PlotNumber == Picnum:
                PlotNumber = 1
            if Red > 50:
                file.Set_PLY_Vertices(dot,linelist_2)
                cv.DestroyWindow('Get_Laser_Dots')
                sys.exit(-1)
                
                
        if(cv.WaitKey(10) != -1):
            break
import cv


class Get_Laser_Dots:
    def __init__(self,image,Picnum,linelist,Cam,Red,Value,Picsize):
        global PlotNumber,xsize,ysize,xxcrop,yycrop,captureR
        global dot

        captureR = Cam
        if not captureR:
            print "Could not initialize capturing..."
            sys.exit(-1)
        
        PlotNumber = 1
        dot = 1

        CamSizeX,CamSizeY = Picsize
        Center = CamSizeX/2
        crop1 = (CamSizeX/5),6
        crop2 = (CamSizeX/2),CamSizeY
        xxcrop,yycrop = crop1
        xsize,ysize = crop2

    def FindDot(self,image,Picnum,linelist,Cam,Red,Value,Picsize):
        global PlotNumber,xsize,ysize,xxcrop,yycrop,captureR
        global on_mouse,dot
        PlotNumber = PlotNumber + 1
        if(image):
            yy = (ysize-yycrop)
            while yy >= 1:
                yy = yy - 1
                ycrop = yy + yycrop
                xx = (xsize-xxcrop)  
                while xx >= 1:
                    xx = xx - 1
                    xcrop = xx + xxcrop
                    x,y =   int(xcrop) , int(ycrop)
                    Pixel = cv.Get2D (image, y, x )
                    blue, green, red,N = Pixel
        
                    if (red>=Red) & (red>=((green+blue)/2)+Value):
                        rr = red
                        x,y =   int(xcrop) , int(ycrop)
                        Pixel = cv.Get2D (image, y, x )
                        blue, green, red,N = Pixel
        
                        while red>=rr :
                            xx = xx - 1
                            xcrop = xx + xxcrop
                            x,y =   int(xcrop) , int(ycrop)
                            Pixel = cv.Get2D (image, y, x )
                            x,y =   int(xcrop) , int(ycrop)
                            Pixel = cv.Get2D (image, y, x )
                            red, green, blue,N = Pixel
                        else:
                            pt1 = int(xcrop) ,int(ycrop)
                            pt2 = int(xcrop+1),int(ycrop+1)
                            cv.Line(image,pt1,pt2, cv.Scalar(0,250,0), 1, 8, 0)
                            if ycrop > 0  and Red > 50:
                                xC,yC =   int(xcrop+8) , int(ycrop)
                                PixelC = cv.Get2D (image, yC, xC )
                                blueC, greenC, redC,N = PixelC
                                linelist.append(xcrop)
                                linelist.append(ycrop)
                                linelist.append(int(redC))
                                linelist.append(int(greenC))
                                linelist.append(int(blueC))
                                linelist.append(int(PlotNumber))
                                dot = dot + 1

                        ycrop = 0
        
        return linelist,image,dot
from math import sin,cos,tan,atan,radians

class Calc_Geometry:
    def __init__(self,Picnum,Datalist,Liniar,Rotary,Picsize,CenterLine):
        global xsize,ysize,xxcrop,yycrop

        CamSizeX,CamSizeY = Picsize
        crop1 = (CamSizeX/5),6
        crop2 = (CamSizeX/2),CamSizeY
        xxcrop,yycrop = crop1
        xsize,ysize = crop2
        
    def Get_Geometry(self,Picnum,Datalist,Liniar,Rotary,Picsize,CenterLine):
        global xsize,ysize,xxcrop,yycrop

        if CenterLine < 10:
            CamSizeX,CamSizeY = Picsize
            CenterLine = CamSizeX/2
        NUMBER = 1
        Geom_list = []
        xyzlist = Datalist 
        for points in range(2, len(xyzlist),6):
            r,ycrop,xcrop,PlotNumber,b,g = xyzlist[points], xyzlist[points-1],\
                                xyzlist[points-2], xyzlist[points-3],\
                                xyzlist[points-4],xyzlist[points-5]

            stepangle = float(360.0 / (Picnum-3))

            if Liniar:
                offset = float(0 * stepangle)
                zc = (((xcrop-CenterLine) * sin(offset * (3.14159/180)))\
                      +PlotNumber)
                yc = -(atan(((500*4.0/5.0)*(3.14159/180)/2.0))*2.0*.210*ycrop)
                xc = -((xcrop-CenterLine) * cos( 0 * (3.14159/180))) 
    
            if Rotary:
                offset = float(PlotNumber * stepangle)
                zc = ((xcrop-CenterLine) * sin(radians(offset))* 2.4)
                xc = -((xcrop-CenterLine) * cos(radians(offset))* 2.4)
                yc = -ycrop
            Geom_list.append(xc)
            Geom_list.append(yc)
            Geom_list.append(zc)
            Geom_list.append(r)
            Geom_list.append(g)
            Geom_list.append(b)
                
            NUMBER = NUMBER + 1
        return NUMBER,Geom_list
import visual as PLOT
from math import *



class Plot_Visual:
    def __init__(self,linelist,LessData):
        global scene,BColor
        scene = PLOT.display(title='GEOMETRY DISPLAY',center = (-2,-250,0),\
                        x=20,y=20,background=([0.0,0.0,0.0]))
        BColor = ([0.60,0.60,0.60])

    def Plot_3D(self,linelist,LessData):
        global dot,scene,BColor

        xyzlist = linelist
        dot = 1
            
        for points in range(2, len(xyzlist),6):
            zc,yc,xc,g,b,r = xyzlist[points], xyzlist[points-1],\
                             xyzlist[points-2], xyzlist[points-3],\
                             xyzlist[points-4],xyzlist[points-5]
        
            x_axis = float(xc) /2
            y_axis = float(yc) /2
            z_axis = float(zc) /2
            fact = (1 / (140 * 1.0))
            r = int(r) * fact
            g = int(g) * fact
            b = int(b) * fact
            scene.background=BColor

            step = ((dot) / LessData)
            step1 = ((dot) - (step*LessData))
            if step1 == 1:
                if r < 80:
                    r = 250
                PLOT.points(pos=(zc, yc, xc), size = 0.0015, color=[(r,g,b)],\
                            center=(x_axis,y_axis,z_axis))
            dot = dot + 1
            drag = 0
        xyzlist = []
import os
import win32file, win32api
import fileinput

class Saving:
    def __init__(self,dot,Datalist):
        print 'start'
    
    def formatPLY(self):
        outPLY = ""
        Value = 1
        plotPLY = "c:/Quick_3D_Scan.ply"
        if os.path.exists(plotPLY):
            os.remove(plotPLY)

        PlyFile = "c:/Quick_3D_Scan.ply"
        outputfile = open(PlyFile,"w")
        str001 = "ply"+"\n" 
        outputfile.write(str001)
        str003 = "format ascii 1.0"+"\n"
        outputfile.write(str003)
        str004 = "comment Awie Spengler generated"+"\n"
        outputfile.write(str004)
        str005 = "element vertex "+"\n"
        outputfile.write(str005)
        str006 = "property float x"+"\n"
        outputfile.write(str006)
        str007 = "property float y"+"\n"
        outputfile.write(str007)
        str008 = "property float z"+"\n"
        outputfile.write(str008)
        str009 = "property uchar red"+"\n"
        outputfile.write(str009)
        str010 = "property uchar green"+"\n"
        outputfile.write(str010)
        str011 = "property uchar blue"+"\n"
        outputfile.write(str011)
        str012 = "property uchar alpha"+"\n"
        outputfile.write(str012)
        str013 = "element face 0"+"\n"
        outputfile.write(str013)
        str014 = "property list uchar int vertex_indices"+"\n"
        outputfile.write(str014)
        str015 = "end_header"+"\n"
        outputfile.write(str015)
        
    def Set_PLY_Vertices(self,Number,Datalist):
        plotdot = "c:/Quick_3D_Scan.ply"
        plotdotBackup = "c:/Quick_3D_Scan.ply"
        temp_dir=win32api.GetFullPathName('c:/')
        outputfile = open(plotdot,"r")
        outputfile.close()
        LNum = str((Number-1))
        A = 'element vertex '+LNum
        for lines in fileinput.FileInput(plotdot,inplace=4):
            lines = lines.replace('element vertex ',A)
            print lines,
            outputfile.close()

        PlyFile = "c:/Quick_3D_Scan.ply"
        outputfile = open(PlyFile,"a")
        xyzlist = Datalist
        outPLY = "" 
        for points in range(2, len(xyzlist),6):
            cz,cy,cx,b,g,r = xyzlist[points], xyzlist[points-1],\
                                xyzlist[points-2], xyzlist[points-3],\
                                xyzlist[points-4], xyzlist[points-5]
            #print cz,cy,cx,b,r,g                    
            outPLY = outPLY + str(cz)
            outPLY = outPLY + " "
            outPLY = outPLY + str(cy)
            outPLY = outPLY + " "
            outPLY = outPLY + str(cx)
            outPLY = outPLY + " "
            outPLY = outPLY + str(r)
            outPLY = outPLY + " "
            outPLY = outPLY + str(g)
            outPLY = outPLY + " "
            outPLY = outPLY + str(b)
            outPLY = outPLY + " "
            outPLY = outPLY + str(255)
            outPLY = outPLY +"\n"
            outputfile.write(outPLY)
            outPLY = ""
        outputfile.close()