Amlo4300
Amlo4300

Reputation: 21

Interfacing LabVIEW with python for triggering USB Camera

LabVIEW supports pallete for calling function from python. Do I need to develop 3 seperate functions each for initializing, capturing and closing? Using OpenCV in python for accessing directshow based usb cameras.

If I create 3 seperate functions, do I need to include them in a same python script? As if I am using while loop for capturing function, functions written after that wont possibly be called. If I keep them in different python scripts, will it recognize that the camera is operating in a same session?

Why does subsequent functions does not recognize 'cam'?

import cv2
import math
import os
camname = -1
img_name = "sample"
filepath = r"C:\Users\Public"
message = "sampler"
caminitok = False
inierror = False

def caminit(camname):
    cam = cv2.VideoCapture(camname)
    if not (cam.isOpened()):
        return inierror == True
    else:
        return inierror == False    

def camcapture(img_name, filepath, caminitok):
    if caminitok == True:
        ret, frame = cam.read()
        os.chdir(filepath)
        cv2.imwrite(img_name, frame)

def camclose():
        cam.release()

Upvotes: 2

Views: 980

Answers (3)

nekomatic
nekomatic

Reputation: 6284

Instead of using global variables, a more 'Pythonic' way of maintaining a reference to an external resource would be to create a class:

import cv2
import os

class Cam:
    def __init__(self, camname):
        self.cam = cv2.VideoCapture(camname)
        self.caminitok = self.cam.isOpened()
    
    def capture(self, img_name, filepath):
        if self.caminitok:
            ret, frame = self.cam.read()
            os.chdir(filepath)
            cv2.imwrite(img_name, frame)

    def __del__(self):
        self.cam.release()

Usage:

>>> c = Cam(0)
>>> c.caminitok
True
>>> c.capture('foo.jpg', 'path/to/folder')
>>> del c

This is the equivalent of a functional global VI in LabVIEW, where internal state is stored in e.g. a shift register.

Upvotes: 1

Amlo4300
Amlo4300

Reputation: 21

import cv2
import math
import os   
imagestate = False
def camcapture(camnum, img_name, filepath):
    cam = cv2.VideoCapture(camnum, cv2.CAP_DSHOW)
    ret, frame = cam.read()
    os.chdir(filepath)
    cv2.imwrite(img_name, frame)
    cam.release()
    return imagestate == True

I just call this script from LabVIEW whenever I need to take a snapshot, works great. I yet dont understand why imagestate is not returned 'True' even if whole script worked OK.

Upvotes: 0

sweber
sweber

Reputation: 2976

This is more a python than a labview problem in the first place.

The variable cam is created within caminit(), and it is deleted at the end of the function. So, camcapture() and camclose() do not know, what cam is.

You can add cam to the list of variables at the beginning. Then, it is always defined, and all functions can read it. BUT if a function wants to write to it, python will create a new variable with the same name, and use that for the rest of the function. At the end, it will forget the new variable, and put the original one in place. Therefore, you need to write global cam in caminit(), which explicitly tells python to write to the global variable. This is not necessary for the other functions, since they work on the variable, but don't write it.

    import cv2
    import math
    import os
    camname = -1
    img_name = "sample"
    filepath = r"C:\Users\Public"
    message = "sampler"
    caminitok = False
    inierror = False

    cam = None ##########   
    
    def caminit(camname):
        global cam ##########
        cam = cv2.VideoCapture(camname)
        if not (cam.isOpened()):
            return inierror == True
        else:
            return inierror == False    
    
    def camcapture(img_name, filepath, caminitok):
        
        if caminitok == True:
            ret, frame = cam.read()
            os.chdir(filepath)
            cv2.imwrite(img_name, frame)
    
    def camclose():
        
            cam.release()

Upvotes: 0

Related Questions