Skip to content

urlcam

plantimager.urlcam Link

Camera Link

Camera(url)

Bases: AbstractCamera

Camera module fetching an image serve at given URL.

Notes

Image should be served as $url/scan.jpg.

See Also

plantimager.hal.AbstractCamera

Examples:

>>> from plantimager.urlcam import Camera
>>> url = "http://192.168.0.1:8080"
>>> cam = Camera(url)
>>> img = cam.grab("img_001")
>>> arr = img.channel("rgb").data
>>> arr.shape
>>> image = Image.fromarray(arr)
>>> image.show()

Parameters:

Name Type Description Default
url str

URL of the camera.

required
Source code in plantimager/urlcam.py
57
58
59
60
61
62
63
64
def __init__(self, url):
    """
    Parameters
    ----------
    url : str
        URL of the camera.
    """
    self.url = url

grab Link

grab(idx, metadata=None)

Grab a picture from the camera.

Parameters:

Name Type Description Default
idx int

Id of the image DataItem to create.

required
metadata dict

Dictionary of metadata associated to the picture.

None

Returns:

Type Description
DataItem

The image data.

Examples:

>>> from plantimager.urlcam import Camera
>>> url = "http://192.168.0.1:8080"
>>> cam = Camera(url)
>>> img = cam.grab("img_001")
>>> arr = img.channel("rgb").data
>>> from PIL import Image
>>> image = Image.fromarray(arr)
>>> image.show()
Source code in plantimager/urlcam.py
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def grab(self, idx: int, metadata: dict = None):
    """Grab a picture from the camera.

    Parameters
    ----------
    idx : int
        Id of the image `DataItem` to create.
    metadata : dict, optional
        Dictionary of metadata associated to the picture.

    Returns
    -------
    plantimager.hal.DataItem
        The image data.

    Examples
    --------
    >>> from plantimager.urlcam import Camera
    >>> url = "http://192.168.0.1:8080"
    >>> cam = Camera(url)
    >>> img = cam.grab("img_001")
    >>> arr = img.channel("rgb").data

    >>> from PIL import Image
    >>> image = Image.fromarray(arr)
    >>> image.show()

    """
    data_item = DataItem(idx, metadata)
    # https://docs.python.org/3/library/http.server.html#http.server.BaseHTTPRequestHandler.wfile
    # wfile:
    #   Contains the output stream for writing a response back to the client.
    #   Proper adherence to the HTTP protocol must be used when writing to this stream in order to achieve successful interoperation with HTTP clients.
    #   Changed in version 3.6: This is an io.BufferedIOBase stream.
    # data = imageio.imread(BytesIO(requests.get(self.url+"scan.jpg").content))
    # _ = requests.get(self.url + "/scan.jpg")  # update the picture
    data = imageio.imread(self.url + "/scan.jpg")  # download it
    data_item.add_channel(self.channels()[0], data)
    return data_item