Repository navigation
opencv-python-headless & opencv-contrib-python #4712
|
"opencv-contrib-python", |
Replies: 2 comments
|
Install
dependencies = [
"flet",
"opencv-python-headless",
]
[tool.flet.android]
extract_packages = ["cv2"]Minimal appRun a real cv2 pipeline and show the result. Since there's no GUI, display images through Flet's import flet as ft
import cv2
import numpy as np
def main(page: ft.Page):
img = np.zeros((200, 200, 3), dtype=np.uint8)
cv2.circle(img, (100, 100), 70, (0, 140, 255), -1)
edges = cv2.Canny(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 50, 150)
ok, png = cv2.imencode(".png", edges) # encode to PNG in memory
page.add(
ft.Text(
f"cv2 {cv2.__version__}: {int((edges > 0).sum())} edge pixels",
weight=ft.FontWeight.BOLD,
),
ft.Image(src=png.tobytes(), width=200, height=200),
)
ft.run(main)On device this shows the edge-detected circle plus the computed edge-pixel count. Recommendations
|
|
Install
# pyproject.toml
dependencies = [
"flet",
"opencv-contrib-python",
]
[tool.flet.android]
extract_packages = ["cv2"]Minimal appRun a real cv2 pipeline and show the result. There's no GUI on a phone, so display images through Flet controls rather than import flet as ft
import cv2
import numpy as np
def main(page: ft.Page):
img = np.zeros((200, 200, 3), dtype=np.uint8)
cv2.circle(img, (100, 100), 70, (0, 140, 255), -1)
edges = cv2.Canny(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 50, 150)
ok, png = cv2.imencode(".png", edges)
page.add(
ft.Text(
f"cv2 {cv2.__version__}: {int((edges > 0).sum())} edge pixels",
weight=ft.FontWeight.BOLD,
),
ft.Image(src=png.tobytes(), width=200, height=200), # src takes bytes
)
ft.run(main)For live/streaming frames (video, real-time filters, per-frame detection), raw = ft.RawImage(width=320, height=240)
page.add(raw)
for frame in your_frames(): # BGR uint8 arrays
await raw.render(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # numpy straight inRecommendations
|
opencv-contrib-python5.0.0.93 is now supported on Android and iOS: https://pypi.flet.dev/opencv-contrib-pythonopencv-contrib-pythonis OpenCV with theopencv_contribextra modules folded into the sameimport cv2— aruco, face, tracking, ximgproc, wechat_qrcode, img_hash and more, on top of everything the base OpenCV gives you.Install
numpycomes in automatically as an OpenCV dependency:Minimal app
Run a real cv2 pipeline and show the result. There's no GUI on a phone, so display images through Flet controls rather than
cv2.imshow: