vision_camera · Camera and images

Takes pictures with the camera and edits images.

Class: Camera

class openpibo.vision_camera.Camera(cam=0, width=None, height=None)[source]

Bases: object

Functions: usb_cameras() imread() read() release() create_matte() imshow() imshow_to_ide() resize() rotate() imwrite() draw_bitmap() rectangle() circle() line() putTextPIL() putText() stylization() detailEnhance() pencilSketch() edgePreservingFilter() flip()

Takes pictures with the camera and edits images.

  • Camera() (cam=0) is the board camera; Camera(1) and Camera(2) are USB cameras (any model that supports V4L2). USB camera numbers follow the USB port order of the connected cameras; see usb_cameras().

  • The board camera opens on the first read(), so an error opening the camera is raised by read().

  • Without a size, read() returns a 480x640 (portrait) image. USB cameras give the same size; if the camera’s aspect ratio differs, the center is cropped to fit.

  • Adds shapes/text to images.

  • Transforms images.

example:

from openpibo.vision_camera import Camera

camera = Camera()        # 보드 카메라
# camera = Camera(1)     # USB 카메라 cam1
# 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
static usb_cameras()[source]

Returns the list of connected USB cameras. The board camera is not included.

example:

Camera.usb_cameras()
# [{'cam': 1, 'dev': '/dev/video1', 'name': 'USB Camera: USB Camera'}]
Returns:

A list of cam (the number to pass to Camera(cam=…)) · dev (device file) · name (the name the camera reports)

release()[source]

Releases the camera so another program can use it. Calling read() again afterwards reopens it.

imread(filename)[source]

Reads an image file.

Parameters:

filename (str) – Image file to use

Returns:

Image object of type numpy.ndarray

read()[source]

Takes a picture with the camera.

The board camera opens on the first call (if the previous program is still releasing the camera, it retries up to three times, one second apart). If a USB camera fails while reading (unplugged and plugged back in, etc.), it is found and opened again once.

Returns:

Image object of type numpy.ndarray (BGR)

create_matte(colors=(255, 255, 255))[source]

Makes a blank image (matte) filled with one color, the same size as read(). It does not open the camera.

example:

img = camera.create_matte((255, 255, 255))   # 흰 바탕
img = camera.create_matte('#ff0000')         # 빨간 바탕
Parameters:

colors (tuple or str) – Color — a (b, g, r) tuple or a '#rrggbb' string

Returns:

Image object of type numpy.ndarray

imshow(img, ratio=1.0)[source]

Shows an image file in the Web IDE.

Parameters:
  • img (numpy.ndarray) – Image object

  • ratio (float) – Image resize ratio (network load when many are used at once)

imshow_to_ide(img, ratio=0.25)[source]

Shows an image file in the Web IDE.

Parameters:
  • img (numpy.ndarray) – Image object

  • ratio (float) – Image resize ratio (network load when many are used at once)

resize(img, w, h)[source]

Resizes an OpenCV image.

Parameters:
  • img (numpy.ndarray) – Image object

  • w (int) – Width of the resized image (in pixels).

  • h (int) – Height of the resized image (in pixels).

Returns:

Resized numpy.ndarray image object

rotate(img, degree=10, ratio=0.9)[source]

Rotates an image.

Parameters:
  • img (numpy.ndarray) – Image object

  • degree (int) – Rotation angle

  • ratio (float) – Scale factor (a number greater than 0. Less than 1.0 shrinks, greater enlarges)

Returns:

Rotated numpy.ndarray image object

imwrite(filename, img)[source]

Saves an image to a file.

Parameters:
  • filename (str) – Path of the file to save (jpg, png)

  • img (numpy.ndarray) – Image object to save

draw_bitmap(w, h, val, background=(255, 255, 255), pixel=(0, 0, 0))[source]

Creates an image from bitmap (dot picture) data, enlarged to the camera image size.

Parameters:
  • w (int) – Bitmap width

  • h (int) – Bitmap height

  • background (tuple) – Background color

  • pixel (tuple) – Bit color

Returns:

numpy.ndarray image object

rectangle(img, p1, p2, colors=(255, 255, 255), tickness=1)[source]

Draws a rectangle on an image.

