Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an example to integration of DXCam and OpenCV for real-time screen capture and FPS display #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/screen_capture_with_opencv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import dxcam
import cv2
import time

# Create a DXCamera instance
camera = dxcam.create()

# Set the region to capture
left, top = (1920 - 640) // 2, (1080 - 640) // 2
right, bottom = left + 640, top + 640
region = (left, top, right, bottom)

# Start the screen capture
camera.start(region=region)

# Initialize variables for FPS calculation
frames = 0
start_time = time.time()

while True:
# Get the latest frame from the frame buffer
frame = camera.get_latest_frame()

# Display the FPS on the captured screen
frames += 1
if time.time() - start_time > 1:
fps = frames / (time.time() - start_time)
cv2.putText(frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
frames = 0
start_time = time.time()

# Display the captured screen
cv2.imshow("Screen Capture", frame)

# Check if the user pressed 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Stop the screen capture
camera.stop()

# Release the resources
cv2.destroyAllWindows()