Skip to content

AidCV Python API

Create Window namedWindow

namedWindow(winname, flags = 0, size = None, port = None) -> None
Window name bound to the window, must be unique.
Window properties, invalid parameter, kept for OpenCV compatibility.
Window size, first value is width, second is height, in the form of [width, height].
Port number to use, a random port will be used when default or unusable port is provided.
None

Builds a web page using the specified port or a random port, while creating a window in the Web Desktop pointing to this page. The web page is bound to the winname, and based on this winname, developers can use other APIs to affect this web page. When multiple Web Desktops are open, this window will appear in each Web Desktop.

Note: Unlike OpenCV, windows created in the Web Desktop cannot be freely resized. If developers want to control the window size to match the image, please explicitly pass the size parameter when using this API.

Usage Example

python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cv2.namedWindow("win0")

cv2.namedWindow("win1", cv2.WINDOW_AUTOSIZE)

cv2.namedWindow("win2", size = (640,480))

cv2.namedWindow("win3", cv2.WINDOW_AUTOSIZE, size = (640,480))

Register Mouse Callback Function setMouseCallback

setMouseCallback(winname, onMouse, userdata = 0) -> None
Name bound to the target window.
Callback function that will be called when the mouse clicks on the target window. The callback function must follow this format: call_back(event, x, y, flags, param). Where event is the constant corresponding to the triggered mouse event type, consistent with OpenCV's definition. x, y are the click positions of the mouse. flags is a constant for mouse event flags, an invalid parameter kept for OpenCV compatibility. param is the parameter passed in by the developer, which is the third parameter of the setMouseCallback function.
Optional parameter passed to the callback function, custom external data defined by the developer.
None
Sets the mouse callback function for the specified window, currently only responds to mouse click events.

Usage Example

python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

import time

def call_back(event, x, y, flags, params):
    print(event,x,y,params)

cv2.namedWindow("win")
cv2.setMouseCallback("win", call_back)
time.sleep(10)

Wait for Key Event waitKey

waitKey(delay = 0) -> int or str
Timeout in milliseconds, will wait indefinitely for a key event when value is less than or equal to 0.
Constant of int or str type, representing the ASCII code or name of the captured key.
Captures the next keyboard key event.

Usage Example

python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cv2.namedWindow("aa")
res = cv2.waitKey(10)
print(res)

Display Image imshow

imshow(winname, frame, port = None) -> None
Name bound to the target window. If the target window does not exist, namedWindow(winname, size = (frame.shape[1], frame.shape[0]), port = port) will be automatically called to create the window.
Image data to be displayed, typically BGR color image data is provided and displayed as RGB color.
None

Displays the specified image in the target window.

Usage Example

python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2
import time

frame = cv2.imread("test.jpg")

cv2.namedWindow("win0")

cv2.imshow("win0", frame)
cv2.imshow("win1", frame)

time.sleep(5)

Destroy Window destroyWindow

destroyWindow(winname) -> None
Name bound to the target window.
None

Destroys and closes the window named winname.

Usage Example

python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2
import time

cv2.namedWindow("win0")
cv2.namedWindow("win1")

time.sleep(3)
cv2.destroyWindow("win0")
time.sleep(5)

Destroy All Windows destroyAllWindows

destroyAllWindows() -> None
None
None

Destroys and closes all windows opened by the current process.

Usage Example

python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2
import time

cv2.namedWindow("win0")
cv2.namedWindow("win1")

time.sleep(3)
cv2.destroyAllWindows()
time.sleep(5)

Class VideoCapture

The VideoCapture class is used to capture video frames from cameras or video files. It supports USB cameras and MIPI cameras. Due to underlying limitations, it currently does not support opening multiple MIPI cameras simultaneously. When used without extended parameters, it behaves consistent with cv2.VideoCapture; using extended parameters initializes the VideoCapture object with AidCV extensions.

Initialization

The VideoCapture class has two initialization methods.

VideoCapture()
None
An initialized VideoCapture instance.
VideoCapture(filename, apiPreference = 0, device = "usb")
Video filename, USB camera node name, or MIPI camera index (0 for front camera, 1 for rear camera).
Backend selection, invalid parameter, kept for OpenCV compatibility.
Camera type, available values are "usb", "mipi".
An initialized VideoCapture instance that has opened the specified device or file.
Usage Example
python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cap = cv2.VideoCapture()

file_cap = cv2.VideoCapture("/home/aidlux/test.mp4")

usb_cap = cv2.VideoCapture("/dev/video2")

mipi_cap = cv2.VideoCapture(0, device = "mipi")

Member Functions

VideoCapture().open(filename, apiPreference = 0) -> None
Video filename, USB camera node name, or MIPI camera index (0 for front camera, 1 for rear camera).
Backend selection, invalid parameter, kept for OpenCV compatibility.
None

