Skip to content

Commit

Permalink
Merge pull request #1 from Agade09/main
Browse files Browse the repository at this point in the history
Performance improvements
  • Loading branch information
AI-M-BOT authored Apr 26, 2023
2 parents fc0a257 + 204a558 commit a0964dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
3 changes: 2 additions & 1 deletion dxcam/dxcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def __init__(
def grab(self, region: Tuple[int, int, int, int] = None):
if region is None:
region = self.region
self._validate_region(region)
else:
self._validate_region(region)
frame = self._grab(region)
return frame

Expand Down
33 changes: 20 additions & 13 deletions dxcam/processor/numpy_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class NumpyProcessor(Processor):
def __init__(self, color_mode):
self.cvtcolor = None
self.color_mode = color_mode
if self.color_mode=='BGRA':
self.color_mode = None

def process_cvtcolor(self, image):
import cv2
Expand All @@ -17,30 +19,33 @@ def process_cvtcolor(self, image):
"RGB": cv2.COLOR_BGRA2RGB,
"RGBA": cv2.COLOR_BGRA2RGBA,
"BGR": cv2.COLOR_BGRA2BGR,
"GRAY": cv2.COLOR_BGRA2GRAY,
"BGRA": None,
"GRAY": cv2.COLOR_BGRA2GRAY
}
cv2_code = color_mapping[self.color_mode]
if cv2_code is not None:
if cv2_code != cv2.COLOR_BGRA2GRAY:
self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)
else:
self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)[
..., np.newaxis
]
if cv2_code != cv2.COLOR_BGRA2GRAY:
self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)
else:
return image
self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)[
..., np.newaxis
]
return self.cvtcolor(image)

def process(self, rect, width, height, region, rotation_angle):
pitch = int(rect.Pitch)

if rotation_angle in (0, 180):
offset = (region[1] if rotation_angle==0 else height-region[3])*pitch
height = region[3] - region[1]
else:
offset = (region[0] if rotation_angle==270 else width-region[2])*pitch
width = region[2] - region[0]

if rotation_angle in (0, 180):
size = pitch * height
else:
size = pitch * width

buffer = ctypes.string_at(rect.pBits, size)
buffer = (ctypes.c_char*size).from_address(ctypes.addressof(rect.pBits.contents)+offset)#Pointer arithmetic
pitch = pitch // 4
if rotation_angle in (0, 180):
image = np.ndarray((height, pitch, 4), dtype=np.uint8, buffer=buffer)
Expand All @@ -62,7 +67,9 @@ def process(self, rect, width, height, region, rotation_angle):
elif rotation_angle in (90, 270) and pitch != height:
image = image[:height, :, :]

if region[2] - region[0] != width or region[3] - region[1] != height:
image = image[region[1] : region[3], region[0] : region[2], :]
if region[3] - region[1] != image.shape[0]:
image = image[region[1] : region[3], :, :]
if region[2] - region[0] != image.shape[1]:
image = image[:, region[0] : region[2], :]

return image

0 comments on commit a0964dc

Please sign in to comment.