代码拉取完成,页面将自动刷新
import cv2
from PyQt5.QtCore import QThread,pyqtSignal
import numpy as np
from PyQt5.QtGui import QImage
from src.utils.GlobalVariable import models
#from PIL import Image, ImageDraw, ImageFont
# 子线程 take photo 并发出信号
class Capture(QThread):
emit_img = pyqtSignal(list,QImage)
def __init__(self):
super().__init__()
self.frame = np.random.randint(255, size=(900, 800, 3),#颜色为0-255的随机数,size为图片大小,3为RGB, dtype=np.uint8数据类型,8个bit
dtype=np.uint8) #待识别帧初始化
self.cap = None
def run(self):
while True:
ret, frame = self.cap.read()
if ret:
p = convertToQtFormat(frame)
self.emit_img.emit([frame],p)
def close(self): #关闭线程
if self.isRunning():
self.terminate()
self.wait()
if self.cap is not None:
self.cap.release()
cv2.destroyAllWindows()
#转换位qt图像格式
def convertToQtFormat(frame_show):
rgbImage = cv2.cvtColor(frame_show, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(rgbImage,cv2.COLOR_RGB2GRAY)
faces = models.detector(gray)
if len(faces) == 1:
face = faces[0]
cv2.rectangle(rgbImage, (face.left(), face.top()), (face.right(), face.bottom()), (255,0,0), 4)
h, w, ch = rgbImage.shape
bytesPerLine = ch * w
convertToQtFormat = QImage(rgbImage.data, w, h, bytesPerLine,
QImage.Format.Format_RGB888)
p = convertToQtFormat.scaled(460, 530)
return p
#为图片渲染中文
# def put_chines_test(frame, chinnes_text):
# rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# location = models.detector(rgbImage)
# if len(location) == 1:
# location = location[0]
# font = ImageFont.truetype("./resources/simsun.ttc",
# 50,
# encoding="utf-8")
# rgbImage = Image.fromarray(rgbImage)
# draw = ImageDraw.Draw(rgbImage)
# draw.text(((location.right() + 6, location.top() - 6)), chinnes_text,
# (0, 0, 255), font)
# rgbImage = np.asarray(rgbImage)
# return rgbImage
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。