Skip to content

Commit

Permalink
Update client.py
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-yanxin authored Nov 7, 2021
1 parent 7c16b36 commit c415f1f
Showing 1 changed file with 180 additions and 38 deletions.
218 changes: 180 additions & 38 deletions client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,44 @@
import numpy as np
import requests
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QWidget
from win32 import win32api, win32gui, win32print
from win32.lib import win32con
from win32.win32api import GetSystemMetrics

#修改数据库连接,并进行数据库迁移操作


def get_real_resolution():
"""获取真实的分辨率"""
hDC = win32gui.GetDC(0)
# 横向分辨率
w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
# 纵向分辨率
h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES)
return w, h


class Ui_MainWindow(QtWidgets.QWidget):

def __init__(self,parent=None):
def __init__(self):
hDC = win32gui.GetDC(0)
# 横向分辨率
w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
# 纵向分辨率
h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES)

self.rate = (w,h)
if self.rate != 1 and self.rate != 1.25 and self.rate != 1.5 and self.rate != 1.75 :
self.rate = 1

super().__init__(parent) #父类的构造函数

super(Ui_MainWindow, self).__init__()


self.timer_camera = QtCore.QTimer() #定义定时器,用于控制显示视频的帧率

Expand All @@ -33,49 +61,164 @@ def __init__(self,parent=None):
'''程序界面布局'''

def set_ui(self):
self.resize(1080*self.rate, 720*self.rate)

self.__layout_main = QtWidgets.QHBoxLayout() #总布局

self.__layout_fun_button = QtWidgets.QVBoxLayout() #按键布局

self.__layout_data_show = QtWidgets.QVBoxLayout() #数据(视频)显示布局

self.button_open_camera = QtWidgets.QPushButton('打开相机') #建立用于打开摄像头的按键

# 窗口无标题栏、窗口置顶、窗口透明
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
# self.setAttribute(Qt.WA_TranslucentBackground)

# 窗口图标
self.icon = QIcon()
self.icon.addPixmap(QPixmap("D:\\project\\Smart_container\\client\\image\\icon.png"), QIcon.Normal, QIcon.On)
self.setWindowIcon(self.icon)

# 矩形框
self.rect_label = QLabel(self)
self.rect_label.setGeometry(QRect(0*self.rate, 0*self.rate, 1080*self.rate, 720*self.rate))
self.rect_label.setStyleSheet("background-color: rgba(255, 255, 255, 0.7);"
"border-width: 5px 5px 5px 5px;"
"border:2px solid #5453FF;"
"border-radius:0px;")

# 打开相机按钮矩形框
self.rect_label = QLabel(self)
self.rect_label.setGeometry(QRect(920*self.rate, 225*self.rate, 95*self.rate, 60*self.rate))
self.rect_label.setStyleSheet("background-color: rgba(255, 255, 255, 0);"
"border-width: 5px 5px 5px 5px;"
"border:2px solid #5453FF;"
"border-radius:15px;")

# 打开相机按钮
self.button_open_camera = QPushButton(self)
self.button_open_camera.setGeometry(QRect(940*self.rate, 230*self.rate, 50*self.rate, 50*self.rate))
self.button_open_camera.setStyleSheet("background: rgba(255, 255, 255, 0);"
"color: #4E4EF2;"
"font: 15pt \"华光粗圆_CNKI\";"
"font-weight:bold;")
self.button_open_camera.setCursor(QCursor(Qt.PointingHandCursor))
self.button_open_camera.setText("拍摄")


# 退出按钮矩形框
self.rect_label = QLabel(self)
self.rect_label.setGeometry(QRect(920*self.rate, 455*self.rate, 95*self.rate, 60*self.rate))
self.rect_label.setStyleSheet("background-color: rgba(255, 255, 255, 0);"
"border-width: 5px 5px 5px 5px;"
"border:2px solid #5453FF;"
"border-radius:15px;")


# 退出按钮
self.button_close = QPushButton(self)
self.button_close.setGeometry(QRect(940*self.rate, 460*self.rate, 50*self.rate, 50*self.rate))
self.button_close.setStyleSheet("background: rgba(255, 255, 255, 0);"
"color: #4E4EF2;"
"font: 15pt \"华光粗圆_CNKI\";"
"font-weight:bold;"
)

self.button_close.setCursor(QCursor(Qt.PointingHandCursor))
self.button_close.setText("退出")