Opens a camera or file.

Usage Example
python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cap = cv2.VideoCapture()
cap.open("/dev/video2")
VideoCapture().isOpened() -> bool
None
Boolean constant indicating whether the camera has been successfully opened.
Opens the corresponding device or file. If the VideoCapture instance has already opened a camera, this function will not perform any operation.
Usage Example
python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cap = cv2.VideoCapture("/dev/video2")

print(cap.isOpened())
VideoCapture().read() -> (bool, numpy.array)
None
(ret, frame)
ret: Boolean constant indicating success or failure of capture.
frame: Numpy array, the captured video frame.
Captures an image frame from an opened device or file.
Usage Example
python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cap = cv2.VideoCapture("/dev/video2")

ret, frame = cap.read()
if ret:
  print(frame.shape)
VideoCapture().get(propId) -> float
Parameter index, maximum value is 18. The parameters corresponding to each index are consistent with OpenCV. Due to underlying implementation issues, when device="mipi", currently only 3: width, 4: height, 5: frame rate are effective.
Constant representing the value of the corresponding parameter.
Gets the default parameters of the camera.
Usage Example
python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cap = cv2.VideoCapture("/dev/video2")
print(cap.get(3), cap.get(4))
VideoCapture().set(propId, value) -> bool
Parameter index, maximum value is 18. The parameters corresponding to each index are consistent with OpenCV. Due to underlying implementation issues, when device="mipi", currently only 3: width, 4: height, 5: frame rate are effective.
Value to be set for the corresponding parameter.
Boolean constant indicating whether the parameter setting was successful.
Sets the specified parameter of the camera. The parameter to be specified and its value must be within the range allowed by the camera.
Usage Example
python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cap = cv2.VideoCapture("/dev/video2")
cap.set(3, 1920)
cap.set(4, 1080)
cap.set(5, 30)
ret, frame = cap.read()
print(frame.shape)
VideoCapture().release()
None
None
Releases the camera opened by this VideoCapture instance.
Usage Example
python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

cap = cv2.VideoCapture("/dev/video2")

cap.release()

Mathematical Calculation sigmoid

sigmoid(x) -> numpy.array
Independent variable of the sigmoid function.
Constant of numpy.array type, with the same shape as parameter x, representing the value of parameter x after calculation by the sigmoid function.

Usage Example

python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

x = np.array(range(8)).reshape(2,2,2)
print(cv2.sigmoid(x))

Mathematical Calculation softmax

softmax(x, axis = 0) -> numpy.array
Independent variable of the softmax function.
Performs calculation along the specified axis.
Constant of numpy.array type, with the same shape as parameter x, representing the value of parameter x on the specified axis after calculation by the softmax function.
#### Usage Example
python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2

x = np.array(range(8)).reshape(2,2,2)
print(cv2.softmax(x, axis = 2))

Generate Color Mask: colormask

colormask(mask, color) -> list
A 2D or 3D mask. 2D masks should be of int type, and 3D masks should be of float type.
A list of mapped colors, each represented by a 3-tuple (r, g, b).
[color_mask] or [color_mask,...]
[color_mask]: A 2D mask returns one color mask image, where each position indicates the class it belongs to.
[color_mask,...]: A 3D mask returns a list of color masks, each representing the probability of belonging to a specific class.

Uses the color to map a 2D or 3D mask mask into a color image. The mapping logic is as follows:

  • For 2D masks, the value at each position in the mask is an integer (e.g., 0, 1, 2,...), which is treated as the class index. Different values map to different colors.
  • For 3D masks, the third dimension represents different classes. The first two dimensions hold the probability that the position belongs to each class. Each class maps to a different color.

Example Usage

python
try: # aidcv-sdk>=1.0.4
    import aidcv as cv2
except:
    import cv2
import numpy as np

mask = np.array([[1, -1, 0], [2, 0, -2], [0, 3, -3]])[None]
colors = [(0,255,255)]
res = cv2.colormask(mask, colors)[0]
print(mask)
res = cv2.resize(res, (500,500), interpolation = cv2.INTER_NEAREST)
cv2.namedWindow("win0")
cv2.imshow("win0", res)
cv2.waitKey(3000)
print("next example")

mask = np.array([[1, 3, 0], [2, 0, 0], [0, 3, 0]])
colors = [(0,255,255),(255,0,0),(0,255,0),(0,0,255)]
res = cv2.colormask(mask, colors)[0]
print(mask)
res = cv2.resize(res, (500,500), interpolation = cv2.INTER_NEAREST)
cv2.imshow("win0", res)
cv2.waitKey(3000)
print("over")