2,856
社区成员




目录
(七) 高通手机跑AI系列之——人像与背景分割-CSDN博客
(一)AidLux 服务进程被杀死(Android 12 及以上系统)
AidLux 是一款基于 ARM 架构的跨生态(Android / 鸿蒙 + Linux)一站式 AIOT 应用开发平台。它能在安卓手机上直接运行,为开发者提供了原生 Android 和原生 Linux 的使用体验。借助 AidLux,开发者可以便捷地在手机端进行 Python 编程,实现各类 AI 和其他应用的开发。
下载完成后,点击安装包进行安装。安装过程中,手机可能会提示需要授予 AidLux 一些系统权限,如媒体和文件、相机、麦克风、后台弹窗等,务必全部授予,以确保 AidLux 能正常运行各项功能。
首次打开 AidLux 应用,软件将进行 Linux 环境配置。该环节依据网络环境及手机硬件性能的不同,大约耗时 1 - 3 分钟。请耐心等待,在此期间保持网络连接稳定。
初始化完成后,进入登录界面。输入注册时使用的账号和密码进行登录。若首次登录,需设置新密码。设置密码时,请牢记密码,以便后续登录使用,记得登录,避免运行代码时出现异常。
成功登录后,会弹出一个界面询问是否进行某些设置,直接点击 “否”。若点击 “是”,可能会进入一个僵持界面,无法进行后续操作。点击 “否” 后,即可进入 AidLux 的 Linux 系统桌面。
若不幸出现异常界面(通常在 Android 12 及以上系统可能出现),可尝试关闭 AidLux 应用后重新打开。这可能是由于系统设置问题导致 AidLux 进程被干扰,重新打开应用一般可解决该问题。
查看Linux的系统版本
lsb_release -a
在 AidLux 中使用 Python 进行编程时,可从一些简单的代码示例入手。
import cv2
import numpy as np
# 创建空白图像
width, height = 400, 300
image = np.ones((height, width, 3), dtype=np.uint8) * 255 # 白色背景
# 文本和按钮属性
text = "Hello World!"
button_text = "Press me!"
button_color = (0, 120, 255) # 蓝色
text_color = (0, 0, 0) # 黑色
# 绘制初始文本
cv2.putText(image, text, (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
# 按钮位置和尺寸
button_x, button_y = 100, 150
button_width, button_height = 200, 50
# 绘制按钮
cv2.rectangle(image, (button_x, button_y), (button_x + button_width, button_y + button_height), button_color, -1)
cv2.putText(image, button_text, (button_x + 50, button_y + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, text_color, 2)
# 按钮点击状态
button_pressed = False
def mouse_callback(event, x, y, flags, param):
global button_pressed, image
# 检查鼠标点击是否在按钮区域内
if event == cv2.EVENT_LBUTTONDOWN:
if button_x <= x <= button_x + button_width and button_y <= y <= button_y + button_height:
button_pressed = not button_pressed
# 更新图像
image = np.ones((height, width, 3), dtype=np.uint8) * 255 # 重置背景
# 根据按钮状态更新文本
display_text = "Button pressed!" if button_pressed else text
cv2.putText(image, display_text, (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
# 绘制按钮(状态可能改变)
cv2.rectangle(image, (button_x, button_y), (button_x + button_width, button_y + button_height),
button_color if not button_pressed else (0, 255, 0), -1)
cv2.putText(image, button_text, (button_x + 50, button_y + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, text_color, 2)
# 创建窗口并设置鼠标回调
cv2.namedWindow("Hello World App")
cv2.setMouseCallback("Hello World App", mouse_callback)
# 主循环
while True:
cv2.imshow("Hello World App", image)
# 按ESC键退出
key = cv2.waitKey(1)
if key == 27: # ESC键
break
cv2.destroyAllWindows()
代码运行效果图:
因为有UI,因此需要在xfce的桌面环境中运行,如果没有UI可以在AidLux提供的web desktop中运行。
import cv2
import numpy as np
# 创建空白图像
width, height = 400, 500
image = np.ones((height, width, 3), dtype=np.uint8) * 255 # 白色背景
# 计算器状态
display_text = "0"
buttons = []
button_size = 70
button_margin = 10
button_color = (0, 120, 255)
text_color = (0, 0, 0)
special_button_color = (255, 100, 100) # DEL和C按钮的颜色
# 按钮定义(行,列,标签)
button_layout = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),
('DEL', 0, 2), ('C', 0, 3) # 添加删除键和清除键
]
# 创建按钮
for label, row, col in button_layout:
x = button_margin + col * (button_size + button_margin)
y = 100 + row * (button_size + button_margin)
buttons.append({
'label': label,
'x': x,
'y': y,
'width': button_size,
'height': button_size
})
def draw_calculator():
global image
image = np.ones((height, width, 3), dtype=np.uint8) * 255 # 重置背景
# 绘制显示区域
cv2.rectangle(image, (button_margin, button_margin),
(width - button_margin, 80), (220, 220, 220), -1)
cv2.putText(image, display_text, (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 1.2, text_color, 2)
# 绘制按钮
for button in buttons:
x, y = button['x'], button['y']
w, h = button['width'], button['height']
# 特殊按钮使用不同颜色
if button['label'] in ['DEL', 'C']:
cv2.rectangle(image, (x, y), (x + w, y + h), special_button_color, -1)
else:
cv2.rectangle(image, (x, y), (x + w, y + h), button_color, -1)
cv2.putText(image, button['label'], (x + 25 if button['label'] != 'DEL' else x + 15, y + 45),
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
def mouse_callback(event, x, y, flags, param):
global display_text
if event == cv2.EVENT_LBUTTONDOWN:
for button in buttons:
bx, by = button['x'], button['y']
bw, bh = button['width'], button['height']
if bx <= x <= bx + bw and by <= y <= by + bh:
label = button['label']
if label == '=':
try:
result = eval(display_text)
display_text = str(result)
except:
display_text = "Error"
elif label == 'DEL':
# 实现回退删除功能
if len(display_text) > 1:
display_text = display_text[:-1]
else:
display_text = "0"
elif label == 'C':
# 实现清除功能
display_text = "0"
else:
if display_text == "0":
display_text = label
else:
display_text += label
draw_calculator()
# 初始化计算器显示
draw_calculator()
# 创建窗口并设置鼠标回调
cv2.namedWindow("Calculator")
cv2.setMouseCallback("Calculator", mouse_callback)
# 主循环
while True:
cv2.imshow("Calculator", image)
# 按ESC键退出
key = cv2.waitKey(1)
if key == 27: # ESC键
break
cv2.destroyAllWindows()
代码运行效果:
import cv2
import numpy as np
# 创建空白图像
width, height = 800, 600
image = np.ones((height, width, 3), dtype=np.uint8) * 255 # 白色背景
# 绘图状态
drawing = False
last_x, last_y = 0, 0
color = (0, 0, 0) # 黑色
size = 2
# 颜色选项
colors = {
'black': (0, 0, 0),
'red': (0, 0, 255),
'green': (0, 255, 0),
'blue': (255, 0, 0),
'yellow': (0, 255, 255)
}
# 尺寸选项
sizes = [1, 2, 5, 10]
# 工具栏高度
toolbar_height = 50
def draw_toolbar():
# 绘制工具栏背景
cv2.rectangle(image, (0, 0), (width, toolbar_height), (200, 200, 200), -1)
# 绘制颜色按钮
x = 10
for name, rgb in colors.items():
bgr = (rgb[2], rgb[1], rgb[0]) # OpenCV使用BGR顺序
cv2.rectangle(image, (x, 10), (x + 30, 40), bgr, -1)
if bgr == color: # 当前选中的颜色
cv2.rectangle(image, (x, 10), (x + 30, 40), (0, 0, 0), 2)
x += 40
# 绘制尺寸按钮
x += 20
for s in sizes:
cv2.rectangle(image, (x, 10), (x + 30, 40), (100, 100, 100), -1)
cv2.putText(image, str(s), (x + 10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
if s == size: # 当前选中的尺寸
cv2.rectangle(image, (x, 10), (x + 30, 40), (0, 0, 0), 2)
x += 40
def mouse_callback(event, x, y, flags, param):
global drawing, last_x, last_y, color, size
if y < toolbar_height: # 点击工具栏
if event == cv2.EVENT_LBUTTONDOWN:
# 检查颜色按钮
color_x = 10
for name, rgb in colors.items():
if color_x <= x <= color_x + 30:
color = (rgb[2], rgb[1], rgb[0]) # OpenCV使用BGR顺序
draw_toolbar()
break
color_x += 40
# 检查尺寸按钮
size_x = color_x + 20
for s in sizes:
if size_x <= x <= size_x + 30:
size = s
draw_toolbar()
break
size_x += 40
else: # 绘图区域
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
last_x, last_y = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
cv2.line(image, (last_x, last_y), (x, y), color, size)
last_x, last_y = x, y
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
cv2.line(image, (last_x, last_y), (x, y), color, size)
# 初始化界面
draw_toolbar()
# 创建窗口并设置鼠标回调
cv2.namedWindow("Drawing App")
cv2.setMouseCallback("Drawing App", mouse_callback)
# 主循环
while True:
cv2.imshow("Drawing App", image)
# 按ESC键退出
key = cv2.waitKey(1)
if key == 27: # ESC键
break
cv2.destroyAllWindows()
示例效果:
import cv2
import numpy as np
# 创建空白图像
width, height = 600, 500
image = np.ones((height, width, 3), dtype=np.uint8) * 255 # 白色背景
# 待办事项列表
tasks = []
task_input = ""
show_keyboard = False
# 字体和颜色设置
font = cv2.FONT_HERSHEY_SIMPLEX
text_color = (0, 0, 0)
button_color = (0, 120, 255)
task_bg_color = (220, 220, 220)
# 按钮和输入框位置
add_button = {'x': 500, 'y': 50, 'width': 80, 'height': 30}
input_box = {'x': 20, 'y': 50, 'width': 460, 'height': 30}
def draw_todo_app():
global image
image = np.ones((height, width, 3), dtype=np.uint8) * 255 # 重置背景
# 绘制标题
cv2.putText(image, "Todo List", (20, 30), font, 1, text_color, 2)
# 绘制输入框
cv2.rectangle(image, (input_box['x'], input_box['y']),
(input_box['x'] + input_box['width'], input_box['y'] + input_box['height']),
(200, 200, 200), -1)
cv2.putText(image, task_input, (input_box['x'] + 10, input_box['y'] + 20), font, 0.5, text_color, 1)
# 绘制添加按钮
cv2.rectangle(image, (add_button['x'], add_button['y']),
(add_button['x'] + add_button['width'], add_button['y'] + add_button['height']),
button_color, -1)
cv2.putText(image, "Add", (add_button['x'] + 25, add_button['y'] + 20), font, 0.5, text_color, 1)
# 绘制任务列表
for i, task in enumerate(tasks):
y_pos = 100 + i * 40
cv2.rectangle(image, (20, y_pos), (width - 20, y_pos + 30), task_bg_color, -1)
# 任务文本
cv2.putText(image, task['text'], (30, y_pos + 20), font, 0.5, text_color, 1)
# 删除按钮
delete_btn = {'x': width - 60, 'y': y_pos, 'width': 40, 'height': 30}
cv2.rectangle(image, (delete_btn['x'], delete_btn['y']),
(delete_btn['x'] + delete_btn['width'], delete_btn['y'] + delete_btn['height']),
(0, 0, 255), -1)
cv2.putText(image, "X", (delete_btn['x'] + 15, delete_btn['y'] + 20), font, 0.5, (255, 255, 255), 1)
# 保存按钮位置供点击检测
task['delete_btn'] = delete_btn
def mouse_callback(event, x, y, flags, param):
global tasks, task_input, show_keyboard
if event == cv2.EVENT_LBUTTONDOWN:
# 检查是否点击添加按钮
if (add_button['x'] <= x <= add_button['x'] + add_button['width'] and
add_button['y'] <= y <= add_button['y'] + add_button['height']):
if task_input.strip():
tasks.append({'text': task_input, 'completed': False})
task_input = ""
draw_todo_app()
# 检查是否点击删除按钮
for task in tasks[:]: # 使用副本避免迭代时修改列表
btn = task['delete_btn']
if btn['x'] <= x <= btn['x'] + btn['width'] and btn['y'] <= y <= btn['y'] + btn['height']:
tasks.remove(task)
draw_todo_app()
break
# 初始化应用
draw_todo_app()
# 创建窗口并设置鼠标回调
cv2.namedWindow("Todo App")
cv2.setMouseCallback("Todo App", mouse_callback)
# 主循环
while True:
cv2.imshow("Todo App", image)
# 处理键盘输入
key = cv2.waitKey(1) & 0xFF
if key == 27: # ESC键退出
break
elif key == 13: # Enter键添加任务
if task_input.strip():
tasks.append({'text': task_input, 'completed': False})
task_input = ""
draw_todo_app()
elif key == 8: # Backspace键删除字符
task_input = task_input[:-1]
draw_todo_app()
elif key >= 32 and key <= 126: # 可打印字符
task_input += chr(key)
draw_todo_app()
cv2.destroyAllWindows()
示例图:
建议使用手机系统自带的输入法。
在 AidLux 中,只有 “/home/AidLux” 目录有权限接受文件上传。若在进行文件上传等操作时遇到权限不足的提示,应确保将文件上传至该目录或其下属有权限的子目录中。同时,在使用文件浏览器进行新建文件夹、新建文件等操作时,若操作失败,也可能是权限问题,可检查当前操作目录是否有权限执行相应操作。
提示:AidLux环境中文件系统(home)有一个好处就是其他Android app无法访问,通过拖拽就能把电脑端的文件复制到手机中,而且只有输入了你的账号和密才能从AidLux入口访问,也是妥妥的隐私保护神器。