鸿蒙跨端表情识别天气应用:基于用户情绪的个性化天气服务 原创

进修的泡芙
发布于 2025-6-22 16:32
浏览
0收藏

鸿蒙跨端表情识别天气应用:基于用户情绪的个性化天气服务

一、项目概述

本应用结合HarmonyOS 5的分布式能力和AI技术,实现以下核心功能:
多设备表情识别:通过手机/平板摄像头实时分析用户表情

情绪化天气播报:根据识别结果自动调整天气信息展示风格

跨端体验同步:在多设备间同步用户的情绪偏好和天气数据

二、技术架构

!https://example.com/harmonyos-weather-arch.png

采用三层架构:
设备层:摄像头采集+表情识别

服务层:分布式数据管理+天气API

展示层:自适应UI+语音合成

三、核心代码实现
表情识别模块

// EmotionDetector.ts
import image from ‘@ohos.multimedia.image’;
import facialExpression from ‘@ohos.ai.facialExpression’;

export class EmotionDetector {
private detector: facialExpression.FacialExpressionDetector;

async init() {
try {
this.detector = await facialExpression.createFacialExpressionDetector();
console.info(‘表情识别器初始化成功’);
catch (err) {

  console.error(初始化失败: {err.code}, {err.message});

}

async detectFromCamera(cameraOutput: image.Image): Promise<string> {
const emotionResult = await this.detector.detect(cameraOutput);
return this.getDominantEmotion(emotionResult);
private getDominantEmotion(result: facialExpression.FacialExpressionResult): string {

const emotions = {
  'neutral': result.neutral,
  'happy': result.happy,
  'sad': result.sad,
  'angry': result.angry,
  'surprised': result.surprised
};

return Object.entries(emotions).reduce((a, b) => a[1] > b[1] ? a : b)[0];

}

分布式数据同步

// WeatherPreferenceSync.ts
import distributedData from ‘@ohos.data.distributedData’;
import deviceManager from ‘@ohos.distributedHardware.deviceManager’;

interface WeatherPreference {
deviceId: string;
preferredStyle: ‘cheerful’ ‘calm’
‘professional’;
lastEmotion: string;
syncTime: number;
export class PreferenceSync {

private kvManager: distributedData.KVManager;
private kvStore: distributedData.KVStore;

async initKVStore() {
const context = getContext(this);
this.kvManager = distributedData.createKVManager({ context });

const options = {
  createIfMissing: true,
  encrypt: true,
  autoSync: true,
  kvStoreType: distributedData.KVStoreType.SINGLE_VERSION
};

this.kvStore = await this.kvManager.getKVStore('weather_preferences', options);

async updatePreference(deviceId: string, emotion: string) {

const style = this.mapEmotionToStyle(emotion);
const pref: WeatherPreference = {
  deviceId,
  preferredStyle: style,
  lastEmotion: emotion,
  syncTime: new Date().getTime()
};

await this.kvStore.put(deviceId, pref);
console.info('偏好设置已更新');

private mapEmotionToStyle(emotion: string): WeatherPreference[‘preferredStyle’] {

const map = {
  'happy': 'cheerful',
  'neutral': 'professional',
  'sad': 'calm',
  'angry': 'professional',
  'surprised': 'cheerful'
};
return map[emotion] || 'professional';

}

天气服务适配器

// WeatherService.ts
import http from ‘@ohos.net.http’;
import common from ‘@ohos.app.ability.common’;

export class WeatherService {
private httpRequest = http.createHttp();
private context: common.Context;

constructor(context: common.Context) {
this.context = context;
async getWeatherWithStyle(location: string, style: string): Promise<WeatherPresentation> {

const weatherData = await this.fetchWeatherData(location);
return this.applyPresentationStyle(weatherData, style);

private async fetchWeatherData(location: string): Promise<RawWeatherData> {

// 实际项目替换为真实天气API
return {
  temp: 25,
  condition: 'sunny',
  humidity: 60,
  windSpeed: 10
};

private applyPresentationStyle(data: RawWeatherData, style: string): WeatherPresentation {

const styles = {
  cheerful: {
    colorScheme: 'bright',
    adjectives: ['美好的', '宜人的'],
    iconStyle: 'cartoon'
  },
  calm: {
    colorScheme: 'pastel',
    adjectives: ['温和的', '舒适的'],
    iconStyle: 'minimal'
  },
  professional: {
    colorScheme: 'monochrome',
    adjectives: ['当前', '预计'],
    iconStyle: 'realistic'

};

return {
  ...data,
  ...styles[style],
  timestamp: new Date().getTime()
};

}

主页面实现(ArkUI)

// Index.ets
import { WeatherService } from ‘./WeatherService’;
import { EmotionDetector } from ‘./EmotionDetector’;
import { PreferenceSync } from ‘./WeatherPreferenceSync’;

@Entry
@Component
struct WeatherApp {
@State currentWeather: WeatherPresentation = {};
@State emotion: string = ‘neutral’;
@State preferredStyle: string = ‘professional’;

private weatherService = new WeatherService(getContext(this));
private emotionDetector = new EmotionDetector();
private preferenceSync = new PreferenceSync();

async aboutToAppear() {
await this.emotionDetector.init();
await this.preferenceSync.initKVStore();
this.loadInitialData();
async loadInitialData() {

// 从分布式数据库获取最新偏好
const deviceId = 'current-device'; // 实际项目获取真实deviceId
const pref = await this.preferenceSync.getPreference(deviceId);
this.preferredStyle = pref?.preferredStyle || 'professional';

// 获取天气数据
this.refreshWeather('Beijing');

async onCameraFrame(image: image.Image) {

this.emotion = await this.emotionDetector.detectFromCamera(image);
await this.preferenceSync.updatePreference('current-device', this.emotion);
this.refreshWeather('Beijing');

async refreshWeather(location: string) {

this.currentWeather = await this.weatherService.getWeatherWithStyle(
  location, 
  this.preferredStyle
);

build() {

Column() {
  // 天气展示区域
  WeatherDisplay({
    weather: this.currentWeather,
    style: this.preferredStyle
  })
  
  // 表情识别状态
  Text(当前情绪: ${this.emotion})
    .fontSize(16)
    .margin(10)
  
  // 分布式设备列表
  DeviceList({
    onDeviceSelected: (deviceId) => {
      this.syncWithDevice(deviceId);

})

}

@Component

struct WeatherDisplay {
@Prop weather: WeatherPresentation;
@Prop style: string;

build() {
Column() {
Image(this.getWeatherIcon())
.width(100)
.height(100)

  Text(${this.weather.adjectives?.[0] || ''}天气)
    .fontSize(24)
    .fontColor(this.getTextColor())
  
  Row() {
    Text(温度: ${this.weather.temp}°C)
    Text(湿度: ${this.weather.humidity}%)

.margin(10)

.padding(20)

.backgroundColor(this.getBackgroundColor())
.borderRadius(16)

private getWeatherIcon(): Resource {

// 根据style返回不同风格的图标资源
return r(app.media.{this.weather.condition}_${this.style});

private getBackgroundColor(): Resource {

const colorMap = {
  'cheerful': $r('app.color.cheerful_bg'),
  'calm': $r('app.color.calm_bg'),
  'professional': $r('app.color.professional_bg')
};
return colorMap[this.style];

private getTextColor(): Resource {

return this.style === 'professional' ? 
  r('app.color.text_dark') : r('app.color.text_light');

}

四、跨设备同步关键逻辑
设备间偏好同步

// 在PreferenceSync类中添加
async syncPreferencesAcrossDevices() {
const devices = deviceManager.getTrustedDeviceListSync();
const currentPref = await this.getCurrentPreference();

await Promise.all(devices.map(async device => {
if (device.deviceId !== ‘local-device’) {
await this.kvStore.put(device.deviceId, currentPref);
}));

// 监听其他设备偏好变化

setupPreferenceListener() {
this.kvStore.on(‘dataChange’, distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE,
async (changedItems) => {
const localDeviceId = ‘local-device’;
for (const { key, value } of changedItems) {
if (key !== localDeviceId && value.syncTime > this.lastSyncTime) {
await this.applyRemotePreference(value);
}

});

自适应UI更新策略

// 在WeatherDisplay组件中添加
@Watch(‘style’)
onStyleChanged() {
this.updatePresentation();
private updatePresentation() {

// 根据新style动态加载资源
animateTo({ duration: 300 }, () => {
this.weatherData = this.reapplyStyle(this.weatherData);
});

五、性能优化方案
表情识别频率控制:

// 节流处理摄像头帧
private lastDetectionTime = 0;
private readonly DETECTION_INTERVAL = 3000; // 3秒

async onCameraFrame(image: image.Image) {
const now = Date.now();
if (now - this.lastDetectionTime < this.DETECTION_INTERVAL) return;

this.emotion = await this.emotionDetector.detectFromCamera(image);
this.lastDetectionTime = now;
// …后续处理

分布式数据压缩:

async compressPreferenceData(pref: WeatherPreference): Promise<Uint8Array> {
const str = JSON.stringify(pref);
// 使用zlib压缩
return zlib.deflateSync(new TextEncoder().encode(str));

本地缓存策略:

const weatherCache = new Map<string, WeatherPresentation>();

async getCachedWeather(location: string): Promise<WeatherPresentation> {
const cacheKey = {location}_{this.preferredStyle};

if (weatherCache.has(cacheKey)) {
const cached = weatherCache.get(cacheKey);
if (Date.now() - cached.timestamp < 600000) { // 10分钟有效期
return cached;
}

const freshData = await this.weatherService.getWeatherWithStyle(location, this.preferredStyle);
weatherCache.set(cacheKey, freshData);
return freshData;

六、项目扩展方向
多模态交互:

结合语音识别确认用户情绪

增加触觉反馈增强情绪表达
智能场景推荐:

  recommendActivities(weather: WeatherPresentation, emotion: string): string[] {
 // 根据天气和情绪推荐活动
 if (emotion = 'happy' && weather.condition = 'sunny') {
   return ['公园散步', '户外摄影'];

// 其他情况处理…

分布式设备协同:

手机识别表情后,智慧屏自动切换展示风格

多设备摄像头协同提高识别准确率
历史情绪分析:

  analyzeEmotionTrend(): EmotionReport {
 // 分析一周情绪变化与天气关系

本方案充分运用了HarmonyOS 5的分布式能力与AI技术栈,实现了"一次开发,多端部署"的个性化天气服务。开发者可基于此框架快速扩展更多智能场景,打造更具情感化的跨设备体验。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
收藏
回复
举报
回复
    相关推荐