iddwsdgh
iddwsdgh

Reputation: 23

How can I screen capture under a blur overlay?

When using PyQt5 and YOLOv5 to detect humans and blur them with an overlay, a flickering issue arises. This happens because the screen capture picks up the overlay itself. The script then mistakenly thinks the object has disappeared (due to its own overlay), removes the overlay, and the process repeats, causing continuous flickering:


import sys, win32api
import torch
import numpy as np
import mss
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPainter, QColor
import win32con
# Load YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5x', pretrained=True)

# Initialize screen capture
sct = mss.mss()
if torch.cuda.is_available():
    print("CUDA ACCELERATION [ENABLED]")
else:
            print("[!] CUDA ACCELERATION IS UNAVAILABLE")
# Screen dimensions
SCREEN_WIDTH, SCREEN_HEIGHT = 1920, 1080

def get_screen():
    monitor = {"top": 0, "left": 0, "width": win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN), "height": win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)}
    screenshot = sct.grab(monitor)
    return np.array(screenshot)

class BlurOverlay(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.detections = []

    def paintEvent(self, event):
        painter = QPainter(self)
        for x1, y1, x2, y2, _, cls in self.detections:
            if cls == 0:  # class 0 is person in COCO dataset
                width, height = int(x2 - x1), int(y2 - y1)
                # Draw dark black rectangle
                painter.setBrush(QColor(0, 0, 0))
                painter.drawRect(int(x1), int(y1), width, height)

    def update_detections(self, detections):
        self.detections = detections
        self.update()

def main():
    app = QApplication(sys.argv)
    overlay = BlurOverlay()
    overlay.show()

    def update_overlay():
        img = get_screen()
        results = model(img)
        detections = results.xyxy[0].cpu().numpy()
        overlay.update_detections(detections)

    timer = QTimer()
    timer.timeout.connect(update_overlay)
    timer.start(30)

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

How can I make it so that the overlay is not captured in the screen capture in order to not see any sort of flickering and consistent blurring like this example: https://imgur.com/a/e2SLnum ?

Upvotes: 0

Views: 78

Answers (0)

Related Questions