self.title_label = QLabel(self)
self.title_label.setGeometry(QRect(180*self.rate, 25*self.rate, 420*self.rate, 50*self.rate))
self.title_label.setStyleSheet("color: #5453FF;"
"background: transparent;"
"font: 20pt \"华光粗圆_CNKI\";"
"font-weight:bold;")
self.title_label.setText("袋鼯麻麻——智能购物平台")

self.setWindowTitle('袋鼯麻麻——智能购物平台')

self.button_close = QtWidgets.QPushButton('退出') #建立用于退出程序的按键
# Paddle Logo
self.paddle_logo=QLabel("图片",self)
self.paddle_pic=QPixmap("D:\\project\\Smart_container\\client\\image\\paddlepaddle.png")
self.paddle_logo.setPixmap(self.paddle_pic)
self.paddle_logo.setGeometry(QRect(3*self.rate, 3*self.rate, 400*self.rate,105*self.rate))

self.button_open_camera.setMinimumHeight(50) #设置按键大小

self.button_close.setMinimumHeight(50)
# 袋鼯 Logo
self.daiwu_logo=QLabel("图片",self)
self.daiwu_pic=QPixmap("D:\\project\\Smart_container\\client\\image\\icon.png")
self.daiwu_logo.setPixmap(self.daiwu_pic)
self.daiwu_logo.setGeometry(QRect(881*self.rate, 10*self.rate, 190*self.rate, 150*self.rate))

self.setWindowTitle('袋鼯麻麻——智能购物平台')
self.setWindowIcon(QIcon('./image/icon.png'))

self.button_close.move(10,100) #移动按键
# 拍摄识别框
self.rect_label = QLabel(self)

'''信息显示'''
self.rect_label.setGeometry(QRect(60*self.rate, 110*self.rate, 820*self.rate,560*self.rate))
self.rect_label.setStyleSheet("background-color: rgba(255, 255, 255, 0);"
"border-width: 5px 5px 5px 5px;"
"border:1px solid #5453FF;"
"border-radius:15px;"
"font-weight:bold;")

self.label_show_camera = QtWidgets.QLabel() #定义显示视频的Label
# add logo
self.add_logo=QLabel("图片",self)
self.add_pic=QPixmap("D:\\project\\Smart_container\\client\\image\\add.png")
self.add_logo.setPixmap(self.add_pic)
self.add_logo.setGeometry(QRect(410*self.rate, 200*self.rate, 120*self.rate,120*self.rate))

self.label_show_camera.setFixedSize(641,481) #给显示视频的Label设置大小为641x481

'''把按键加入到按键布局中'''
self.text_label = QLabel(self)
self.text_label.setGeometry(QRect(200*self.rate, 350*self.rate, 540*self.rate, 300*self.rate))
self.text_label.setStyleSheet("background-color: rgba(255, 255, 255, 0);"
"border-width: 5px 5px 5px 5px;"
"border:2px dotted #5453FF;"
"border-radius:15px;")

self.__layout_fun_button.addWidget(self.button_open_camera) #把打开摄像头的按键放到按键布局中
self.text = QLabel(self)
self.text.setGeometry(QRect(340*self.rate, 350*self.rate, 420*self.rate, 50*self.rate))
self.text.setStyleSheet("color: #5453FF;"
"background: transparent;"
"font: 10pt \"华光粗圆_CNKI\";"
)

self.__layout_fun_button.addWidget(self.button_close) #把退出程序的按键放到按键布局中

'''把某些控件加入到总布局中'''
self.text.setText("袋鼯麻麻——智能购物平台使用指南")
self.note = QLabel(self)
self.note.setGeometry(QRect(290*self.rate, 420*self.rate, 420*self.rate, 200*self.rate))
self.note.setStyleSheet("color: #5453FF;"
"background: transparent;"
"font: 10pt \"华光粗圆_CNKI\";"
)
self.note.setText("->点击拍摄即可打开摄像头;\n\n->请务必将商品放置在摄像区域,以免识别遗漏;\n\n->放置完毕后点击识别等待片刻;\n\n->片刻后系统将自动结算商品总价;\n\n->若系统未正常返回商品价格,则证明\n 存在数据库中没有的商品,请联系管理员更新。")

self.__layout_main.addLayout(self.__layout_fun_button) #把按键布局加入到总布局中

