1.绘制方框
2.删除方框
3.显示图片
4.调整亮度,对比度,反色
5.放大,缩小
6.鼠标移入,移出
7.位置映射
8.鼠标按压,释放,移动事件
import copy
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import cv2
import numpy as np
AREA_SIZE = 240
MAX_EXPAND = 4
bodyParam_dool_list_opt = [[121, 79], [121, 211], [80, 131], [159, 127]]
class ExpandLabel(QLabel):
change_judge_num = pyqtSignal(int)
dool_line = pyqtSignal(list)
rm_dool_line = pyqtSignal(list)
change_judgetimes_num = pyqtSignal()
change_signal_pic_mode = pyqtSignal(int)
dool_line_warning = pyqtSignal(list)
change_automatic_alarm_num = pyqtSignal(list)
change_dool_box_line = pyqtSignal(list)
single_automatic_alarms_num = pyqtSignal()
achive_most_num = pyqtSignal()
def __init__(self):
super().__init__()
# 人像框
self.data = []
# 自动报警框
self.data2 = []
self.data3 = []
# 方框颜色(黄)
self.data_color = []
# 移动数据 左上角和右下角两个点
self.move_data = None
# 鼠标释放点坐标
self.x0 = 0
self.y0 = 0
# 鼠标释放点坐标
self.x1 = 0
self.y1 = 0
# 鼠标位置 在框内的
self.x2 = 0
self.y2 = 0
# 鼠标移动过程中坐标点
self.now_x = 0
self.now_y = 0
# 画框结束,结束为False ,画框过程中为True
self.flag = False
# 控制放大缩小,默认状态不放大 0
self.mode = 0
self.single = 1
# 图片放大倍率
self.expend_num = MAX_EXPAND
# 反色
self.flag2 = 0
# 亮度
self.light = 0
# 对比度
self.contrast = 1
# 右边显示的数字
self.contrast_num = 0
self.light_num = 0
self.img2 = None
# 拉框次数
self.judge_num = 0
# 鼠标移入、移出label中,记录鼠标状态
self.enter_mouse = self.mode
self.setMouseTracking(True)
self.pixmap = QPixmap()
self.mypixmap = QPixmap()
# 头、胯,左肩、右肩位置,从上位机返回的数据中读取到的计算出的中心点
self.head = []
self.crotch = []
self.l_shoulder = []
self.r_shoulder = []
# 存储上位机返回的所有数据
self.json_opt = None
# 中间方框点坐标
self.center_pos = []
# 人偶图对应的头、胯,左肩、右肩位置坐标,微调提高精度拉框对应
self.doll_calculate_point = [[121, 79], [100, 264], [76, 117], [156, 115]]
# 放在此处,便于调试使用,应该放在主控图片发送过来时解析人像中人体位置信息,获取比例,并计算人偶图对应的中心点坐标
# self.process_data()
self.alarmParam_front = []
self.alarmParam_back = []
self.img = None
self.x = None
self.y = None
self.w = None
self.h = None
def set_img_black(self, img):
'''
设置黑色图片显示在label上
1.页面初始化时加载
2.没有图片,或者一张图判图完成后加载
Args:
img_src:
Returns:
'''
self.img = cv2.imread(img, cv2.COLOR_BGR2GRAY)
self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
if self.img is not None:
showImage = QImage(self.img.data, self.img.shape[1], self.img.shape[0],
QImage.Format_Indexed8)
self.setPixmap(QPixmap.fromImage(showImage))
else:
self.update()
def set_img_human(self, img_data):
'''
设置前部人像图显示
接收到图片后加载
带马赛克功能 self.do_mosaic(x, y, w, h, neighbor=5)
Args:
img_data:
Returns:
'''
img_decode = cv2.imdecode(img_data, 1)
self.img = cv2.cvtColor(img_decode, cv2.COLOR_BGR2GRAY)
x, y, w, h = self.x, self.y, self.w, self.h
self.img = self.do_mosaic(x, y, w, h, neighbor=5)
if self.img is not None:
showImage = QImage(self.img.data, self.img.shape[1], self.img.shape[0],
QImage.Format_Indexed8)
self.setPixmap(QPixmap.fromImage(showImage))
else:
self.update()
def set_img_human_back(self, img_data):
'''
设置背部人像图显示
接收到图片后加载,没有马赛克功能
Args:
img_data:
Returns:
'''
img_decode = cv2.imdecode(img_data, 1)
self.img = cv2.cvtColor(img_decode, cv2.COLOR_BGR2GRAY)
if self.img is not None:
showImage = QImage(self.img.data, self.img.shape[1], self.img.shape[0],
QImage.Format_Indexed8)
self.setPixmap(QPixmap.fromImage(showImage))
else:
self.update()
def do_mosaic(self, x, y, w, h, neighbor=5):
"""
马赛克的实现原理是把图像上某个像素点一定范围邻域内的所有点用邻域内左上像素点的颜色代替,这样可以模糊细节,但是可以保留大体的轮廓。
:param frame: opencv frame
:param int x : 马赛克左顶点x
:param int y: 马赛克左顶点y
:param int w: 马赛克宽
:param int h: 马赛克高
:param int neighbor: 马赛克每一块的宽
"""
frame = self.img
fh, fw = frame.shape[0], frame.shape[1]
if (y + h > fh) or (x + w > fw):
return
for i in range(0, h - neighbor, neighbor): # 关键点0 减去neightbour 防止溢出
for j in range(0, w - neighbor, neighbor):
rect = [j + x, i + y, neighbor, neighbor]
color = frame[i + y][j + x].tolist() # 关键点1 tolist
left_up = (rect[0], rect[1])
right_down = (rect[0] + neighbor - 1, rect[1] + neighbor - 1) # 关键点2 减去一个像素
cv2.rectangle(frame, left_up, right_down, color, -1)
return frame
def changeBloack(self):
'''
反色
Args:
img_src:
Returns:
'''
if self.img2 is not None:
if self.flag2 == 1:
self.changeBloack_changeimg_status()
self.flag2 = 0
else:
self.changeBloack_changeimg_status()
self.flag2 = 1
else:
self.img = 255 - self.img
showImage = QImage(self.img.data, self.img.shape[1]