AidCV Python API
Camera and image-processing interfaces.
Create Window namedWindow
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| API | namedWindow(winname, flags = 0, size = None, port = None) -> None | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| winname | str | No default | Window name bound to the window, must be unique. | — |
| flags | int | 0 | Window properties, invalid parameter, kept for OpenCV compatibility. | — |
| size | No restriction | None | Window size, first value is width, second is height, in the form of [width, height]. | — |
| port | int | None | Port number to use, a random port will be used when default or unusable port is provided. | — |
| Return Value | 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
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
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| API | setMouseCallback(winname, onMouse, userdata = 0) -> None | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| winname | str | No default | Name bound to the target window. | — |
| onMouse | function | No default | 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. | — |
| userdata | No restriction | 0 | Optional parameter passed to the callback function, custom external data defined by the developer. | — |
| Return Value | None | — | — | — |
Sets the mouse callback function for the specified window, currently only responds to mouse click events.
Usage Example
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
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| API | waitKey(delay = 0) -> int or str | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| delay | int | 0 | Timeout in milliseconds, will wait indefinitely for a key event when value is less than or equal to 0. | — |
| Return Value | Constant of int or str type, representing the ASCII code or name of the captured key. | — | — | — |
Captures the next keyboard key event.
Usage Example
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
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| API | imshow(winname, frame, port = None) -> None | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| winname | str | No default | 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. | — |
| frame | numpy.array | No default | Image data to be displayed, typically BGR color image data is provided and displayed as RGB color. | — |
| Return Value | None | — | — | — |
Displays the specified image in the target window.
Usage Example
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
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| API | destroyWindow(winname) -> None | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| winname | str | No default | Name bound to the target window. | — |
| Return Value | None | — | — | — |
Destroys and closes the window named winname.
Usage Example
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
| Field 1 | Field 2 |
|---|---|
| API | destroyAllWindows() -> None |
| Parameters | None |
| Return Value | None |
Destroys and closes all windows opened by the current process.
Usage Example
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.
| Field 1 | Field 2 |
|---|---|
| Initialization | VideoCapture() |
| Parameters | None |
| Return Value | An initialized VideoCapture instance. |
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| Initialization | VideoCapture(filename, apiPreference = 0, device = "usb") | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| filename | str or int | No default | Video filename, USB camera node name, or MIPI camera index (0 for front camera, 1 for rear camera). | — |
| apiPreference | int | 0 | Backend selection, invalid parameter, kept for OpenCV compatibility. | — |
| device | str | "usb" | Camera type, available values are "usb", "mipi". | — |
| Return Value | An initialized VideoCapture instance that has opened the specified device or file. | — | — | — |
Usage Example
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
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| Member Function | VideoCapture().open(filename, apiPreference = 0) -> None | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| filename | str or int | No default | Video filename, USB camera node name, or MIPI camera index (0 for front camera, 1 for rear camera). | — |
| apiPreference | int | 0 | Backend selection, invalid parameter, kept for OpenCV compatibility. | — |
| Return Value | None | — | — | — |
Opens a camera or file.
Usage Example
try: # aidcv-sdk>=1.0.4
import aidcv as cv2
except:
import cv2
cap = cv2.VideoCapture()
cap.open("/dev/video2")
| Field 1 | Field 2 |
|---|---|
| Member Function | VideoCapture().isOpened() -> bool |
| Parameters | None |
| Return Value | 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
try: # aidcv-sdk>=1.0.4
import aidcv as cv2
except:
import cv2
cap = cv2.VideoCapture("/dev/video2")
print(cap.isOpened())
| Field 1 | Field 2 |
|---|---|
| Member Function | VideoCapture().read() -> (bool, numpy.array) |
| Parameters | None |
| Return Value | (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
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)
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| Member Function | VideoCapture().get(propId) -> float | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| propId | int | No default | 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. | — |
| Return Value | Constant representing the value of the corresponding parameter. | — | — | — |
Gets the default parameters of the camera.
Usage Example
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))
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| Member Function | VideoCapture().set(propId, value) -> bool | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| propId | int | No default | 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 | int | No default | Value to be set for the corresponding parameter. | — |
| Return Value | 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
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)
| Field 1 | Field 2 |
|---|---|
| Member Function | VideoCapture().release() |
| Parameters | None |
| Return Value | None |
Releases the camera opened by this VideoCapture instance.
Usage Example
try: # aidcv-sdk>=1.0.4
import aidcv as cv2
except:
import cv2
cap = cv2.VideoCapture("/dev/video2")
cap.release()
Mathematical Calculation sigmoid
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| API | sigmoid(x) -> numpy.array | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| x | numpy.array | No default | Independent variable of the sigmoid function. | — |
| Return Value | 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
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
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| API | softmax(x, axis = 0) -> numpy.array | — | — | — |
| Parameters | Parameter Name | Type | Default Value | Description |
| x | numpy.array | No default | Independent variable of the softmax function. | — |
| axis | int | 0 | Performs calculation along the specified axis. | — |
| Return Value | 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
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
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |
|---|---|---|---|---|
| API | colormask(mask, color) -> list | — | — | — |
| Parameters | Name | Type | Default | Description |
| mask | numpy.array | None | A 2D or 3D mask. 2D masks should be of int type, and 3D masks should be of float type. | — |
| color | list | None | A list of mapped colors, each represented by a 3-tuple (r, g, b). | — |
| Return | [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
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")