在UI原型设计和Python脚本实现UI原型两个blog中已经跟大家介绍了整个智能魔镜端的交互UI的设计原型和实现的Python脚本及方式,这里将进一步教大家如何实现整个UI交互的逻辑控制程序,根据智能魔镜整体设计文章中的设计方案,智能魔镜交互主要是依托摄像头、超声波和显示屏与用户进行交互,智能魔镜依托超声波模块实时的监控镜子前面的人体动态,在有人靠近的时候启动摄像头,然后通过图像识别来处理和识别当前的用户身份,最后根据用户身份启动显示模块控制显示,将指定的用户消息推送显示在当前使用镜子的用户面前。
以上就是在智能魔镜中的显示控制逻辑设计中需要完成的内容,在具体实现的过程中,对于显示逻辑控制这一块,主要是通过构建了一个magic_mirroWindows.py文件来实现,在这里实现了对魔镜的交互UI类的实例化,同时创建了一个magic_mirroWindows类来实现魔镜UI控制的各个逻辑控制功能,其中主逻辑是通过实例化该类来启动魔镜的UI交互程序,在UI交互程序控制中,首先会通过函数调用刷新页面的公共显示信息,然后调用定时器对执行过程进行控制,定时的检测当前镜子前面的情况,然后根据定时检测的结果来执行相应的刷新程序,具体的代码设计如下:
class magic_mirroWindows(QtWidgets.QWidget):
def __init__(self):
super(magic_mirroWindows,self).__init__()
self.new=Ui_MainWindow()
self.new.setupUi(self)
#save user video Info
self.push_video_userID=[]
self.videoPathList=[]
#self.viedoMsg.setWindowFlags(
self.zhishuUpdateTime=0
#set Text
self.tp=QtGui.QPalette(self)
self.tp.setColor(QtGui.QPalette.WindowText,QtGui.QColor(255,255,255))
self.movie=QtGui.QMovie()
#default userID =0 ,camera and face model not find user Infomation
self.current_userID=0
self.currentUserName=""
#default supper sound sensor status, suppper sound sensor not find people
self.curren_su_status=1
#self.showPushInfo(4)
#set html view is black
self.new.webMessageText.setHtml("
self.new.webMemorandum.setHtml("")
self.new.webZhishu.setHtml("")
self.img=QtGui.QImage()
self.manager = QtNetwork.QNetworkAccessManager()
#self.weather=getWeatherInfo()
self.weather_updateOK=0
self.new.helloWelcome.setText("
hello,welcom2...
")self.Reply=self.manager.get(QtNetwork.QNetworkRequest(QtCore.QUrl("http://wthrcdn.etouch.cn/WeatherApi?city=%E6%B7%B1%E5%9C%B3")))
self.Reply.finished.connect(self.replayFinished)
forecast_date_list.append(self.new.forecast_0_date)
forecast_date_list.append(self.new.forecast_1_date)
forecast_date_list.append(self.new.forecast_2_date)
forecast_date_list.append(self.new.forecast_3_date)
forecast_date_list.append(self.new.forecast_4_date)
forecast_temp_list.append(self.new.forecast_0_temp)
forecast_temp_list.append(self.new.forecast_1_temp)
forecast_temp_list.append(self.new.forecast_2_temp)
forecast_temp_list.append(self.new.forecast_3_temp)
forecast_temp_list.append(self.new.forecast_4_temp)
forecast_type_list.append(self.new.forecast_0_type)
forecast_type_list.append(self.new.forecast_1_type)
forecast_type_list.append(self.new.forecast_2_type)
forecast_type_list.append(self.new.forecast_3_type)
forecast_type_list.append(self.new.forecast_4_type)
#定时刷新
self.timer=QtCore.QTimer(self)
#self.count=0
self.timer.timeout.connect(self.timerControl)
# test button
self.new.closeButton.setStyleSheet("QPushButton {border-image:url(:/images/close);}QPushButton:hover{border-image:url(:/images/close_on);}QPushButton:hover:pressed{border-image:url(:/images/close);}")
time = QtCore.QDateTime.currentDateTime()
self.new.date.setPalette(self.tp)
self.new.date.setText(time.toString("yyyy-MM-dd"))
palette1 = QtGui.QPalette(self)
palette1.setColor(self.backgroundRole(),QtGui.QColor(0,0,0))
self.setPalette(palette1)
self.setWindowTitle('w')
self.setWindowFlags(Qt.FramelessWindowHint)
def paintEvent(self,QPaintEvent):
painter=QtGui.QPainter(self)
#painter.drawPixmap(0, 0, pixmap) #绘制UI
def replayFinished(self):
global weather
print("weather replayFinished")
qbyte = self.Reply.readAll()
pbyte = bytes(qbyte.data())
str(pbyte, encoding = "utf-8")
weatherInfo = bytes.decode(pbyte)
weather=getWeatherInfo(weatherInfo)
self.updateUIdata(weather)
def updateUIdata(self,weatherResult):
for i in range(0,5):
forecast_date_list[i].setPalette(self.tp)
forecast_date_list[i].setText(weatherResult.forecast_weather_info_date[i])
forecast_temp_list[i].setPalette(self.tp)
forecast_temp_list[i].setText(weatherResult.forecast_weather_info_low[i][3:6]+"~"+weatherResult.forecast_weather_info_high[i][3:6])
forecast_type_list[i].setPalette(self.tp)
forecast_type_list[i].setPixmap(QtGui.QPixmap(":/images/"+weatherResult.forecast_weather_info_dtype[i]))
self.new.city.setPalette(self.tp)
self.new.city.setText(weatherResult.weather_city)
self.new.temp.setPalette(self.tp)
self.new.temp.setText(weatherResult.weather_wendu+"℃")
self.new.fengli.setPalette(self.tp)
self.new.fengli.setText(weatherResult.weather_fengli)
self.new.fengxiang.setPalette(self.tp)
self.new.fengxiang.setText(weatherResult.weather_fengxiang)
self.new.shidu.setPalette(self.tp)
self.new.shidu.setText(weatherResult.weather_shidu)
self.new.label_3.setPalette(self.tp)
self.new.label_3.setText("湿度")
self.updateZhishu()
self.timer.start(1000)
def updateZhishu(self):
global weather
if self.zhishuUpdateTime == 0:
i=random.randint(0,5)
format="
%s:
%s
"values=(weather.weather_zhishu_name[i],weather.weather_zhishu_data[i])
result= format % values
self.new.webZhishu.setHtml(result)
self.zhishuUpdateTime=5
else:
self.zhishuUpdateTime=self.zhishuUpdateTime-1
# start Personalized Display by userID when system detect someone in front of the magic mirror
def startPersonalizedDisplay(self,userID):
self.showPushInfo(userID)
#display user video msg
def detectPeople(self):
MMDB=MagicMirrorDB("./db.sqlite3")
#get System Status
#statusName peopeleInfront
#statusValue{0,1,2},0 no people in front,1 have people in front of mirror but no detect people ID
#2 have people in front of mirror and detect people ID return ID
result=MMDB.getSystemStatusValue("peopleInfront")
print("peopleInfront")
print(result)
if result[0]==1:
return 0
elif result[0]==2:
print("front people ID:")
print(result[1])
return result[1] #return userID
else:
return -1
def timerControl(self):
#self.updateZhishu()
# test have people
detectResult=self.detectPeople()
if detectResult==-1:
print("no people")
self.hide()
elif detectResult==0:
self.showNormalInfo()
self.show()
print("have people but no ID")
else:
self.show()
print(detectResult)
print("in front of magicmirror")
if self.current_userID!=detectResult:
self.startPersonalizedDisplay(detectResult)
self.current_userID=detectResult
else:
return
def showNormalInfo(self):
self.new.webMessageText.setHtml("")
self.new.webMemorandum.setHtml("")
self.new.helloWelcome.setText("
hello,welcom2...
")def showPushInfo(self,userID):
MMDB=MMDB=MagicMirrorDB("db.sqlite3")
#update welcome info by userID
userName=MMDB.getUserName(userID)
format="
Hello %s Welcome...
"values=(userName)
result=format % values
self.new.helloWelcome.setText(result)
#update pushInfo
#infoID,pushID,infoSubject,infoContent,pushTime
msgList=MMDB.getTextMessageList(userID)
print("test0")
print(msgList)
print("hello")
i=0
pushName=""
msg_info=""
pushTime=""
msgStr=""
selfMsgStr=""
if msgList!=None:
for msg in msgList:
print(msg)
print("test1")
print(msg)
pushName=MMDB.getUserName(msg[1])
msg_info=msg[3]
pushTime=msg[4]
if msg[1]!=userID:
msgStr+=("
"+pushTime+"
"+""+pushName+"to you:"+"
"+msg_info+"
")else:
selfMsgStr+=("
"+msg_info+"
")self.new.webMessageText.setHtml("")
self.new.webMemorandum.setHtml("")
videoMsgList=MMDB.getUserVideoMessageList(self.current_userID)
print("videoMsgList")
print(videoMsgList)
#show message end get video message info
if videoMsgList!=None:
print("start play video msg")
i=0
for videoMsg in videoMsgList:
if i%2==0:
print(videoMsg)
self.push_video_userID.append(videoMsg)
else:
print(videoMsg)
self.videoPathList.append(videoMsg)
i=i+1
videocmd="mplayer -vo x11 -playlist "
for video in self.videoPathList:
videocmd=videocmd+' '+video
os.system(videocmd)
最后,只需要将主函数写入该文件中,启动该类进行实例化即可完成整个交互逻辑控制程序的启动,也就是整个UI交互程序的启动,具体的主函数代码如下:
if __name__=="__main__":
import sys
import os
app=QtWidgets.QApplication(sys.argv)
pixmap = QtGui.QPixmap(":/images/UI1")
myshow=weatherWindows()
myshow.show()
sys.exit(app.exec_())
在这种设计中,我们将整个魔镜的UI设计和数据设计进行了分离,最终在整合的过程中,只需要在启动超声波、摄像头监控程序和数据库程序,然后启动UI交互程序即可完成整个智能魔镜的模的整合。
评论
查看更多