Parameters:
  • img (numpy.ndarray) – Image object

  • p1 (tuple(int, int)) – Top-left coordinates (x, y)

  • p2 (tuple(int, int)) – Bottom-right coordinates (x, y)

  • colors (tuple(int, int, int)) – BGR value (b, g, r) or hex value ‘#rrggbb’

  • tickness (int) – Thickness of the rectangle border (in pixels); -1 fills it

circle(img, p, r, colors=(255, 255, 255), tickness=1)[source]

Draws a circle on an image.

Parameters:
  • img (numpy.ndarray) – Image object

  • p (tuple(int, int)) – Top-left coordinates (x, y)

  • r (int) – Radius

  • colors (tuple(int, int, int)) – BGR value (b, g, r) or hex value ‘#rrggbb’

  • tickness (int) – Thickness of the circle outline (in pixels); -1 fills it

line(img, p1, p2, colors=(255, 255, 255), tickness=1)[source]

Draws a straight line on an image.

Parameters:
  • img (numpy.ndarray) – Image object

  • p1 (tuple(int, int)) – Start coordinates (x, y)

  • p2 (tuple(int, int)) – End coordinates (x, y)

  • colors (tuple(int, int, int)) – BGR value (b, g, r) or hex value ‘#rrggbb’

  • tickness (int) – Line thickness (in pixels)

putTextPIL(img, text, points, size=30, colors=(255, 255, 255))[source]

Writes text on an image. (Korean/English supported - uses pillow)

Parameters:
  • img (numpy.ndarray) – Image object

  • text (str) – Text to display

  • points (tuple(int, int)) – Top-left coordinates of the text block (x, y)

  • size (int) – Font size of the text

  • colors (tuple(int, int, int)) – Text color BGR value (b, g, r) or hex value ‘#rrggbb’

putText(img, text, points, size=1, colors=(255, 255, 255), tickness=1)[source]

Writes text on an image. (English only)

Parameters:
  • img (numpy.ndarray) – Image object

  • text (str) – Text to display

  • points (tuple(int, int)) – Bottom-left coordinates of the text block (x, y)

  • size (int) – Font size of the text

  • colors (tuple(int, int, int)) – Text color BGR value (b, g, r) or hex value ‘#rrggbb’

  • tickness (int) – Text thickness

stylization(img, sigma_s=100, sigma_r=0.5)[source]

Converts to a cartoon image. (opencv api) low speed

Parameters:
  • img (numpy.ndarray) – Image object

  • sigma_s (float) – Degree of blur preservation of the image (1-200)

  • sigma_r (float) – Degree of edge application of the image (0-1)

Returns:

numpy.ndarray image object

detailEnhance(img, sigma_s=100, sigma_r=0.05)[source]

Converts to a cartoon image. (opencv api)

Parameters:
  • img (numpy.ndarray) – Image object

  • sigma_s (float) – Degree of blur preservation of the image (1-200)

  • sigma_r (float) – Degree of edge application of the image (0-1)

Returns:

numpy.ndarray image object

pencilSketch(img, sigma_s=100, sigma_r=0.2, shade_factor=0.018)[source]

Converts to a sketch image.

example:

img = camera.read()
camera.sketchize(img)
Parameters:
  • img (numpy.ndarray) – Image object

  • sigma_s (float) – Degree of blur preservation of the image (1-200)

  • sigma_r (float) – Degree of edge application of the image (0-1)

  • shade_factor (float) – Brightness level of the image (0-0.1)

Returns:

numpy.ndarray image object / grayscale, bgr

edgePreservingFilter(img, flags=1, sigma_s=60, sigma_r=0.4)[source]

Converts to a blurred image.

Parameters:
  • img (numpy.ndarray) – Image object

  • flags (int) – Filter type 1 (RECURS_FILTER) or 2 (NORMCONV_FILTER)

  • sigma_s (float) – Degree of blur preservation of the image (1-200)

  • sigma_r (float) – Degree of edge application of the image (0-1)

Returns:

numpy.ndarray image object

flip(img, flags=1)[source]

Converts to a vertically/horizontally flipped image

Parameters:
  • img (numpy.ndarray) – Image object

  • flags (int) – 0: vertical flip, 1: horizontal flip, -1: vertical and horizontal flip

Returns:

numpy.ndarray image object