Best Python code snippet using fMBT_python
nvcstest.py
Source:nvcstest.py  
...44    def setupGraph(self):45        self.obGraph.setImager(self.options.imager_id)46    def runTest(self, args):47        print "capturing jpeg image..."48        self.obCamera.startPreview()49        fileName = "out_%s_test" % self.testID50        jpegFilePath = os.path.join(self.testDir, fileName + ".jpg")51        self.obCamera.captureJpegImage(jpegFilePath)52        self.obCamera.stopPreview()53        if (os.path.exists(jpegFilePath) != True):54            self.logger.error("Couldn't capture the jpeg image: %s" % jpegFilePath)55            retVal = NvCSTestResult.ERROR56        return NvCSTestResult.SUCCESS57class NvCSMultipleRawTest(NvCSTestBase):58    "Multiple Raw Image Test"59    imageNumValues = range(1, 101)60    def __init__(self, options):61        NvCSTestBase.__init__(self, "Multiple_Raw")62        self.options = options63    def setupGraph(self):64        self.obGraph.setImager(self.options.imager_id)65    def __del__(self):66        if (os.path.exists(self.testDir)):67            shutil.rmtree(self.testDir)68    def runPreTest(self):69        if (not self.obCamera.isRawCaptureSupported()):70            self.logger.info("raw capture is not supported")71            return NvCSTestResult.SKIPPED72        else:73            return NvCSTestResult.SUCCESS74    def runTest(self, args):75        retVal = NvCSTestResult.SUCCESS76        for imageNum in self.imageNumValues:77            # take an image78            fileName = "out_%s_%d_test" % (self.testID, imageNum)79            jpegFilePath = os.path.join(self.testDir, fileName + ".jpg")80            rawfilePath = os.path.join(self.testDir, fileName + ".nvraw")81            self.obCamera.startPreview()82            self.logger.debug("capturing image: %d" % imageNum)83            self.obCamera.captureConcurrentJpegAndRawImage(jpegFilePath)84            self.obCamera.stopPreview()85            if os.path.exists(rawfilePath) != True:86                self.logger.error("couldn't find file: %s" % rawfilePath)87                retVal = NvCSTestResult.ERROR88                break89            if not self.nvrf.readFile(rawfilePath):90                self.logger.error("couldn't open the file: %s" % rawfilePath)91                retVal = NvCSTestResult.ERROR92            pattern = ".*"93            if((imageNum % 10) == 0):94                regexOb = re.compile(pattern)95                for fname in os.listdir(self.testDir):96                    if regexOb.search(fname):97                        self.logger.debug("removing file %s" % fname)98                        os.remove(os.path.join(self.testDir, fname))99        return retVal100class NvCSRAWCapTest(NvCSTestBase):101    "RAW Capture Test"102    def __init__(self, options):103        NvCSTestBase.__init__(self, "RAW_Capture")104        self.options = options105    def setupGraph(self):106        self.obGraph.setImager(self.options.imager_id)107        self.obGraph.setGraphType(GraphType.RAW)108        return NvCSTestResult.SUCCESS109    def runTest(self, args):110        retVal = NvCSTestResult.SUCCESS111        self.obCamera.startPreview()112        fileName = "out_%s_test" % self.testID113        filePath = os.path.join(self.testDir, fileName + ".nvraw")114        try:115            self.obCamera.captureRAWImage(filePath)116        except nvcamera.NvCameraException, err:117            if (err.value == nvcamera.NvError_NotSupported):118                self.logger.info("raw capture is not supported")119                return NvCSTestResult.SKIPPED120            else:121                raise122        self.obCamera.stopPreview()123        if (os.path.exists(filePath) != True):124            self.logger.error("Couldn't capture the raw image: %s" % filePath)125            retVal = NvCSTestResult.ERROR126        return retVal127class NvCSConcurrentRawCapTest(NvCSTestBase):128    "Concurrent raw capture test"129    def __init__(self, options):130        NvCSTestBase.__init__(self, "Concurrent_Raw_Capture")131        self.options = options132    def setupGraph(self):133        self.obGraph.setImager(self.options.imager_id)134    def runPreTest(self):135        if (not self.obCamera.isRawCaptureSupported()):136            self.logger.info("raw capture is not supported")137            return NvCSTestResult.SKIPPED138        else:139            return NvCSTestResult.SUCCESS140    def runTest(self, args):141        retVal = NvCSTestResult.SUCCESS142        self.obCamera.startPreview()143        fileName = "out_%s_test" % self.testID144        jpegFilePath = os.path.join(self.testDir, fileName + ".jpg")145        rawFilePath = os.path.join(self.testDir, fileName + ".nvraw")146        self.obCamera.captureConcurrentJpegAndRawImage(jpegFilePath)147        self.obCamera.stopPreview()148        if (os.path.exists(rawFilePath) != True):149            self.logger.error("Couldn't capture the raw image: %s" % rawFilePath)150            retVal = NvCSTestResult.ERROR151        if not self.nvrf.readFile(rawFilePath):152            self.logger.error("couldn't open the file: %s" % rawFilePath)153            retVal = NvCSTestResult.ERROR154        # Check no values over 2**bitsPerSample155        if (max(self.nvrf._pixelData.tolist()) > (2**self.nvrf._bitsPerSample)):156            self.logger.error("pixel value is over %d." % 2**self.nvrf._bitsPerSample)157            retVal = NvCSTestResult.ERROR158        return retVal159class NvCSPropertyTest(NvCSTestBase):160    "Property verification test"161    def __init__(self, options):162        NvCSTestBase.__init__(self, "Property_Verification")163        self.options = options164    def setupGraph(self):165        self.obGraph.setImager(self.options.imager_id)166    def runTest(self, args):167        # take an image with contrast168        print "capturing jpeg image with contrast value %d..." % 100169        self.obCamera.setAttr(nvcamera.attr_contrast, 50)170        self.captureJpegImage("out_%s_%s_%d_test" % (self.testID, "contrast", 100))171        # take an image with brightness172        self.obCamera.setAttr(nvcamera.attr_brightness, 14.0)173        print "capturing jpeg image with brightness value %d..." % 50.0174        self.captureJpegImage("out_%s_%s_%d_test" % (self.testID, "brightness", 50.0))175        # take an image with max (5.0) saturation176        print "capturing jpeg image with saturation value %d..." % 0.0177        self.obCamera.setAttr(nvcamera.attr_saturation, 5.0)178        self.captureJpegImage("out_%s_%s_%f_test" % (self.testID, "saturation", 0.0))179        return NvCSTestResult.SUCCESS180class NvCSEffectTest(NvCSTestBase):181    "Effect verification test"182    effectValues = ["None",183                    "Noise",184                    "Emboss",185                    "Negative",186                    "Sketch",187                    "OilPaint",188                    "Hatch",189                    "Gpen",190                    "Antialias",191                    "DeRing",192                    "Solarize",193                    "Posterize",194                    "Sepia",195                    "BW"196                  ]197    def __init__(self, options):198        NvCSTestBase.__init__(self, "Effect_Verification")199        self.options = options200    def setupGraph(self):201        self.obGraph.setImager(self.options.imager_id)202    def runTest(self, args):203        for effectVal in self.effectValues:204            # take an image with specified effect205            print "capturing image with %s effect..." % effectVal206            self.obCamera.setAttr(nvcamera.attr_effect, effectVal)207            # take an image208            self.captureJpegImage("out_%s_%s_test" % (self.testID, effectVal))209        return NvCSTestResult.SUCCESS210class NvCSFlickerTest(NvCSTestBase):211    "Flicker verification test"212    flickerValues = ["off", "auto", "50", "60"]213    def __init__(self, options):214        NvCSTestBase.__init__(self, "Flicker")215        self.options = options216    def setupGraph(self):217        self.obGraph.setImager(self.options.imager_id)218    def runTest(self, args):219        for flickerVal in self.flickerValues:220            # take an image with specified Flicker effect221            print "capturing jpeg image flicker value %s..." % flickerVal222            self.obCamera.setAttr(nvcamera.attr_flicker, flickerVal)223            # take an image224            self.captureJpegImage("out_%s_%s_test" % (self.testID, flickerVal))225        return NvCSTestResult.SUCCESS226class NvCSAWBTest(NvCSTestBase):227    "Auto White Balance test"228    awbValues = ["Off",229                 "Auto",230                 "Tungsten",231                 "Fluorescent",232                 "Sunny",233                 "Shade",234                 "Cloudy",235                 "Incandescent",236                 "Flash",237                 "Horizon"]238    def __init__(self, options):239        NvCSTestBase.__init__(self, "AWB")240        self.options = options241    def setupGraph(self):242        self.obGraph.setImager(self.options.imager_id)243    def runTest(self, args):244        # take an image with specified AWB setting245        for awbValue in self.awbValues:246            print "capturing jpeg image with AWB set to %s..." % awbValue247            self.obCamera.setAttr(nvcamera.attr_whitebalance, awbValue)248            # take an image249            self.captureJpegImage("out_%s_%s_test" % (self.testID, awbValue))250        return NvCSTestResult.SUCCESS251class NvCSMeteringTest(NvCSTestBase):252    "Metering test"253    meteringmodeValues = ["center", "spot", "fullframe"]254    def __init__(self, options):255        NvCSTestBase.__init__(self, "Metering")256        self.options = options257    def setupGraph(self):258        self.obGraph.setImager(self.options.imager_id)259    def runTest(self, args):260        # take an image with specified meteringmode value261        for meteringMode in self.meteringmodeValues:262            print "capturing jpeg image with Metering set to %s..." % meteringMode263            self.obCamera.setAttr(nvcamera.attr_meteringmode, meteringMode)264            # take an image265            self.captureJpegImage("out_%s_%s_test" % (self.testID, meteringMode))266        return NvCSTestResult.SUCCESS267class NvCSAFTest(NvCSTestBase):268    "AF test"269    afValues = [-1, 0]270    def __init__(self, options):271        NvCSTestBase.__init__(self, "AF")272        self.options = options273    def setupGraph(self):274        self.obGraph.setImager(self.options.imager_id)275    def runTest(self, args):276        for afVal in self.afValues:277            # take an image with specified AF value278            print "capturing jpeg image with AF set to %d..." % afVal279            self.obCamera.setAttr(nvcamera.attr_autofocus, afVal)280            self.startPreview()281            # take an image282            self.captureJpegImage("out_%s_%d_test" % (self.testID, afVal))283        return NvCSTestResult.SUCCESS284class NvCSAutoFrameRateTest(NvCSTestBase):285    "FPS test"286    def __init__(self, options):287        NvCSTestBase.__init__(self, "FPS")288        self.options = options289    def setupGraph(self):290        self.obGraph.setImager(self.options.imager_id)291    def runTest(self, args):292        # take an image with 0 AFR293        self.obCamera.setAttr(nvcamera.attr_autoframerate, 0)294        self.startPreview()295        # take an image296        self.captureJpegImage("out_%s_%d_test" % (self.testID, 0))297        return NvCSTestResult.SUCCESS298class NvCSJpegRotationTest(NvCSTestBase):299    "JPEG Rotation test"300    rotationValues = ["None", "Rotate90", "Rotate180", "Rotate270"]301    def __init__(self, options):302        NvCSTestBase.__init__(self, "Jpeg_Rotation")303        self.options = options304    def setupGraph(self):305        self.obGraph.setImager(self.options.imager_id)306    def runTest(self, args):307        for rotationVal in self.rotationValues:308            # take an image specified rotation309            print "capturing jpeg image with rotation set to %s..." % rotationVal310            self.obCamera.setAttr(nvcamera.attr_transform, rotationVal)311            self.startPreview()312            # take an image313            self.captureJpegImage("out_%s_%s_test" % (self.testID, rotationVal))...polypServer.py
Source:polypServer.py  
...33        self.active = True34    def stopCamera(self) :35        self.camera.close()36        self.active = False37    def startPreview(self):38        if self.active == False :39            self.initCamera()40        self.camera.start_preview()41        self.preview = True42        time.sleep(2)43        return json.jsonify(camera="Active", preview=True)44    def stopPreview(self):45        self.camera.stop_preview()46        self.preview = False47        return json.jsonify(camera="Active", preview=False)48    def getPicture(self):49        if( self.active == False ) :50            self.startPreview()51        self.camera.capture("snap.png", use_video_port=True)52        return send_file("snap.png")53# =======================================================================54app = PolypServer("polypserver1")55@app.route('/')56def service():57    return app.service()58@app.route('/camera/on')59def camactive():60    return app.startPreview()61@app.route('/camera/off')62def camdeactive():63    return app.stopPreview()64@app.route('/camera/get')65def camsnap():66    return app.getPicture()67if __name__ == "__main__":...TimelapseClass.py
Source:TimelapseClass.py  
1from time import sleep2import sys3from threading import Thread, Event4from picamera import PiCamera5class Timelapse():6    def __init__(self):7        8        self.width = 32809        self.height = 246410        self.frames = 1011        self.interval = 1012        self.File = "/home/pi/"13        14    def ChangeWidth(self,width):15        self.width = width16        return self17    18    def ChangeHeight(self,height):19        self.height = height20        return self21        22    def ChangeNumberOfFrames(self,frames):23        self.frames = frames24        return self25    def ChangeInterval(self,interval):26        self.interval = interval27        return self28    29    def ChangeFile(self,file):30        self.File = str(file)31        return self32    33    def StartTimelapseThread(self):34        self.tstopped = False35        t = Thread(target=self.StartTimelapse, args=())36        t.daemon = True37        t.start()38        return self39    def StartTimelapse(self):        40        if self.tstopped:41            return 42        camera = PiCamera()43        camera.resolution = (self.width,self.height)44        camera.start_preview(fullscreen=False, window=(200,200,800,800))45        sleep(self.interval)46        for i in range(self.frames):47            camera.capture(self.File + "/" + str(i) + ".jpg")48            sleep(self.interval)49        camera.close()50        camera.stop_preview()51        self.tstopped = True52        53    def StartPreviewThread(self):54        self.pstopped = False55        self.stop = False56        p = Thread(target=self.StartPreview, args=())57        p.daemon = True58        p.start()59        return self60        61    def StartPreview(self):62        if self.pstopped:63            return64        camera = PiCamera()65        camera.resolution = (self.width,self.height)66        camera.start_preview(fullscreen=False, window=(200,200,800,800))67        FramesTaken = 068        while self.stop == False:69            FramesTaken = FramesTaken + 1           70        else:71            camera.close()72            camera.stop_preview()73    74    #def StartPreview(self):75        76            77    def StopPreview(self):78        self.stop = True...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
