ra1nty / DXcam

A Python high-performance screen capture library for Windows using Desktop Duplication API
MIT License
504 stars 70 forks source link

cv2.error: OpenCV(4.5.5) #6

Closed Strive1s closed 2 years ago

Strive1s commented 2 years ago

import dxcam import cv2

camera = dxcam.create() camera.grab() region=[0,0,480,480] while True: img = camera.grab(region=region) img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR) cv2.imshow("window",img) if cv2.waitKey(1) & 0xFF ==ord('d'): cv2.destroyAllWindows() break I always get an error when I test, the program can only run for a short time before the error occurs

Erro Code: Traceback (most recent call last): File "c:/Users/Administrator/Desktop/DXcam-main/dxcamtest.py", line 9, in img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR) cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

ra1nty commented 2 years ago

It is worth noting that .grab will return None if there is no new frame since the last time you called .grab. Usually it means there's nothing new to render since last time (E.g. You are idling).

You should add an additional condition to check if the returned value is None. Also, if you want BGR output you can simply use:

camera = dxcam.create(output_color=“BGR”)
Strive1s commented 2 years ago

import dxcam import cv2

left, top = (2560 - 640) // 2, (1440 - 640) // 2 right, bottom = left + 640, top + 640 region = (left, top, right, bottom) camera = dxcam.create(output_color="BGR") while True: img = camera.grab(region=region) if camera.grab(region=region) is not None: cv2.imshow('window',img) if cv2.waitKey(1) & 0xFF ==ord('d'): cv2.destroyAllWindows() break

I changed my code and it still seems to be a problem, it still doesn't work.

ra1nty commented 2 years ago
import dxcam
import cv2
left, top = (2560 - 640) // 2, (1440 - 640) // 2
right, bottom = left + 640, top + 640
region = (left, top, right, bottom)
camera = dxcam.create(output_color="BGR")
while True:
    img = camera.grab(region=region)
    if img is not None:
        cv2.imshow('window',img)
    if cv2.waitKey(1) & 0xFF ==ord('d'):
        cv2.destroyAllWindows()
        break

Check the returned variable from .grab