self.__layout_main.addWidget(self.label_show_camera) #把用于显示视频的Label加入到总布局中
# 版本号
self.version_label = QLabel(self)
self.version_label.setGeometry(QRect(920*self.rate, 600*self.rate, 200*self.rate, 20*self.rate))
self.version_label.setStyleSheet("color: #5453FF;"
"background: transparent;"
"font: 10pt \"微软雅黑\";"
"font-weight:bold;")
self.version_label.setText("版本号: Ver 1.0 ")

'''总布局布置好后就可以把总布局作为参数传入下面函数'''

self.setLayout(self.__layout_main) #到这步才会显示所有控件
self.author_label = QLabel(self)
self.author_label.setGeometry(QRect(920*self.rate, 630*self.rate, 200*self.rate, 20*self.rate))
self.author_label.setStyleSheet("color: #5453FF;"
"background: transparent;"
"font: 8pt \"微软雅黑\";"
)
self.author_label.setText("——By thomas-yanxin")


# '''信息显示'''

self.label_show_camera = QtWidgets.QLabel(self) #定义显示视频的Label
self.label_show_camera.setGeometry(QRect(60*self.rate, 110*self.rate, 820*self.rate,560*self.rate))
self.label_show_camera.setFixedSize(820*self.rate,560*self.rate)



'''初始化所有槽函数'''

def slot_init(self):
Expand Down Expand Up @@ -107,15 +250,21 @@ def button_open_camera_clicked(self):

else:

self.timer_camera.stop() #关闭定时器
self.container_recognition()


def open_camera(self):

self.cap.release() #释放视频流
flag = self.cap.open(self.CAM_NUM) #参数是0,表示打开笔记本的内置摄像头,参数是视频文件路径则打开视频

self.label_show_camera.clear() #清空视频显示区域
if flag == False: #flag表示open()成不成功

self.button_open_camera.setText('打开相机')
msg = QtWidgets.QMessageBox.warning(self,'warning',"请检查相机于电脑是否连接正确",buttons=QtWidgets.QMessageBox.Ok)

else:

self.timer_camera.start(30) #定时器开始计时30ms,结果是每过30ms从摄像头中取一帧显示

self.container_recognition()


def lable_close(self):
Expand All @@ -135,7 +284,7 @@ def show_camera(self):

flag,self.image = self.cap.read() #从视频流中读取

show = cv2.resize(self.image,(640,480)) #把读到的帧的大小重新设置为 640x480
show = cv2.resize(self.image,(820*self.rate,560*self.rate)) #把读到的帧的大小重新设置为 640x480

show = cv2.cvtColor(show,cv2.COLOR_BGR2RGB) #视频色彩转换回RGB,这样才是现实的颜色

Expand All @@ -150,15 +299,10 @@ def deplay(self, value):
if value == "Please connect root to upload container's name and it's price!\n":

QMessageBox.information(self, "Warning","{}".format(value), QMessageBox.Yes | QMessageBox.No)

else:
QMessageBox.information(self, "价格清单","您一共购买了:\n{}".format(value), QMessageBox.Yes | QMessageBox.No)

self.lable_close()

self.message = " No face_recognition"

self.button_open_camera.setText('打开相机')


def getByte(self, path):

Expand All @@ -173,16 +317,16 @@ def getByte(self, path):

def container_recognition(self):

self.picture_file = '.\\test_client_pic\\test_client_pic.jpg'
self.picture_file = '.\\test_client_pic.jpg'

cv2.imwrite(self.picture_file, self.image)

img_str = self.getByte('.\\test_client_pic\\test_client_pic.jpg')
img_str = self.getByte('.\\test_client_pic.jpg')

requestsss={'name':'测试图片', 'image':img_str}
req = json.dumps(requestsss) #字典数据结构变json(所有程序语言都认识的字符串)

res=requests.post('localhost/reference_client/', data=req)
res=requests.post('http://106.12.78.130:8001/reference_client/', data=req)
print(type(res.text))
json_res = json.loads(res.text)
print(json_res['container'])
Expand Down Expand Up @@ -222,5 +366,3 @@ def container_recognition(self):
ui.show() #调用ui的show()以显示。同样show()是源于父类QtWidgets.QWidget的

sys.exit(app.exec_()) #不加这句,程序界面会一闪而过


0 comments on commit c415f1f

Please sign in to comment.