在UI原型設(shè)計(jì)和Python腳本實(shí)現(xiàn)UI原型兩個(gè)blog中已經(jīng)跟大家介紹了整個(gè)智能魔鏡端的交互UI的設(shè)計(jì)原型和實(shí)現(xiàn)的Python腳本及方式,這里將進(jìn)一步教大家如何實(shí)現(xiàn)整個(gè)UI交互的邏輯控制程序,根據(jù)智能魔鏡整體設(shè)計(jì)文章中的設(shè)計(jì)方案,智能魔鏡交互主要是依托攝像頭、超聲波和顯示屏與用戶進(jìn)行交互,智能魔鏡依托超聲波模塊實(shí)時(shí)的監(jiān)控鏡子前面的人體動(dòng)態(tài),在有人靠近的時(shí)候啟動(dòng)攝像頭,然后通過圖像識(shí)別來處理和識(shí)別當(dāng)前的用戶身份,最后根據(jù)用戶身份啟動(dòng)顯示模塊控制顯示,將指定的用戶消息推送顯示在當(dāng)前使用鏡子的用戶面前。
以上就是在智能魔鏡中的顯示控制邏輯設(shè)計(jì)中需要完成的內(nèi)容,在具體實(shí)現(xiàn)的過程中,對(duì)于顯示邏輯控制這一塊,主要是通過構(gòu)建了一個(gè)magic_mirroWindows.py文件來實(shí)現(xiàn),在這里實(shí)現(xiàn)了對(duì)魔鏡的交互UI類的實(shí)例化,同時(shí)創(chuàng)建了一個(gè)magic_mirroWindows類來實(shí)現(xiàn)魔鏡UI控制的各個(gè)邏輯控制功能,其中主邏輯是通過實(shí)例化該類來啟動(dòng)魔鏡的UI交互程序,在UI交互程序控制中,首先會(huì)通過函數(shù)調(diào)用刷新頁面的公共顯示信息,然后調(diào)用定時(shí)器對(duì)執(zhí)行過程進(jìn)行控制,定時(shí)的檢測(cè)當(dāng)前鏡子前面的情況,然后根據(jù)定時(shí)檢測(cè)的結(jié)果來執(zhí)行相應(yīng)的刷新程序,具體的代碼設(shè)計(jì)如下:
? ? ?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)
#定時(shí)刷新
? ? ? ? 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)
最后,只需要將主函數(shù)寫入該文件中,啟動(dòng)該類進(jìn)行實(shí)例化即可完成整個(gè)交互邏輯控制程序的啟動(dòng),也就是整個(gè)UI交互程序的啟動(dòng),具體的主函數(shù)代碼如下:
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_())??
在這種設(shè)計(jì)中,我們將整個(gè)魔鏡的UI設(shè)計(jì)和數(shù)據(jù)設(shè)計(jì)進(jìn)行了分離,最終在整合的過程中,只需要在啟動(dòng)超聲波、攝像頭監(jiān)控程序和數(shù)據(jù)庫程序,然后啟動(dòng)UI交互程序即可完成整個(gè)智能魔鏡的模的整合。
評(píng)論