Skip to content

Instantly share code, notes, and snippets.

@Vaiz
Created January 27, 2024 13:27
Show Gist options
  • Save Vaiz/fa6a20f51dceb9d36c1e9639267b1355 to your computer and use it in GitHub Desktop.
Save Vaiz/fa6a20f51dceb9d36c1e9639267b1355 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
from mss import mss
import time
from collections import deque
def capture_screen_area(x, y, width, height):
"""Capture a specific area of the screen using mss and return it as an OpenCV image."""
monitor = {"top": y, "left": x, "width": width, "height": height}
screenshot = sct.grab(monitor)
frame = np.array(screenshot)
frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR) # Note the change to BGRA
return frame
def show_video_feed_with_delay(buffer, delay=10):
"""Show the video feed from the buffer with a specified delay."""
#while True:
while buffer and (time.time() - buffer[0][1] >= delay):
frame, _ = buffer.popleft()
cv2.imshow('Delayed Video Feed', frame)
# Define the region to capture (x, y, width, height)
region = (100, 100, 400, 400)
window_position = (400, 400)
sct = mss()
cv2.imshow('Delayed Video Feed', capture_screen_area(*region))
cv2.moveWindow('Delayed Video Feed', *window_position)
# Buffer to store frames along with their capture time
frame_buffer = deque()
# Start capturing frames
while True:
frame = capture_screen_area(*region)
timestamp = time.time()
frame_buffer.append((frame, timestamp))
# Display the frames with a delay
show_video_feed_with_delay(frame_buffer)
# Adjust this to control frame rate
if cv2.waitKey(50) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment